Skip to content

Commit

Permalink
feat: exports info
Browse files Browse the repository at this point in the history
  • Loading branch information
SyMind committed Dec 12, 2024
1 parent 532a15d commit 32cff6e
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 2 deletions.
7 changes: 7 additions & 0 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,12 @@ export declare class JsEntries {
values(): Array<EntryDataDto>
}

export declare class JsExportsInfo {
isUsed(runtime: string | string[] | undefined): boolean
isModuleUsed(runtime: string | string[] | undefined): boolean
setUsedInUnknownWay(runtime: string | string[] | undefined): boolean
}

export declare class JsModule {
get context(): string | undefined
get originalSource(): JsCompatSource | undefined
Expand All @@ -229,6 +235,7 @@ export declare class JsModuleGraph {
getModule(jsDependency: JsDependency): JsModule | null
getUsedExports(jsModule: JsModule, jsRuntime: string | Array<string>): boolean | Array<string> | null
getIssuer(module: JsModule): JsModule | null
getExportsInfo(module: JsModule): JsExportsInfo
}

export declare class JsResolver {
Expand Down
4 changes: 3 additions & 1 deletion crates/rspack_binding_values/src/dependency.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{cell::RefCell, ptr::NonNull};

use napi::bindgen_prelude::ToNapiValue;
use napi::{bindgen_prelude::ToNapiValue, Either};
use napi_derive::napi;
use rspack_core::{Compilation, CompilationId, Dependency, DependencyId};
use rspack_napi::OneShotRef;
Expand Down Expand Up @@ -178,3 +178,5 @@ impl ToNapiValue for JsDependencyWrapper {
})
}
}

pub type JsRuntimeSpec = Either<String, Vec<String>>;
79 changes: 79 additions & 0 deletions crates/rspack_binding_values/src/exports_info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use std::{ptr::NonNull, sync::Arc};

use napi::Either;
use napi_derive::napi;
use rspack_core::{Compilation, ExportsInfo, ModuleGraph, RuntimeSpec};

use crate::JsRuntimeSpec;

#[napi]
pub struct JsExportsInfo {
exports_info: ExportsInfo,
compilation: NonNull<Compilation>,
}

impl JsExportsInfo {
pub fn new(exports_info: ExportsInfo, compilation: &Compilation) -> Self {
#[allow(clippy::unwrap_used)]
Self {
exports_info,
compilation: NonNull::new(compilation as *const Compilation as *mut Compilation).unwrap(),
}
}

fn as_ref(&self) -> napi::Result<ModuleGraph<'static>> {
let compilation = unsafe { self.compilation.as_ref() };
let module_graph = compilation.get_module_graph();
Ok(module_graph)
}

fn as_mut(&mut self) -> napi::Result<ModuleGraph<'static>> {
let compilation = unsafe { self.compilation.as_mut() };
let module_graph = compilation.get_module_graph_mut();
Ok(module_graph)
}
}

#[napi]
impl JsExportsInfo {
#[napi(ts_args_type = "runtime: string | string[] | undefined")]
pub fn is_used(&self, js_runtime: Option<JsRuntimeSpec>) -> napi::Result<bool> {
let module_graph = self.as_ref()?;
let runtime: Option<RuntimeSpec> = js_runtime.map(|js_rt| match js_rt {
Either::A(str) => vec![str].into_iter().map(Arc::from).collect(),
Either::B(vec) => vec.into_iter().map(Arc::from).collect(),
});
Ok(self.exports_info.is_used(&module_graph, runtime.as_ref()))
}

#[napi(ts_args_type = "runtime: string | string[] | undefined")]
pub fn is_module_used(&self, js_runtime: Option<JsRuntimeSpec>) -> napi::Result<bool> {
let module_graph = self.as_ref()?;
let runtime: Option<RuntimeSpec> = js_runtime.map(|js_rt| match js_rt {
Either::A(str) => vec![str].into_iter().map(Arc::from).collect(),
Either::B(vec) => vec.into_iter().map(Arc::from).collect(),
});
Ok(
self
.exports_info
.is_module_used(&module_graph, runtime.as_ref()),
)
}

#[napi(ts_args_type = "runtime: string | string[] | undefined")]
pub fn set_used_in_unknown_way(
&mut self,
js_runtime: Option<JsRuntimeSpec>,
) -> napi::Result<bool> {
let mut module_graph = self.as_mut()?;
let runtime: Option<RuntimeSpec> = js_runtime.map(|js_rt| match js_rt {
Either::A(str) => vec![str].into_iter().map(Arc::from).collect(),
Either::B(vec) => vec.into_iter().map(Arc::from).collect(),
});
Ok(
self
.exports_info
.set_used_in_unknown_way(&mut module_graph, runtime.as_ref()),
)
}
}
2 changes: 2 additions & 0 deletions crates/rspack_binding_values/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod compilation;
mod context_module_factory;
mod dependency;
mod dependency_block;
mod exports_info;
mod filename;
mod html;
mod identifier;
Expand Down Expand Up @@ -37,6 +38,7 @@ pub use compilation::*;
pub use context_module_factory::*;
pub use dependency::*;
pub use dependency_block::*;
pub use exports_info::*;
pub use filename::*;
pub use html::*;
pub use module::*;
Expand Down
9 changes: 8 additions & 1 deletion crates/rspack_binding_values/src/module_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use napi_derive::napi;
use rspack_core::{Compilation, ModuleGraph, RuntimeSpec};
use rustc_hash::FxHashSet;

