-
Notifications
You must be signed in to change notification settings - Fork 2
/
webpack.config.ts
237 lines (216 loc) · 6.67 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
import * as path from 'path';
import * as webpack from 'webpack';
import * as MiniCssExtractPlugin from 'mini-css-extract-plugin';
const HtmlMinimizerPlugin = require('html-minimizer-webpack-plugin');
import * as CssMinimizerPlugin from 'css-minimizer-webpack-plugin';
import * as TerserPlugin from 'terser-webpack-plugin';
import * as HtmlWebpackPlugin from 'html-webpack-plugin';
import {CleanWebpackPlugin} from 'clean-webpack-plugin';
import * as CopyPlugin from 'copy-webpack-plugin';
import * as CompressionPlugin from 'compression-webpack-plugin';
import {WebpackManifestPlugin} from 'webpack-manifest-plugin';
import type {Manifest} from 'webpack-manifest-plugin';
import type * as devServer from 'webpack-dev-server';
const isProduction = process.env.NODE_ENV === 'production';
const isDev = process.env.NODE_ENV === 'development';
const outputDirectory = path.resolve(__dirname, 'dist');
const publicDirectory = path.resolve(__dirname, 'public');
const config: webpack.Configuration | {devServer?: devServer.Configuration} = {
mode: isDev ? 'development' : 'production',
devServer: {
compress: true,
port: process.env.PORT || 8080,
},
// Can be extended with other entry points.
entry: ['./src/index.ts'],
output: {
path: outputDirectory,
// We want to hash filenames in production, so changed files
// would have a different name and won't be cached.
filename: isDev ? '[name].js' : '[name].[contenthash:8].js',
// Needed if we use code splitting.
chunkFilename: isDev ? '[name].js' : '[name].[contenthash:8].chunk.js',
publicPath: '/',
},
// Fail out on the first error instead of tolerating it.
bail: !isDev,
// This option controls if and how source maps are generated.
devtool: isDev ? 'cheap-module-source-map' : 'source-map',
optimization: {
minimize: isProduction,
minimizer: [new HtmlMinimizerPlugin(), new CssMinimizerPlugin(), new TerserPlugin()],
},
watchOptions: {
ignored: /node_modules/,
},
// This allows us to write 'import filename' without extension.
resolve: {
extensions: ['.js', '.ts', '.glsl', '.vert', '.frag'],
},
module: {
// Makes missing exports an error instead of warning.
strictExportPresence: true,
rules: [
{
// Ignore Declaration typescript files.
exclude: /\.d\.ts/,
},
{
test: /\.worker\.[tj]s$/,
loader: 'worker-loader',
},
{
test: /\.(js|mjs|jsx|ts|tsx)$/,
exclude: /node_modules/,
loader: 'ts-loader',
},
{
test: /\.s[ac]ss$/i,
use: [
isDev ? 'style-loader' : MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
sourceMap: isDev,
modules: {
auto: true,
localIdentName: isDev ? '[name]__[local]_[contenthash:8]' : '[hash:base64]',
},
},
},
],
},
{
test: /\.css$/,
use: [
isDev ? 'style-loader' : MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
sourceMap: isDev,
},
},
],
},
{
test: /\.(jpe?g|png|gif|webp)$/,
loader: 'url-loader',
options: {
limit: 10 * 1024,
},
},
{
test: /\.svg$/,
exclude: /node_modules/,
use: [
{
loader: 'svg-url-loader',
options: {
limit: 10 * 1024,
noquotes: true,
},
},
{
loader: 'svgo-loader',
options: {
plugins: [{removeTitle: true}, {mergePaths: true}],
multipass: true,
},
},
],
},
{
test: /\.(glsl|vert|frag)$/,
exclude: /node_modules/,
use: {
loader: 'webpack-glsl-minify',
options: {
// Don't obfuscate shader code in dev mode
disableMangle: !isProduction,
preserveAll: !isProduction,
esModule: true,
},
},
},
{
test: /\.wasm$/,
type: 'javascript/auto',
use: {
loader: 'file-loader',
},
},
],
},
plugins: [
// Pass environment variables that are used in the code.
new webpack.EnvironmentPlugin(Object.keys(process.env)),
// Show progess when it's a production mode.
!isDev && new webpack.ProgressPlugin(),
// Clean output folder before a production build.
!isDev && new CleanWebpackPlugin(),
// Copy content to the output directory without processing it.
new CopyPlugin({
patterns: [
{
from: publicDirectory,
globOptions: {
ignore: ['**/index.html'],
},
to: outputDirectory,
noErrorOnMissing: true,
},
],
}),
// Add scripts to the final HTML
new HtmlWebpackPlugin({
inject: true,
template: `html-loader!${path.resolve(publicDirectory, 'index.html')}`,
filename: 'index.html',
minify: isProduction
? {
removeComments: true,
collapseWhitespace: true,
removeRedundantAttributes: true,
useShortDoctype: true,
removeEmptyAttributes: true,
removeStyleLinkTypeAttributes: true,
keepClosingSlash: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true,
}
: undefined,
}),
// Generate a manifest file which contains a mapping of all asset filenames
// to their corresponding output file.
new WebpackManifestPlugin({
fileName: 'asset-manifest.json',
publicPath: '/',
generate: (seed, files) => {
const manifest: Manifest = {};
files.forEach(file => {
manifest[file.name] = file.path;
});
return manifest;
},
}),
// Updates modules while an application is running, without a full reload.
isDev && new webpack.HotModuleReplacementPlugin(),
// TODO(ts): The current @types/compression-webpack-plugin is still targeting webpack@4, for now we just as any it.
isProduction &&
(new CompressionPlugin({
test: /\.(js|svg)$/,
filename: '[path].br[query]',
algorithm: 'brotliCompress',
compressionOptions: {level: 11},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
}) as any),
isProduction &&
new MiniCssExtractPlugin({
filename: '[name].[contenthash].css',
chunkFilename: '[id].[contenthash].css',
ignoreOrder: true,
}),
].filter(Boolean),
};
export default config;