This repository has been archived by the owner on May 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
108 lines (99 loc) · 2.71 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
// @ts-check
/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable @typescript-eslint/no-require-imports */
const path = require("path");
const {
HotModuleReplacementPlugin, DefinePlugin,
} = require("webpack");
const TerserPlugin = require("terser-webpack-plugin");
const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
const isProduction = process.env.NODE_ENV === "production" || (() => {
const { argv } = process;
const mode = argv.indexOf("--mode");
if (mode > 0) {
return argv[mode + 1] === "production";
}
return false;
})();
console.log(`Webpack running in ${isProduction ? "production" : "development"} mode.`);
const createModule = (target = "ES5") => ({
rules: [
{
test: /\.tsx?$/,
use: [
{
loader: "ts-loader",
options: {
compilerOptions: {
target,
},
},
},
],
},
],
});
/** @type {import("webpack").Configuration} */
const commonConfig = {
mode: isProduction ? "production" : "development",
devtool: "source-map",
resolve: {
extensions: [".ts", ".tsx", ".js"],
},
};
/**
* @param {string} name
*/
const createClientConfig = (name, target = "ES5") => {
/** @type {import("webpack").Configuration} */
const clientConfig = {
...commonConfig,
module: createModule(target),
entry: isProduction ? ["./src/entry/client.ts"] : ["webpack-hot-middleware/client", "./src/entry/client.ts"],
output: {
path: path.resolve(__dirname, "build/client"),
filename: `${name}.js`,
},
plugins: [],
optimization: {
minimizer: [
new TerserPlugin({
sourceMap: true,
terserOptions: {
ecma: 2019,
},
}),
],
},
};
if (isProduction) {
if (name === "modern") {
clientConfig.plugins.push(new BundleAnalyzerPlugin({
analyzerMode: "static",
openAnalyzer: false,
reportFilename: "webpack-bundle-analyzer.html",
}));
}
} else {
clientConfig.plugins.push(new HotModuleReplacementPlugin());
}
return clientConfig;
};
/** @type {import("webpack").Configuration} */
const serverConfig = {
...commonConfig,
devtool: isProduction ? "source-map" : undefined,
module: createModule("ES2019"),
entry: isProduction ? "./src/productionStart.ts" : "./src/entry/server.ts",
output: {
libraryTarget: "commonjs",
path: path.resolve(__dirname, "build/server"),
filename: "server.js",
},
target: "node",
optimization: {
minimize: false,
},
plugins: [new DefinePlugin({ BUILD_DATE: new Date().getTime() })],
};
module.exports = [createClientConfig("modern", "ES2019"), createClientConfig("legacy", "ES5"), serverConfig];