Skip to content

Commit

Permalink
feat: JsDependency and JsDependencyMut
Browse files Browse the repository at this point in the history
  • Loading branch information
SyMind committed Sep 23, 2024
1 parent 261c8fc commit 771b014
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 93 deletions.
26 changes: 13 additions & 13 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class ExternalObject<T> {
}
}
export class DependenciesBlockDto {
get dependencies(): Array<JsCompiledDependency>
get dependencies(): Array<JsDependency>
get blocks(): Array<DependenciesBlockDto>
}
export type DependenciesBlockDTO = DependenciesBlockDto
Expand All @@ -42,8 +42,8 @@ export class DependenciesDto {
export type DependenciesDTO = DependenciesDto

export class EntryDataDto {
get dependencies(): Array<JsCompiledDependency>
get includeDependencies(): Array<JsCompiledDependency>
get dependencies(): Array<JsDependency>
get includeDependencies(): Array<JsDependency>
get options(): EntryOptionsDto
}
export type EntryDataDTO = EntryDataDto
Expand Down Expand Up @@ -113,13 +113,6 @@ export class JsCompilation {
addRuntimeModule(chunkUkey: number, runtimeModule: JsAddingRuntimeModule): void
}

export class JsCompiledDependency {
get type(): string
get category(): string
get request(): string | undefined
get critical(): boolean
}

export class JsContextModuleFactoryAfterResolveData {
get resource(): string
set resource(resource: string)
Expand All @@ -131,7 +124,7 @@ export class JsContextModuleFactoryAfterResolveData {
set regExp(rawRegExp: RawRegex | undefined)
get recursive(): boolean
set recursive(recursive: boolean)
get dependencies(): Array<JsDependency>
get dependencies(): Array<JsDependencyMut>
}

export class JsContextModuleFactoryBeforeResolveData {
Expand All @@ -150,6 +143,13 @@ export class JsDependency {
get category(): string
get request(): string | undefined
get critical(): boolean
}

export class JsDependencyMut {
get type(): string
get category(): string
get request(): string | undefined
get critical(): boolean
set critical(val: boolean)
}

Expand Down Expand Up @@ -534,8 +534,8 @@ export interface JsDiagnosticLocation {
}

export interface JsEntryData {
dependencies: Array<JsCompiledDependency>
includeDependencies: Array<JsCompiledDependency>
dependencies: Array<JsDependency>
includeDependencies: Array<JsDependency>
options: JsEntryOptions
}

Expand Down
45 changes: 21 additions & 24 deletions crates/rspack_binding_values/src/compilation/entries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use napi_derive::napi;
use rspack_core::{ChunkLoading, Compilation, EntryData, EntryOptions, EntryRuntime};
use rspack_napi::napi::bindgen_prelude::*;

use crate::{dependency::JsCompiledDependency, entry::JsEntryOptions, library::JsLibraryOptions};
use crate::{dependency::JsDependency, entry::JsEntryOptions, library::JsLibraryOptions};

#[napi]
pub struct EntryOptionsDTO(EntryOptions);
Expand Down Expand Up @@ -152,8 +152,8 @@ impl EntryOptionsDTO {

#[napi(object, object_to_js = false)]
pub struct JsEntryData {
pub dependencies: Vec<ClassInstance<JsCompiledDependency>>,
pub include_dependencies: Vec<ClassInstance<JsCompiledDependency>>,
pub dependencies: Vec<ClassInstance<JsDependency>>,
pub include_dependencies: Vec<ClassInstance<JsDependency>>,
pub options: JsEntryOptions,
}

Expand All @@ -163,12 +163,12 @@ impl From<JsEntryData> for EntryData {
dependencies: value
.dependencies
.into_iter()
.map(|dep| dep.dependency_id)
.map(|dep| *dep.id())
.collect::<Vec<_>>(),
include_dependencies: value
.include_dependencies
.into_iter()
.map(|dep| dep.dependency_id)
.map(|dep| *dep.id())
.collect::<Vec<_>>(),
options: value.options.into(),
}
Expand All @@ -184,36 +184,33 @@ pub struct EntryDataDTO {
#[napi]
impl EntryDataDTO {
#[napi(getter)]
pub fn dependencies(&'static self, env: Env) -> Result<Vec<ClassInstance<JsCompiledDependency>>> {
pub fn dependencies(&'static self) -> Vec<JsDependency> {
let module_graph = self.compilation.get_module_graph();
self
.entry_data
.dependencies
.clone()
.into_iter()
.map(|id| {
let js_dep = JsCompiledDependency::new(id, self.compilation);
let instance = js_dep.into_instance(env)?;
Ok(instance)
.iter()
.map(|dependency_id| {
#[allow(clippy::unwrap_used)]
let dep = module_graph.dependency_by_id(dependency_id).unwrap();
JsDependency::new(dep)
})
.collect::<Result<Vec<ClassInstance<JsCompiledDependency>>>>()
.collect::<Vec<_>>()
}

#[napi(getter)]
pub fn include_dependencies(
&'static self,
env: Env,
) -> Result<Vec<ClassInstance<JsCompiledDependency>>> {
pub fn include_dependencies(&'static self) -> Vec<JsDependency> {
let module_graph = self.compilation.get_module_graph();
self
.entry_data
.include_dependencies
.clone()
.into_iter()
.map(|id| {
let js_dep = JsCompiledDependency::new(id, self.compilation);
let instance = js_dep.into_instance(env)?;
Ok(instance)
.iter()
.map(|dependency_id| {
#[allow(clippy::unwrap_used)]
let dep = module_graph.dependency_by_id(dependency_id).unwrap();
JsDependency::new(dep)
})
.collect::<Result<Vec<ClassInstance<JsCompiledDependency>>>>()
.collect::<Vec<_>>()
}

#[napi(getter)]
Expand Down
6 changes: 3 additions & 3 deletions crates/rspack_binding_values/src/context_module_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use napi::bindgen_prelude::{
use napi_derive::napi;
use rspack_core::{AfterResolveData, BeforeResolveData};

use crate::{JsDependency, RawRegex};
use crate::{JsDependencyMut, RawRegex};

#[napi]
pub struct JsContextModuleFactoryBeforeResolveData(Box<BeforeResolveData>);
Expand Down Expand Up @@ -175,12 +175,12 @@ impl JsContextModuleFactoryAfterResolveData {
}

#[napi(getter)]
pub fn dependencies(&mut self) -> Vec<JsDependency> {
pub fn dependencies(&mut self) -> Vec<JsDependencyMut> {
self
.0
.dependencies
.iter_mut()
.map(JsDependency::new)
.map(JsDependencyMut::new)
.collect::<Vec<_>>()
}
}
Expand Down
60 changes: 20 additions & 40 deletions crates/rspack_binding_values/src/dependency.rs
Original file line number Diff line number Diff line change
@@ -1,66 +1,46 @@
use napi_derive::napi;
use rspack_core::{
BoxDependency, Compilation, ContextDependency, Dependency, DependencyId, ModuleDependency,
ModuleGraph,
};
use rspack_core::BoxDependency;

// JsCompiledDependency allows JS-side access to a Dependency instance that has already
// JsDependency allows JS-side access to a Dependency instance that has already
// been processed and stored in the Compilation.
#[napi]
pub struct JsCompiledDependency {
pub(crate) dependency_id: DependencyId,
pub(crate) module_graph: ModuleGraph<'static>,
}

impl JsCompiledDependency {
pub(crate) fn new(dependency_id: DependencyId, compilation: &'static Compilation) -> Self {
let module_graph = compilation.get_module_graph();
Self {
dependency_id,
module_graph,
}
}

fn dependency(&self) -> &dyn Dependency {
self
.module_graph
.dependency_by_id(&self.dependency_id)
.unwrap_or_else(|| panic!("Failed to get dependency by id = {:?}", &self.dependency_id))
.as_ref()
}
pub struct JsDependency(&'static BoxDependency);

fn module_dependency(&self) -> Option<&dyn ModuleDependency> {
self.dependency().as_module_dependency()
}

fn context_dependency(&self) -> Option<&dyn ContextDependency> {
self.dependency().as_context_dependency()
impl JsDependency {
pub(crate) fn new(dependency: &BoxDependency) -> Self {
// SAFETY:
// The lifetime of the &mut BoxDependency reference is extended to 'static.
// This is safe because the JS side will guarantee that the JsDependency instance's
// lifetime is properly managed and restricted.
let dependency =
unsafe { std::mem::transmute::<&BoxDependency, &'static BoxDependency>(dependency) };
Self(dependency)
}
}

#[napi]
impl JsCompiledDependency {
impl JsDependency {
#[napi(getter)]
pub fn get_type(&self) -> &str {
self.dependency().dependency_type().as_str()
self.0.dependency_type().as_str()
}

#[napi(getter)]
pub fn category(&self) -> &str {
self.dependency().category().as_str()
self.0.category().as_str()
}

#[napi(getter)]
pub fn request(&self) -> napi::Either<&str, ()> {
match self.module_dependency() {
match self.0.as_module_dependency() {
Some(dep) => napi::Either::A(dep.request()),
None => napi::Either::B(()),
}
}

#[napi(getter)]
pub fn critical(&self) -> bool {
match self.context_dependency() {
match self.0.as_context_dependency() {
Some(dep) => dep.critical().is_some(),
None => false,
}
Expand All @@ -70,9 +50,9 @@ impl JsCompiledDependency {
// JsDependency represents a Dependency instance that is currently being processed.
// It is in the make stage and has not yet been added to the Compilation.
#[napi]
pub struct JsDependency(&'static mut BoxDependency);
pub struct JsDependencyMut(&'static mut BoxDependency);

impl JsDependency {
impl JsDependencyMut {
pub(crate) fn new(dependency: &mut BoxDependency) -> Self {
// SAFETY:
// The lifetime of the &mut BoxDependency reference is extended to 'static.
Expand All @@ -85,7 +65,7 @@ impl JsDependency {
}

#[napi]
impl JsDependency {
impl JsDependencyMut {
#[napi(getter)]
pub fn get_type(&self) -> &str {
self.0.dependency_type().as_str()
Expand Down
11 changes: 7 additions & 4 deletions crates/rspack_binding_values/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use rustc_hash::FxHashMap as HashMap;
use sys::napi_env;

use super::{JsCompatSource, ToJsCompatSource};
use crate::{JsChunk, JsCodegenerationResults, JsCompiledDependency};
use crate::{JsChunk, JsCodegenerationResults, JsDependency};

#[derive(Default)]
#[napi(object)]
Expand Down Expand Up @@ -52,14 +52,17 @@ impl DependenciesBlockDTO {
#[napi]
impl DependenciesBlockDTO {
#[napi(getter)]
pub fn dependencies(&self) -> Vec<JsCompiledDependency> {
pub fn dependencies(&self) -> Vec<JsDependency> {
let module_graph = self.compilation.get_module_graph();
let block = self.block(&module_graph);
block
.get_dependencies()
.iter()
.cloned()
.map(|dep_id| JsCompiledDependency::new(dep_id, self.compilation))
.map(|dependency_id| {
#[allow(clippy::unwrap_used)]
let dep = module_graph.dependency_by_id(dependency_id).unwrap();
JsDependency::new(dep)
})
.collect::<Vec<_>>()
}

Expand Down
4 changes: 2 additions & 2 deletions packages/rspack/etc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ import { JsChunkGroup } from '@rspack/binding';
import { JsChunkGroupOrigin } from '@rspack/binding';
import type { JsCodegenerationResult } from '@rspack/binding';
import { JsCompilation } from '@rspack/binding';
import { JsCompiledDependency } from '@rspack/binding';
import type { JsContextModuleFactoryAfterResolveData } from '@rspack/binding';
import type { JsContextModuleFactoryBeforeResolveData } from '@rspack/binding';
import type { JsCreateData } from '@rspack/binding';
import { JsDependency } from '@rspack/binding';
import { JsDependencyMut } from '@rspack/binding';
import type { JsFactoryMeta } from '@rspack/binding';
import { JsHtmlPluginTag } from '@rspack/binding';
import { JsLibraryOptions } from '@rspack/binding';
Expand Down Expand Up @@ -1891,7 +1891,7 @@ class Dependency {
// (undocumented)
static __drop(dependency: Dependency): void;
// (undocumented)
static __from_binding(binding: JsDependency | JsCompiledDependency): Dependency;
static __from_binding(binding: JsDependencyMut | JsDependency): Dependency;
// (undocumented)
get category(): string;
// (undocumented)
Expand Down
12 changes: 5 additions & 7 deletions packages/rspack/src/Dependency.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { type JsCompiledDependency, JsDependency } from "@rspack/binding";
import { type JsDependency, JsDependencyMut } from "@rspack/binding";

export class Dependency {
#binding: JsDependency | JsCompiledDependency;
#binding: JsDependencyMut | JsDependency;
#dropped = false;

static __from_binding(
binding: JsDependency | JsCompiledDependency
): Dependency {
static __from_binding(binding: JsDependencyMut | JsDependency): Dependency {
return new Dependency(binding);
}

Expand All @@ -22,7 +20,7 @@ export class Dependency {
}
}

private constructor(binding: JsDependency | JsCompiledDependency) {
private constructor(binding: JsDependencyMut | JsDependency) {
this.#binding = binding;
}

Expand Down Expand Up @@ -50,7 +48,7 @@ export class Dependency {
this.ensureValidLifecycle();
if (
typeof critital === "boolean" &&
this.#binding instanceof JsDependency
this.#binding instanceof JsDependencyMut
) {
this.#binding.critical = critital;
}
Expand Down

0 comments on commit 771b014

Please sign in to comment.