-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrspack.config.ts
270 lines (247 loc) · 8.91 KB
/
rspack.config.ts
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
import { type Compiler, CopyRspackPlugin, type CopyRspackPluginOptions } from "@rspack/core";
import ForkTsCheckerWebpackPlugin from "fork-ts-checker-webpack-plugin";
import LicensePlugin from "webpack-license-plugin";
import UnPluginTypia from "@ryoppippi/unplugin-typia/rspack";
import { defineConfig } from "@rspack/cli";
import { exec } from "child_process";
import { glob } from "glob";
import path from "path";
import { watch } from "chokidar";
const userScripts = glob.sync("./src/ts/userScript/*.user.ts");
const userScriptEntries: Record<string, string> = {};
for (const userScript of userScripts) {
const key = `../userScript/${path.basename(userScript, ".ts")}.js`;
userScriptEntries[key] = `./${userScript}`;
}
class RunCommandsPlugin {
private readonly env: Record<string, unknown>;
public constructor(env: Record<string, unknown>) {
this.env = env;
}
private static updateManifest(): void {
exec("npx tsx ./script/copyManifest.ts", (err, stdout) => {
if (err) {
// eslint-disable-next-line no-console
console.error(`Error: ${err.message}`);
} else {
// eslint-disable-next-line no-console
console.log(stdout);
}
});
}
private static updatePrivacyPolicy(callback?: () => void): void {
exec("npx tsx ./script/updatePrivacyPolicy.ts", (err, stdout) => {
if (err) {
// eslint-disable-next-line no-console
console.error(`Error: ${err.message}`);
} else {
// eslint-disable-next-line no-console
console.log(stdout);
if (callback) {
callback();
}
}
});
}
// eslint-disable-next-line max-lines-per-function
public apply(compiler: Compiler): void {
let isFirstRun = true;
let typeWatcher: null | ReturnType<typeof watch> = null;
let manifestWatcher: null | ReturnType<typeof watch> = null;
let localesWatcher: null | ReturnType<typeof watch> = null;
let isWatchMode = false;
// eslint-disable-next-line max-statements
compiler.hooks.watchRun.tapAsync("RunCommandsPlugin", (_params, callback) => {
isWatchMode = true;
if (!manifestWatcher || !typeWatcher || !localesWatcher) {
manifestWatcher = watch("src/manifest/", {
ignored: (pathString, stats) => Boolean(stats && stats.isFile() && !pathString.endsWith(".json"))
});
manifestWatcher.on("change", (pathString: string) => {
// eslint-disable-next-line no-console
console.log(`Manifest file changed: ${pathString}`);
RunCommandsPlugin.updateManifest();
});
typeWatcher = watch("src/types/", {
ignored: (pathString, stats) => Boolean(stats && stats.isFile() && !pathString.endsWith(".ts"))
});
typeWatcher.on("change", (pathString: string) => {
// eslint-disable-next-line no-console
console.log(`Type definition file changed: ${pathString}`);
compiler.watching?.invalidate();
});
if (!localesWatcher) {
localesWatcher = watch("src/_locales/", {
ignored: (pathString, stats) =>
Boolean(stats && stats.isFile() && !pathString.endsWith(".json"))
});
localesWatcher.on("change", (pathString: string) => {
// eslint-disable-next-line no-console
console.log(`Locale file changed: ${pathString}`);
RunCommandsPlugin.updatePrivacyPolicy();
});
}
callback();
} else {
callback();
}
});
compiler.hooks.afterEmit.tapAsync("RunCommandsPlugin", (_compilation, callback) => {
if (!isWatchMode || isFirstRun) {
RunCommandsPlugin.updateManifest();
RunCommandsPlugin.updatePrivacyPolicy();
}
isFirstRun = false;
if (this.env["updateUserScripts"]) {
exec("npx tsx ./script/addUserScriptsComment.ts", (err, stdout) => {
if (err) {
// eslint-disable-next-line no-console
console.error(`Error: ${err.message}`);
} else {
// eslint-disable-next-line no-console
console.log(stdout);
}
callback();
});
} else {
callback();
}
});
}
}
const scripts = {
"js/background.js": "./src/ts/background.ts",
"js/browserAction.js": "./src/ts/browserAction/browserAction.ts",
"js/contentScript.js": "./src/ts/contentScript.ts",
"js/initialSetup.js": "./src/ts/initialSetup.ts",
"js/ossLicenses.js": "./src/ts/ossLicenses.ts",
"js/pageScript.js": "./src/ts/pageScript.ts",
"js/privacyPolicy.js": "./src/ts/privacyPolicy.ts"
} as const;
const chromeScripts: Record<string, string> = {};
for (const key in scripts) {
if (Object.hasOwn(scripts, key)) {
chromeScripts[`./chrome/${key}`] = scripts[key as keyof typeof scripts];
}
}
const firefoxScripts: Record<string, string> = {};
for (const key in scripts) {
if (Object.hasOwn(scripts, key)) {
firefoxScripts[`./firefox/${key}`] = scripts[key as keyof typeof scripts];
}
}
const copyTargets = [
{
from: "./src/css/",
to: "css/[name][ext]"
},
{
from: "./src/html/",
to: "html/[name][ext]"
},
{
from: "./src/image/",
to: "image/[name][ext]"
},
{
from: "LICENSE",
to: ""
},
{
context: "./src/_locales/",
from: "**/*",
to: "_locales/"
}
] as const;
const chromeCopyTargets: CopyRspackPluginOptions["patterns"] = [];
for (const target of copyTargets) {
chromeCopyTargets.push({
...target,
to: `./chrome/${target.to}`
});
}
const firefoxCopyTargets: CopyRspackPluginOptions["patterns"] = [];
for (const target of copyTargets) {
firefoxCopyTargets.push({
...target,
to: `./firefox/${target.to}`
});
}
const unacceptableLicenseTest = (licenseIdentifier: string): boolean => {
const acceptableLicenses: readonly string[] = ["BSD-3-Clause", "Apache-2.0", "MIT", "0BSD", "MPL-2.0"];
return !acceptableLicenses.includes(licenseIdentifier);
};
const isProduction = process.env["NODE_ENV"] === "production";
/* eslint-disable sort-keys */
// eslint-disable-next-line max-lines-per-function
const config = defineConfig((env) => ({
mode: isProduction ? "production" : "development",
devtool: isProduction ? false : "source-map",
entry: {
...chromeScripts,
...firefoxScripts,
...(env["updateUserScripts"] ? userScriptEntries : {})
},
output: {
filename: "[name]",
clean: true
},
module: {
rules: [
{
test: /\.ts$/u,
exclude: [/node_modules/u],
loader: "builtin:swc-loader",
options: {
// Ref: https://github.com/lit/lit/issues/4580#issuecomment-1996733777
jsc: {
parser: {
syntax: "typescript",
decorators: true
},
keepClassNames: true,
transform: {
legacyDecorator: true,
useDefineForClassFields: false
}
}
},
type: "javascript/auto"
},
{
test: /\.css$/u,
use: ["style-loader", "css-loader"],
type: "javascript/auto"
}
]
},
resolve: {
extensions: [".ts", ".js"]
},
watchOptions: {
ignored: /src\/types\/.*(?<!\.d\.ts)$/u
},
plugins: [
new RunCommandsPlugin(env),
new CopyRspackPlugin({
patterns: [...chromeCopyTargets, ...firefoxCopyTargets]
}),
new LicensePlugin({
outputFilename: "./chrome/json/oss-licenses.json",
unacceptableLicenseTest,
includeNoticeText: true
}),
new LicensePlugin({
outputFilename: "./firefox/json/oss-licenses.json",
unacceptableLicenseTest,
includeNoticeText: true
}),
new ForkTsCheckerWebpackPlugin(),
// eslint-disable-next-line new-cap
UnPluginTypia({
cache: true
})
]
}));
/* eslint-enable sort-keys */
export default config;