forked from zulip/zulip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.ts
282 lines (274 loc) · 10.8 KB
/
webpack.config.ts
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/// <reference types="webpack-dev-server" />
import path from "path";
import CssMinimizerPlugin from "css-minimizer-webpack-plugin";
import HtmlWebpackPlugin from "html-webpack-plugin";
import MiniCssExtractPlugin from "mini-css-extract-plugin";
import TerserPlugin from "terser-webpack-plugin";
import webpack from "webpack";
import BundleTracker from "webpack-bundle-tracker";
import DebugRequirePlugin from "./tools/debug-require-webpack-plugin";
import assets from "./tools/webpack.assets.json";
import dev_assets from "./tools/webpack.dev-assets.json";
const cacheLoader: webpack.RuleSetUseItem = {
loader: "cache-loader",
options: {
cacheDirectory: path.resolve(__dirname, "var/webpack-cache"),
},
};
export default (_env: unknown, argv: {mode?: string}): webpack.Configuration[] => {
const production: boolean = argv.mode === "production";
const config: webpack.Configuration = {
name: "frontend",
mode: production ? "production" : "development",
context: __dirname,
entry: production
? assets
: Object.fromEntries(
Object.entries({...assets, ...dev_assets}).map(([name, paths]) => [
name,
[...paths, "./static/js/debug"],
]),
),
module: {
rules: [
{
test: require.resolve("./static/js/zulip_test"),
loader: "expose-loader",
options: {exposes: "zulip_test"},
},
{
test: require.resolve("./tools/debug-require"),
loader: "expose-loader",
options: {exposes: "require"},
},
{
test: require.resolve("jquery"),
loader: "expose-loader",
options: {exposes: ["$", "jQuery"]},
},
// Generate webfont
{
test: /\.font\.js$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: "css-loader",
options: {
url: false, // webfonts-loader generates public relative URLs
},
},
{
loader: "webfonts-loader",
options: {
fileName: production
? "files/[fontname].[chunkhash].[ext]"
: "files/[fontname].[ext]",
publicPath: "",
},
},
],
},
// Transpile .js and .ts files with Babel
{
test: /\.(js|ts)$/,
include: [
path.resolve(__dirname, "static/shared/js"),
path.resolve(__dirname, "static/js"),
],
use: [cacheLoader, "babel-loader"],
},
// regular css files
{
test: /\.css$/,
exclude: path.resolve(__dirname, "static/styles"),
use: [
MiniCssExtractPlugin.loader,
cacheLoader,
{
loader: "css-loader",
options: {
sourceMap: true,
},
},
],
},
// PostCSS loader
{
test: /\.css$/,
include: path.resolve(__dirname, "static/styles"),
use: [
MiniCssExtractPlugin.loader,
cacheLoader,
{
loader: "css-loader",
options: {
importLoaders: 1,
sourceMap: true,
},
},
{
loader: "postcss-loader",
options: {
sourceMap: true,
},
},
],
},
{
test: /\.hbs$/,
use: [
cacheLoader,
{
loader: "handlebars-loader",
options: {
ignoreHelpers: true,
// Tell webpack not to explicitly require these.
knownHelpers: [
"if",
"unless",
"each",
"with",
// The ones below are defined in static/js/templates.js
"plural",
"eq",
"and",
"or",
"not",
"t",
"tr",
"rendered_markdown",
],
preventIndent: true,
},
},
],
},
// load fonts and files
{
test: /\.(eot|jpg|svg|ttf|otf|png|woff2?)$/,
use: [
{
loader: "file-loader",
options: {
name: production ? "[name].[hash].[ext]" : "[path][name].[ext]",
outputPath: "files/",
},
},
],
},
],
},
output: {
path: path.resolve(__dirname, "static/webpack-bundles"),
filename: production ? "[name].[contenthash].js" : "[name].js",
chunkFilename: production ? "[contenthash].js" : "[id].js",
},
resolve: {
extensions: [".ts", ".js"],
},
// We prefer cheap-module-source-map over any eval-* options
// because stacktrace-gps doesn't currently support extracting
// the source snippets with the eval-* options.
devtool: production ? "source-map" : "cheap-module-source-map",
optimization: {
minimizer: [
new CssMinimizerPlugin({
sourceMap: true,
minify: (data: Record<string, string>, sourceMap) => {
// css-minimizer-webpack-plugin needs this require
// inside the function.
// eslint-disable-next-line @typescript-eslint/consistent-type-imports, @typescript-eslint/no-var-requires
const CleanCSS: typeof import("clean-css") = require("clean-css");
const [[filename, styles]] = Object.entries(data);
const out = new CleanCSS({sourceMap: true}).minify({
[filename]: {styles, sourceMap},
});
return {css: out.styles, map: out.sourceMap, warnings: out.warnings};
},
}),
new TerserPlugin({
cache: true,
parallel: true,
}),
],
splitChunks: {
chunks: "all",
// webpack/examples/many-pages suggests 20 requests for HTTP/2
maxAsyncRequests: 20,
maxInitialRequests: 20,
},
},
plugins: [
new webpack.ProgressPlugin({
handler(percentage) {
if (percentage === 1) {
console.log(
"\u001B[34mi 「wdm」\u001B[0m:",
"Webpack compilation successful.",
);
}
},
}),
new DebugRequirePlugin(),
new BundleTracker({
filename: production
? "../../webpack-stats-production.json"
: "../../var/webpack-stats-dev.json",
relativePath: true,
// Respecify many defaults until https://github.com/django-webpack/webpack-bundle-tracker/pull/55 is merged
path: path.resolve(__dirname, "static/webpack-bundles"),
integrity: false,
integrityHashes: [],
}),
...(production
? []
: [
// Better logging from console for hot reload
new webpack.NamedModulesPlugin(),
// script-loader should load sourceURL in dev
new webpack.LoaderOptionsPlugin({debug: true}),
]),
// Extract CSS from files
new MiniCssExtractPlugin({
filename: production ? "[name].[contenthash].css" : "[name].css",
chunkFilename: production ? "[contenthash].css" : "[id].css",
}),
new HtmlWebpackPlugin({
filename: "5xx.html",
template: "static/html/5xx.html",
chunks: ["error-styles"],
}),
],
devServer: {
clientLogLevel: "error",
headers: {
"Access-Control-Allow-Origin": "*",
},
publicPath: "/webpack/",
stats: "errors-only",
noInfo: true,
watchOptions: {
ignored: [
"**/node_modules/**",
// Prevent Emacs file locks from crashing webpack-dev-server
// https://github.com/webpack/webpack-dev-server/issues/2821
"**/.#*",
],
},
},
};
const serverConfig: webpack.Configuration = {
name: "server",
mode: production ? "production" : "development",
target: "node",
context: __dirname,
entry: {
"katex-cli": "shebang-loader!katex/cli",
},
output: {
path: path.resolve(__dirname, "static/webpack-bundles"),
filename: "[name].js",
},
};
return [config, serverConfig];
};