-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
158 lines (132 loc) · 4.51 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
/*eslint-env node */
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 APP_FILE_SERVE_PREFIX = "http://files.archiveweb.page/";
const AWP_PACKAGE = require("./package.json");
const WARCIO_PACKAGE = require("./node_modules/warcio/package.json");
const BANNER =
"[name].js is part of the Webrecorder Extension (https://replayweb.page) Copyright (C) 2020-2021, Webrecorder Software. Licensed under the Affero General Public License v3.";
const manifest = require("./src/ext/manifest.json");
const Dotenv = require('dotenv-webpack');
const defaultDefines = {
__AWP_VERSION__: JSON.stringify(AWP_PACKAGE.version),
__WARCIO_VERSION__: JSON.stringify(WARCIO_PACKAGE.version),
__SW_NAME__: JSON.stringify("sw.js"),
__APP_FILE_SERVE_PREFIX__: JSON.stringify(APP_FILE_SERVE_PREFIX),
__WEB3_STORAGE_TOKEN__: JSON.stringify(""),
};
const DIST_EXT = path.join(__dirname, "dist", "ext");
const moduleSettings = {
rules: [
{
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: ["thread-loader", "raw-loader"],
},
],
};
const optimization = {
minimize: true,
minimizer: [
new TerserPlugin({
extractComments: false,
}),
],
};
// ===========================================================================
function sharedBuild(outputPath, { plugins = [], copy = [], entry = {}, extra = {}, flat = false } = {}, env, argv) {
if (copy.length) {
plugins.push(new CopyPlugin({ patterns: copy }));
}
return {
mode: "production",
target: "web",
entry: {
...entry,
},
optimization,
//resolve: { fallback },
devtool: "inline-source-map",
output: {
path: outputPath,
filename: (chunkData) => {
const name = "[name].js";
const replayName = "./replay/" + name;
switch (chunkData.chunk.name) {
case "ui":
return flat ? name : replayName;
case "sw":
return replayName;
default:
return name;
}
},
libraryTarget: "global",
globalObject: "self",
},
plugins: [
new Dotenv({
allowEmptyValues: true, // allow empty variables (e.g. `FOO=`) (treat it as empty string, rather than missing)
systemvars: true, // load all the predefined 'process.env' variables which will trump anything local per dotenv specs.
silent: false, // hide any errors
defaults: false, // load '.env.defaults' as the default values if empty.
prefix: 'process.env.' // reference your env variables as 'import.meta.env.ENV_VAR'.
}),
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,
...extra,
};
}
// ===========================================================================
const extensionWebConfig = (env, argv) => {
const icon = argv.mode === "production" ? "logo.png" : "logo-dev.png";
const generateManifest = (name, value) => {
switch (value) {
case "$VERSION":
return AWP_PACKAGE.version + (process.env.EXTENSION_VERSION || "111");
case "$ICON":
return icon;
case "$ADMIN_URL":
return argv.mode === "production" ? (process.env.ADMIN_URL)+"/" : '*://*/*' ;
}
return value;
};
const plugins = [new GenerateJsonPlugin("manifest.json", manifest, generateManifest, 2)];
const copy = [{ from: "src/static/", to: "./" }];
const entry = {
bg: "./src/ext/bg.js",
popup: "./src/popup.js",
content: "./src/ext/content.js"
};
return sharedBuild(DIST_EXT, { plugins, copy, entry }, env, argv);
};
// ===========================================================================
module.exports = [extensionWebConfig];