-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathwebpack.extension.js
258 lines (249 loc) · 7.78 KB
/
webpack.extension.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
const path = require("path");
const fs = require("fs");
const webpack = require("webpack");
const CopyPlugin = require("copy-webpack-plugin");
const ForkTsCheckerWebpackPlugin = require("fork-ts-checker-webpack-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const WebpackShellPluginNext = require("webpack-shell-plugin-next");
const dotenv = require("dotenv");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const {
getPlugins,
getRules,
optimization,
resolveExtensions,
resolvePlugins,
} = require("./config/common");
const updateManifest = (manifestPath, isChrome) => {
if (!isChrome) {
return "";
}
const originalManifestString = fs.readFileSync(manifestPath);
const manifest = JSON.parse(originalManifestString);
manifest.key = process.env.EXTENSION_KEY || envFile.EXTENSION_KEY || "";
// Remove localhost from externally_connectable
if (
manifest["externally_connectable"] &&
manifest["externally_connectable"]["matches"]
) {
let matches = manifest["externally_connectable"]["matches"];
matches = matches.filter((match) => !match.includes("localhost"));
manifest["externally_connectable"]["matches"] = matches;
}
fs.writeFileSync(manifestPath, JSON.stringify(manifest, undefined, 2));
return originalManifestString;
};
/** @type { import('webpack').ConfigurationFactory } */
const createServiceWorkerConfig = (env, argv) => {
const devMode = argv.mode !== "production";
const targetBrowser = env.targetBrowser;
const isChrome = targetBrowser === "chrome";
const analyze = argv.analyze === "true";
const envFile = dotenv.config(
devMode ? undefined : { path: "./.env.prod" },
).parsed;
const envKeys = Object.keys(envFile).reduce((prev, next) => {
prev[`process.env.${next}`] = JSON.stringify(envFile[next]);
return prev;
}, {});
return {
mode: devMode ? "development" : "production",
target: "webworker",
entry: {
"js/background": path.join(
__dirname,
"src/extension/background/index.tsx",
),
},
output: {
path: path.join(__dirname, "dist", "extension"),
filename: "[name].js",
publicPath: "/",
},
module: {
rules: getRules(devMode, __dirname, "img"),
},
plugins: [
...getPlugins(devMode, envKeys, analyze),
new CleanWebpackPlugin(),
new MiniCssExtractPlugin(),
devMode && new ForkTsCheckerWebpackPlugin(),
].filter(Boolean),
resolve: {
plugins: resolvePlugins,
extensions: resolveExtensions,
},
// Dev server is only added to this one as adding to both is not supported yet
...(devMode && {
devtool: "",
devServer: {
static: {
directory: path.join(__dirname, "dist", "extension"),
watch: true,
},
devMiddleware: {
writeToDisk: true,
},
compress: false,
// When used in dev mode, the local server should be started separately as well for the extension to connect to.
// The server at this port will not be used by the extension
port: 12345,
hot: false,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods":
"GET, POST, PUT, DELETE, PATCH, OPTIONS",
"Access-Control-Allow-Headers":
"X-Requested-With, content-type, Authorization",
},
historyApiFallback: {
index: "/",
},
},
}),
...(!devMode && {
optimization,
}),
};
};
/** @type { import('webpack').ConfigurationFactory } */
const createContentAndPopupConfig = (env, argv) => {
const devMode = argv.mode !== "production";
const targetBrowser = env.targetBrowser;
const isChrome = targetBrowser === "chrome";
const analyze = argv.analyze === "true";
const envFile = dotenv.config(
devMode ? undefined : { path: "./.env.prod" },
).parsed;
const envKeys = Object.keys(envFile).reduce((prev, next) => {
prev[`process.env.${next}`] = JSON.stringify(envFile[next]);
return prev;
}, {});
let originalManifestString = "";
const manifestPath = path.join(
__dirname,
"extension-statics",
`manifest-${targetBrowser}.json`,
);
// Update manifest.json with key for build, remove it afterwards
if (!devMode && isChrome) {
originalManifestString = updateManifest(manifestPath, isChrome);
}
return {
mode: devMode ? "development" : "production",
target: "web",
entry: {
"js/popup": path.join(__dirname, "src/extension/popup/index.tsx"),
"js/content": path.join(__dirname, "src/extension/content/index.tsx"),
},
output: {
path: path.join(__dirname, "dist", "extension"),
filename: "[name].js",
publicPath: "/",
},
module: {
rules: getRules(devMode, __dirname, "img"),
},
plugins: [
new webpack.ProvidePlugin({ process: "process/browser" }),
...getPlugins(devMode, envKeys, analyze),
// Clean plugin is omitted here as the previous config will trigger it already
new MiniCssExtractPlugin(),
new CopyPlugin({
patterns: [
{
from: path.resolve(__dirname, "extension-statics"),
to: path.resolve(__dirname, "dist", "extension"),
globOptions: {
ignore: ["**/manifest-*.json"],
},
},
{
from: path.resolve(
__dirname,
"extension-statics",
`manifest-${targetBrowser}.json`,
),
to: path.resolve(__dirname, "dist", "extension", `manifest.json`),
},
{
from: path.resolve(
__dirname,
"src",
"extension",
"popup",
"popup.html",
),
to: path.resolve(__dirname, "dist", "extension", "popup.html"),
},
{
from: path.resolve(
__dirname,
"src",
"extension",
"background",
"background.html",
),
to: path.resolve(__dirname, "dist", "extension", "background.html"),
},
{
from: "src/libs/subtitle-octopus/*.{js,data,mem,wasm}",
to: path.resolve(
__dirname,
"dist",
"extension",
"js",
"subtitle-octopus",
"[name][ext]",
),
},
{
from: "src/libs/subtitle-octopus/assets/*.*",
to: path.resolve(
__dirname,
"dist",
"extension",
"sub-assets",
"[name][ext]",
),
},
],
}),
devMode && new ForkTsCheckerWebpackPlugin(),
!devMode &&
new WebpackShellPluginNext({
onBuildEnd: {
scripts: [`node zip-extension.js --target=${targetBrowser}`],
blocking: true,
parallel: false,
},
}),
!devMode && {
apply: (compiler) => {
compiler.hooks.afterEmit.tap("AfterEmitPlugin", (compilation) => {
// Restore original manifest without the key property
if (originalManifestString) {
fs.writeFileSync(manifestPath, originalManifestString);
}
});
},
},
].filter(Boolean),
resolve: {
plugins: resolvePlugins,
extensions: resolveExtensions,
alias: {
process: "process/browser",
},
},
...(!devMode && {
optimization,
}),
};
};
/** @type { import('webpack').MultiConfigurationFactory } */
module.exports = (env, argv) => {
const serviceWorkerConfig = createServiceWorkerConfig(env, argv);
const contentAndPopupConfig = createContentAndPopupConfig(env, argv);
return [serviceWorkerConfig, contentAndPopupConfig];
};