-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.common.js
68 lines (67 loc) · 1.88 KB
/
webpack.common.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
const { Compilation, ProvidePlugin } = require('webpack');
const { ConcatSource, RawSource } = require('webpack-sources');
const TerserPlugin = require('terser-webpack-plugin');
module.exports = (outputName, production) => ({
entry: './src/initialize.tsx',
mode: production ? 'production' : 'development',
// sourcemaps don't work; generated code is readable anyawys
devtool: false,
optimization: {
minimize: production,
minimizer: production
? [new TerserPlugin({
test: /.*/,
parallel: true,
extractComments: {
banner: false
}
})]
: []
},
module: {
rules: [
{
test: /\.[jt]sx?$/,
exclude: /node_modules/,
loader: 'babel-loader'
}
]
},
resolve: {
extensions: ['.ts', '.tsx', '.js', '.jsx']
},
output: {
filename: outputName
},
plugins: [
new ProvidePlugin({
process: 'process/browser',
Buffer: ['buffer', 'Buffer']
}),
{
// wrap output in script tags
// pile of hacks
apply(compiler) {
compiler.hooks.compilation.tap('ScriptWrapPlugin', compilation => {
compilation.hooks.processAssets.tap(
{
name: 'ScriptWrapPlugin',
stage: Compilation.PROCESS_ASSETS_STAGE_SUMMARIZE
},
() => {
const source = new ConcatSource();
// unicode BOM to ensure UTF-8 encoding
source.add(new RawSource(Buffer.from([0xef, 0xbb, 0xbf])));
if (!production) source.add('<!doctype html>\n<script>\n');
else source.add('<script>');
source.add(compilation.assets[outputName]);
if (!production) source.add('\n</script>\n');
else source.add('</script>');
compilation.assets[outputName] = source;
}
);
});
}
}
]
});