-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebpack.config.js
128 lines (121 loc) · 3.46 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
118
119
120
121
122
123
124
125
126
127
128
const path = require("path");
const { EnvironmentPlugin } = require("webpack");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const TerserPlugin = require("terser-webpack-plugin");
const BundleAnalyzerPlugin = require("webpack-bundle-analyzer")
.BundleAnalyzerPlugin;
// NOTE ClosureWebpackPlugin doesn't work with debug flags, as of 4 Sep 2020.
// https://github.com/webpack-contrib/closure-webpack-plugin/issues/118
// const ClosurePlugin = require("closure-webpack-plugin");
module.exports = (env) => {
let mode = "production";
let debug = false;
let useLocalBabylon = false;
let minifyAssets = true;
let useSourceMap = false;
let useAnalyzer = false;
switch (env.TARGET) {
case "dev":
mode = "development";
debug = true;
minifyAssets = false;
useSourceMap = true;
useLocalBabylon = true;
break;
case "build":
useLocalBabylon = true;
break;
case "zip":
break;
case "analyze":
useAnalyzer = true;
break;
}
env.DEBUG = debug;
const config = {
mode,
entry: "./site/index.js",
output: {
path: path.resolve(__dirname, "public"),
filename: "game.js",
},
stats: { preset: "minimal" },
devtool: useSourceMap ? "eval-cheap-source-map" : false,
externals: { BABYLON: "BABYLON" },
plugins: [
new CleanWebpackPlugin(),
new EnvironmentPlugin(env),
new HtmlWebpackPlugin({
inject: "head",
scriptLoading: "defer",
template: path.resolve(__dirname, "site/index.html"),
templateParameters: {
debugScript: debug ? "<script defer src='debug.js'></script>" : "",
babylonSrc: useLocalBabylon
? "babylon.js"
: "https://js13kgames.com/webxr-src/2020/babylon.js",
},
minify: minifyAssets
? {
collapseWhitespace: true,
minifyCSS: true,
removeComments: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
useShortDoctype: true,
}
: false,
}),
],
};
if (debug || useLocalBabylon) {
const patterns = [];
if (useLocalBabylon) {
patterns.push({ from: "site/vendor/babylon.js", to: "babylon.js" });
}
if (debug) {
patterns.push({ from: "site/debug.js", to: "debug.js" });
}
config.plugins.push(new CopyWebpackPlugin({ patterns }));
}
if (useAnalyzer) {
config.plugins.push(new BundleAnalyzerPlugin());
}
if (minifyAssets) {
config.optimization = {
minimizer: [
new TerserPlugin({
extractComments: false,
exclude: /babylon\.js$/,
terserOptions: {
ecma: 2019,
compress: {
booleans_as_integers: true,
// drop_console: true,
ecma: 2019,
keep_fargs: false,
module: true,
passes: 2,
},
mangle: {
properties: {
regex: /^[_$]/,
reserved: ["___", "__M"],
// debug: "",
},
module: true,
toplevel: true,
},
output: {
comments: false,
},
},
}),
],
};
}
return config;
};