Skip to content

Commit 262cf63

Browse files
authored
fix: should run invalidation when watching is invalid (#8966)
fix: should run next invalidation when watching is invalid
1 parent f6210c1 commit 262cf63

File tree

3 files changed

+70
-10
lines changed

3 files changed

+70
-10
lines changed

crates/rspack_plugin_extract_css/src/plugin.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,6 @@ fn get_undo_path(filename: &str, output_path: &str, enforce_relative: bool) -> S
727727
let mut depth: isize = -1;
728728
let mut append = "".into();
729729

730-
// eslint-disable-next-line no-param-reassign
731730
let output_path = output_path.strip_suffix('\\').unwrap_or(output_path);
732731
let mut output_path = output_path
733732
.strip_suffix('/')
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
const path = require('path');
2+
3+
const mockFn = jest.fn(() => {});
4+
5+
class MyPlugin {
6+
apply(compiler) {
7+
compiler.hooks.watchRun.tap("MyPlugin", mockFn)
8+
}
9+
}
10+
11+
/** @type {import('../../dist').TCompilerCaseConfig} */
12+
module.exports = {
13+
description: "should be invalidated correctly",
14+
options(context) {
15+
return {
16+
context: context.getSource(),
17+
entry: "./abc",
18+
plugins: [new MyPlugin()]
19+
};
20+
},
21+
async build(_, compiler) {
22+
try {
23+
await new Promise((resolve, reject) => {
24+
let firstRun = true;
25+
compiler.watch({}, (err) => {
26+
if (err) {
27+
return reject(err);
28+
}
29+
if (firstRun) {
30+
firstRun = false;
31+
compiler.watching.lazyCompilationInvalidate(path.resolve(__dirname, "../fixtures/a.js"));
32+
compiler.watching.lazyCompilationInvalidate(path.resolve(__dirname, "../fixtures/b.js"));
33+
setTimeout(() => {
34+
resolve()
35+
}, 2000)
36+
}
37+
});
38+
});
39+
} catch(err) {
40+
throw err
41+
}
42+
43+
},
44+
async check() {
45+
expect(mockFn).toHaveBeenCalledTimes(3);
46+
}
47+
};

packages/rspack/src/Watching.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -330,19 +330,33 @@ export class Watching {
330330
}
331331
};
332332

333-
const cbs = this.callbacks;
334-
this.callbacks = [];
335-
const startTime = this.startTime; // store last startTime for compilation
336-
// reset startTime for next compilation, before throwing error
337-
this.startTime = undefined;
338333
if (error) {
339334
return handleError(error);
340335
}
341336
assert(compilation);
342337

338+
stats = new Stats(compilation);
339+
340+
if (
341+
this.invalid &&
342+
!this.suspended &&
343+
!this.blocked &&
344+
!(this.isBlocked() && (this.blocked = true))
345+
) {
346+
this.#go();
347+
return;
348+
}
349+
350+
const startTime = this.startTime; // store last startTime for compilation
351+
// reset startTime for next compilation, before throwing error
352+
this.startTime = undefined;
343353
compilation.startTime = startTime;
344354
compilation.endTime = Date.now();
345-
stats = new Stats(compilation);
355+
const cbs = this.callbacks;
356+
this.callbacks = [];
357+
const fileDependencies = new Set([...compilation.fileDependencies]);
358+
const contextDependencies = new Set([...compilation.contextDependencies]);
359+
const missingDependencies = new Set([...compilation.missingDependencies]);
346360

347361
this.compiler.hooks.done.callAsync(stats, err => {
348362
if (err) return handleError(err, cbs);
@@ -351,9 +365,9 @@ export class Watching {
351365
process.nextTick(() => {
352366
if (!this.#closed) {
353367
this.watch(
354-
compilation.fileDependencies,
355-
compilation.contextDependencies,
356-
compilation.missingDependencies
368+
fileDependencies,
369+
contextDependencies,
370+
missingDependencies
357371
);
358372
}
359373
});

0 commit comments

Comments
 (0)