-
Notifications
You must be signed in to change notification settings - Fork 22
/
webpack.config.mjs
355 lines (311 loc) · 11.5 KB
/
webpack.config.mjs
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
* Copyright (C) 2024 PixieBrix, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import path from "node:path";
import { execSync } from "node:child_process";
import webpack from "webpack";
import MiniCssExtractPlugin from "mini-css-extract-plugin";
import WebExtensionTarget from "webpack-target-webextension";
import NodePolyfillPlugin from "node-polyfill-webpack-plugin";
import WebpackBuildNotifierPlugin from "webpack-build-notifier";
import TerserPlugin from "terser-webpack-plugin";
import CssMinimizerPlugin from "css-minimizer-webpack-plugin";
import { BundleAnalyzerPlugin } from "webpack-bundle-analyzer";
import CopyPlugin from "copy-webpack-plugin";
import { compact } from "lodash-es";
import mergeWithShared from "./webpack.sharedConfig.js";
import { parseEnv, loadEnv } from "./scripts/env.mjs";
import customizeManifest from "./scripts/manifest.mjs";
import { createRequire } from "node:module";
import DiscardFilePlugin from "./scripts/DiscardFilePlugin.mjs";
import isolatedComponentList from "./src/components/isolatedComponentList.mjs";
import ReactRefreshWebpackPlugin from "@pmmmwh/react-refresh-webpack-plugin";
const require = createRequire(import.meta.url);
loadEnv();
console.log("SOURCE_VERSION:", process.env.SOURCE_VERSION);
console.log("SERVICE_URL:", process.env.SERVICE_URL);
console.log("MARKETPLACE_URL:", process.env.MARKETPLACE_URL);
console.log("CHROME_EXTENSION_ID:", process.env.CHROME_EXTENSION_ID);
console.log("MV:", process.env.MV);
console.log(
"REQUIRE_OPTIONAL_PERMISSIONS_IN_MANIFEST:",
process.env.REQUIRE_OPTIONAL_PERMISSIONS_IN_MANIFEST,
);
if (!process.env.SOURCE_VERSION) {
process.env.SOURCE_VERSION = execSync("git rev-parse --short HEAD")
.toString()
.trim();
}
// Configure sourcemaps
// Disable sourcemaps on CI unless it's a PUBLIC_RELEASE
const produceSourcemap =
!parseEnv(process.env.CI) || parseEnv(process.env.PUBLIC_RELEASE);
const sourceMapPublicUrl =
parseEnv(process.env.PUBLIC_RELEASE) &&
`${process.env.SOURCE_MAP_URL_BASE}/${process.env.SOURCE_MAP_PATH}/`;
let sourcemapsLogMessage = "Sourcemaps: ";
if (sourceMapPublicUrl) {
sourcemapsLogMessage += sourceMapPublicUrl;
} else if (produceSourcemap) {
sourcemapsLogMessage += "Local";
} else {
sourcemapsLogMessage += "None";
}
console.log(sourcemapsLogMessage);
const isProd = (options) => options.mode === "production";
function mockHeavyDependencies() {
if (process.env.DEV_SLIM.toLowerCase() === "true") {
console.warn(
"Mocking dependencies for development build: @/icons/list, uipath/robot",
);
return {
"@/icons/list": path.resolve("src/__mocks__/@/icons/list"),
"@uipath/robot": path.resolve("src/__mocks__/@uipath/robot"),
};
}
}
const isHMR = Boolean(process.env.HMR);
const createConfig = (env, options) =>
mergeWithShared({
node: {
global: true,
},
// Don't use `eval` maps https://stackoverflow.com/a/57460886/402560
// Explicitly handled by `SourceMapDevToolPlugin` below
devtool: false,
output: {
path: path.resolve("dist"),
chunkFilename: "bundles/[name].bundle.js",
environment: {
// https://github.com/awesome-webextension/webpack-target-webextension#code-splitting
dynamicImport: true,
},
},
entry: Object.fromEntries(
[
"background/background",
"contentScript/contentScript",
"contentScript/loadActivationEnhancements",
"contentScript/setExtensionIdInApp",
"pageEditor/pageEditor",
"extensionConsole/options",
"sidebar/sidebar",
"sandbox/sandbox",
"tinyPages/ephemeralForm",
"tinyPages/walkthroughModal",
"tinyPages/ephemeralPanel",
"tinyPages/restrictedUrlPopup",
// Tiny files without imports
"tinyPages/frame",
"tinyPages/devtools",
// The script that gets injected into the host page
"pageScript/pageScript",
"tinyPages/offscreen",
// The isolated components whose CSS will be loaded in a shadow DOM
...isolatedComponentList,
].map((name) => [path.basename(name), `./src/${name}`]),
),
resolve: {
alias: {
...mockHeavyDependencies(),
...(isProd(options) || process.env.DEV_REDUX_LOGGER === "false"
? { "redux-logger": false }
: {}),
},
},
optimization: {
// Module concatenation mangles class names https://github.com/pixiebrix/pixiebrix-extension/issues/4763
concatenateModules: false,
// Chrome bug https://bugs.chromium.org/p/chromium/issues/detail?id=1108199
splitChunks: {
automaticNameDelimiter: "-",
cacheGroups: {
vendors: false,
},
},
minimizer: [
new TerserPlugin({
parallel: true,
terserOptions: {
// https://github.com/webpack-contrib/terser-webpack-plugin#terseroptions
// Keep error classnames because we perform name comparison (see selectSpecificError)
// We use Action for SubmitPanelAction, AbortPanelAction, etc.
keep_classnames: /.*(Error|Action)$/,
},
}),
new CssMinimizerPlugin(),
],
},
performance: {
// Silence warnings because the size includes the sourcemaps
maxEntrypointSize: 15_120_000,
maxAssetSize: 15_120_000,
},
// Enable HMR
devServer: {
hot: true,
},
plugins: compact([
produceSourcemap &&
new webpack.SourceMapDevToolPlugin({
publicPath: sourceMapPublicUrl,
// The sourcemap will be inlined if `undefined`. Only inlined sourcemaps work locally
// https://bugs.chromium.org/p/chromium/issues/detail?id=974543
// NOTE: Datadog requires .js.map as the extension: https://github.com/DataDog/datadog-ci/issues/870
// The [file] already includes the js file extension
filename: sourceMapPublicUrl && "[file].map[query]",
}),
// Only notifies when watching. `zsh-notify` is suggested for the `build` script
!isProd(options) &&
process.env.DEV_NOTIFY !== "false" &&
new WebpackBuildNotifierPlugin({
title: "PB Extension",
showDuration: true,
}),
isProd(options) &&
new BundleAnalyzerPlugin({
analyzerMode: "static",
reportFilename: path.resolve("report.html"),
excludeAssets: /svg-icons/,
}),
new NodePolyfillPlugin({
// Specify the least amount of polyfills.
// By default it polyfills even `console`
includeAliases: ["buffer", "Buffer", "http", "https"],
}),
new WebExtensionTarget({
weakRuntimeCheck: true,
// Required to support sandboxed iframes
// https://github.com/awesome-webextension/webpack-target-webextension/pull/42
background: {
// Do not use serviceWorkerEntry:
// https://github.com/awesome-webextension/webpack-target-webextension/issues/24#issuecomment-1914057083
pageEntry: "background",
},
}),
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
browser: "webextension-polyfill",
process: path.resolve("src/vendors/process.js"),
}),
// This will inject the current ENVs into the bundle, if found
new webpack.EnvironmentPlugin({
// If not found, these values will be used as defaults
DEBUG: !isProd(options),
NODE_DEBUG: false,
REDUX_DEV_TOOLS: !isProd(options),
NPM_PACKAGE_VERSION: process.env.npm_package_version,
ENVIRONMENT: options.mode,
SOURCE_MAP_PUBLIC_PATH:
sourceMapPublicUrl ?? "extension://dynamichost/",
// Record telemetry events in development?
DEV_EVENT_TELEMETRY: false,
SANDBOX_LOGGING: false,
IS_BETA: process.env.RELEASE_CHANNEL === "beta",
SHADOW_DOM: "open",
// If not found, "undefined" will cause the build to fail
SERVICE_URL: undefined,
MARKETPLACE_URL: undefined,
SOURCE_VERSION: undefined,
CHROME_EXTENSION_ID: undefined,
// If not found, "null" will leave the ENV unset in the bundle
// DataDog RUM/Logging
DATADOG_APPLICATION_ID: null,
DATADOG_CLIENT_TOKEN: null,
}),
new MiniCssExtractPlugin({
chunkFilename: "css/[id].css",
}),
new CopyPlugin({
patterns: [
{
from: "src/manifest.json",
transform(jsonString) {
const manifest = JSON.parse(jsonString);
const customizedManifest = customizeManifest(manifest, {
isProduction: isProd(options),
manifestVersion: process.env.MV === "2" ? 2 : 3, // Default to 3 if missing
env: process.env,
isBeta: process.env.RELEASE_CHANNEL === "beta",
});
return JSON.stringify(customizedManifest, null, 2);
},
},
{
from: "src/*/*.html", // Only one level deep
to: "[name][ext]", // Flat output, no subfolders
},
"static",
],
}),
// These files are not used, they're only webpack entry points in order to generate
// a full CSS files that can be injected in shadow DOM. See this for more context:
// https://github.com/webpack-contrib/mini-css-extract-plugin/issues/1092#issuecomment-2037540032
new DiscardFilePlugin(isolatedComponentList),
isHMR &&
new ReactRefreshWebpackPlugin({
overlay: false,
}),
]),
module: {
rules: [
{
test: /\.s?css$/,
resourceQuery: { not: [/loadAsUrl|loadAsText/] },
use: [MiniCssExtractPlugin.loader, "css-loader"],
},
{
test: /\.scss$/,
use: [
{
loader: "sass-loader",
options: {
sassOptions: {
// Due to warnings in dart-sass https://github.com/pixiebrix/pixiebrix-extension/pull/1070
quietDeps: true,
},
},
},
],
},
// Pull bootstrap-icons and simple-icons from CDN to reduce bundle size.
{
test: /bootstrap-icons\/.*\.svg$/,
type: "asset/resource",
generator: {
emit: false,
publicPath: `https://cdn.jsdelivr.net/npm/bootstrap-icons@${
require("bootstrap-icons/package.json").version
}/`,
filename: "icons/[name][ext]",
},
},
{
test: /simple-icons\/.*\.svg$/,
type: "asset/resource",
generator: {
emit: false,
publicPath: `https://cdn.jsdelivr.net/npm/simple-icons@${
require("simple-icons/package.json").version
}/`,
filename: "icons/[name][ext]",
},
},
],
},
});
export default createConfig;