-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
webpack.config.js
228 lines (212 loc) · 7.83 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
const path = require("path");
const HtmlWebPackPlugin = require("html-webpack-plugin");
const ReactRefreshWebpackPlugin = require("@pmmmwh/react-refresh-webpack-plugin");
const TerserPlugin = require("terser-webpack-plugin");
const CopyPlugin = require("copy-webpack-plugin");
const ForkTsCheckerWebpackPlugin = require("fork-ts-checker-webpack-plugin");
const { createBabelConfig } = require("./babel.config.js");
const { DefinePlugin } = require("webpack");
const JSONManifestPlugin = require("./jsonmanifest-plugin");
const { version } = require("./package.json");
const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const createStats = verbose => ({
assets: verbose,
builtAt: verbose,
cached: false,
children: false,
chunks: false,
colors: true,
entrypoints: true,
hash: false,
modules: false,
performance: false,
timings: verbose,
version: verbose
});
const TGS_WEBPANEL_GITHUB_PATH =
"https://tgstation.github.io/tgstation-server-webpanel/webpanel/" + version + "/";
module.exports = (env, options) => {
const github = env.GITHUB_CD;
const prodLike = github || options.mode === "production";
const publicPath = github ? TGS_WEBPANEL_GITHUB_PATH : prodLike ? "/app/" : "/";
console.log(`Building in mode ${options.mode}${github ? "-GITHUB" : ""}`);
return {
context: path.resolve(__dirname),
// Our application entry point.
entry: {
main: ["./src"]
},
devtool: "source-map",
module: {
rules: [
{
test: /\.[jt]sx?$/,
exclude: /node_modules/,
use: [
{
loader: "babel-loader",
options: createBabelConfig({ mode: options.mode })
}
]
},
{
test: /\.css$/,
exclude: /node_modules/,
use: [MiniCssExtractPlugin.loader, "css-loader"],
sideEffects: true
},
{
test: /\.(scss)$/,
use: [MiniCssExtractPlugin.loader, "css-loader", "postcss-loader", "fast-sass-loader"],
sideEffects: true
},
{
test: /\.(png|jpe?g|gif|svg)$/i,
type: "asset/resource",
generator: {
publicPath: publicPath
}
}
]
},
performance: {
},
resolve: {
extensions: [".tsx", ".ts", ".jsx", ".js"],
// polyfill
fallback: {
http: require.resolve("stream-http"),
https: require.resolve("https-browserify"),
buffer: require.resolve("buffer"),
util: require.resolve("util")
}
},
output: {
path: path.resolve(__dirname, "./dist"), // cwp cant read this, it's dumb (even though this is the default as of wbpack5)
publicPath: publicPath,
filename: `[name]${prodLike ? ".[contenthash]" : ""}.bundle.js`,
chunkLoadTimeout: 15000,
clean: true
},
cache: {
type: "filesystem",
cacheLocation: path.resolve(__dirname, `.yarn/webpack/${options.mode}`),
buildDependencies: {
config: [__filename]
}
},
optimization: {
emitOnErrors: false,
minimizer: [
new TerserPlugin({
extractComments: false,
terserOptions: {
output: {
ascii_only: true,
comments: false
}
}
}),
new CssMinimizerPlugin({
minimizerOptions: {
cssModules: true,
},
}),
],
splitChunks: {
chunks: "all",
cacheGroups: {
packages: {
test: /[\\/]node_modules[\\/]/,
idHint: "packages"
},
api: {
priority: 1,
test: /[\\/]ApiClient[\\/]/,
idHint: "api",
enforce: true
},
styles: {
test: /\.(scss)$/,
idHint: "styles",
enforce: true
}
}
}
},
devServer: {
static: path.join(__dirname, "dist"),
compress: true,
hot: true,
host: "0.0.0.0", // technically insecure? don't put nudes in your app, helps to test with mobile
port: 8080,
historyApiFallback: true,
setupMiddlewares: (middlewares, devServer) => {
if (!devServer) {
throw new Error('webpack-dev-server is not defined');
}
middlewares.push({
name: 'channel-json',
middleware: (req, res, next) => {
if (!req.header("X-Webpanel-Fetch-Channel")) return next();
if (req.path === "/channel.json") return next();
let vary = req.header("Vary")
if (vary === undefined) vary = ""
if (vary) vary += ", "
vary += "X-Webpanel-Fetch-Channel"
res.header("Vary", vary)
res.redirect(301, "/channel.json")
},
});
return middlewares;
},
},
stats: createStats(true),
plugins: [
new MiniCssExtractPlugin(),
new CopyPlugin({
patterns: [
{
from: "public",
toType: "dir",
globOptions: {
ignore: prodLike ? ["**/channel.json"] : []
}
}
]
}),
new ForkTsCheckerWebpackPlugin(),
new DefinePlugin({
API_VERSION: JSON.stringify(
require("./src/ApiClient/generatedcode/swagger.json").info.version
),
VERSION: JSON.stringify(
github ? (env.GITHUB_TAG || env.GITHUB_SHA) : require("./package.json").version
),
MODE: JSON.stringify(prodLike ? (github ? "GITHUB" : "PROD") : "DEV"),
// The basepath remains /app because its for the router which is located at /app/
DEFAULT_BASEPATH: JSON.stringify(prodLike ? "/app/" : "/"),
DEFAULT_APIPATH: JSON.stringify(prodLike ? "/" : "http://localhost:5000/")
}),
!github &&
new HtmlWebPackPlugin({
filename: "index.html",
template: "./src/index.html",
inject: false,
publicPath: publicPath
}),
options.mode !== "production" &&
new ReactRefreshWebpackPlugin({
overlay: {
sockIntegration: "wds"
}
}),
new JSONManifestPlugin({ version: require("./package.json").version }),
/*new BundleAnalyzerPlugin({
openAnalyzer: false
})*/
].filter(Boolean)
};
};