-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwebpack.config.js
More file actions
353 lines (331 loc) · 10 KB
/
webpack.config.js
File metadata and controls
353 lines (331 loc) · 10 KB
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
var webpack = require("webpack"),
path = require("path"),
fileSystem = require("fs"),
env = require("./utils/env"),
{ CleanWebpackPlugin } = require("clean-webpack-plugin"),
CopyWebpackPlugin = require("copy-webpack-plugin"),
HtmlWebpackPlugin = require("html-webpack-plugin"),
TerserPlugin = require("terser-webpack-plugin"),
copyOnChange = require("./utils/copy-on-change.js");
// load the secrets
var alias = {};
var secretsPath = path.join(__dirname, "secrets." + env.NODE_ENV + ".js");
var fileExtensions = ["jpg", "jpeg", "gif", "eot", "otf", "ttf"];
if (fileSystem.existsSync(secretsPath)) {
alias["secrets"] = secretsPath;
}
alias.utils = path.join(__dirname, "src/utils.js");
var options = {
mode: process.env.NODE_ENV || "development",
entry: {
// popup: path.join(__dirname, "src", "js", "popup.js"),
inject: path.join(__dirname, "src", "content", "inject.js"),
"inject-pdf-reader": path.join(
__dirname,
"src",
"content",
"inject-pdf-reader.js",
),
"inject-reader-customization": path.join(
__dirname,
"src",
"content",
"inject-reader-customization.js",
),
background: path.join(__dirname, "src", "background", "main.js"),
"pdf-viewer": path.join(__dirname, "src", "content", "pdf-viewer.js"),
"custom-font": path.join(__dirname, "src", "custom-font", "index.js"),
"custom-site": path.join(__dirname, "src", "custom-site", "index.js"),
share: path.join(__dirname, "src", "share.js"),
"tts-player-webcomponent": path.join(
__dirname,
"src",
"content",
"ttsPlayer",
"webcomponent.js",
),
"translator-webcomponent": path.join(
__dirname,
"src",
"content",
"translation",
"webcomponent.js",
),
},
output: {
path: path.join(__dirname, "build"),
filename: "[name].bundle.js",
clean: true,
publicPath: "/",
},
module: {
rules: [
// Rule for raw CSS imports (for site customization styles)
{
test: /\.css$/,
resourceQuery: /raw/,
type: "asset/source",
},
{
test: /\.css$/,
resourceQuery: { not: [/raw/] },
use: ["style-loader", "css-loader"],
},
// Rule for inline SCSS imports (must come first to catch ?inline query)
{
test: /\.scss$/,
resourceQuery: /inline/,
use: [
"raw-loader",
{
loader: "sass-loader",
options: {
sassOptions: {
outputStyle: "compressed",
includePaths: ["node_modules"], // For @picocss imports
},
},
},
],
},
// Add CSS Modules support for .module.scss files
{
test: /\.module\.scss$/,
use: [
"style-loader",
{
loader: "css-loader",
options: {
modules: {
localIdentName: "[name]__[local]___[hash:base64:5]",
exportLocalsConvention: "camelCase", // This is important!
},
esModule: false, // Important for proper export
},
},
"sass-loader",
],
},
{
test: /\.scss$/,
exclude: /\.module\.scss$/, // Exclude module files from this rule
resourceQuery: { not: [/inline/] }, // Exclude inline queries
use: [
{
loader: "css-loader",
options: {
esModule: false, // Disable ES modules to allow CommonJS-style imports
exportType: "string", // Export CSS as a string
},
},
"sass-loader", // Compiles SCSS to CSS
],
},
{
test: /\.less$/,
use: [
{
loader: "style-loader", // creates style nodes from JS strings
},
{
loader: "css-loader", // translates CSS into CommonJS
},
{
loader: "less-loader", // compiles Less to CSS
},
],
},
{
test: /\.(png|woff|woff2)$/,
type: "asset/inline", // Inline the font files as Base64
},
{
test: new RegExp(".(" + fileExtensions.join("|") + ")$"),
type: "asset/resource",
exclude: /node_modules/,
},
{
test: /\.html$/,
loader: "html-loader",
exclude: /node_modules/,
},
{
test: /\.mjs$/,
type: "javascript/auto",
},
],
},
resolve: {
alias: alias,
},
plugins: [
// clean the build folder
new CleanWebpackPlugin({ verbose: false }),
new webpack.ProgressPlugin(),
// expose and write the allowed env vars on the compiled bundle
new webpack.EnvironmentPlugin(["NODE_ENV"]),
new CopyWebpackPlugin({
patterns: [
{
from: "src/manifest.json",
to: path.join(__dirname, "build"),
force: true,
transform: function (content) {
// generates the manifest file using the package.json informations
const json = {
description: process.env.npm_package_description,
version: process.env.npm_package_version,
...JSON.parse(content.toString()),
};
if (env.BROWSER === "Firefox") {
json.manifest_version = 2; // Firefox has host permission issue with manifest v3
json.browser_action = json.action;
delete json.host_permissions;
delete json.action;
delete json.minimum_chrome_version;
json.background = {
scripts: ["background.bundle.js"],
};
// Firefox requires host permission for all urls to inject script when it's not trigger by user activities
json.permissions.push("<all_urls>");
// web_accessible_resources in MV2 is an array of strings for Firefox
json.web_accessible_resources =
json.web_accessible_resources[0].resources;
// Firefox Android requires data_collection_permissions and id
json.browser_specific_settings = {
gecko: {
id: "{8a1b9de7-0601-4ccb-93b8-3f60fa7a5772}",
data_collection_permissions: {
required: ["authenticationInfo", "websiteActivity"],
},
},
};
}
if (env.NODE_ENV === "development") {
json.content_scripts[0].matches.push("http://localhost:4567/*");
if (env.BROWSER === "Firefox") {
json.web_accessible_resources.push("*.js.map");
} else {
json.web_accessible_resources[0].resources.push("*.js.map");
}
}
return Buffer.from(JSON.stringify(json));
},
},
],
}),
new CopyWebpackPlugin({
patterns: [
{
from: "src/images",
to: path.join(__dirname, "build/images"),
force: true,
},
],
}),
new HtmlWebpackPlugin({
template: path.join(__dirname, "src", "options.html"),
filename: "options.html",
cache: false,
chunks: ["inject"],
}),
new HtmlWebpackPlugin({
template: path.join(__dirname, "src", "pdf-viewer.html"),
filename: "pdf-viewer.html",
cache: false,
chunks: ["pdf-viewer"],
}),
new HtmlWebpackPlugin({
template: path.join(__dirname, "src", "custom-font", "index.html"),
filename: "custom-font.html",
cache: false,
chunks: ["custom-font"],
}),
new HtmlWebpackPlugin({
template: path.join(__dirname, "src", "custom-site", "index.html"),
filename: "custom-site.html",
cache: false,
chunks: ["custom-site"],
}),
new HtmlWebpackPlugin({
template: path.join(__dirname, "src", "share.html"),
filename: "share.html",
chunks: ["share"],
cache: false,
}),
],
infrastructureLogging: {
level: "info",
},
};
if (env.NODE_ENV === "development") {
console.log("Run in dev");
options.devtool = "cheap-module-source-map";
} else {
console.log("Run in prod");
options.optimization = {
minimize: true,
minimizer: [
new TerserPlugin({
extractComments: false,
}),
],
};
}
// copy and watch some shared files to dictionariez during development & production build
(() => {
function setupCopyWatchs() {
copyOnChange({
sourceFile: path.join(
__dirname,
"build",
"tts-player-webcomponent.bundle.js",
),
destDir: path.join(__dirname, "..", "dictionariez", "src", "content"),
watchMode: env.NODE_ENV === "development",
});
copyOnChange({
sourceFile: path.join(
__dirname,
"build",
"translator-webcomponent.bundle.js",
),
destDir: path.join(__dirname, "..", "dictionariez", "src", "content"),
watchMode: env.NODE_ENV === "development",
});
copyOnChange({
sourceFiles: [
path.resolve("src/content/detectLanguage.js"),
path.resolve("src/content/getTextFromNode.js"),
path.resolve("src/utils.js"),
],
destDir: path.resolve("../dictionariez/src/shared-readonly"),
watchMode: env.NODE_ENV === "development",
});
copyOnChange({
sourceFiles: [
path.resolve("src/background/tts-speak.js"),
path.resolve("src/background/translate.js"),
path.resolve("src/background/pnl-cloud.js"),
],
destDir: path.resolve("../dictionariez/src/background"),
watchMode: env.NODE_ENV === "development",
});
copyOnChange({
sourceFiles: [path.resolve("src/utils.js")],
destDir: path.resolve("../cats-love-youtube/captionz-ext/src"),
watchMode: env.NODE_ENV === "development",
});
}
if (env.NODE_ENV === "development") {
setupCopyWatchs();
} else {
// Add webpack plugin for production copy
options.plugins.push({
apply(compiler) {
compiler.hooks.afterEmit.tap("ProductionCopyPlugin", setupCopyWatchs);
},
});
}
})();
module.exports = options;