Skip to content

Commit

Permalink
feat: support compilation.modules[i].dependencies (#8050)
Browse files Browse the repository at this point in the history
  • Loading branch information
inottn authored Nov 5, 2024
1 parent f8a4d27 commit 2c8e351
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ export class JsModule {
get type(): string
get layer(): string | undefined
get blocks(): Array<JsDependenciesBlock>
get dependencies(): Array<JsDependency>
size(ty?: string | undefined | null): number
get modules(): JsModule[] | undefined
get useSourceMap(): boolean
Expand Down
23 changes: 23 additions & 0 deletions crates/rspack_binding_values/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,29 @@ impl JsModule {
})
}

#[napi(getter)]
pub fn dependencies(&self) -> napi::Result<Vec<JsDependency>> {
Ok(match self.compilation {
Some(compilation) => {
let compilation = unsafe { compilation.as_ref() };
let module_graph = compilation.get_module_graph();
let module = self.as_ref()?;
let dependencies = module.get_dependencies();
dependencies
.iter()
.filter_map(|dependency_id| {
module_graph
.dependency_by_id(dependency_id)
.map(JsDependency::new)
})
.collect::<Vec<_>>()
}
None => {
vec![]
}
})
}

#[napi]
pub fn size(&mut self, ty: Option<String>) -> napi::Result<f64> {
let module = self.as_ref()?;
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
import("./a");
import "./b";
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ class Plugin {
() => {
const module = Array.from(compilation.modules).find(module => module.rawRequest === "./index.js");
const block = module.blocks[0];
const dependency = module.dependencies[0];
expect(block.dependencies[0].request).toBe("./a");
expect(dependency.request).toBe("./b");
}
)
});
Expand Down
9 changes: 9 additions & 0 deletions packages/rspack/src/Module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,15 @@ export class Module {
return [];
}
},
dependencies: {
enumerable: true,
get(): Dependency[] {
if ("dependencies" in module) {
return module.dependencies.map(d => Dependency.__from_binding(d));
}
return [];
}
},
useSourceMap: {
enumerable: true,
get(): boolean {
Expand Down

1 comment on commit 2c8e351

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Ran ecosystem CI: Open

suite result
${{ matrix.suite }} undefined skipped

Please sign in to comment.