-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathwebpack.config.js
117 lines (107 loc) · 4.3 KB
/
webpack.config.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* eslint-env node */
// This file is used to generate UMD & ESM bundles.
const fs = require('fs');
const path = require('path');
const copyDir = require('copy-dir');
const WatchExternalFilesPlugin = require('webpack-watch-files-plugin').default;
const { convertJsType } = require('./tool/lib/convertor');
const resolve = file => path.resolve(__dirname, file);
const copyOpts = {
utimes: true, // keep add time and modify time
mode: true, // keep file mode
cover: true // cover file when exists, default is true
};
// build umd & esm versions for browser
// main: cjs -> nodejs
// module: esm -> vite & modern browsers
// browser: umd -> the majority browsers
module.exports = env => {
const target = env.target === 'esm' ? 'esm' : 'umd';
const entry = env.entry === 'pure' ? 'pure' : 'mixed';
return {
mode: 'production', // development, production
optimization: { minimize: false },
devtool: 'source-map',
entry: entry === 'pure' ? './src/browser.pure.ts' : './src/browser.ts',
// devtool: 'inline-source-map',
module: {
rules: [
{
test: /\.tsx?$/,
loader: 'ts-loader',
options: { configFile: target === 'esm' ? 'tsconfig.esm.json' : 'tsconfig.json' },
},
],
},
resolve: {
extensions: ['.tsx', '.ts', '.js'],
},
plugins: [
// add js files in tool directory to watch list
new WatchExternalFilesPlugin({
files: [ './tool/**/*.js', ]
}),
// copy "tool" to dist
{
apply: (compiler) => {
compiler.hooks.beforeCompile.tapAsync('Before', (_, callback) => {
copyDir.sync(resolve('./tool'), resolve('./dist/tool'), copyOpts);
copyDir.sync(resolve('./package.json'), resolve('./dist/package.json'), copyOpts);
copyDir.sync(resolve('./README.MD'), resolve('./dist/README.MD'), copyOpts);
copyDir.sync(resolve('./README-CN.MD'), resolve('./dist/README-CN.MD'), copyOpts);
callback();
});
}
},
// generate ESM plugins
{
apply: (compiler) => {
compiler.hooks.done.tap('After', () => {
if (target !== 'esm') {
// for backward compatibility
const index = resolve('./dist/src/index.js');
fs.writeFileSync(index, fs.readFileSync(index, 'utf8') + '\nmodule.exports = Index;');
return;
}
console.log('Generating ESM plugins...');
convertJsType('esm', {
[resolve('./tool/plugin/faker.js')]: resolve('./dist/tool/plugin/faker.esm.mjs'),
[resolve('./tool/plugin/cache.js')]: resolve('./dist/tool/plugin/cache.esm.mjs'),
[resolve('./tool/plugin/vite.js')]: resolve('./dist/tool/plugin/vite.esm.mjs'),
[resolve('./tool/plugin/webpack.js')]: resolve('./dist/tool/plugin/webpack.esm.mjs'),
});
// Copy a redundant plugin directory for backward compatibility.
console.log('Copy a redundant plugin directory for backward compatibility.');
fs.existsSync(resolve('./dist/plugin')) || fs.mkdirSync(resolve('./dist/plugin'));
for(const name of fs.readdirSync(resolve('./dist/tool/plugin/'))) {
const [dist, plugin] = [resolve('./dist/plugin/' + name), `'../tool/plugin/${name}'`];
if (/\.js$/.test(name)) {
fs.writeFileSync(dist, `module.exports = require(${plugin});`);
} else if (/\.mjs/.test(name)) {
fs.writeFileSync(dist, `import { default as mod } from ${plugin};\nexport default mod;`);
}
}
});
}
},
],
experiments: target === 'esm' ? { outputModule: true, } : undefined,
target: 'web',
output: target === 'esm'
? {
filename: entry === 'pure' ? 'http-request-mock.pure.esm.mjs' : 'http-request-mock.esm.mjs',
path: path.resolve(__dirname, 'dist'),
library: {
type: 'module'
}
}
: {
filename: entry === 'pure' ? 'http-request-mock.pure.js' : 'http-request-mock.js',
path: path.resolve(__dirname, 'dist'),
library: 'HttpRequestMock',
globalObject: 'typeof self !== \'undefined\' ? self : this',
libraryTarget: 'umd',
libraryExport: 'default'
}
};
};