-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathwebpack.base.config.js
More file actions
160 lines (157 loc) · 4.43 KB
/
webpack.base.config.js
File metadata and controls
160 lines (157 loc) · 4.43 KB
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
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const CopyPlugin = require('copy-webpack-plugin')
const paths = {
src: path.resolve(__dirname, 'src'),
public: path.resolve(__dirname, 'public'),
build: path.resolve(__dirname, 'build', 'frontend')
}
paths.entryPoint = path.resolve(paths.src, 'index.js')
const createBabelLoader = ({
isDevelopment
}) => ({
test: /\.(js|jsx|ts|tsx)$/i,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: [
'@babel/preset-env',
'@babel/preset-typescript',
['@babel/preset-react', { runtime: 'automatic' }]
],
plugins: [
[
'@babel/plugin-proposal-decorators',
{ legacy: true }
],
...(isDevelopment ? [
'react-refresh/babel'
] : [])
]
}
}
})
const createCopyPlugin = ({
isDevelopment
}) => (
new CopyPlugin({
patterns: [
{ from: 'public', to: isDevelopment ? 'static/frontend' : '' }
]
})
)
const createHtmlWebpackPlugin = ({
isDevelopment
}) => {
/**
* In production, we have an extra step where we generate an index template
* which is then turned into our final index file using sed substitutions from
* environment variables.
*
* In development, we just want to directly generate an index.html.
*/
return new HtmlWebpackPlugin({
template: path.resolve(paths.public, 'index.ejs'),
filename: isDevelopment ? "index.html" : "index_template.html",
templateParameters: {
isDevelopment,
publicUrl: '/static/frontend/'
},
minify: {
html5: true,
collapseWhitespace: true,
minifyCSS: true,
minifyJS: true,
minifyURLs: true,
removeAttributeQuotes: false,
removeComments: true,
removeEmptyAttributes: true,
removeOptionalTags: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
useShortDoctype: true
}
})
}
const createMiniCssExtractPlugin = ({
isDevelopment
}) => (
// CSS HMR doesn't work with contenthash
isDevelopment ? new MiniCssExtractPlugin({
filename: '[name].css',
chunkFilename: '[id].css'
}) : new MiniCssExtractPlugin({
filename: '[name].[contenthash].css',
chunkFilename: '[id].[contenthash].css'
})
)
const baseConfig = {
entry: paths.entryPoint,
output: {
path: paths.build,
filename: '[name].[fullhash:8].bundle.js',
sourceMapFilename: '[file].map[query]',
chunkFilename: '[id].[chunkhash:8].chunk.js',
publicPath: '/'
},
module: {
rules: [
{
test: /\.css$/i,
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
importLoaders: 1
}
},
'postcss-loader'
]
},
{
test: /\.less$/i,
use: [
{
loader: MiniCssExtractPlugin.loader,
},
{
loader: 'css-loader'
},
{
loader: 'less-loader',
options: {
lessOptions: {
javascriptEnabled: true
}
}
}
]
},
{
test: /\.(png|svg|jpg|jpeg|gif)$/i,
type: 'asset/resource',
},
]
},
plugins: [],
resolve: {
extensions: ['.js', '.jsx', '.ts', '.tsx']
},
performance: {
hints: 'warning',
maxAssetSize: 200000,
maxEntrypointSize: 400000
}
}
module.exports = {
paths,
baseConfig,
createBabelLoader,
createCopyPlugin,
createHtmlWebpackPlugin,
createMiniCssExtractPlugin
}