forked from online-go/online-go.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
198 lines (174 loc) · 6.42 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
"use strict";
var path = require("path");
let fs = require("fs");
var webpack = require("webpack");
const pkg = require("./package.json");
const TerserPlugin = require("terser-webpack-plugin");
const ForkTsCheckerWebpackPlugin = require("fork-ts-checker-webpack-plugin");
const CircularDependencyPlugin = require("circular-dependency-plugin");
const BundleAnalyzerPlugin = require("webpack-bundle-analyzer").BundleAnalyzerPlugin;
let plugins = [];
plugins.push(
new ForkTsCheckerWebpackPlugin({
typescript: {
diagnosticOptions: {
syntactic: true,
semantic: true,
declaration: true,
global: true,
},
},
}),
);
plugins.push(
new webpack.BannerPlugin(
`Copyright (C) Online-Go.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
`,
),
);
module.exports = (env, argv) => {
const production = argv.mode === "production";
let alias = {};
if (production) {
console.log("Production build, enabling react profiling");
alias = {
"react-dom$": "react-dom/profiling",
"scheduler/tracing": "scheduler/tracing-profiling",
};
}
plugins.push(
new CircularDependencyPlugin({
// LearningHub is because it's all internal and fixing it would involve globbing a lot of the broken out pages together.
// Player is because PlayerDetails has the Player link in it. We should probably make a lighter weight Player
// that can be used there instead, but doesn't seem worth the trouble right now as it's an internal thing anyways.
exclude: /node_modules|LearningHub|Player/,
failOnError: true,
allowAsyncCycles: false,
cwd: process.cwd(),
onDetected({ module: webpackModuleRecord, paths, compilation }) {
compilation.errors.push(new Error("Circular dependency found:\n " + paths.join("\n -> ")));
},
}),
);
plugins.push(
new webpack.EnvironmentPlugin({
NODE_ENV: production ? "production" : "development",
DEBUG: false,
}),
);
let defines = {
CLIENT: true,
SERVER: false,
};
plugins.push(new webpack.DefinePlugin(defines));
if (process.env.ANALYZE) {
plugins.push(
new BundleAnalyzerPlugin({
analyzerPort: 18888,
}),
);
}
const config = {
mode: production ? "production" : "development",
entry: {
ogs: "./src/main.tsx",
},
resolve: {
modules: ["src/lib", "src/components", "src/views", "src", "node_modules"],
alias: alias,
extensions: [".webpack.js", ".web.js", ".ts", ".tsx", ".js"],
},
output: {
path: __dirname + "/dist",
filename: production ? "[name].min.js" : "[name].js",
},
module: {
rules: [
{
test: /\.js$/,
use: ["source-map-loader"],
enforce: "pre",
},
// All files with a '.ts' or '.tsx' extension will be handled by 'ts-loader'.
{
test: /\.tsx?$/,
exclude: /node_modules/,
use: [
// cache is set to true for development in webpack 5 https://webpack.js.org/configuration/cache/
// { loader: 'cache-loader' },
{
loader: "ts-loader",
options: {
configFile: "tsconfig.json",
transpileOnly: true,
happyPackMode: true,
},
},
],
},
],
},
performance: {
maxAssetSize: 1024 * 1024 * 2.5,
maxEntrypointSize: 1024 * 1024 * 2.5,
},
optimization: {
splitChunks: {
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/, // <-- use the test property to specify which deps go here
name: "vendor",
chunks: "all",
priority: -10,
},
},
},
minimizer: [
new TerserPlugin({
parallel: true,
terserOptions: {},
}),
],
},
plugins: plugins,
//devtool: production ? 'source-map' : 'eval-source-map',
/* NOTE: The default needs to be source-map for the i18n translation stuff to work. Specifically, using eval-source-map makes it impossible for our xgettext-js parser to parse the embedded source. */
devtool: "source-map",
// When importing a module whose path matches one of the following, just
// assume a corresponding global variable exists and use that instead.
// This is important because it allows us to avoid bundling all of our
// dependencies, which allows browsers to cache those libraries between builds.
externals: {
goban: "goban",
},
devServer: {
stats: {
assets: true,
children: false,
chunks: true,
hash: true,
modules: true,
publicPath: true,
timings: true,
version: true,
warnings: true,
},
},
};
if (!production) {
config.optimization.removeAvailableModules = false;
config.optimization.removeEmptyChunks = false;
config.optimization.splitChunks = false;
}
return config;
};