-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathwebpack.config.js
More file actions
99 lines (92 loc) · 2.28 KB
/
webpack.config.js
File metadata and controls
99 lines (92 loc) · 2.28 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
import { resolve as _resolve, join } from 'node:path'
import autoprefixer from 'autoprefixer'
import { globSync as glob } from 'tinyglobby'
import MiniCssExtractPlugin from 'mini-css-extract-plugin'
import { PurgeCSSPlugin } from 'purgecss-webpack-plugin'
import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer'
import CompressionPlugin from 'compression-webpack-plugin'
const isProd = process.env.NODE_ENV === 'production'
const browsers = [
'last 2 versions',
'ios_saf >= 8',
'edge >= 12',
'chrome >= 49',
'firefox >= 49',
'> 1%',
'not dead'
]
const __dirname = new URL('.', import.meta.url).pathname
/** @type {import('webpack').Configuration} */
const cfg = {
entry: {
main: _resolve(__dirname, 'src', 'main.tsx')
},
mode: isProd ? 'production' : 'development',
output: {
path: join(__dirname, 'public'),
filename: '[name].min.js',
publicPath: '/'
},
plugins: [
new MiniCssExtractPlugin({ filename: '[name].min.css' })
],
devtool: isProd ? false : 'source-map',
module: {
rules: [{
test: /\.tsx?$/,
exclude: /node_modules/,
use: {
loader: 'esbuild-loader',
options: {
loader: 'tsx',
target: 'es2022',
sourcemap: process.env.NODE_ENV !== 'production'
}
}
}, {
test: /\.scss$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
sourceMap: true
}
},
{
loader: 'postcss-loader',
options: {
postcssOptions: {
plugins: [autoprefixer({ overrideBrowserslist: browsers })]
}
}
}, {
loader: 'sass-loader',
options: {
sourceMap: true
}
}
]
}]
},
resolve: {
modules: ['node_modules', _resolve(__dirname, 'src')],
}
}
if (isProd) {
cfg.plugins.push(new PurgeCSSPlugin({
minimize: true,
moduleExtensions: ['.js', '.jsx', '.ts', '.tsx'],
paths: glob([
join(__dirname, 'src', '**/*.{js,jsx,ts,tsx}'),
join(__dirname, 'public', '*.html')
])
}))
}
if (isProd) {
cfg.plugins.push(new CompressionPlugin())
}
if (process.env.ANALYZE_BUNDLE) {
cfg.plugins.push(new BundleAnalyzerPlugin())
}
export default cfg