-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.config.js
224 lines (213 loc) · 5.41 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
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
const path = require("path")
const { CleanWebpackPlugin } = require("clean-webpack-plugin")
const HtmlWebpackPlugin = require("html-webpack-plugin")
const WebextensionPlugin = require("webpack-webextension-plugin")
const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer")
const ForkTsCheckerWebpackPlugin = require("fork-ts-checker-webpack-plugin")
const ZipPlugin = require("zip-webpack-plugin")
const TerserPlugin = require("terser-webpack-plugin")
const CopyWebpackPlugin = require("copy-webpack-plugin")
const DefinePlugin = require("webpack").DefinePlugin
const PACKAGE = require("./package.json")
const getTargetsBrowserlist = targetBrowser => {
if (targetBrowser === "chromium") {
return "last 3 chrome version, last 3 edge version, last 3 opera version"
} else if (targetBrowser === "firefox") {
return "last 3 firefox version"
}
throw new Error("browser argument not specified")
}
const getTargetBrowser = env => {
if (env.chromium) {
return "chromium"
} else if (env.firefox) {
return "firefox"
}
throw new Error("invalid browser argument")
}
const getMode = env => {
if (env.prod) {
return "production"
} else if (env.dev) {
return "development"
}
throw new Error("invalid mode argument")
}
const getFirefoxDebugSettings = (targetBrowser, mode) => {
if (targetBrowser === "firefox" && mode === "development") {
/* eslint-disable camelcase */
return {
browser_specific_settings: {
gecko: {
id: "dawdle_block@eerolehtinen.fi",
strict_min_version: "42.0",
},
},
}
/* eslint-disable camelcase */
}
return undefined
}
module.exports = env => {
const targetBrowser = getTargetBrowser(env)
const mode = getMode(env)
const browserlist = getTargetsBrowserlist(targetBrowser)
const config = {
mode,
entry: {
background: path.join(__dirname, "src/background/index.ts"),
options: path.join(__dirname, "src/options/index.tsx"),
popup: path.join(__dirname, "src/popup/index.tsx"),
},
output: {
path: path.join(__dirname, `dist/${targetBrowser}`),
filename: "js/[name].js",
},
module: {
rules: [
{
exclude: /node_modules/,
test: /\.tsx?$/,
use: [
{
loader: "babel-loader",
options: {
cacheDirectory: true,
presets: [
// Overrides default preset-env in babel.config.js
[
"@babel/preset-env",
{
useBuiltIns: "usage",
corejs: { version: "3.15", proposals: false },
// Use targets based on our selected browser
targets: browserlist,
},
],
],
},
},
],
},
{
include: /node_modules\/@fontsource/,
test: /\.css$/,
use: [
{ loader: "style-loader" }, // Creates style nodes from JS strings
{
loader: "css-loader", // Translates CSS into CommonJS
options: {
url: {
filter: url => {
if (url.endsWith(".woff")) return false
return true
},
},
},
},
],
},
{
test: /\.woff2?$/,
type: "asset/resource",
generator: {
filename: "fonts/[name][ext][query]",
},
},
],
},
resolve: {
extensions: [".ts", ".tsx", ".js", ".jsx"],
alias: {
// Resolve preact compatibility layer to react
react: "preact/compat",
"react-dom/test-utils": "preact/test-utils",
"react-dom": "preact/compat",
},
},
performance: {
assetFilter: asset => !asset.endsWith(".zip"),
},
plugins: [
// Clean old builds
new CleanWebpackPlugin({
cleanOnceBeforeBuildPatterns: [
path.resolve(__dirname, `./dist/${targetBrowser}/**/*`),
path.resolve(__dirname, `./dist/${targetBrowser}*.zip`),
path.resolve(__dirname, `./dist/${targetBrowser}_report.html`),
],
cleanStaleWebpackAssets: false,
verbose: false,
}),
// Copy views to dist and inject associated js files
new HtmlWebpackPlugin({
template: "static/views/popup.html",
inject: "body",
chunks: ["popup"],
filename: "popup.html",
}),
new HtmlWebpackPlugin({
template: "static/views/options.html",
inject: "body",
chunks: ["options"],
filename: "options.html",
}),
new CopyWebpackPlugin({
patterns: [{ from: "static/images", to: "images" }],
}),
// Parse manifest.json and apply browser specific settings
new WebextensionPlugin({
vendor: targetBrowser,
autoreload: false,
manifestDefaults: {
version: PACKAGE.version,
...getFirefoxDebugSettings(targetBrowser, mode),
},
}),
// Do type checking
new ForkTsCheckerWebpackPlugin({
typescript: {
diagnosticOptions: {
semantic: true,
syntactic: true,
},
},
}),
new DefinePlugin({
__DEV__: JSON.stringify(mode === "development"),
}),
],
optimization: {
minimize: true,
minimizer: [
// Disable all comments in output
new TerserPlugin({
terserOptions: {
format: {
comments: false,
},
},
extractComments: false,
}),
],
},
}
if (mode === "development") {
config.devtool = "inline-source-map"
}
// Zip and analyze bundle only when mode is production
if (mode === "production") {
config.plugins = [
...config.plugins,
new ZipPlugin({
path: "../",
filename: `${targetBrowser}_v${PACKAGE.version}.zip`,
}),
new BundleAnalyzerPlugin({
analyzerMode: "static",
reportFilename: `../${targetBrowser}_report.html`,
}),
]
}
return config
}