1 | const path = require('path');
|
2 | const log = require('./utils/log.js');
|
3 | const match = require('minimatch');
|
4 |
|
5 | const utils = {
|
6 |
|
7 | appRoot : process.cwd(),
|
8 | require : (modPath)=>require(utils.resolve(modPath)),
|
9 | resolve : (modPath)=>require.resolve(path.resolve(utils.appRoot, modPath)),
|
10 | decache : (modPath)=>delete require.cache[utils.resolve(modPath)],
|
11 |
|
12 | paths : (paths, entryName)=>{
|
13 | return {
|
14 | entry : `${paths.build}/${entryName}`,
|
15 | code : `${paths.build}/${entryName}/${paths.code}`,
|
16 | style : `${paths.build}/${entryName}/${paths.style}`,
|
17 | render : `${paths.build}/${entryName}/${paths.render}`,
|
18 | static : `${paths.build}/${entryName}/${paths.static}`,
|
19 | libs : `${paths.build}/${paths.libs}`
|
20 | }
|
21 | },
|
22 |
|
23 |
|
24 | bundle : async (bundler)=>{
|
25 | return new Promise((resolve, reject)=>bundler.bundle((err, buf) => err ? reject(err) : resolve(buf.toString())))
|
26 | .catch((err)=>{
|
27 | log.bundleError(err);
|
28 | throw err;
|
29 | })
|
30 | },
|
31 | regex : (pattern, source)=>{
|
32 | let result = [], match;
|
33 | do{
|
34 | match = pattern.exec(source);
|
35 | if(!match) continue;
|
36 | result.push(match);
|
37 | }while(match);
|
38 | return result;
|
39 | },
|
40 |
|
41 | shouldBundle : (filepath, modName, opts)=>{
|
42 | if(!filepath) {
|
43 | log.requireError(modName);
|
44 | return false;
|
45 | }
|
46 | const isLib = match(filepath, '**/node_modules/**');
|
47 | const bundle = opts.bundle.some((bundlePath)=>match(filepath, bundlePath))
|
48 | return !isLib || bundle;
|
49 | },
|
50 | getCaller : (offset=0)=>{
|
51 | const cache = Error.prepareStackTrace;
|
52 | Error.prepareStackTrace = (_, stack)=>stack;
|
53 | const target = (new Error()).stack[2+offset];
|
54 | Error.prepareStackTrace = cache;
|
55 | return {
|
56 | name : target.getFunctionName(),
|
57 | file : target.getFileName(),
|
58 | line : target.getLineNumber(),
|
59 | col : target.getColumnNumber(),
|
60 | };
|
61 | }
|
62 | };
|
63 |
|
64 | module.exports = utils; |
\ | No newline at end of file |