-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
158 lines (149 loc) · 4.63 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
const path = require('path'),
glob = require('glob'),
HtmlWebpackPlugin = require('html-webpack-plugin'),
workboxPlugin = require('workbox-webpack-plugin'),
MiniCssExtractPlugin = require('mini-css-extract-plugin'),
CopyPlugin = require('copy-webpack-plugin'),
ScriptExtHtmlWebpackPlugin = require('script-ext-html-webpack-plugin'),
webpack = require('webpack'),
TerserPlugin = require('terser-webpack-plugin'),
Critters = require('critters-webpack-plugin')
const devMode = process.env.NODE_ENV !== 'production'
const PATHS = {
src: path.join(__dirname, 'src'),
}
module.exports = {
mode: devMode ? 'development' : 'production',
devtool: 'source-map',
entry: './src/app/app.tsx',
output: {
chunkFilename: '[name].[hash:4].js',
filename: '[name].[hash:4].js',
path: path.join(__dirname, '/dist'),
},
optimization: devMode
? {}
: {
minimizer: [new TerserPlugin()],
runtimeChunk: 'single',
splitChunks: {
chunks: 'all',
maxInitialRequests: Infinity,
minSize: 0,
cacheGroups: {
styles: {
name: 'styles',
test: /\.css$/,
chunks: 'all',
enforce: true,
},
vendor: {
test: /[\\/]node_modules[\\/]/,
name(module) {
// get the name. E.g. node_modules/packageName/not/this/part.js
// or node_modules/packageName
const packageName = module.context.match(
/[\\/]node_modules[\\/](.*?)([\\/]|$)/
)[1]
// npm package names are URL-safe, but some servers don't like @ symbols
return `npm.${packageName.replace('@', '')}`
},
},
},
},
},
resolve: {
extensions: ['.ts', '.tsx', '.js', '.json'],
alias: {
'@app': path.resolve('./src/app/'),
'@assets': path.resolve('./src/assets/'),
'@pages': path.resolve('./src/pages/'),
'@services': path.resolve('./src/services/'),
'@tests': path.resolve('./src/'),
'@components': path.resolve('./src/components/'),
'@theme': path.resolve('./src/theme/'),
'@data': path.resolve('./src/data/'),
'react-dom': devMode ? '@hot-loader/react-dom' : 'react-dom',
},
},
module: {
rules: [
{ test: /\.tsx?$/, loader: 'awesome-typescript-loader' },
{ enforce: 'pre', test: /\.js$/, loader: 'source-map-loader' },
{
test: /\.(sa|sc|c)ss$/,
use: [
devMode ? 'style-loader' : MiniCssExtractPlugin.loader,
{ loader: 'css-loader', options: { importLoaders: 1 } },
'postcss-loader',
'sass-loader',
],
},
{
test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/],
loader: require.resolve('url-loader'),
options: {
limit: 10000,
name: 'static/media/[name].[hash:8].[ext]',
},
},
{
test: [/\.eot$/, /\.ttf$/, /\.svg$/, /\.woff$/, /\.woff2$/],
loader: require.resolve('file-loader'),
options: {
name: 'static/media/[name].[hash:8].[ext]',
},
},
],
},
plugins: devMode
? [
new HtmlWebpackPlugin({
template: './src/index.html',
}),
]
: [
new webpack.HashedModuleIdsPlugin(),
new MiniCssExtractPlugin({
filename: devMode ? '[name].css' : '[name].[contenthash:4].css',
chunkFilename: devMode ? '[id].css' : '[id].[contenthash:4].css',
}),
new HtmlWebpackPlugin({
template: './src/index.html',
minify: {
removeComments: true,
collapseWhitespace: true,
removeRedundantAttributes: true,
useShortDoctype: true,
removeEmptyAttributes: true,
removeStyleLinkTypeAttributes: true,
keepClosingSlash: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true,
},
}),
new ScriptExtHtmlWebpackPlugin({
defaultAttribute: 'defer',
}),
new Critters({
preload: 'swap',
preloadFonts: true,
pruneSource: false,
}),
new CopyPlugin([
{ from: './src/web.config', to: './web.config' },
{
from: './src/manifest.json',
to: './manifest.json',
},
]),
new workboxPlugin.GenerateSW({
swDest: 'sw.js',
clientsClaim: true,
skipWaiting: true,
exclude: ['web.config', '*.json'],
navigateFallback: '/index.html',
}),
],
}