-
Notifications
You must be signed in to change notification settings - Fork 33
/
compileModules.js
102 lines (86 loc) · 2.46 KB
/
compileModules.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**
* @file 编译外部依赖模块,原因:
* 1. 有些模块中有 process.env.NODE_ENV 判断。
* 2. 微信小程序不识别项目文件夹之外的依赖
* @author meixuguang
*/
/* eslint-disable fecs-no-require */
/* globals Set */
const fs = require('fs-extra');
const webpack = require('webpack');
const log = require('../../helper/log');
const {getPathToCWD} = require('../../helper/path');
const path = require('path');
const modules = {
'@marsjs/core': {
needCompile: false,
path: './mars-core'
},
'vuex': {
needCompile: true,
path: './mars_modules/vuex'
}
};
const inProcessingModules = new Set();
/**
* compile
*
* @param {string} val key
* @param {string} key key
* @param {string} destPath dest
* @return {Promise}
*/
function compile(val, key, destPath) {
if (!modules[key].needCompile) {
return Promise.resolve();
}
const entry = require.resolve(key, {
paths: [process.cwd()]
});
let modulePath;
if (val === modules[key].path) {
// 直接写的包名,为了能引用到,生成为 index.js
modulePath = val + '/index.js';
}
else {
modulePath = './mars_modules' + entry.slice(entry.lastIndexOf('node_modules')).replace('node_modules', '');
}
if (fs.existsSync(path.resolve(destPath, modulePath)) || inProcessingModules.has(entry)) {
return Promise.resolve();
}
log.info('[compile:module]:', getPathToCWD(entry));
inProcessingModules.add(entry);
return new Promise((resolve, reject) => {
webpack({
entry: [entry],
output: {
path: destPath,
filename: modulePath,
libraryTarget: 'commonjs'
},
mode: process.env.NODE_ENV === 'production' ? 'production' : 'development'
}, (err, stats) => {
if (err) {
log.error(err.stack || err);
if (err.details) {
log.error(err.details);
}
return resolve();
}
const info = stats.toJson();
if (stats.hasErrors()) {
log.error(info.errors);
}
if (stats.hasWarnings()) {
log.warn(info.warnings);
}
inProcessingModules.delete(entry);
resolve();
// Done processing
});
});
}
module.exports = {
compile,
modules
};