Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add more api #7348

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ export class JsStats {
getLogging(acceptedTypes: number): Array<JsStatsLogging>
}

export class RawExternalItemFnCtx {
data(): RawExternalItemFnCtxData
getResolver(): JsResolver
}

export class Rspack {
constructor(options: RawOptions, builtinPlugins: Array<BuiltinPlugin>, registerJsTaps: RegisterJsTaps, outputFilesystem: ThreadsafeNodeFS, resolverFactoryReference: JsResolverFactory)
setNonSkippableRegisters(kinds: Array<RegisterJsTapKind>): void
Expand All @@ -143,8 +148,14 @@ export function __chunk_graph_inner_get_chunk_modules(jsChunkUkey: number, compi

export function __chunk_graph_inner_get_chunk_modules_iterable_by_source_type(jsChunkUkey: number, sourceType: string, compilation: JsCompilation): Array<JsModule>

export function __chunk_graph_inner_get_module_id(module: string, compilation: JsCompilation): string | null

export function __chunk_group_inner_children_iterable(ukey: number, compilation: JsCompilation): Array<JsChunkGroup>

export function __chunk_group_inner_get_chunk_group(ukey: number, compilation: JsCompilation): JsChunkGroup

export function __chunk_group_inner_parents_iterable(ukey: number, compilation: JsCompilation): Array<JsChunkGroup>

export function __chunk_inner_can_be_initial(jsChunkUkey: number, compilation: JsCompilation): boolean

export function __chunk_inner_get_all_async_chunks(jsChunkUkey: number, compilation: JsCompilation): Array<JsChunk>
Expand All @@ -159,6 +170,8 @@ export function __chunk_inner_is_only_initial(jsChunkUkey: number, compilation:

export function __entrypoint_inner_get_runtime_chunk(ukey: number, compilation: JsCompilation): JsChunk

export function __module_graph_inner_is_async(jsModuleIdentifier: string, compilation: JsCompilation): boolean | null

export interface BuiltinPlugin {
name: BuiltinPluginName
options: unknown
Expand Down Expand Up @@ -242,6 +255,7 @@ export function cleanupGlobalTrace(): void

export interface ContextInfo {
issuer: string
issuerLayer?: string
}

export interface JsAdditionalTreeRuntimeRequirementsArg {
Expand Down Expand Up @@ -350,7 +364,6 @@ export interface JsChunkAssetArgs {
}

export interface JsChunkGroup {
__inner_parents: Array<number>
__inner_ukey: number
chunks: Array<JsChunk>
index?: number
Expand Down Expand Up @@ -1120,7 +1133,7 @@ export interface RawExposeOptions {
import: Array<string>
}

export interface RawExternalItemFnCtx {
export interface RawExternalItemFnCtxData {
request: string
context: string
dependencyType: string
Expand Down
44 changes: 41 additions & 3 deletions crates/rspack_binding_options/src/options/raw_external.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use std::collections::HashMap;
use std::fmt::Debug;
use std::sync::Arc;

use napi::bindgen_prelude::Either4;
use napi_derive::napi;
use rspack_core::ExternalItemFnCtx;
use rspack_binding_values::JsResolver;
use rspack_core::{ExternalItem, ExternalItemFnResult, ExternalItemValue};
use rspack_core::{ExternalItemFnCtx, ResolveOptionsWithDependencyType, ResolverFactory};
use rspack_napi::regexp::{JsRegExp, JsRegExpExt};
use rspack_napi::threadsafe_function::ThreadsafeFunction;

Expand Down Expand Up @@ -66,17 +68,50 @@ impl From<RawExternalItemFnResult> for ExternalItemFnResult {
#[napi(object)]
pub struct ContextInfo {
pub issuer: String,
pub issuer_layer: Option<String>,
}

#[derive(Debug, Clone)]
#[napi(object)]
#[derive(Debug)]
#[napi]
pub struct RawExternalItemFnCtx {
request: String,
context: String,
dependency_type: String,
context_info: ContextInfo,
resolve_options_with_dependency_type: ResolveOptionsWithDependencyType,
resolver_factory: Arc<ResolverFactory>,
}

#[derive(Debug)]
#[napi(object)]
pub struct RawExternalItemFnCtxData {
pub request: String,
pub context: String,
pub dependency_type: String,
pub context_info: ContextInfo,
}

#[napi]
impl RawExternalItemFnCtx {
#[napi]
pub fn data(&self) -> RawExternalItemFnCtxData {
RawExternalItemFnCtxData {
request: self.request.clone(),
context: self.context.clone(),
dependency_type: self.dependency_type.clone(),
context_info: self.context_info.clone(),
}
}

#[napi]
pub fn get_resolver(&self) -> JsResolver {
JsResolver::new(
self.resolver_factory.clone(),
self.resolve_options_with_dependency_type.clone(),
)
}
}

impl From<ExternalItemFnCtx> for RawExternalItemFnCtx {
fn from(value: ExternalItemFnCtx) -> Self {
Self {
Expand All @@ -85,7 +120,10 @@ impl From<ExternalItemFnCtx> for RawExternalItemFnCtx {
context: value.context,
context_info: ContextInfo {
issuer: value.context_info.issuer,
issuer_layer: value.context_info.issuer_layer,
},
resolve_options_with_dependency_type: value.resolve_options_with_dependency_type,
resolver_factory: value.resolver_factory,
}
}
}
Expand Down
11 changes: 10 additions & 1 deletion crates/rspack_binding_values/src/chunk_graph.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use napi_derive::napi;
use rspack_core::{ChunkUkey, SourceType};
use rspack_core::{ChunkUkey, ModuleIdentifier, SourceType};
use rspack_napi::napi::Result;

use crate::{JsChunk, JsCompilation, JsModule, ToJsModule};
Expand Down Expand Up @@ -71,3 +71,12 @@ pub fn get_chunk_modules_iterable_by_source_type(
.collect(),
)
}

#[napi(js_name = "__chunk_graph_inner_get_module_id")]
pub fn get_module_id(module: String, compilation: &JsCompilation) -> Option<String> {
let compilation = &compilation.0;
compilation
.chunk_graph
.get_module_id(ModuleIdentifier::from(module))
.map(|s| s.to_string())
}
25 changes: 22 additions & 3 deletions crates/rspack_binding_values/src/chunk_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

#[napi(object)]
pub struct JsChunkGroup {
#[napi(js_name = "__inner_parents")]
pub inner_parents: Vec<u32>,
#[napi(js_name = "__inner_ukey")]
pub inner_ukey: u32,
pub chunks: Vec<JsChunk>,
Expand All @@ -27,7 +25,6 @@
.map(|k| JsChunk::from(compilation.chunk_by_ukey.expect_get(k)))
.collect(),
index: cg.index,
inner_parents: cg.parents.iter().map(|ukey| ukey.as_u32()).collect(),
inner_ukey: cg.ukey.as_u32(),
name: cg.name().map(|name| name.to_string()),
is_initial: cg.is_initial(),
Expand All @@ -47,6 +44,28 @@
JsChunkGroup::from_chunk_group(cg, compilation)
}

#[napi(js_name = "__chunk_group_inner_parents_iterable")]
pub fn parents_iterable(ukey: u32, compilation: &JsCompilation) -> Vec<JsChunkGroup> {
let compilation = &compilation.0;
let cg = chunk_group(ukey, compilation);
cg.parents_iterable()
.map(|k| {
JsChunkGroup::from_chunk_group(compilation.chunk_group_by_ukey.expect_get(&k), compilation)

Check failure on line 53 in crates/rspack_binding_values/src/chunk_group.rs

View workflow job for this annotation

GitHub Actions / Rust check

this expression creates a reference which is immediately dereferenced by the compiler
})
.collect()
}

#[napi(js_name = "__chunk_group_inner_children_iterable")]
pub fn children_iterable(ukey: u32, compilation: &JsCompilation) -> Vec<JsChunkGroup> {
let compilation = &compilation.0;
let cg = chunk_group(ukey, compilation);
cg.children_iterable()
.map(|k| {
JsChunkGroup::from_chunk_group(compilation.chunk_group_by_ukey.expect_get(&k), compilation)

Check failure on line 64 in crates/rspack_binding_values/src/chunk_group.rs

View workflow job for this annotation

GitHub Actions / Rust check

this expression creates a reference which is immediately dereferenced by the compiler
})
.collect()
}

#[napi(js_name = "__entrypoint_inner_get_runtime_chunk")]
pub fn get_runtime_chunk(ukey: u32, compilation: &JsCompilation) -> JsChunk {
let compilation = &compilation.0;
Expand Down
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 context_module_factory;
mod filename;
mod identifier;
mod module;
mod module_graph;
mod normal_module_factory;
mod options;
mod path_data;
Expand All @@ -32,6 +33,7 @@ pub use compilation::*;
pub use context_module_factory::*;
pub use filename::*;
pub use module::*;
pub use module_graph::*;
pub use normal_module_factory::*;
pub use options::*;
pub use path_data::*;
Expand Down
11 changes: 11 additions & 0 deletions crates/rspack_binding_values/src/module_graph.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use napi_derive::napi;
use rspack_core::ModuleIdentifier;

use crate::JsCompilation;

#[napi(js_name = "__module_graph_inner_is_async")]
pub fn is_async(js_module_identifier: String, compilation: &JsCompilation) -> Option<bool> {
let compilation = &compilation.0;
let module_graph = compilation.get_module_graph();
module_graph.is_async(&ModuleIdentifier::from(js_module_identifier))
}
1 change: 1 addition & 0 deletions crates/rspack_binding_values/src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::raw_resolve::{
};

#[napi]
#[derive(Debug)]
pub struct JsResolver {
resolver_factory: Arc<ResolverFactory>,
resolver: Arc<Resolver>,
Expand Down
6 changes: 5 additions & 1 deletion crates/rspack_core/src/chunk_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub struct ChunkGroup {
pub parents: UkeySet<ChunkGroupUkey>,
pub(crate) module_pre_order_indices: IdentifierMap<usize>,
pub(crate) module_post_order_indices: IdentifierMap<usize>,
pub(crate) children: UkeySet<ChunkGroupUkey>,
pub children: UkeySet<ChunkGroupUkey>,
async_entrypoints: UkeySet<ChunkGroupUkey>,
// ChunkGroupInfo
pub(crate) next_pre_order_index: usize,
Expand Down Expand Up @@ -85,6 +85,10 @@ impl ChunkGroup {
self.parents.iter()
}

pub fn children_iterable(&self) -> impl Iterator<Item = &ChunkGroupUkey> {
self.children.iter()
}

pub fn module_post_order_index(&self, module_identifier: &ModuleIdentifier) -> Option<usize> {
// A module could split into another ChunkGroup, which doesn't have the module_post_order_indices of the module
self
Expand Down
6 changes: 4 additions & 2 deletions crates/rspack_core/src/compiler/make/repair/factorize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
utils::task_loop::{Task, TaskResult, TaskType},
BoxDependency, CompilerOptions, Context, DependencyId, ExportInfoData, ExportsInfoData,
ModuleFactory, ModuleFactoryCreateData, ModuleFactoryResult, ModuleIdentifier, ModuleLayer,
ModuleProfile, Resolve,
ModuleProfile, Resolve, ResolverFactory,
};

#[derive(Debug)]
Expand All @@ -24,9 +24,10 @@ pub struct FactorizeTask {
pub issuer_layer: Option<ModuleLayer>,
pub dependency: BoxDependency,
pub dependencies: Vec<DependencyId>,
pub resolve_options: Option<Box<Resolve>>,
pub resolve_options: Option<Arc<Resolve>>,
pub options: Arc<CompilerOptions>,
pub current_profile: Option<Box<ModuleProfile>>,
pub resolver_factory: Arc<ResolverFactory>,
}

#[async_trait::async_trait]
Expand Down Expand Up @@ -88,6 +89,7 @@ impl Task<MakeTaskContext> for FactorizeTask {
issuer: self.issuer,
issuer_identifier: self.original_module_identifier,
issuer_layer,
resolver_factory: self.resolver_factory,

file_dependencies: Default::default(),
missing_dependencies: Default::default(),
Expand Down
1 change: 1 addition & 0 deletions crates/rspack_core/src/compiler/make/repair/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ pub fn repair(
resolve_options: parent_module.and_then(|module| module.get_resolve_options()),
options: compilation.options.clone(),
current_profile,
resolver_factory: compilation.resolver_factory.clone(),
}))
})
.collect::<Vec<_>>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ impl Task<MakeTaskContext> for ProcessDependenciesTask {
resolve_options: module.get_resolve_options(),
options: context.compiler_options.clone(),
current_profile,
resolver_factory: context.resolver_factory.clone(),
}));
}
Ok(res)
Expand Down
1 change: 1 addition & 0 deletions crates/rspack_core/src/compiler/module_executor/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ impl Task<MakeTaskContext> for EntryTask {
.compiler_options
.profile
.then(Box::<ModuleProfile>::default),
resolver_factory: context.resolver_factory.clone(),
})])
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/rspack_core/src/concatenated_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub struct RootModuleContext {
pub readable_identifier: String,
pub name_for_condition: Option<Box<str>>,
pub lib_indent: Option<String>,
pub resolve_options: Option<Box<Resolve>>,
pub resolve_options: Option<Arc<Resolve>>,
pub code_generation_dependencies: Option<Vec<Box<dyn ModuleDependency>>>,
pub presentational_dependencies: Option<Vec<Box<dyn DependencyTemplate>>>,
pub context: Option<Context>,
Expand Down Expand Up @@ -1296,7 +1296,7 @@ impl Module for ConcatenatedModule {
self.root_module_ctxt.lib_indent.clone().map(Cow::Owned)
}

fn get_resolve_options(&self) -> Option<Box<Resolve>> {
fn get_resolve_options(&self) -> Option<Arc<Resolve>> {
self.root_module_ctxt.resolve_options.clone()
}

Expand Down
8 changes: 6 additions & 2 deletions crates/rspack_core/src/context_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ pub struct ContextModuleOptions {
pub resource_fragment: String,
pub context_options: ContextOptions,
pub layer: Option<ModuleLayer>,
pub resolve_options: Option<Box<Resolve>>,
pub resolve_options: Option<Arc<Resolve>>,
pub type_prefix: ContextTypePrefix,
}

Expand Down Expand Up @@ -1101,7 +1101,11 @@ impl ContextModule {
tracing::trace!("resolving context module path {}", self.options.resource);

let resolver = &self.resolve_factory.get(ResolveOptionsWithDependencyType {
resolve_options: self.options.resolve_options.clone(),
resolve_options: self
.options
.resolve_options
.clone()
.map(|r| Box::new(Arc::unwrap_or_clone(r))),
resolve_to_context: false,
dependency_category: self.options.context_options.category,
});
Expand Down
3 changes: 2 additions & 1 deletion crates/rspack_core/src/module.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::fmt::Display;
use std::hash::Hash;
use std::path::PathBuf;
use std::sync::Arc;
use std::{any::Any, borrow::Cow, fmt::Debug};

use async_trait::async_trait;
Expand Down Expand Up @@ -319,7 +320,7 @@ pub trait Module:
/// Resolve options matched by module rules.
/// e.g `javascript/esm` may have special resolving options like `fullySpecified`.
/// `css` and `css/module` may have special resolving options like `preferRelative`.
fn get_resolve_options(&self) -> Option<Box<Resolve>> {
fn get_resolve_options(&self) -> Option<Arc<Resolve>> {
None
}

Expand Down
4 changes: 3 additions & 1 deletion crates/rspack_core/src/module_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ use rustc_hash::FxHashSet as HashSet;

use crate::{
BoxDependency, BoxModule, CompilerOptions, Context, ModuleIdentifier, ModuleLayer, Resolve,
ResolverFactory,
};

#[derive(Debug, Clone)]
pub struct ModuleFactoryCreateData {
pub resolve_options: Option<Box<Resolve>>,
pub resolve_options: Option<Arc<Resolve>>,
pub options: Arc<CompilerOptions>,
pub context: Context,
pub dependency: BoxDependency,
pub issuer: Option<Box<str>>,
pub issuer_identifier: Option<ModuleIdentifier>,
pub issuer_layer: Option<ModuleLayer>,
pub resolver_factory: Arc<ResolverFactory>,

pub file_dependencies: HashSet<PathBuf>,
pub context_dependencies: HashSet<PathBuf>,
Expand Down
Loading
Loading