-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.ts
275 lines (265 loc) · 7.86 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
import * as autoprefixer from 'autoprefixer';
import * as dotenv from 'dotenv';
import * as TSCheckerPlugin from 'fork-ts-checker-webpack-plugin';
import * as HTMLPlugin from 'html-webpack-plugin';
import * as MiniCSSExtractPlugin from 'mini-css-extract-plugin';
import * as OptimizeCSSPlugin from 'optimize-css-assets-webpack-plugin';
import * as path from 'path';
// @ts-ignore
import * as RemovePlugin from 'remove-files-webpack-plugin';
// eslint-disable-next-line @blueprintjs/classes-constants
import * as HTMLScriptPlugin from 'script-ext-html-webpack-plugin';
import * as TerserPlugin from 'terser-webpack-plugin';
import {
Configuration,
EnvironmentPlugin,
HotModuleReplacementPlugin,
NormalModuleReplacementPlugin,
} from 'webpack';
import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer';
import * as FilterWarningsPlugin from 'webpack-filter-warnings-plugin';
import * as NotifierPlugin from 'webpack-notifier';
import { Configuration as WebpackDevServerConfiguration } from 'webpack-dev-server';
dotenv.config();
const devMode = process.env.NODE_ENV === 'development';
const hmr = !!process.env.WEBPACK_HMR;
const devServer = process.argv.some(v => v === 'webpack-dev-server');
const faviconTags = `
<!-- put favicon tags here -->
`;
const statsConfig = devMode
? {
all: false,
assets: true,
colors: true,
hash: true,
builtAt: true,
timings: true,
errors: true,
errorDetails: true,
logging: 'info',
warnings: true,
version: true,
context: path.resolve(__dirname, 'src'),
}
: {};
export default <Configuration>{
mode: devMode ? 'development' : 'production',
entry: './src/pages',
output: {
path: path.resolve(__dirname, 'dist'),
filename: devMode ? '[name].js' : '[name].[contenthash].js',
chunkFilename: devMode ? '[name].js' : '[name].[contenthash].js',
pathinfo: true,
publicPath: '/',
},
experiments: { topLevelAwait: true },
resolve: {
extensions: ['.ts', '.tsx', '.js'],
alias: {
react: 'preact/compat',
'react-dom/test-utils': 'preact/test-utils',
'react-dom': 'preact/compat',
},
},
devServer: <WebpackDevServerConfiguration>{
index: './views/index.html',
historyApiFallback: {
rewrites: [
{ from: /.*/, to: '/views/index.html' },
],
},
host: '0.0.0.0',
// hot: hmr,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, PATCH, OPTIONS',
'Access-Control-Allow-Headers': 'X-Requested-With, content-type, Authorization',
},
public: 'http://localhost:8080',
contentBase: [path.join(__dirname, 'public'), path.join(__dirname, 'dist')],
stats: statsConfig,
},
devtool: devMode ? 'inline-source-map' : 'source-map',
watchOptions: {
ignored: ['./public/**', './server/**', './node_modules/**'],
},
optimization: devMode
? {
runtimeChunk: 'single',
minimize: false,
minimizer: undefined,
removeAvailableModules: false,
removeEmptyChunks: false,
splitChunks: false,
}
: {
// TODO fix this
usedExports: true,
// runtimeChunk: 'single',
minimize: true,
minimizer: [
new TerserPlugin({
test: /\.(j|t)sx?$/i,
sourceMap: true,
extractComments: false,
}),
],
splitChunks: {
// chunks: 'initial',
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name: 'vendor',
chunks: 'all',
priority: 10,
},
common: {
test: /[\\/]src[\\/](common|components)[\\/]/,
name: 'common',
chunks: 'all',
priority: 6,
},
default: {
minChunks: 2,
priority: 0,
reuseExistingChunk: true,
},
},
},
},
stats: statsConfig,
plugins: (() => {
const plugins: Array<any> = [
// ...dllNames.map(name => {
// const manifest = require(path.resolve(__dirname, `dist/library/${name}.manifest.json`));
// console.log(inspect(manifest));
// return new DllReferencePlugin({
// context: '/',
// manifest,
// name,
// });
// }),
new RemovePlugin({
before: {
exclude: ['./dist/library'],
test: [
{
folder: './dist',
method: () => true,
},
{
folder: './dist/views',
method: (itemPath: string) => /analyze/gi.test(itemPath),
},
],
logWarning: true,
logError: true,
log: true,
logDebug: true,
},
watch: { beforeForFirstBuild: true },
}),
new TSCheckerPlugin({ eslint: true, silent: true }),
new EnvironmentPlugin(['BLUEPRINT_NAMESPACE']),
new FilterWarningsPlugin({
exclude: [
/export 'unstable_renderSubtreeIntoContainer' \(imported as 'ReactDOM'\) was not found in 'react-dom'/,
],
}),
new NormalModuleReplacementPlugin(
/.*\/generated\/iconSvgPaths.*/,
path.resolve(__dirname, 'src/library/iconSvgPaths.js')
),
new HTMLScriptPlugin({
sync: /runtime(\..+)?\.js/,
// async: /vendor(-jquery)?(\..+)?\.js/,
defaultAttribute: 'defer',
}),
// html plugins
...((pages: {
[chunk: string]: HTMLPlugin.Options & {
preBody?: string;
postBody?: string;
preHead?: string;
postHead?: string;
};
}) => {
const output: Array<HTMLPlugin> = [];
for (const [chunk, options] of Object.entries(pages))
output.push(
new HTMLPlugin({
inject: false,
chunks: [chunk],
minify: 'auto',
faviconTags,
template: './src/pages/template.ejs',
...options,
})
);
return output;
})({
main: { title: 'Home', filename: './views/index.html' },
}),
];
devMode
? plugins.push(
new NotifierPlugin({ title: 'webpack' }),
new BundleAnalyzerPlugin({ openAnalyzer: false })
)
: plugins.push(
new OptimizeCSSPlugin(),
new BundleAnalyzerPlugin({
openAnalyzer: false,
analyzerMode: 'static',
reportFilename: `./views/analyze.html`,
})
);
hmr
? plugins.push(new HotModuleReplacementPlugin())
: plugins.push(
new MiniCSSExtractPlugin({
filename: devMode ? '[name].css' : '[name].[contenthash].css',
chunkFilename: devMode ? '[name].css' : '[name].[contenthash].css',
})
);
return plugins;
})(),
module: {
rules: [
{
test: /\.(jp(e?)g|png|gif|mp3|ttf|eot|woff)$/i,
loader: 'file-loader',
options: {
name: () => (devMode ? '[path][name].[ext]' : '[contenthash].[ext]'),
},
},
{ test: /\.svg$/i, loader: 'svg-inline-loader' },
{
test: /\.(sa|sc|c)ss$/i,
use: [
hmr ? 'style-loader' : MiniCSSExtractPlugin.loader,
{ loader: 'css-loader', options: { sourceMap: true } },
{ loader: 'postcss-loader', options: { sourceMap: true, plugins: [autoprefixer()] } },
{ loader: 'sass-loader' },
],
},
{
test: /\.js(x?)$/i,
enforce: 'pre',
loader: 'source-map-loader',
include: path.resolve(__dirname, 'src'),
},
{
test: /\.ts(x?)/i,
use: [
'babel-loader',
{
loader: 'ts-loader',
options: { transpileOnly: true, experimentalWatchApi: true },
},
],
},
],
},
};