Skip to content

Commit 3b236bd

Browse files
refactor: improve structure of RunCommandsPlugin (#1117)
1 parent 381b9f9 commit 3b236bd

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

rspack.config.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ for (const userScript of userScripts) {
1616

1717
class RunCommandsPlugin {
1818
private readonly env: Record<string, unknown>;
19-
private isFirstRun = true;
20-
private localesWatcher: null | ReturnType<typeof watch> = null;
2119

2220
public constructor(env: Record<string, unknown>) {
2321
this.env = env;
@@ -69,14 +67,16 @@ class RunCommandsPlugin {
6967

7068
// eslint-disable-next-line max-lines-per-function
7169
public apply(compiler: Compiler): void {
70+
let isFirstRun = true;
7271
let typeWatcher: null | ReturnType<typeof watch> = null;
7372
let manifestWatcher: null | ReturnType<typeof watch> = null;
73+
let localesWatcher: null | ReturnType<typeof watch> = null;
7474
let isWatchMode = false;
7575

7676
compiler.hooks.beforeCompile.tapAsync("RunCommandsPlugin", (_params, callback) => {
7777
if (!isWatchMode) {
7878
RunCommandsPlugin.generateTypeGuards(callback);
79-
} else if (this.isFirstRun) {
79+
} else if (isFirstRun) {
8080
// `this.isFirstRun` is also used in the afterEmit hook, so it should be set to false in there.
8181
RunCommandsPlugin.generateTypeGuards(callback);
8282
} else {
@@ -88,7 +88,7 @@ class RunCommandsPlugin {
8888
compiler.hooks.watchRun.tapAsync("RunCommandsPlugin", (_params, callback) => {
8989
isWatchMode = true;
9090

91-
if (!manifestWatcher || !typeWatcher || !this.localesWatcher) {
91+
if (!manifestWatcher || !typeWatcher || !localesWatcher) {
9292
manifestWatcher = watch("src/manifest/", {
9393
ignored: (pathString, stats) => Boolean(stats && stats.isFile() && !pathString.endsWith(".json"))
9494
});
@@ -112,12 +112,12 @@ class RunCommandsPlugin {
112112
});
113113
});
114114

115-
if (!this.localesWatcher) {
116-
this.localesWatcher = watch("src/_locales/", {
115+
if (!localesWatcher) {
116+
localesWatcher = watch("src/_locales/", {
117117
ignored: (pathString, stats) =>
118118
Boolean(stats && stats.isFile() && !pathString.endsWith(".json"))
119119
});
120-
this.localesWatcher.on("change", (pathString: string) => {
120+
localesWatcher.on("change", (pathString: string) => {
121121
// eslint-disable-next-line no-console
122122
console.log(`Locale file changed: ${pathString}`);
123123
RunCommandsPlugin.updatePrivacyPolicy();
@@ -130,15 +130,13 @@ class RunCommandsPlugin {
130130
});
131131

132132
compiler.hooks.afterEmit.tapAsync("RunCommandsPlugin", (_compilation, callback) => {
133-
if (!isWatchMode) {
134-
RunCommandsPlugin.updateManifest();
135-
RunCommandsPlugin.updatePrivacyPolicy();
136-
} else if (this.isFirstRun) {
137-
this.isFirstRun = false;
133+
if (!isWatchMode || isFirstRun) {
138134
RunCommandsPlugin.updateManifest();
139135
RunCommandsPlugin.updatePrivacyPolicy();
140136
}
141137

138+
isFirstRun = false;
139+
142140
if (this.env.updateUserScripts) {
143141
exec("npx tsx ./script/addUserScriptsComment.ts", (err, stdout) => {
144142
if (err) {

0 commit comments

Comments
 (0)