-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
354 lines (341 loc) · 9.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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
const pkg = require("./package.json"),
fs = require("fs"),
path = require("path"),
{ BundleAnalyzerPlugin } = require("webpack-bundle-analyzer"),
CaseSensitivePathsPlugin = require("case-sensitive-paths-webpack-plugin"),
{ CleanWebpackPlugin } = require("clean-webpack-plugin"),
CopyWebpackPlugin = require("copy-webpack-plugin"),
FaviconsWebpackPlugin = require("favicons-webpack-plugin"),
FriendlyErrorsWebpackPlugin = require("friendly-errors-webpack-plugin"),
HtmlWebpackPlugin = require("html-webpack-plugin"),
ManifestPlugin = require("webpack-manifest-plugin"),
MiniCssExtractPlugin = require("mini-css-extract-plugin"),
OptimizeCssnanoPlugin = require("@intervolga/optimize-cssnano-plugin"),
PreloadWebpackPlugin = require("preload-webpack-plugin"),
WorkboxWebpackPlugin = require("workbox-webpack-plugin"),
{
EnvironmentPlugin,
HashedModuleIdsPlugin,
NamedChunksPlugin
} = require("webpack");
// Indicate if the build is optimised for production deployment.
const isProduction = process.env.NODE_ENV === "production";
// Indicate if the `webpack-dev-server` should be running with HTTPS.
const isSSLEnabled = process.env.HTTP_SSL_ENABLED === "true";
// Indicate the folder that contains Svelte SPA source code.
const srcDir = "web/src";
// Indicate the folder that contains the optimised build assets.
const distDir = "dist";
// Indicate the folder that contains the public assets which are directly copied over to `distDir`.
const publicDir = "web/public";
// Indicate the SSL key/cert file location which will be used by `webpack-dev-server` when `HTTP_SSL_ENABLED` is
// set to `true`. By default, `HTTP_SSL_CERT_PATH` is set to `./tmp/ssl`.
const ssl = {
key: `${process.env.HTTP_SSL_CERT_PATH}/key.pem`,
cert: `${process.env.HTTP_SSL_CERT_PATH}/cert.pem`
};
// Indicate the HTTPS configuration for `webpack-dev-server` to use when `HTTP_SSL_ENABLED` is set to `true`.
const https = (() => {
return isSSLEnabled && fs.existsSync(ssl.key) && fs.existsSync(ssl.cert)
? {
key: fs.readFileSync(path.resolve(__dirname, ssl.key)),
cert: fs.readFileSync(path.resolve(__dirname, ssl.cert))
}
: false;
})();
// Indicate the server-side rendering routes which is set by appy's `start` and `build` commands so that the service
// worker doesn't handle navigation fallback to `/index.html` when the current route matching 1 of these routes.
const ssrRoutes = (() => {
let routes = [];
if (
process.env.APPY_SSR_ROUTES !== undefined &&
process.env.APPY_SSR_ROUTES !== ""
) {
routes = routes.concat(process.env.APPY_SSR_ROUTES.split(","));
}
return routes;
})();
// Configure the `webpack-dev-server` for local development use.
const devServer = {
historyApiFallback: true,
https,
host: process.env.HTTP_HOST || "0.0.0.0",
port:
parseInt(
isSSLEnabled
? process.env.HTTP_SSL_PORT || 3443
: process.env.HTTP_PORT || 3000
) + 1,
hot: true,
overlay: {
warnings: true,
errors: true
}
};
module.exports = {
mode: isProduction ? "production" : "development",
devServer,
devtool: isProduction ? "false" : "source-map",
entry: {
app: path.resolve(__dirname, srcDir, "main.ts")
},
module: {
rules: [
{
test: /\.css$/,
use: [MiniCssExtractPlugin.loader, "css-loader", "postcss-loader"]
},
{
test: /\.(png|jpe?g|gif|webp)(\?.*)?$/,
use: [
{
loader: "url-loader",
options: {
limit: 4096,
fallback: {
loader: "file-loader",
options: {
name: "images/[name].[contenthash:12].[ext]"
}
}
}
}
]
},
{
test: /\.(svg)(\?.*)?$/,
use: [
{
loader: "file-loader",
options: {
name: "images/[name].[contenthash:12].[ext]"
}
}
]
},
{
test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
use: [
{
loader: "url-loader",
options: {
limit: 4096,
fallback: {
loader: "file-loader",
options: {
name: "medias/[name].[contenthash:12].[ext]"
}
}
}
}
]
},
{
test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/i,
use: [
{
loader: "url-loader",
options: {
limit: 4096,
fallback: {
loader: "file-loader",
options: {
name: "fonts/[name].[contenthash:12].[ext]"
}
}
}
}
]
},
{
test: /\.tsx?$/,
exclude: /node_modules/,
use: [
{
loader: "babel-loader?cacheDirectory=true"
},
{
loader: "ts-loader",
options: {
transpileOnly: true,
happyPackMode: false,
appendTsxSuffixTo: ["\\.svelte$"]
}
}
]
},
{
test: /\.svelte$/,
exclude: /node_modules/,
use: [
{
loader: "babel-loader?cacheDirectory=true"
},
{
loader: "svelte-loader",
options: {
emitCss: isProduction,
hotReload: !isProduction,
preprocess: require("./svelte.config").preprocess
}
}
]
}
]
},
output: {
chunkFilename: isProduction
? "scripts/[name].[contenthash:12].js"
: "scripts/[name].js",
filename: isProduction
? "scripts/[name].[contenthash:12].js"
: "scripts/[name].js",
path: path.resolve(__dirname, distDir),
publicPath: "/"
},
plugins: [
new CleanWebpackPlugin(),
new EnvironmentPlugin({
NODE_ENV: process.env.NODE_ENV,
BASE_URL: "/"
}),
new CaseSensitivePathsPlugin(),
new FriendlyErrorsWebpackPlugin({
additionalTransformers: [],
additionalFormatters: []
}),
...(isProduction
? [
new MiniCssExtractPlugin({
filename: "styles/[name].[contenthash:12].css",
chunkFilename: "styles/[name].[contenthash:12].css"
}),
new OptimizeCssnanoPlugin({
sourceMap: true,
cssnanoOptions: {
preset: [
"default",
{
mergeLonghand: false,
cssDeclarationSorter: false
}
]
}
}),
new HashedModuleIdsPlugin({
hashDigest: "hex"
}),
new NamedChunksPlugin(function() {})
]
: []),
new HtmlWebpackPlugin({
title: pkg.name,
template: path.resolve(__dirname, publicDir, "index.html"),
minify: isProduction
? {
removeComments: true,
collapseWhitespace: true,
removeAttributeQuotes: true,
collapseBooleanAttributes: true,
removeScriptTypeAttributes: true
}
: {}
}),
new PreloadWebpackPlugin({
rel: "preload",
include: "initial",
fileBlacklist: [/\.map$/, /hot-update\.js$/]
}),
new PreloadWebpackPlugin({
rel: "prefetch",
include: "asyncChunks"
}),
new CopyWebpackPlugin(
[
{
from: path.resolve(__dirname, publicDir),
to: path.resolve(__dirname, distDir),
toType: "dir",
ignore: [
{
glob: "index.html",
matchBase: false
}
]
},
{
from: path.resolve(__dirname, "assets"),
to: path.resolve(
__dirname,
`${distDir}/[path][name].[contenthash:12].[ext]`
)
}
],
{ copyUnmodified: true, ignore: [".DS_Store", ".gitkeep"] }
),
new ManifestPlugin({
map: function(file) {
file.name = file.name.replace(/(\.[a-z0-9]{12})(\..*)$/i, "$2");
return file;
}
}),
...(isProduction
? [
new BundleAnalyzerPlugin({
analyzerMode: "disabled",
analyzerHost: devServer.host,
analyzerPort: parseInt(devServer.port) + 2,
openAnalyzer: false
})
]
: []),
new FaviconsWebpackPlugin({
cache: !isProduction,
favicons: Object.assign(
{},
(() =>
Object.assign({}, pkg.pwa, {
appName: pkg.name,
appShortName: pkg.name,
appDescription: pkg.description
}))(),
{
icons: {
coast: false,
firefox: false,
yandex: false
}
}
),
inject: true,
logo: path.resolve(__dirname, `${srcDir}/assets/images/logo.png`),
prefix: "pwa/"
}),
new WorkboxWebpackPlugin.GenerateSW({
skipWaiting: true,
clientsClaim: true,
navigateFallback: "/index.html",
navigateFallbackBlacklist: ssrRoutes
.concat(["/service-worker.js"])
.map(p => new RegExp(p))
})
],
resolve: {
alias: {
"@": path.resolve(__dirname, srcDir),
svelte: path.resolve(__dirname, "node_modules", "svelte")
},
extensions: [".js", ".jsx", ".json", ".mjs", ".svelte", ".ts", ".tsx"],
mainFields: ["svelte", "browser", "module", "main"]
},
stats: isProduction
? {
assets: true,
assetsSort: "!size",
builtAt: false,
children: false,
colors: true,
modules: false
}
: "minimal"
};