use crate::{JsDependency, JsModule, JsModuleWrapper};
use crate::{JsDependency, JsExportsInfo, JsModule, JsModuleWrapper};

#[napi]
pub struct JsModuleGraph {
Expand Down Expand Up @@ -80,4 +80,11 @@ impl JsModuleGraph {
.map(|module| JsModuleWrapper::new(module.as_ref(), compilation.id(), Some(compilation))),
)
}

#[napi]
pub fn get_exports_info(&self, module: &JsModule) -> napi::Result<JsExportsInfo> {
let (compilation, module_graph) = self.as_ref()?;
let exports_info = module_graph.get_exports_info(&module.identifier);
Ok(JsExportsInfo::new(exports_info, &compilation))

Check failure on line 88 in crates/rspack_binding_values/src/module_graph.rs

View workflow job for this annotation

GitHub Actions / Rust check

this expression creates a reference which is immediately dereferenced by the compiler
}
}
20 changes: 20 additions & 0 deletions packages/rspack/etc/core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import type { JsContextModuleFactoryBeforeResolveData } from '@rspack/binding';
import type { JsCreateData } from '@rspack/binding';
import type { JsDependenciesBlock } from '@rspack/binding';
import type { JsDependency } from '@rspack/binding';
import { JsExportsInfo } from '@rspack/binding';
import type { JsFactoryMeta } from '@rspack/binding';
import { JsHtmlPluginTag } from '@rspack/binding';
import { JsLibraryOptions } from '@rspack/binding';
Expand Down Expand Up @@ -1959,6 +1960,20 @@ export interface ExperimentsNormalized {
topLevelAwait?: boolean;
}

// @public (undocumented)
class ExportsInfo {
// (undocumented)
static __from_binding(binding: JsExportsInfo): ExportsInfo;
// (undocumented)
static __to_binding(module: ExportsInfo): JsExportsInfo;
// (undocumented)
isModuleUsed(runtime: RuntimeSpec): boolean;
// (undocumented)
isUsed(runtime: RuntimeSpec): boolean;
// (undocumented)
setUsedInUnknownWay(runtime: RuntimeSpec): boolean;
}

// @public (undocumented)
type ExportsPresence = "error" | "warn" | "auto" | false;

Expand Down Expand Up @@ -3705,6 +3720,8 @@ class ModuleGraph {
// (undocumented)
static __from_binding(binding: JsModuleGraph): ModuleGraph;
// (undocumented)
getExportsInfo(module: Module): ExportsInfo;
// (undocumented)
getIssuer(module: Module): Module | null;
// (undocumented)
getModule(dependency: Dependency): Module | null;
Expand Down Expand Up @@ -9747,6 +9764,9 @@ enum RuntimeModuleStage {
// @public (undocumented)
type RuntimePlugins = string[];

// @public (undocumented)
type RuntimeSpec = string | string[] | undefined;

// @public (undocumented)
type SafeParseError<Input> = {
success: false;
Expand Down
31 changes: 31 additions & 0 deletions packages/rspack/src/ExportsInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { JsExportsInfo } from "@rspack/binding";

export type RuntimeSpec = string | string[] | undefined;

export class ExportsInfo {
#inner: JsExportsInfo;

static __from_binding(binding: JsExportsInfo) {
return new ExportsInfo(binding);
}

static __to_binding(module: ExportsInfo): JsExportsInfo {
return module.#inner;
}

private constructor(binding: JsExportsInfo) {
this.#inner = binding;
}

isUsed(runtime: RuntimeSpec): boolean {
return this.#inner.isUsed(runtime);
}

isModuleUsed(runtime: RuntimeSpec): boolean {
return this.#inner.isModuleUsed(runtime);
}

setUsedInUnknownWay(runtime: RuntimeSpec): boolean {
return this.#inner.setUsedInUnknownWay(runtime);
}
}
7 changes: 7 additions & 0 deletions packages/rspack/src/ModuleGraph.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { JsModuleGraph } from "@rspack/binding";
import { Dependency } from "./Dependency";
import { Module } from "./Module";
import { ExportsInfo } from "./ExportsInfo";

export default class ModuleGraph {
static __from_binding(binding: JsModuleGraph) {
Expand All @@ -22,4 +23,10 @@ export default class ModuleGraph {
const binding = this.#inner.getIssuer(Module.__to_binding(module));
return binding ? Module.__from_binding(binding) : null;
}

getExportsInfo(module: Module): ExportsInfo {
return ExportsInfo.__from_binding(
this.#inner.getExportsInfo(Module.__to_binding(module))
);
}
}

0 comments on commit 32cff6e

Please sign in to comment.