Repository navigation
Expand file tree
/
Copy pathwebpack.config.js
More file actions
128 lines (125 loc) · 3.48 KB
/
Copy pathwebpack.config.js
File metadata and controls
128 lines (125 loc) · 3.48 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
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const addViewBoxPlugin = {
name: 'addViewBox',
fn: () => ({
element: {
enter: (node, parent) => {
const { width, height, viewBox } = node.attributes;
if (node.name === 'svg' && parent.type === 'root' && !viewBox && width && height) {
node.attributes.viewBox = `0 0 ${parseFloat(width)} ${parseFloat(height)}`;
}
},
},
}),
};
module.exports = (_env, argv) => {
const isProd = argv.mode === 'production';
return {
entry: './src/index.jsx',
output: {
path: path.resolve(__dirname, 'dist'),
filename: isProd ? 'js/[name].[contenthash:8].js' : 'js/[name].js',
assetModuleFilename: 'assets/[name].[contenthash:8][ext]',
// Relative paths, so the build works on any sub-path (e.g. GitHub Pages).
publicPath: '',
clean: true,
},
resolve: {
extensions: ['.js', '.jsx'],
},
devtool: isProd ? false : 'eval-cheap-module-source-map',
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: { envName: isProd ? 'production' : 'development' },
},
},
{
test: /\.s?css$/,
use: [
isProd
? // CSS is emitted to css/, so asset URLs inside it must go one level up.
{ loader: MiniCssExtractPlugin.loader, options: { publicPath: '../' } }
: 'style-loader',
{ loader: 'css-loader', options: { importLoaders: 2 } },
'postcss-loader',
'sass-loader',
],
},
{
// `import Icon from './icon.svg'` -> inline React component (fill/stroke styled from CSS).
test: /\.svg$/,
issuer: /\.jsx?$/,
resourceQuery: { not: [/url/] },
use: [
{
loader: '@svgr/webpack',
options: {
svgoConfig: {
plugins: [
{
name: 'preset-default',
params: { overrides: { removeViewBox: false, cleanupIds: false } },
},
addViewBoxPlugin,
],
},
},
},
],
},
{
test: /\.svg$/,
resourceQuery: /url/,
type: 'asset/resource',
},
{
test: /\.svg$/,
issuer: { not: [/\.jsx?$/] },
type: 'asset/resource',
},
{
test: /\.(png|jpe?g|webp)$/,
type: 'asset/resource',
},
],
},
plugins: [
new HtmlWebpackPlugin({
template: './public/index.html',
favicon: './public/favicon-32x32.png',
}),
isProd &&
new MiniCssExtractPlugin({
filename: 'css/[name].[contenthash:8].css',
}),
].filter(Boolean),
devServer: {
port: 3000,
open: false,
hot: true,
},
optimization: {
splitChunks: {
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name: 'vendor',
chunks: 'all',
},
},
},
},
performance: {
hints: isProd ? 'warning' : false,
// React alone is ~215 KiB raw (~68 KiB gzipped).
maxEntrypointSize: 320 * 1024,
},
};
};