hw-weapp-compiler 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/config/constants.js +37 -0
- package/build/config/getAppConfig.js +9 -4
- package/build/config/getConfig.js +3 -1
- package/build/config/{getEntrys.js → getEntries.js} +26 -23
- package/build/dev.js +24 -19
- package/build/loader/js-loader.js +29 -14
- package/build/loader/wxml-loader.js +22 -15
- package/build/loader/wxs-loader.js +16 -10
- package/build/plugin/index.js +206 -177
- package/build/prod.js +29 -20
- package/build/utils/buildEnv.js +10 -2
- package/build/utils/compatiblePath.js +3 -1
- package/build/utils/createCompilerHandler.js +29 -0
- package/build/utils/getWxmlAssets.js +27 -31
- package/build/utils/isSubpackage.js +6 -4
- package/build/utils/loadModule.js +4 -7
- package/build/utils/traverseDir.js +3 -1
- package/build/utils/upload.js +14 -22
- package/build/webpack.config.js +42 -78
- package/index.js +1 -1
- package/package.json +3 -3
- package/build/config/env.js +0 -7
- package/build/config/getAssets.js +0 -1
- package/build/config/getContext.js +0 -5
- package/build/config/getEnv.js +0 -5
- package/build/config/getOutput.js +0 -7
- package/build/config/getResourceAccept.js +0 -1
- package/build/utils/recordEnv.js +0 -11
package/build/plugin/index.js
CHANGED
|
@@ -1,27 +1,206 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WeappPlugin - 微信小程序 webpack 构建插件
|
|
3
|
+
*
|
|
4
|
+
* 负责在构建产物中注入公共模块引用、runtime 全局暴露、分包公共模块引用,
|
|
5
|
+
* 以及在开发模式下压缩公共模块代码。
|
|
6
|
+
*/
|
|
1
7
|
const path = require('path');
|
|
2
8
|
const UglifyJS = require('uglify-js');
|
|
3
9
|
const hasha = require('hasha');
|
|
4
10
|
const { RawSource } = require('webpack-sources');
|
|
5
11
|
|
|
6
12
|
const { isNodeModulesUsingComponent } = require('../utils/isNodeModulesUsingComponent');
|
|
7
|
-
const getAssets = require('../config/getAssets');
|
|
8
13
|
const { addToUploadQueue } = require('../utils/upload');
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
const ENV = require('../config/env');
|
|
14
|
+
const { compatiblePath } = require('../utils/compatiblePath');
|
|
15
|
+
const { ENV, PATTERNS, ASSET_EXT_PATTERN, ASSETS_DIR } = require('../config/constants');
|
|
12
16
|
|
|
13
|
-
const assetsDir = getAssets();
|
|
14
17
|
const pluginName = 'WeappCompilerPlugin';
|
|
15
18
|
const contentCache = {};
|
|
16
19
|
|
|
20
|
+
/**
|
|
21
|
+
* 检测产物中存在哪些公共模块和分包模块
|
|
22
|
+
*/
|
|
23
|
+
function detectCommonModules(items) {
|
|
24
|
+
const flags = {
|
|
25
|
+
hasCommonWxss: false,
|
|
26
|
+
hasVendorWxss: false,
|
|
27
|
+
hasCommonJs: false,
|
|
28
|
+
hasVendorJs: false,
|
|
29
|
+
hasRuntimeJs: false,
|
|
30
|
+
};
|
|
31
|
+
const subpackages = {};
|
|
32
|
+
|
|
33
|
+
for (let index = 0; index < items.length; index += 1) {
|
|
34
|
+
const item = items[index];
|
|
35
|
+
|
|
36
|
+
if (!flags.hasCommonWxss && item.name === 'commons.wxss') {
|
|
37
|
+
flags.hasCommonWxss = true;
|
|
38
|
+
}
|
|
39
|
+
if (!flags.hasVendorWxss && item.name === 'vendors_wxss.wxss') {
|
|
40
|
+
flags.hasVendorWxss = true;
|
|
41
|
+
}
|
|
42
|
+
if (!flags.hasCommonJs && item.name === 'commons.js') {
|
|
43
|
+
flags.hasCommonJs = true;
|
|
44
|
+
}
|
|
45
|
+
if (!flags.hasVendorJs && item.name === 'vendors.js') {
|
|
46
|
+
flags.hasVendorJs = true;
|
|
47
|
+
}
|
|
48
|
+
if (!flags.hasRuntimeJs && item.name === 'runtime.js') {
|
|
49
|
+
flags.hasRuntimeJs = true;
|
|
50
|
+
}
|
|
51
|
+
if (/subpackage_common/.test(item.name)) {
|
|
52
|
+
const pathInfo = path.parse(item.name);
|
|
53
|
+
const dir = compatiblePath(pathInfo.dir);
|
|
54
|
+
|
|
55
|
+
if (!subpackages[dir]) {
|
|
56
|
+
subpackages[dir] = { js: '', wxss: '' };
|
|
57
|
+
}
|
|
58
|
+
if (PATTERNS.JS_EXT.test(item.name)) {
|
|
59
|
+
subpackages[dir].js = compatiblePath(item.name);
|
|
60
|
+
}
|
|
61
|
+
if (PATTERNS.WXSS_EXT.test(item.name)) {
|
|
62
|
+
subpackages[dir].wxss = compatiblePath(item.name);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return { flags, subpackages };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* 注入全局引用模块和公共模块到各个 JS/WXSS 文件
|
|
72
|
+
*/
|
|
73
|
+
function injectGlobalModules(content, assetName, flags) {
|
|
74
|
+
let result = content;
|
|
75
|
+
|
|
76
|
+
// 注入全局 var self = global
|
|
77
|
+
if (PATTERNS.JS_EXT.test(assetName) && !/(var self = global;)/.test(result)) {
|
|
78
|
+
result = `var self = global; \n${result}`;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// runtime 暴露到全局
|
|
82
|
+
if (assetName === 'runtime.js') {
|
|
83
|
+
result = result.replace(
|
|
84
|
+
'var __webpack_module_cache__ = {};',
|
|
85
|
+
`
|
|
86
|
+
if (!global.__webpack_module_cache__) {
|
|
87
|
+
global.__webpack_module_cache__ = {};
|
|
88
|
+
}
|
|
89
|
+
var __webpack_module_cache__ = global.__webpack_module_cache__;
|
|
90
|
+
global.__webpack_require__ = __webpack_require__;
|
|
91
|
+
`,
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// 注入公共模块 js
|
|
96
|
+
if (assetName === 'app.js') {
|
|
97
|
+
// sdk2.17.3 window 下没有 regeneratorRuntime
|
|
98
|
+
result = result.replace(
|
|
99
|
+
'Function("r", "regeneratorRuntime = r")(runtime);',
|
|
100
|
+
'global.regeneratorRuntime = runtime',
|
|
101
|
+
);
|
|
102
|
+
if (!/require('\.\/commons\.js')/.test(result) && flags.hasCommonJs) {
|
|
103
|
+
result = `require('./commons.js');\n${result}`;
|
|
104
|
+
}
|
|
105
|
+
if (!/require('\.\/vendors\.js')/.test(result) && flags.hasVendorJs) {
|
|
106
|
+
result = `require('./vendors.js');\n${result}`;
|
|
107
|
+
}
|
|
108
|
+
if (!/require('\.\/runtime\.js')/.test(result) && flags.hasRuntimeJs) {
|
|
109
|
+
result = `require('./runtime.js');\n${result}`;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// 注入公共模块 css
|
|
114
|
+
if (assetName === 'app.wxss') {
|
|
115
|
+
if (!/@import '\.\/commons\.wxss'/.test(result) && flags.hasCommonWxss) {
|
|
116
|
+
result = `@import './commons.wxss';\n${result}`;
|
|
117
|
+
}
|
|
118
|
+
if (!/@import '\.\/vendors_wxss\.wxss'/.test(result) && flags.hasVendorWxss) {
|
|
119
|
+
result = `@import './vendors_wxss.wxss';\n${result}`;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return result;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* 注入分包公共模块引用
|
|
128
|
+
*/
|
|
129
|
+
function injectSubpackageRequires(content, assetName, subpackages) {
|
|
130
|
+
let result = content;
|
|
131
|
+
|
|
132
|
+
Object.keys(subpackages).forEach((subpackage) => {
|
|
133
|
+
const res = subpackages[subpackage];
|
|
134
|
+
|
|
135
|
+
if (
|
|
136
|
+
assetName.startsWith(subpackage) &&
|
|
137
|
+
!/(subpackage_common\.(js|wxss))$/.test(assetName)
|
|
138
|
+
) {
|
|
139
|
+
if (
|
|
140
|
+
res.js &&
|
|
141
|
+
PATTERNS.JS_EXT.test(assetName) &&
|
|
142
|
+
!/'subpackage_common\.js'\);/.test(result)
|
|
143
|
+
) {
|
|
144
|
+
result = `require('${compatiblePath(
|
|
145
|
+
path.relative(path.parse(assetName).dir, res.js),
|
|
146
|
+
)}');\n${result}`;
|
|
147
|
+
}
|
|
148
|
+
if (
|
|
149
|
+
res.wxss &&
|
|
150
|
+
PATTERNS.WXSS_EXT.test(assetName) &&
|
|
151
|
+
!/'subpackage_common\.wxss';/.test(result)
|
|
152
|
+
) {
|
|
153
|
+
result = `@import '${compatiblePath(
|
|
154
|
+
path.relative(path.parse(assetName).dir, res.wxss),
|
|
155
|
+
)}';\n${result}`;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
return result;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* 在开发模式下压缩公共模块代码
|
|
165
|
+
*/
|
|
166
|
+
function minifyCommonModules(content, assetName, isJs, isDev) {
|
|
167
|
+
if (!isDev) {
|
|
168
|
+
return content;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const isCommon = assetName === 'commons.js' || assetName === 'vendors.js';
|
|
172
|
+
const isNodeModule = isNodeModulesUsingComponent(assetName);
|
|
173
|
+
|
|
174
|
+
if (!isCommon && !isNodeModule) {
|
|
175
|
+
return content;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (!isJs) {
|
|
179
|
+
return content;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const hash = hasha(content);
|
|
183
|
+
if (contentCache[assetName] && contentCache[assetName].hash === hash) {
|
|
184
|
+
return contentCache[assetName].content;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const result = UglifyJS.minify(content, {
|
|
188
|
+
mangle: false,
|
|
189
|
+
compress: {
|
|
190
|
+
drop_console: false,
|
|
191
|
+
drop_debugger: false,
|
|
192
|
+
},
|
|
193
|
+
sourceMap: false,
|
|
194
|
+
}).code;
|
|
195
|
+
|
|
196
|
+
contentCache[assetName] = { hash, content: result };
|
|
197
|
+
return result;
|
|
198
|
+
}
|
|
199
|
+
|
|
17
200
|
class WeappPlugin {
|
|
18
|
-
// eslint-disable-next-line
|
|
19
201
|
constructor() {}
|
|
20
202
|
|
|
21
|
-
// eslint-disable-next-line
|
|
22
203
|
apply(compiler) {
|
|
23
|
-
// const { RawSource } = compiler.webpack.sources;
|
|
24
|
-
|
|
25
204
|
let obsAssets = [];
|
|
26
205
|
|
|
27
206
|
compiler.hooks.done.tap(pluginName, () => {
|
|
@@ -37,7 +216,7 @@ class WeappPlugin {
|
|
|
37
216
|
(assets) => {
|
|
38
217
|
obsAssets = obsAssets.concat(
|
|
39
218
|
Object.keys(assets).filter((key) => {
|
|
40
|
-
return
|
|
219
|
+
return ASSET_EXT_PATTERN.test(key) && key.includes(`${ASSETS_DIR}/`);
|
|
41
220
|
}),
|
|
42
221
|
);
|
|
43
222
|
},
|
|
@@ -49,179 +228,29 @@ class WeappPlugin {
|
|
|
49
228
|
stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONS,
|
|
50
229
|
},
|
|
51
230
|
async (assets) => {
|
|
52
|
-
// this callback will run against assets added later by plugins.
|
|
53
231
|
const items = Object.entries(assets)
|
|
54
|
-
.map(([name, source]) => {
|
|
55
|
-
|
|
56
|
-
name,
|
|
57
|
-
source,
|
|
58
|
-
};
|
|
59
|
-
})
|
|
60
|
-
.filter((item) => {
|
|
61
|
-
return /(\.(js|wxss))$/g.test(item.name);
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
let hasCommonWxss = false;
|
|
65
|
-
let hasVendorWxss = false;
|
|
66
|
-
let hasCommonJs = false;
|
|
67
|
-
let hasRuntimeJs = false;
|
|
68
|
-
let hasVendorJs = false;
|
|
69
|
-
const subpackages = {};
|
|
70
|
-
|
|
71
|
-
// 检测公共模块
|
|
72
|
-
for (let index = 0; index < items.length; index += 1) {
|
|
73
|
-
const item = items[index];
|
|
74
|
-
|
|
75
|
-
if (hasCommonWxss === false && item.name === 'commons.wxss') {
|
|
76
|
-
hasCommonWxss = true;
|
|
77
|
-
}
|
|
78
|
-
if (hasVendorWxss === false && item.name === 'vendors_wxss.wxss') {
|
|
79
|
-
hasVendorWxss = true;
|
|
80
|
-
}
|
|
81
|
-
if (hasCommonJs === false && item.name === 'commons.js') {
|
|
82
|
-
hasCommonJs = true;
|
|
83
|
-
}
|
|
84
|
-
if (hasVendorJs === false && item.name === 'vendors.js') {
|
|
85
|
-
hasVendorJs = true;
|
|
86
|
-
}
|
|
87
|
-
if (hasRuntimeJs === false && item.name === 'runtime.js') {
|
|
88
|
-
hasRuntimeJs = true;
|
|
89
|
-
}
|
|
90
|
-
if (/subpackage_common/g.test(item.name)) {
|
|
91
|
-
const pathInfo = path.parse(item.name);
|
|
92
|
-
|
|
93
|
-
if (!subpackages[compatiblePath(pathInfo.dir)]) {
|
|
94
|
-
subpackages[compatiblePath(pathInfo.dir)] = {
|
|
95
|
-
js: '',
|
|
96
|
-
wxss: '',
|
|
97
|
-
};
|
|
98
|
-
}
|
|
99
|
-
if (/(\.js)$/g.test(item.name)) {
|
|
100
|
-
subpackages[compatiblePath(pathInfo.dir)].js = compatiblePath(item.name);
|
|
101
|
-
}
|
|
102
|
-
if (/(\.wxss)$/g.test(item.name)) {
|
|
103
|
-
subpackages[compatiblePath(pathInfo.dir)].wxss = compatiblePath(item.name);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
for (let index = 0; index < items.length; index += 1) {
|
|
109
|
-
const asset = items[index];
|
|
232
|
+
.map(([name, source]) => ({ name, source }))
|
|
233
|
+
.filter((item) => PATTERNS.JS_EXT.test(item.name) || PATTERNS.WXSS_EXT.test(item.name));
|
|
110
234
|
|
|
111
|
-
|
|
112
|
-
const isJs = /(\.(js))$/g.test(assetName);
|
|
113
|
-
const { source } = asset;
|
|
114
|
-
let content = source.source();
|
|
235
|
+
const { flags, subpackages } = detectCommonModules(items);
|
|
115
236
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
237
|
+
await Promise.all(
|
|
238
|
+
items.map(async (asset) => {
|
|
239
|
+
const assetName = compatiblePath(asset.name);
|
|
240
|
+
const isJs = PATTERNS.JS_EXT.test(assetName);
|
|
241
|
+
let content = asset.source.source();
|
|
119
242
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
if (!/(var self = global;)/g.test(content)) {
|
|
123
|
-
content = `var self = global; \n${content}`;
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
// runtime 暴露到全局
|
|
128
|
-
if (assetName === 'runtime.js') {
|
|
129
|
-
content = content.replace(
|
|
130
|
-
'var __webpack_module_cache__ = {};',
|
|
131
|
-
`
|
|
132
|
-
if (!global.__webpack_module_cache__) {
|
|
133
|
-
global.__webpack_module_cache__ = {};
|
|
134
|
-
}
|
|
135
|
-
var __webpack_module_cache__ = global.__webpack_module_cache__;
|
|
136
|
-
global.__webpack_require__ = __webpack_require__;
|
|
137
|
-
`,
|
|
138
|
-
);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
// 注入公共模块 js
|
|
142
|
-
if (assetName === 'app.js') {
|
|
143
|
-
// sdk2.17.3 window 下没有 regeneratorRuntime
|
|
144
|
-
content = content.replace(
|
|
145
|
-
'Function("r", "regeneratorRuntime = r")(runtime);',
|
|
146
|
-
'global.regeneratorRuntime = runtime',
|
|
147
|
-
);
|
|
148
|
-
if (!/require('\.\/commons\.js')/g.test(content) && hasCommonJs) {
|
|
149
|
-
content = `require('./commons.js');\n${content}`;
|
|
243
|
+
if (content.toString) {
|
|
244
|
+
content = content.toString();
|
|
150
245
|
}
|
|
151
|
-
if (!/require('\.\/vendors\.js')/g.test(content) && hasVendorJs) {
|
|
152
|
-
content = `require('./vendors.js');\n${content}`;
|
|
153
|
-
}
|
|
154
|
-
if (!/require('\.\/runtime\.js')/g.test(content) && hasRuntimeJs) {
|
|
155
|
-
content = `require('./runtime.js');\n${content}`;
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
246
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
// 分包注入公共模块
|
|
170
|
-
Object.keys(subpackages).forEach((subpackage) => {
|
|
171
|
-
const res = subpackages[subpackage];
|
|
172
|
-
|
|
173
|
-
if (
|
|
174
|
-
assetName.startsWith(subpackage) &&
|
|
175
|
-
!/(subpackage_common\.(js|wxss))$/g.test(assetName)
|
|
176
|
-
) {
|
|
177
|
-
if (
|
|
178
|
-
res.js &&
|
|
179
|
-
/(\.js)$/g.test(assetName) &&
|
|
180
|
-
!/'subpackage_common\.js'\);/g.test(content)
|
|
181
|
-
) {
|
|
182
|
-
content = `require('${compatiblePath(
|
|
183
|
-
path.relative(path.parse(assetName).dir, res.js),
|
|
184
|
-
)}');\n${content}`;
|
|
185
|
-
}
|
|
186
|
-
if (
|
|
187
|
-
res.wxss &&
|
|
188
|
-
/(\.wxss)$/g.test(assetName) &&
|
|
189
|
-
!/'subpackage_common\.wxss';/g.test(content)
|
|
190
|
-
) {
|
|
191
|
-
content = `@import '${compatiblePath(
|
|
192
|
-
path.relative(path.parse(assetName).dir, res.wxss),
|
|
193
|
-
)}';\n${content}`;
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
});
|
|
197
|
-
|
|
198
|
-
// 压缩 公共模块
|
|
199
|
-
if (
|
|
200
|
-
(assetName === 'commons.js' ||
|
|
201
|
-
assetName === 'vendors.js' ||
|
|
202
|
-
isNodeModulesUsingComponent(asset.name)) &&
|
|
203
|
-
compiler.options.mode === ENV.DEV
|
|
204
|
-
) {
|
|
205
|
-
const hash = hasha(content);
|
|
206
|
-
if (contentCache[assetName] && contentCache[assetName].hash === hash) {
|
|
207
|
-
content = contentCache[assetName].content;
|
|
208
|
-
} else if (isJs) {
|
|
209
|
-
content = UglifyJS.minify(content, {
|
|
210
|
-
mangle: false,
|
|
211
|
-
compress: {
|
|
212
|
-
drop_console: false,
|
|
213
|
-
drop_debugger: false,
|
|
214
|
-
},
|
|
215
|
-
sourceMap: false,
|
|
216
|
-
}).code;
|
|
217
|
-
contentCache[assetName] = {
|
|
218
|
-
hash,
|
|
219
|
-
content,
|
|
220
|
-
};
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
compilation.updateAsset(asset.name, new RawSource(content));
|
|
224
|
-
}
|
|
247
|
+
content = injectGlobalModules(content, assetName, flags);
|
|
248
|
+
content = injectSubpackageRequires(content, assetName, subpackages);
|
|
249
|
+
content = minifyCommonModules(content, assetName, isJs, compiler.options.mode === ENV.DEV);
|
|
250
|
+
|
|
251
|
+
compilation.updateAsset(asset.name, new RawSource(content));
|
|
252
|
+
}),
|
|
253
|
+
);
|
|
225
254
|
},
|
|
226
255
|
);
|
|
227
256
|
});
|
package/build/prod.js
CHANGED
|
@@ -1,34 +1,43 @@
|
|
|
1
|
-
const
|
|
1
|
+
const path = require('path');
|
|
2
2
|
const webpack = require('webpack');
|
|
3
|
-
const
|
|
3
|
+
const TerserPlugin = require('terser-webpack-plugin');
|
|
4
|
+
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
|
|
5
|
+
const { ENV } = require('./config/constants');
|
|
4
6
|
const webpackConfig = require('./webpack.config');
|
|
7
|
+
const { createCompilerHandler } = require('./utils/createCompilerHandler');
|
|
8
|
+
const { getBuildEnv } = require('./utils/buildEnv');
|
|
5
9
|
|
|
6
10
|
module.exports = (opts) => {
|
|
7
11
|
const compiler = webpack(
|
|
8
12
|
webpackConfig(
|
|
9
13
|
{
|
|
10
14
|
mode: ENV.PROD,
|
|
11
|
-
devtool:
|
|
15
|
+
devtool: false,
|
|
16
|
+
output: {
|
|
17
|
+
clean: {
|
|
18
|
+
keep: /project\.config\.json/,
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
optimization: {
|
|
22
|
+
minimizer: [
|
|
23
|
+
new TerserPlugin({
|
|
24
|
+
extractComments: false,
|
|
25
|
+
}),
|
|
26
|
+
new CssMinimizerPlugin(),
|
|
27
|
+
],
|
|
28
|
+
},
|
|
29
|
+
cache: {
|
|
30
|
+
type: 'filesystem',
|
|
31
|
+
cacheDirectory: path.resolve(process.cwd(), 'node_modules/.cache/webpack'),
|
|
32
|
+
buildDependencies: {
|
|
33
|
+
config: [path.resolve(__dirname, 'webpack.config.js')],
|
|
34
|
+
},
|
|
35
|
+
name: `${getBuildEnv()}-${process.version.slice(0, 6)}`,
|
|
36
|
+
},
|
|
12
37
|
},
|
|
13
38
|
opts || {},
|
|
14
39
|
),
|
|
15
40
|
);
|
|
16
41
|
|
|
17
|
-
compiler.run((
|
|
18
|
-
if (err) {
|
|
19
|
-
console.error(err);
|
|
20
|
-
return;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
if (stats.hasErrors()) {
|
|
24
|
-
console.log(
|
|
25
|
-
stats.toString({
|
|
26
|
-
chunks: false, // Makes the build much quieter
|
|
27
|
-
colors: true, // Shows colors in the console
|
|
28
|
-
}),
|
|
29
|
-
);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
console.log(chalk.green(`completed in ${(stats.endTime - stats.startTime) / 1000} seconds`));
|
|
33
|
-
});
|
|
42
|
+
compiler.run(createCompilerHandler('build'));
|
|
34
43
|
};
|
package/build/utils/buildEnv.js
CHANGED
|
@@ -1,8 +1,16 @@
|
|
|
1
|
-
const
|
|
2
|
-
const
|
|
1
|
+
const fs = require('fs-extra');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const { ENV, DIST_DIR } = require('../config/constants');
|
|
3
4
|
|
|
4
5
|
let env = '';
|
|
5
6
|
|
|
7
|
+
function recordEnv(config) {
|
|
8
|
+
fs.writeJSONSync(path.join(DIST_DIR, 'weapp.env.json'), {
|
|
9
|
+
env: ENV.DEV,
|
|
10
|
+
...config,
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
|
|
6
14
|
function setBuildEnv({ mode, development, simulation, production }) {
|
|
7
15
|
env = mode;
|
|
8
16
|
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
const chalk = require('chalk');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 创建 webpack 编译结果回调处理器
|
|
5
|
+
* @param {string} label - 构建阶段标签,如 'dev' 或 'build'
|
|
6
|
+
*/
|
|
7
|
+
function createCompilerHandler(label) {
|
|
8
|
+
return (err, stats) => {
|
|
9
|
+
if (err) {
|
|
10
|
+
console.error(err);
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
if (stats.hasErrors()) {
|
|
15
|
+
console.log(
|
|
16
|
+
stats.toString({
|
|
17
|
+
chunks: false,
|
|
18
|
+
colors: true,
|
|
19
|
+
}),
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
console.log(chalk.green(`${label} completed in ${(stats.endTime - stats.startTime) / 1000} seconds`));
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
module.exports = {
|
|
28
|
+
createCompilerHandler,
|
|
29
|
+
};
|
|
@@ -1,29 +1,19 @@
|
|
|
1
1
|
const htmlparser2 = require('htmlparser2');
|
|
2
2
|
const path = require('path');
|
|
3
3
|
const fse = require('fs-extra');
|
|
4
|
-
const
|
|
5
|
-
const getContext = require('../config/getContext');
|
|
6
|
-
|
|
7
|
-
const context = getContext();
|
|
4
|
+
const { ASSET_EXT_PATTERN, PATTERNS, SRC_DIR } = require('../config/constants');
|
|
8
5
|
|
|
9
6
|
function getWxmlAssets(filePath, content) {
|
|
10
7
|
const resolvePath = (attr, dir) => {
|
|
11
|
-
// console.log('-------------');
|
|
12
|
-
// console.log(attr, dir);
|
|
13
|
-
// console.log('-------------');
|
|
14
8
|
return new Promise((resolve) => {
|
|
15
|
-
this.resolve(
|
|
9
|
+
this.resolve(SRC_DIR, attr, async (err, result) => {
|
|
16
10
|
let res = result;
|
|
17
11
|
if (err) {
|
|
18
12
|
if (await fse.pathExists(path.resolve(dir, attr))) {
|
|
19
13
|
res = path.resolve(dir, attr);
|
|
20
14
|
resolve(res);
|
|
21
15
|
} else {
|
|
22
|
-
// console.log('-------------');
|
|
23
|
-
// console.log(attr);
|
|
24
16
|
this.emitError(err);
|
|
25
|
-
// this.emitWarning(err);
|
|
26
|
-
// reject(err);
|
|
27
17
|
resolve(res);
|
|
28
18
|
}
|
|
29
19
|
} else {
|
|
@@ -69,31 +59,35 @@ function getWxmlAssets(filePath, content) {
|
|
|
69
59
|
.filter((item) => !!item)
|
|
70
60
|
.map((item) => item.split('?')[0])
|
|
71
61
|
.filter((item) => !/^(http:|https:)/.test(item))
|
|
72
|
-
.filter((item) => !/{{.*}}
|
|
73
|
-
.filter((item) =>
|
|
62
|
+
.filter((item) => !/{{.*}}/.test(item))
|
|
63
|
+
.filter((item) => !/\+.*\+/.test(item));
|
|
74
64
|
|
|
75
|
-
const assets = filteredAttrs.filter((item) =>
|
|
76
|
-
const wxmls = allAttrs.filter((item) =>
|
|
65
|
+
const assets = filteredAttrs.filter((item) => ASSET_EXT_PATTERN.test(item));
|
|
66
|
+
const wxmls = allAttrs.filter((item) => PATTERNS.WXS_EXT.test(item) || PATTERNS.WXML_EXT.test(item));
|
|
77
67
|
|
|
78
68
|
const assetsImports = [];
|
|
79
69
|
const wxmlsImports = [];
|
|
80
70
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
71
|
+
// 并行解析 wxml 路径
|
|
72
|
+
const wxmlResults = await Promise.all(
|
|
73
|
+
wxmls.map(async (attr) => {
|
|
74
|
+
const result = await resolvePath(attr, path.parse(filePath).dir);
|
|
75
|
+
return result ? [attr, result] : null;
|
|
76
|
+
}),
|
|
77
|
+
);
|
|
78
|
+
for (let index = 0; index < wxmlResults.length; index += 1) {
|
|
79
|
+
if (wxmlResults[index]) wxmlsImports.push(wxmlResults[index]);
|
|
88
80
|
}
|
|
89
81
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
82
|
+
// 并行解析 asset 路径
|
|
83
|
+
const assetResults = await Promise.all(
|
|
84
|
+
assets.map(async (attr) => {
|
|
85
|
+
const result = await resolvePath(attr, path.parse(filePath).dir);
|
|
86
|
+
return result ? [attr, result] : null;
|
|
87
|
+
}),
|
|
88
|
+
);
|
|
89
|
+
for (let index = 0; index < assetResults.length; index += 1) {
|
|
90
|
+
if (assetResults[index]) assetsImports.push(assetResults[index]);
|
|
97
91
|
}
|
|
98
92
|
|
|
99
93
|
resolve([assetsImports, wxmlsImports]);
|
|
@@ -104,4 +98,6 @@ function getWxmlAssets(filePath, content) {
|
|
|
104
98
|
});
|
|
105
99
|
}
|
|
106
100
|
|
|
107
|
-
module.exports =
|
|
101
|
+
module.exports = {
|
|
102
|
+
getWxmlAssets,
|
|
103
|
+
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
const compatiblePath = require('./compatiblePath');
|
|
2
|
-
const getAppConfig = require('../config/getAppConfig');
|
|
1
|
+
const { compatiblePath } = require('./compatiblePath');
|
|
2
|
+
const { getAppConfig } = require('../config/getAppConfig');
|
|
3
3
|
|
|
4
4
|
const appConfig = getAppConfig();
|
|
5
5
|
|
|
@@ -9,7 +9,7 @@ function isSubpackage(file) {
|
|
|
9
9
|
(appConfig.subpackages || []).forEach((pkg) => {
|
|
10
10
|
let { root } = pkg;
|
|
11
11
|
|
|
12
|
-
if (
|
|
12
|
+
if (!/\/$/.test(root)) {
|
|
13
13
|
root = `${root}/`;
|
|
14
14
|
}
|
|
15
15
|
|
|
@@ -19,4 +19,6 @@ function isSubpackage(file) {
|
|
|
19
19
|
});
|
|
20
20
|
return isSub;
|
|
21
21
|
}
|
|
22
|
-
module.exports =
|
|
22
|
+
module.exports = {
|
|
23
|
+
isSubpackage,
|
|
24
|
+
};
|
|
@@ -3,17 +3,14 @@ function loadModule(file) {
|
|
|
3
3
|
this.addDependency(file);
|
|
4
4
|
this.loadModule(file, (err, src) => {
|
|
5
5
|
if (err) {
|
|
6
|
-
// console.log();
|
|
7
|
-
// console.log(file, this.resourcePath);
|
|
8
|
-
// console.log();
|
|
9
6
|
this.emitError(err);
|
|
10
|
-
|
|
11
|
-
// reject(err);
|
|
12
|
-
resolve(src);
|
|
7
|
+
resolve(null);
|
|
13
8
|
} else {
|
|
14
9
|
resolve(src);
|
|
15
10
|
}
|
|
16
11
|
});
|
|
17
12
|
});
|
|
18
13
|
}
|
|
19
|
-
module.exports =
|
|
14
|
+
module.exports = {
|
|
15
|
+
loadModule,
|
|
16
|
+
};
|