diff --git a/packages/rspack-test-tools/tests/compilerCases/invalidation-multi-times.js b/packages/rspack-test-tools/tests/compilerCases/invalidation-multi-times.js new file mode 100644 index 000000000000..34e5f653f8a9 --- /dev/null +++ b/packages/rspack-test-tools/tests/compilerCases/invalidation-multi-times.js @@ -0,0 +1,47 @@ +const path = require('path'); + +const mockFn = jest.fn(() => {}); + +class MyPlugin { + apply(compiler) { + compiler.hooks.watchRun.tap("MyPlugin", mockFn) + } +} + +/** @type {import('../../dist').TCompilerCaseConfig} */ +module.exports = { + description: "should be invalidated correctly", + options(context) { + return { + context: context.getSource(), + entry: "./abc", + plugins: [new MyPlugin()] + }; + }, + async build(_, compiler) { + try { + await new Promise((resolve, reject) => { + let firstRun = true; + compiler.watch({}, (err) => { + if (err) { + return reject(err); + } + if (firstRun) { + firstRun = false; + compiler.watching.lazyCompilationInvalidate(path.resolve(__dirname, "../fixtures/a.js")); + compiler.watching.lazyCompilationInvalidate(path.resolve(__dirname, "../fixtures/b.js")); + setTimeout(() => { + resolve() + }, 2000) + } + }); + }); + } catch(err) { + throw err + } + + }, + async check() { + expect(mockFn).toHaveBeenCalledTimes(3); + } +}; diff --git a/packages/rspack/src/Watching.ts b/packages/rspack/src/Watching.ts index 0e127c96af6d..0c0b37930a84 100644 --- a/packages/rspack/src/Watching.ts +++ b/packages/rspack/src/Watching.ts @@ -344,6 +344,16 @@ export class Watching { compilation.endTime = Date.now(); stats = new Stats(compilation); + if ( + this.invalid && + !this.suspended && + !this.blocked && + !(this.isBlocked() && (this.blocked = true)) + ) { + this.#go(); + return; + } + this.compiler.hooks.done.callAsync(stats, err => { if (err) return handleError(err, cbs); this.handler(null, stats);