Skip to content

Commit 4d3782f

Browse files
committed
add test
1 parent f5bfd49 commit 4d3782f

File tree

5 files changed

+70
-16
lines changed

5 files changed

+70
-16
lines changed

crates/node_binding/src/compilation/mod.rs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -767,24 +767,15 @@ impl JsCompilation {
767767
.into_iter()
768768
.map(|dependency_id| {
769769
let module_graph = compilation.get_module_graph();
770-
match module_graph.module_graph_module_by_dependency_id(&dependency_id) {
771-
Some(module) => match module_graph.module_by_identifier(&module.module_identifier) {
772-
Some(module) => {
773-
let js_module =
774-
JsModuleWrapper::new(module.identifier(), None, compilation.compiler_id());
775-
(Either::B(()), Either::B(js_module))
776-
}
777-
None => (
778-
Either::A(format!(
779-
"Module created by {:#?} cannot be found",
780-
dependency_id
781-
)),
782-
Either::A(()),
783-
),
784-
},
770+
match module_graph.get_module_by_dependency_id(&dependency_id) {
771+
Some(module) => {
772+
let js_module =
773+
JsModuleWrapper::new(module.identifier(), None, compilation.compiler_id());
774+
(Either::B(()), Either::B(js_module))
775+
}
785776
None => (
786777
Either::A(format!(
787-
"Module created by {:#?} cannot be found",
778+
"Module created by {:?} cannot be found",
788779
dependency_id
789780
)),
790781
Either::A(()),
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
STATE.foo = 42;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
__webpack_require__("./foo.js");
2+
3+
it("should addInclude foo.js", () => {
4+
expect(STATE.foo).toBe(42);
5+
const builtModules = Object.fromEntries(__STATS__.modules.map(m => [m.name, m.built]));
6+
expect(builtModules).toEqual({ "./index.js": true, "./foo.js": true });
7+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
__webpack_require__("./foo.js");
2+
3+
it("should addInclude foo.js", () => {
4+
expect(STATE.foo).toBe(42);
5+
const builtModules = Object.fromEntries(__STATS__.modules.map(m => [m.name, m.built]));
6+
expect(builtModules).toEqual({ "./index.js": true, "./foo.js": false });
7+
});
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
let step = 0;
2+
let factorizeRequests = [];
3+
4+
/** @type {import("@rspack/core").Configuration} */
5+
module.exports = {
6+
plugins: [
7+
function (compiler) {
8+
const PLUGIN_NAME = "TEST_PLUGIN";
9+
const { EntryPlugin } = compiler.webpack;
10+
compiler.hooks.finishMake.tapPromise(PLUGIN_NAME, compilation => {
11+
return new Promise((resolve, reject) => {
12+
compilation.addInclude(
13+
compiler.context,
14+
EntryPlugin.createDependency("./foo.js"),
15+
{},
16+
err => {
17+
if (err) return reject(err);
18+
return resolve();
19+
}
20+
);
21+
});
22+
});
23+
24+
compiler.hooks.compilation.tap(
25+
PLUGIN_NAME,
26+
(compilation, { normalModuleFactory }) => {
27+
normalModuleFactory.hooks.factorize.tap(PLUGIN_NAME, data => {
28+
factorizeRequests.push(data.request);
29+
});
30+
}
31+
);
32+
compiler.hooks.done.tap(PLUGIN_NAME, () => {
33+
if (step === 0) {
34+
expect(factorizeRequests.length).toBe(2);
35+
expect(factorizeRequests.includes("./index.js")).toBe(true);
36+
expect(factorizeRequests.includes("./foo.js")).toBe(true);
37+
} else if (step === 1) {
38+
expect(factorizeRequests.length).toBe(1);
39+
expect(factorizeRequests.includes("./index.js")).toBe(true);
40+
} else {
41+
throw new Error("Unexpected step");
42+
}
43+
step += 1;
44+
factorizeRequests = [];
45+
});
46+
}
47+
]
48+
};

0 commit comments

Comments
 (0)