-
-
Notifications
You must be signed in to change notification settings - Fork 62
/
webpack.config.js
277 lines (245 loc) · 7.32 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
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
/* eslint-env node */
/* eslint @typescript-eslint/no-var-requires: "off" */
const path = require("path");
const webpack = require("webpack");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const GenerateJsonPlugin = require("generate-json-webpack-plugin");
const CopyPlugin = require("copy-webpack-plugin");
const TerserPlugin = require("terser-webpack-plugin");
const TsconfigPathsPlugin = require("tsconfig-paths-webpack-plugin");
const AWP_PACKAGE = require("./package.json");
const RWP_PACKAGE = require("./node_modules/replaywebpage/package.json");
const WARCIO_PACKAGE = require("./node_modules/warcio/package.json");
const BANNER = `[name].js is part of the Webrecorder ArchiveWeb.page (https://archiveweb.page) Copyright (C) 2020-${new Date().getFullYear()}, Webrecorder Software. Licensed under the Affero General Public License v3.`;
const manifest = require("./src/ext/manifest.json");
const defaultDefines = {
__AWP_VERSION__: JSON.stringify(AWP_PACKAGE.version),
__VERSION__: JSON.stringify(RWP_PACKAGE.version),
__WARCIO_VERSION__: JSON.stringify(WARCIO_PACKAGE.version),
__SW_NAME__: JSON.stringify("sw.js"),
__WEB3_STORAGE_TOKEN__: JSON.stringify(""),
};
const DIST_EXT = path.join(__dirname, "dist", "ext");
const DIST_ELECTRON = path.join(__dirname, "dist", "electron");
const DIST_EMBED = path.join(__dirname, "dist", "embed");
/** @type {import('webpack').Configuration['module']} */
const moduleSettings = {
rules: [
{
test: /\.tsx?$/,
loader: "ts-loader",
options: {
onlyCompileBundledFiles: true,
},
},
{
test: /\.svg$/,
use: "svg-inline-loader",
},
{
test: /\.s(a|c)ss$/,
use: ["css-loader", "sass-loader"],
},
{
test: /(dist\/wombat.js|src\/wombatWorkers.js|behaviors.js|extractPDF.js|ruffle.js|index.html)$/i,
use: "raw-loader",
},
],
};
/** @type {import('webpack').Configuration['optimization']} */
const optimization = {
minimize: true,
minimizer: [
new TerserPlugin({
extractComments: false,
}),
],
};
/** @type {import('webpack').Configuration['resolve']} */
const resolve = {
extensions: [".ts", ".js"],
plugins: [new TsconfigPathsPlugin()],
};
// ===========================================================================
/** @returns {import('webpack').Configuration} */
const electronMainConfig = (/*env, argv*/) => {
return {
target: "electron-main",
mode: "production",
entry: {
electron: "./src/electron/electron-rec-main.ts",
},
optimization,
output: {
path: DIST_ELECTRON,
filename: "[name].js",
},
node: {
__dirname: false,
__filename: false,
},
plugins: [
new webpack.DefinePlugin(defaultDefines),
new webpack.BannerPlugin(BANNER),
new CopyPlugin({
patterns: [{ from: "build/extra_prebuilds/", to: "prebuilds" }],
}),
],
module: moduleSettings,
resolve,
};
};
// ===========================================================================
/** @returns {import('webpack').Configuration} */
const electronPreloadConfig = (/*env, argv*/) => {
return {
target: "electron-preload",
mode: "production",
entry: {
preload: "./src/electron/electron-rec-preload.ts",
"rec-preload": "./src/electron/rec-preload.ts",
},
optimization,
output: {
path: DIST_ELECTRON,
filename: "[name].js",
},
plugins: [new webpack.DefinePlugin(defaultDefines)],
module: moduleSettings,
resolve,
};
};
// ===========================================================================
/**
* @param {string} outputPath
* @param {Object} [param1={}]
* @param {import('webpack').Configuration['plugins']} [param1.plugins=[]]
* @param {import('copy-webpack-plugin').CopyPlugin['Configuration']['patterns']} [param1.copy=[]]
* @param {import('webpack').Configuration['entry']} [param1.entry={}]
* @param {Partial<import('webpack').Configuration>} [param1.extra={}]
* @param {Partial<import('webpack').Configuration['target']>} [param1.target={}]
* @param {boolean} [param1.flat=false]
* @returns {import('webpack').Configuration}
*/
function sharedBuild(
outputPath,
{
plugins = [],
copy = [],
entry = {},
extra = {},
replayDir = false,
target = "web",
} = {},
argv,
) {
if (copy.length) {
plugins.push(new CopyPlugin({ patterns: copy }));
}
return {
mode: "production",
target,
entry: {
ui: "./src/ui/app.ts",
sw: "./src/sw/main.ts",
...entry,
},
devtool: argv.mode === "production" ? undefined : "inline-source-map",
optimization: argv.mode === "production" ? optimization : undefined,
output: {
path: outputPath,
filename: (chunkData) => {
const name = "[name].js";
switch (chunkData.chunk.name) {
case "sw":
return replayDir ? `./replay/${name}` : name;
default:
return name;
}
},
libraryTarget: "global",
globalObject: "self",
},
plugins: [
new webpack.NormalModuleReplacementPlugin(/^node:*/, (resource) => {
switch (resource.request) {
case "node:stream":
resource.request = "stream-browserify";
break;
}
}),
new webpack.ProvidePlugin({
process: "process/browser.js",
Buffer: ["buffer", "Buffer"],
}),
new MiniCssExtractPlugin(),
new webpack.BannerPlugin(BANNER),
new webpack.DefinePlugin({ ...defaultDefines }),
...plugins,
],
module: moduleSettings,
resolve,
...extra,
};
}
// ===========================================================================
const extensionWebConfig = (env, argv) => {
const icon = argv.mode === "production" ? "icon.png" : "icon-dev.png";
const generateManifest = (name, value) => {
switch (value) {
case "$VERSION":
return AWP_PACKAGE.version;
case "$ICON":
return icon;
}
return value;
};
const plugins = [
new GenerateJsonPlugin("manifest.json", manifest, generateManifest, 2),
];
const copy = [
{ from: "static", to: "./" },
{ from: "ruffle", to: "./ruffle/" },
];
const entry = {
bg: "./src/ext/bg.ts",
popup: "./src/popup.ts",
};
return sharedBuild(DIST_EXT, { plugins, copy, entry }, argv);
};
// ===========================================================================
const electronWebConfig = (env, argv) => {
const entry = {
"rec-window": "./src/electron/rec-window.ts",
};
const copy = [
{ from: "static/", to: "./" },
{ from: "ruffle", to: "./ruffle/" },
{ from: "src/electron/rec-window.html", to: "" },
];
return sharedBuild(DIST_ELECTRON, { copy, entry }, argv);
};
// ===========================================================================
const embedWebConfig = (env, argv) => {
const copy = [{ from: "src/embed.html", to: "./index.html" }];
const extra = {
devServer: {
compress: true,
port: 10001,
open: true,
static: {
directory: DIST_EMBED,
},
},
};
const replayDir = true;
return sharedBuild(DIST_EMBED, { copy, extra, replayDir }, argv);
};
// ===========================================================================
module.exports = [
extensionWebConfig,
electronWebConfig,
embedWebConfig,
electronMainConfig,
electronPreloadConfig,
];