-
Notifications
You must be signed in to change notification settings - Fork 2k
/
webpack.demo.config.js
107 lines (102 loc) · 3.57 KB
/
webpack.demo.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
/* eslint-env es6 */
"use strict";
const webpack = require('webpack');
const path = require('path');
const pkg = require('./package.json');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const HtmlWebpackPlugin = require('html-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
module.exports = (env = {}) => {
const version = pkg.version;
const analyze = env.ANALYZE || false;
const devServer = Boolean(process.env.WEBPACK_DEV_SERVER);
const minimize = !devServer;
const sizeThreshold = 600 * 1024;
const publicPath = devServer ? '/' : './js';
const devPath = './src/module/main.js';
const buildPath = './build/matter.js';
const resolve = relativePath => path.resolve(__dirname, relativePath);
const name = 'matter-demo';
const banner =
`${name} bundle ${version} by @liabru
${pkg.homepage}
License ${pkg.license}`;
return {
entry: { [name]: './demo/src/index.js' },
node: false,
devtool: devServer ? false : 'none',
output: {
library: 'MatterDemo',
libraryTarget: 'umd',
umdNamedDefine: true,
globalObject: 'this',
publicPath,
path: resolve('./demo/js'),
filename: `[name].[contenthash:6]${minimize ? '.min' : ''}.js`
},
resolve: {
alias:{
'matter-js': resolve(devPath),
'MatterDev': resolve(devPath),
'MatterBuild': resolve(
devServer ? buildPath : devPath
)
}
},
optimization: {
minimize,
minimizer: [new TerserPlugin({ extractComments: false })],
moduleIds: 'hashed',
runtimeChunk: { name: `${name}.main` },
splitChunks: {
automaticNameDelimiter: '.',
cacheGroups: {
default: false,
vendors: false,
vendor: {
chunks: 'all',
enforce: true,
test: /[\\/]node_modules[\\/]/,
name: module => {
if (devServer) {
return `${name}.vendor`;
}
const parser = /[\\/]node_modules[\\/](.*?)([\\/]|$)/;
const moduleName = module.context.match(parser)[1];
return `${name}.${moduleName.replace('@', '')}`;
}
},
},
},
},
performance: {
maxEntrypointSize: sizeThreshold,
maxAssetSize: sizeThreshold
},
plugins: [
new webpack.BannerPlugin(banner),
new webpack.DefinePlugin({
__MATTER_VERSION__: JSON.stringify('*'),
__MATTER_IS_DEV__: devServer
}),
new HtmlWebpackPlugin({
template: resolve('./demo/src/index.ejs'),
filename: devServer ? 'index.html'
: resolve('./demo/index.html'),
inject: false,
minify: false,
publicPath
})
].concat(analyze ? [new BundleAnalyzerPlugin({
openAnalyzer: true
})] : []),
devServer: {
contentBase: [resolve('./demo')],
watchContentBase: true,
hot: false,
compress: true,
overlay: true,
port: 8000
}
};
};