Skip to content

Commit

Permalink
feat: support compilation.modules[i].dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
inottn committed Oct 3, 2024
1 parent 8f39056 commit 471f6e1
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 3 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 @@ -186,6 +186,7 @@ export class ModuleDto {
get type(): string
get layer(): string | undefined
get blocks(): Array<DependenciesBlockDto>
get dependencies(): Array<DependencyDto>
size(ty?: string | undefined | null): number
}
export type ModuleDTO = ModuleDto
Expand Down
11 changes: 11 additions & 0 deletions crates/rspack_binding_values/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,17 @@ impl ModuleDTO {
.collect::<Vec<_>>()
}

#[napi(getter)]
pub fn dependencies(&self) -> Vec<DependencyDTO> {
let module = self.module();
let dependencies = module.get_dependencies();
dependencies
.iter()
.cloned()
.map(|dependency_id| DependencyDTO::new(dependency_id, self.compilation))
.collect::<Vec<_>>()
}

#[napi]
pub fn size(&self, ty: Option<String>) -> f64 {
let module = self.module();
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
6 changes: 4 additions & 2 deletions packages/rspack/etc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -5885,6 +5885,8 @@ export class Module {
// (undocumented)
context?: Readonly<string>;
// (undocumented)
get dependencies(): Dependency[];
// (undocumented)
factoryMeta?: Readonly<JsFactoryMeta>;
// (undocumented)
identifier(): string;
Expand Down Expand Up @@ -12681,6 +12683,7 @@ export const rspackOptions: z.ZodObject<{
maxEntrypointSize?: number | undefined;
}>, z.ZodLiteral<false>]>>;
}, "strict", z.ZodTypeAny, {
dependencies?: string[] | undefined;
module?: {
parser?: {
javascript?: {
Expand Down Expand Up @@ -12810,7 +12813,6 @@ export const rspackOptions: z.ZodObject<{
defaultRules?: (false | "" | 0 | "..." | RuleSetRule | null | undefined)[] | undefined;
noParse?: string | RegExp | ((args_0: string, ...args_1: unknown[]) => boolean) | (string | RegExp | ((args_0: string, ...args_1: unknown[]) => boolean))[] | undefined;
} | undefined;
dependencies?: string[] | undefined;
name?: string | undefined;
context?: string | undefined;
performance?: false | {
Expand Down Expand Up @@ -13263,6 +13265,7 @@ export const rspackOptions: z.ZodObject<{
devServer?: DevServer | undefined;
bail?: boolean | undefined;
}, {
dependencies?: string[] | undefined;
module?: {
parser?: {
javascript?: {
Expand Down Expand Up @@ -13392,7 +13395,6 @@ export const rspackOptions: z.ZodObject<{
defaultRules?: (false | "" | 0 | "..." | RuleSetRule | null | undefined)[] | undefined;
noParse?: string | RegExp | ((args_0: string, ...args_1: unknown[]) => boolean) | (string | RegExp | ((args_0: string, ...args_1: unknown[]) => boolean))[] | undefined;
} | undefined;
dependencies?: string[] | undefined;
name?: string | undefined;
context?: string | undefined;
performance?: false | {
Expand Down
9 changes: 8 additions & 1 deletion packages/rspack/src/Module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { Source } from "webpack-sources";

import type { Compilation } from "./Compilation";
import { DependenciesBlock } from "./DependenciesBlock";
import type { Dependency } from "./Dependency";
import { Dependency } from "./Dependency";
import { JsSource } from "./util/source";

export type ResourceData = {
Expand Down Expand Up @@ -200,6 +200,13 @@ export class Module {
return [];
}

get dependencies(): Dependency[] {
if ("dependencies" in this.#inner) {
return this.#inner.dependencies.map(d => new Dependency(d));
}
return [];
}

size(type?: string): number {
if ("size" in this.#inner) {
return this.#inner.size(type);
Expand Down

0 comments on commit 471f6e1

Please sign in to comment.