-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathwebpack.config.mts
262 lines (255 loc) · 6.78 KB
/
webpack.config.mts
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
import path from 'node:path'
import process from 'node:process'
import { CleanWebpackPlugin } from 'clean-webpack-plugin'
import CopyWebpackPlugin from 'copy-webpack-plugin'
import DuplicatePackageCheckerPlugin from 'duplicate-package-checker-webpack-plugin'
import HTMLInlineCSSWebpackPluginModule from 'html-inline-css-webpack-plugin'
import HtmlWebpackPlugin from 'html-webpack-plugin'
import * as lightningCss from 'lightningcss'
import { LightningCssMinifyPlugin } from 'lightningcss-loader'
import MiniCssExtractPlugin from 'mini-css-extract-plugin'
import ScriptExtHtmlWebpackPlugin from 'script-ext-html-webpack-plugin'
import TerserPlugin from 'terser-webpack-plugin'
import webpack, {
type Configuration,
type WebpackPluginInstance,
} from 'webpack'
import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer'
import ZipPlugin from 'zip-webpack-plugin'
import pkg from './package.json' with { type: 'json' }
const HTMLInlineCSSWebpackPlugin =
// @ts-expect-error Hack to import default module directly
HTMLInlineCSSWebpackPluginModule.default as typeof HTMLInlineCSSWebpackPluginModule
const { ProgressPlugin } = webpack
const isCI = process.env['CI'] === 'true'
const isProductionBuild = process.env['NODE_ENV'] === 'production'
const isDevelopmentBuild = !isProductionBuild
const appNames = ['options', 'popup'] as const
const commonChunkName = 'common'
const outputDir = path.join('build', process.env['NODE_ENV'] || 'development')
const sourceDir = 'src'
const webpackConfig: Readonly<Configuration> = {
devtool: isDevelopmentBuild ? 'inline-source-map' : false,
entry: Object.fromEntries(
appNames.map((appName) => [appName, `./${sourceDir}/${appName}`]),
),
mode: isDevelopmentBuild ? 'development' : 'production',
module: {
rules: [
{
test: /\.css$/u,
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
modules: {
localIdentName: '[local]_[hash:base64:5]',
},
importLoaders: 2,
},
},
{
loader: 'lightningcss-loader',
options: {
implementation: lightningCss,
// ignore css modules syntax as it is handled by css-loader
errorRecovery: true,
},
},
{
loader: 'postcss-loader',
options: {
postcssOptions: {
plugins: ['postcss-normalize'],
},
},
},
],
},
{
test: /\.tsx?$/u,
loader: 'swc-loader',
},
{
test: /\.woff2$/u,
type: 'asset/resource',
generator: {
filename: 'fonts/[name][ext]',
},
},
{
test: /\.(png|webp)$/u,
loader: 'file-loader',
options: {
name: 'images/[name].[ext]',
},
},
...(isDevelopmentBuild
? [
{
test: /icon\d+\.png$/u,
loader: 'image-process-loader',
options: {
greyscale: true,
},
},
]
: []),
{
test: /\.svg$/u,
resourceQuery: /svgUse/u,
issuer: /\.tsx?$/u,
type: 'javascript/auto',
use: '@svg-use/webpack',
},
{
test: /\.svg$/u,
resourceQuery: { not: [/svgUse/u] },
type: 'asset',
generator: {
filename: 'images/[name][ext]',
},
},
{
test: /\/manifest\.yml$/u,
type: 'asset/resource',
use: [
{
loader: 'extract-loader',
options: {
publicPath: './',
},
},
{
loader: 'chrome-manifest-loader',
options: {
mapMinimumChromeVersion: true,
mapVersion: true,
},
},
{
loader: 'yaml-loader',
options: { asJSON: true },
},
],
generator: {
filename: '[name].json',
},
},
],
},
optimization: {
...(isProductionBuild && {
minimize: true,
minimizer: [
new LightningCssMinifyPlugin({
implementation: lightningCss,
}),
new TerserPlugin({
minify: TerserPlugin.swcMinify,
terserOptions: {
compress: {
// arguments: true, // this breaks the build
drop_console: true,
reduce_vars: true,
},
ecma: 2020 as const,
module: true,
},
}),
],
}),
splitChunks: {
chunks: 'all',
name: commonChunkName,
},
},
output: {
path: path.resolve(import.meta.dirname, outputDir),
filename: path.join('js', '[name].js'),
},
plugins: [
...(!isCI ? [new ProgressPlugin({})] : []),
new CleanWebpackPlugin({
cleanStaleWebpackAssets: false,
}),
new CopyWebpackPlugin({
patterns: [
{
context: path.join(sourceDir, 'core'),
from: path.join('_locales', '**', 'messages.json'),
},
{
from: 'LICENSE',
},
],
}),
new DuplicatePackageCheckerPlugin({
emitError: true,
strict: true,
}) as WebpackPluginInstance,
...appNames.map((appName) => {
return new HtmlWebpackPlugin({
chunks: [commonChunkName, appName],
filename: `${appName}.html`,
inject: 'head',
minify: {
collapseWhitespace: true,
removeAttributeQuotes: true,
removeComments: true,
useShortDoctype: true,
},
template: path.join(sourceDir, 'template.html'),
title: pkg.name,
})
}),
new MiniCssExtractPlugin(),
...(isProductionBuild
? [
// this plugin does not update the inline css on watch mode
new HTMLInlineCSSWebpackPlugin({
styleTagFactory: ({ style }) => `<style>${style}</style>`,
}),
]
: []),
new ScriptExtHtmlWebpackPlugin({
defaultAttribute: 'async',
}),
...(isProductionBuild && !isCI
? [
new BundleAnalyzerPlugin({
analyzerMode: 'static',
reportFilename: path.join('..', 'report.html'),
}),
]
: []),
...(isProductionBuild
? [
new ZipPlugin({
filename: `${pkg.version}.zip`,
}),
]
: []),
],
resolve: {
extensionAlias: {
'.cjs': ['.cts', '.cjs'],
'.js': ['.tsx', '.ts', '.js'],
'.mjs': ['.mts', '.mjs'],
},
extensions: [
'.wasm',
'.tsx',
'.ts',
'.mts',
'.cts',
'.js',
'.mjs',
'.cjs',
'.json',
],
},
watch: isDevelopmentBuild,
}
export default webpackConfig