-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrspack.config.ts
More file actions
166 lines (153 loc) · 5.19 KB
/
rspack.config.ts
File metadata and controls
166 lines (153 loc) · 5.19 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
import {
type Compiler,
CopyRspackPlugin,
LightningCssMinimizerRspackPlugin,
SwcJsMinimizerRspackPlugin
} from "@rspack/core";
import ForkTsCheckerWebpackPlugin from "fork-ts-checker-webpack-plugin";
import { defineConfig } from "@rspack/cli";
import { exec } from "child_process";
import { watch } from "chokidar";
class RunCommandsPlugin {
private readonly env: Record<string, unknown>;
public constructor(env: Record<string, unknown>) {
this.env = env;
}
private static copyManifest(callback?: () => void): void {
exec("cross-env NODE_OPTIONS=--experimental-transform-types node ./script/copyManifest.ts", (err, stdout) => {
// eslint-disable-next-line no-console
console.log("Copying manifest files...");
if (err) {
// eslint-disable-next-line no-console
console.error(`Error: ${err.message}`);
} else {
// eslint-disable-next-line no-console
console.log(stdout);
// eslint-disable-next-line no-console
console.log("Finished copying manifest files.");
if (callback) {
callback();
}
}
});
}
// eslint-disable-next-line max-lines-per-function
public apply(compiler: Compiler): void {
let isWatchMode = false;
let isFirstRun = true;
let manifestWatcher: ReturnType<typeof watch> | null = null;
compiler.hooks.watchRun.tapAsync("RunCommandsPlugin", (_params, callback) => {
isWatchMode = true;
if (manifestWatcher) {
callback();
return;
}
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.copyManifest();
});
callback();
});
compiler.hooks.afterEmit.tapAsync("RunCommandsPlugin", (_compilation, callback) => {
if (!isWatchMode || isFirstRun) {
RunCommandsPlugin.copyManifest();
}
isFirstRun = false;
if (this.env["updateUserScripts"]) {
exec(
"cross-env NODE_OPTIONS=--experimental-transform-types node ./script/addUserScriptComment.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 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: {
"./chrome/js/index.js": "./src/ts/index.ts",
"./firefox/js/index.js": "./src/ts/index.ts",
...(env["updateUserScripts"] ? { "../index.user.js": "./src/ts/index.ts" } : {})
},
output: {
filename: "[name]",
clean: true
},
optimization: {
minimizer: [
new SwcJsMinimizerRspackPlugin({
extractComments: true
}),
new LightningCssMinimizerRspackPlugin({})
]
},
module: {
rules: [
{
test: /\.ts$/u,
exclude: [/node_modules/u],
loader: "builtin:swc-loader",
options: {
jsc: {
parser: {
syntax: "typescript"
}
}
},
type: "javascript/auto"
}
]
},
resolve: {
extensions: [".ts", ".js"]
},
plugins: [
new RunCommandsPlugin(env),
new CopyRspackPlugin({
patterns: [
{
context: "./src/_locales/",
from: "**/*",
to: "chrome/_locales/"
},
{
context: "./src/_locales/",
from: "**/*",
to: "firefox/_locales/"
},
{
context: "./public/",
from: "**/*",
to: "chrome/"
},
{
context: "./public/",
from: "**/*",
to: "firefox/"
}
]
}),
new ForkTsCheckerWebpackPlugin()
]
}));
/* eslint-enable sort-keys */
export default config;