/
WebpackAssetsPlugin.js
67 lines (59 loc) · 2.12 KB
/
WebpackAssetsPlugin.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
/**
* 对于 mp 类型的小程序
* 合并所有页面文件到 app.js
* 并增加执行的 wrapper
*/
const { readFileSync, existsSync } = require('fs');
const { join } = require('path');
const { ConcatSource } = require('webpack-sources');
const SCHEMA_VAR = '__SCHEMA_MOCK_DATA__';
const globalPolyfills = [
"var __global__ = typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : new Function('return this')();",
"typeof polyfill === 'function' && polyfill(__global__);",
];
module.exports = class WebpackAssetsPlugin {
constructor(opts = {}) {
this.target = opts.target || 'web';
}
apply(compiler) {
compiler.hooks.compilation.tap('compilation', (compilation) => {
compilation.hooks.optimizeAssets.tap('MiniAppPlugin', () => {
const reg = new RegExp(/\.js$/);
const args = [];
// 删除原 assets
Object.keys(compilation.assets).forEach((key) => {
if (reg.test(key)) {
args.push(compilation.assets[key]);
delete compilation.assets[key];
}
});
// polyfill global context
args.unshift(new ConcatSource(globalPolyfills.join('')));
let injectSchemaMockData;
const mockDataPath = join(compiler.context, 'schema/mock-data.json');
if (existsSync(mockDataPath)) {
try {
const mockData = JSON.parse(readFileSync(mockDataPath));
// Inject schema data
injectSchemaMockData = `
try {
${SCHEMA_VAR} = ${JSON.stringify(mockData)};
} catch(err) {
console.warn('Inject schema data with error', err);
}
`;
} catch (err) {
throw new Error('Please check schema/mock-data.json is a valid JSON string.');
}
}
if (injectSchemaMockData) {
args.unshift(injectSchemaMockData);
}
args.unshift(ConcatSource); // firstArg for apply
const app = new (ConcatSource.bind.apply(ConcatSource, args))();
compilation.assets['app.js'] = app;
global.AppJSContent = app.source();
});
});
}
};