-
Notifications
You must be signed in to change notification settings - Fork 22
/
webpack.config.js
208 lines (203 loc) · 6 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
module.exports = function (env = {}) {
/* eslint import/no-extraneous-dependencies:0, global-require:0, func-names:0 */
const webpack = require("webpack");
const { EsbuildPlugin } = require("esbuild-loader");
const path = require("path");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const browserslistToEsbuild = require("browserslist-to-esbuild");
const CopyPlugin = require("copy-webpack-plugin");
const srcPath = path.resolve(__dirname, "frontend/javascripts/");
const nodePath = "node_modules";
const protoPath = path.join(__dirname, "webknossos-datastore/proto/");
const publicPath = "/assets/bundle/";
const buildTarget = browserslistToEsbuild([
"last 3 Chrome versions",
"last 3 Firefox versions",
"last 2 Edge versions",
"last 1 Safari versions",
"last 1 iOS versions",
]);
const plugins = [
new webpack.DefinePlugin({
"process.env.NODE_ENV": env.production ? '"production"' : '"development"',
"process.env.BABEL_ENV": process.env.BABEL_ENV,
}),
new webpack.IgnorePlugin({ resourceRegExp: /^\.\/locale$/, contextRegExp: /moment$/ }),
new webpack.ProvidePlugin({
// Needed for saxophone, i.e. readable-stream, since it is used without importing
// Corresponding issue: https://github.com/nodejs/readable-stream/issues/450
process: "process/browser",
}),
new MiniCssExtractPlugin({
filename: "[name].css",
chunkFilename: "[name].[contenthash].css",
}),
new CopyPlugin({
patterns: [
// For CSP, see https://gildas-lormeau.github.io/zip.js/api/interfaces/Configuration.html#workerScripts
{ from: "node_modules/@zip.js/zip.js/dist/z-worker.js", to: "[name][ext]" },
],
}),
];
const cssLoaderUrlFilter = {
// Don't try to handle urls that already point to the assets directory
filter: (url) => !url.startsWith("/assets/"),
};
return {
experiments: { asyncWebAssembly: true },
entry: {
main: "main.tsx",
},
mode: env.production ? "production" : "development",
output: {
path: `${__dirname}/public/bundle`,
filename: "[name].js",
sourceMapFilename: "[file].[contenthash].map",
chunkFilename: "[name].[contenthash].js",
publicPath,
},
module: {
rules: [
{
test: /\.worker\.ts$/,
use: [
{
loader: "worker-loader",
options: {
filename: "[name].[contenthash].worker.js",
},
},
],
},
{
test: /\.tsx?$/,
exclude: /(node_modules|bower_components)/,
loader: "esbuild-loader",
options: {
target: buildTarget,
},
},
{
test: /\.less$/,
use: [
MiniCssExtractPlugin.loader,
{ loader: "css-loader", options: { url: cssLoaderUrlFilter } },
{
loader: "less-loader",
options: {
lessOptions: {
javascriptEnabled: true,
},
},
},
],
},
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
{ loader: "css-loader", options: { url: cssLoaderUrlFilter } },
{
loader: "less-loader",
options: {
lessOptions: {
javascriptEnabled: true,
},
},
},
],
},
{
test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
type: "asset",
parser: { dataUrlCondition: { maxSize: 10000 } },
// generator: { mimetype: "application/font-woff" },
},
{
test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
type: "asset/resource",
},
{
test: /\.png$/,
type: "asset",
parser: { dataUrlCondition: { maxSize: 10000 } },
},
{ test: /\.jpg$/, type: "asset/resource" },
{ test: /\.proto$/, use: ["json-loader", "proto-loader6"] },
{
test: /\.m?js/,
resolve: {
fullySpecified: false,
},
},
],
},
resolve: {
modules: [srcPath, nodePath, protoPath],
alias: {
react: path.resolve("./node_modules/react"),
},
extensions: [".ts", ".tsx", ".js", ".json"],
fallback: {
// Needed for jsonschema
url: require.resolve("url/"),
// Needed for lz4-wasm-nodejs (which is only used in test context, which is
// why we use empty modules)
path: false,
util: false,
fs: false,
// Needed for mock-require
module: false,
},
},
optimization: {
minimize: env.production,
minimizer: [
new EsbuildPlugin({
target: buildTarget, // Syntax to transpile to (see options below for possible values)
}),
],
splitChunks: {
chunks: "all",
// Use a consistent name for the vendors chunk
name: "vendors~main",
cacheGroups: {
html2canvas: {
test: /[\\/]node_modules[\\/](html2canvas)[\\/]/,
chunks: "all",
name: "vendors~html2canvas",
},
},
},
},
// See https://webpack.js.org/configuration/devtool/
devtool: env.production ? "source-map" : "eval-source-map",
plugins,
devServer: {
static: {
directory: `${__dirname}/public`,
},
devMiddleware: {
publicPath,
},
port: env.PORT != null ? env.PORT : 9002,
hot: false,
liveReload: false,
client: {
overlay: false,
logging: "none",
},
},
cache: {
type: "filesystem",
buildDependencies: {
config: [__filename],
},
},
stats: {
preset: "minimal",
},
// Ignore the lengthy warning considering STLExporter which is added to the exports dynamically
ignoreWarnings: [/export 'STLExporter'/],
};
};