-
-
Notifications
You must be signed in to change notification settings - Fork 632
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: introduce AsyncTask, make rebuildModule safer (#9244)
fix: introduce AsyncTask to make rebuildModule safer and correct the order of invoking module_executor hook_after_make
- Loading branch information
Showing
11 changed files
with
151 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/rspack-test-tools/tests/configCases/compilation/rebuild-module-multi-times/a.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const a = 1; |
7 changes: 7 additions & 0 deletions
7
packages/rspack-test-tools/tests/configCases/compilation/rebuild-module-multi-times/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { a as a1 } from "./a?1"; | ||
import { a as a2 } from "./a?2"; | ||
import { a as a3 } from "./a?3"; | ||
|
||
it("compilation rebuild module should works", () => { | ||
expect(a1 + a2 + a3).toBe(15) | ||
}); |
5 changes: 5 additions & 0 deletions
5
...ages/rspack-test-tools/tests/configCases/compilation/rebuild-module-multi-times/loader.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
let times = 0; | ||
module.exports = function loader(content) { | ||
times++; | ||
return content.replace("1", times); | ||
}; |
49 changes: 49 additions & 0 deletions
49
...pack-test-tools/tests/configCases/compilation/rebuild-module-multi-times/rspack.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
const pluginName = "plugin"; | ||
|
||
class Plugin { | ||
apply(compiler) { | ||
let initial = true; | ||
compiler.hooks.compilation.tap(pluginName, compilation => { | ||
compilation.hooks.finishModules.tapPromise(pluginName, async modules => { | ||
modules = [...modules]; | ||
if (initial) { | ||
initial = false; | ||
|
||
const results = await Promise.all( | ||
modules.map(m => { | ||
return new Promise((resolve, reject) => { | ||
compilation.rebuildModule(m, (err, module) => { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
resolve(module); | ||
} | ||
}); | ||
}); | ||
}) | ||
); | ||
|
||
// should compile success | ||
expect(results.length).toBe(4); | ||
} | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
/**@type {import("@rspack/core").Configuration}*/ | ||
module.exports = { | ||
module: { | ||
rules: [ | ||
{ | ||
test: /a\.js$/, | ||
use: [ | ||
{ | ||
loader: "./loader" | ||
} | ||
] | ||
} | ||
] | ||
}, | ||
plugins: [new Plugin()] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
type TaskCallback<Ret> = (err: Error | null, ret: Ret | null) => void; | ||
|
||
export class AsyncTask<Param, Ret> { | ||
#isRunning = false; | ||
#params: Param[] = []; | ||
#callbacks: TaskCallback<Ret>[] = []; | ||
|
||
#task: ( | ||
param: Param[], | ||
callback: (results: [Error | null, Ret | null][]) => void | ||
) => void; | ||
|
||
constructor( | ||
task: ( | ||
param: Param[], | ||
callback: (results: [Error | null, Ret | null][]) => void | ||
) => void | ||
) { | ||
this.#task = task; | ||
} | ||
|
||
#exec_internal() { | ||
const params = this.#params; | ||
const callbacks = this.#callbacks; | ||
this.#params = []; | ||
this.#callbacks = []; | ||
|
||
this.#task(params, results => { | ||
this.#isRunning = false; | ||
if (this.#params.length) { | ||
this.#isRunning = true; | ||
queueMicrotask(() => this.#exec_internal()); | ||
} | ||
|
||
for (let i = 0; i < results.length; i++) { | ||
const [err, result] = results[i]; | ||
const callback = callbacks[i]; | ||
callback(err, result); | ||
} | ||
}); | ||
} | ||
|
||
exec(param: Param, callback: TaskCallback<Ret>) { | ||
if (!this.#isRunning) { | ||
queueMicrotask(() => this.#exec_internal()); | ||
this.#isRunning = true; | ||
} | ||
|
||
this.#params.push(param); | ||
this.#callbacks.push(callback); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4d4dddd
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 Benchmark detail: Open
4d4dddd
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 Ecosystem CI detail: Open