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(compilation): add ImportModule and executeModule #4718

Merged
Merged
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
19 changes: 19 additions & 0 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export class JsCompilation {
addMissingDependencies(deps: Array<string>): void
addBuildDependencies(deps: Array<string>): void
rebuildModule(moduleIdentifiers: Array<string>, f: (...args: any[]) => any): void
importModule(request: string, publicPath: string | undefined | null, baseUri: string | undefined | null, originalModule: string | undefined | null, originalModuleContext: string | undefined | null, callback: (...args: any[]) => any): void
}

export class JsStats {
Expand Down Expand Up @@ -253,6 +254,11 @@ export interface JsAssetInfoRelated {
sourceMap?: string
}

export interface JsBuildTimeExecutionOption {
publicPath?: string
baseUri?: string
}

export interface JsChunk {
__inner_ukey: number
__inner_groups: Array<number>
Expand Down Expand Up @@ -302,8 +308,20 @@ export interface JsCompatSource {

export interface JsExecuteModuleArg {
entry: string
request: string
options: JsBuildTimeExecutionOption
runtimeModules: Array<string>
codegenResults: JsCodegenerationResults
id: number
}

export interface JsExecuteModuleResult {
fileDependencies: Array<string>
contextDependencies: Array<string>
buildDependencies: Array<string>
missingDependencies: Array<string>
assets: Array<string>
id: number
}

export interface JsHooks {
Expand Down Expand Up @@ -392,6 +410,7 @@ export interface JsLoaderContext {
* @internal
*/
diagnosticsExternal: ExternalObject<'Diagnostic[]'>
_moduleIdentifier: string
}

export interface JsModule {
Expand Down
22 changes: 16 additions & 6 deletions crates/node_binding/src/plugins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ use rspack_binding_values::{
ToJsCompatSource,
};
use rspack_binding_values::{BeforeResolveData, JsAssetEmittedArgs, ToJsModule};
use rspack_binding_values::{CreateModuleData, JsExecuteModuleArg};
use rspack_binding_values::{CreateModuleData, JsBuildTimeExecutionOption, JsExecuteModuleArg};
use rspack_binding_values::{JsResolveForSchemeInput, JsResolveForSchemeResult};
use rspack_core::{
Chunk, ChunkAssetArgs, Compilation, ModuleIdentifier, NormalModuleAfterResolveArgs, RuntimeModule,
BuildTimeExecutionOption, Chunk, ChunkAssetArgs, Compilation, ModuleIdentifier,
NormalModuleAfterResolveArgs, RuntimeModule,
};
use rspack_core::{NormalModuleBeforeResolveArgs, PluginNormalModuleFactoryAfterResolveOutput};
use rspack_core::{
Expand Down Expand Up @@ -71,7 +72,7 @@ pub struct JsHooksAdapter {
ThreadsafeFunction<JsResolveForSchemeInput, JsResolveForSchemeResult>,
pub succeed_module_tsfn: ThreadsafeFunction<JsModule, ()>,
pub still_valid_module_tsfn: ThreadsafeFunction<JsModule, ()>,
pub execute_module_tsfn: ThreadsafeFunction<JsExecuteModuleArg, Option<String>>,
pub execute_module_tsfn: ThreadsafeFunction<JsExecuteModuleArg, ()>,
pub runtime_module_tsfn: ThreadsafeFunction<JsRuntimeModuleArg, Option<JsRuntimeModule>>,
}

Expand Down Expand Up @@ -832,23 +833,32 @@ impl rspack_core::Plugin for JsHooksAdapter {
fn execute_module(
&self,
entry: ModuleIdentifier,
request: &str,
options: &BuildTimeExecutionOption,
runtime_modules: Vec<ModuleIdentifier>,
codegen_results: &rspack_core::CodeGenerationResults,
) -> rspack_error::Result<Option<String>> {
id: u32,
) -> rspack_error::Result<()> {
if self.is_hook_disabled(&Hook::ExecuteModule) {
return Ok(None);
return Ok(());
}

self
.execute_module_tsfn
.call(
JsExecuteModuleArg {
entry: entry.to_string(),
request: request.into(),
options: JsBuildTimeExecutionOption {
public_path: options.public_path.clone(),
base_uri: options.base_uri.clone(),
},
runtime_modules: runtime_modules
.into_iter()
.map(|id| id.to_string())
.collect(),
codegen_results: codegen_results.clone().into(),
id,
},
ThreadsafeFunctionCallMode::NonBlocking,
)
Expand Down Expand Up @@ -1031,7 +1041,7 @@ impl JsHooksAdapter {
js_fn_into_threadsafe_fn!(succeed_module, env);
let still_valid_module_tsfn: ThreadsafeFunction<JsModule, ()> =
js_fn_into_threadsafe_fn!(still_valid_module, env);
let execute_module_tsfn: ThreadsafeFunction<JsExecuteModuleArg, Option<String>> =
let execute_module_tsfn: ThreadsafeFunction<JsExecuteModuleArg, ()> =
js_fn_into_threadsafe_fn!(execute_module, env);
let runtime_module_tsfn: ThreadsafeFunction<JsRuntimeModuleArg, Option<JsRuntimeModule>> =
js_fn_into_threadsafe_fn!(runtime_module, env);
Expand Down
13 changes: 13 additions & 0 deletions crates/rspack_binding_options/src/options/raw_module/js_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ fn sync_loader_context(
} else {
loader_context.additional_data.remove::<String>();
}
loader_context.asset_filenames = loader_result.asset_filenames.into_iter().collect();

Ok(())
}
Expand Down Expand Up @@ -256,6 +257,9 @@ pub struct JsLoaderContext {
/// @internal
#[napi(ts_type = "ExternalObject<'Diagnostic[]'>")]
pub diagnostics_external: External<Vec<Diagnostic>>,

#[napi(js_name = "_moduleIdentifier")]
pub module_identifier: String,
}

impl TryFrom<&mut rspack_core::LoaderContext<'_, rspack_core::LoaderRunnerContext>>
Expand Down Expand Up @@ -316,6 +320,7 @@ impl TryFrom<&mut rspack_core::LoaderContext<'_, rspack_core::LoaderRunnerContex
additional_data_external: External::new(cx.additional_data.clone()),
context_external: External::new(cx.context.clone()),
diagnostics_external: External::new(cx.__diagnostics.drain(..).collect()),
module_identifier: cx.context.module.to_string(),
})
}
}
Expand Down Expand Up @@ -414,6 +419,7 @@ pub struct JsLoaderResult {
pub context_dependencies: Vec<String>,
pub missing_dependencies: Vec<String>,
pub build_dependencies: Vec<String>,
pub asset_filenames: Vec<String>,
pub source_map: Option<Buffer>,
pub additional_data: Option<Buffer>,
pub additional_data_external: External<AdditionalData>,
Expand All @@ -439,6 +445,12 @@ impl napi::bindgen_prelude::FromNapiValue for JsLoaderResult {
) -> napi::bindgen_prelude::Result<Self> {
let obj = napi::bindgen_prelude::Object::from_napi_value(env, napi_val)?;
let content_: Option<Buffer> = obj.get("content")?;
let asset_filenames_: Vec<String> = obj.get("assetFilenames")?.ok_or_else(|| {
napi::bindgen_prelude::Error::new(
napi::bindgen_prelude::Status::InvalidArg,
format!("Missing field `{}`", "assetFilenames"),
)
})?;
let file_dependencies_: Vec<String> = obj.get("fileDependencies")?.ok_or_else(|| {
napi::bindgen_prelude::Error::new(
napi::bindgen_prelude::Status::InvalidArg,
Expand Down Expand Up @@ -493,6 +505,7 @@ impl napi::bindgen_prelude::FromNapiValue for JsLoaderResult {
context_dependencies: context_dependencies_,
missing_dependencies: missing_dependencies_,
build_dependencies: build_dependencies_,
asset_filenames: asset_filenames_,
source_map: source_map_,
additional_data: additional_data_,
additional_data_external: additional_data_external_,
Expand Down
40 changes: 22 additions & 18 deletions crates/rspack_binding_values/src/codegen_result.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::HashMap;

use napi_derive::napi;
use rspack_core::{CodeGenerationResult, CodeGenerationResults};
use rspack_core::{get_runtime_key, CodeGenerationResult, CodeGenerationResults};

#[napi(object)]
#[derive(Debug)]
Expand Down Expand Up @@ -36,23 +36,27 @@ impl From<CodeGenerationResults> for JsCodegenerationResults {
.map
.into_iter()
.map(|(module_id, runtime_result_map)| {
(
module_id.to_string(),
runtime_result_map
.map
.into_iter()
.map(|(k, result_id)| {
(
k,
id_result_map
.get(&result_id)
.expect("should exist codegenResult")
.clone()
.into(),
)
})
.collect(),
)
let mut runtime_map: HashMap<String, JsCodegenerationResult> = Default::default();
match &runtime_result_map.mode {
rspack_core::RuntimeMode::Empty => {}
rspack_core::RuntimeMode::SingleEntry => {
runtime_map.insert(
get_runtime_key(runtime_result_map.single_runtime.expect("exist")),
id_result_map
.get(&runtime_result_map.single_value.expect("TODO"))
.expect("TODO")
.clone()
.into(),
);
}
rspack_core::RuntimeMode::Map => {
runtime_result_map.map.into_iter().for_each(|(k, v)| {
runtime_map.insert(k, id_result_map.get(&v).expect("TODO").clone().into());
});
}
};

(module_id.to_string(), runtime_map)
})
.collect(),
}
Expand Down
68 changes: 68 additions & 0 deletions crates/rspack_binding_values/src/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,10 +429,78 @@ impl JsCompilation {
)
})
}

#[allow(clippy::too_many_arguments)]
#[napi]
pub fn import_module(
&'static self,
env: Env,
request: String,
public_path: Option<String>,
base_uri: Option<String>,
original_module: Option<String>,
original_module_context: Option<String>,
callback: JsFunction,
) -> Result<()> {
callbackify(env, callback, async {
self
.inner
.import_module(
request,
public_path,
base_uri,
original_module.map(|s| s.into()),
original_module_context.map(|ctx| Box::new(rspack_core::Context::new(ctx))),
)
.await
.map(|res| JsExecuteModuleResult {
file_dependencies: res
.file_dependencies
.into_iter()
.map(|d| d.to_string_lossy().to_string())
.collect(),
context_dependencies: res
.context_dependencies
.into_iter()
.map(|d| d.to_string_lossy().to_string())
.collect(),
build_dependencies: res
.build_dependencies
.into_iter()
.map(|d| d.to_string_lossy().to_string())
.collect(),
missing_dependencies: res
.missing_dependencies
.into_iter()
.map(|d| d.to_string_lossy().to_string())
.collect(),
assets: res.assets.into_iter().collect(),
id: res.id,
})
.map_err(|e| Error::new(napi::Status::GenericFailure, format!("{e}")))
})
}
}

#[napi(object)]
pub struct JsExecuteModuleResult {
pub file_dependencies: Vec<String>,
pub context_dependencies: Vec<String>,
pub build_dependencies: Vec<String>,
pub missing_dependencies: Vec<String>,
pub assets: Vec<String>,
pub id: u32,
}

impl JsCompilation {
pub fn from_compilation(inner: &'static mut rspack_core::Compilation) -> Self {
Self { inner }
}
}

#[napi(object)]
#[derive(Clone, Debug)]
pub struct JsBuildTimeExecutionOption {
pub public_path: Option<String>,
pub base_uri: Option<String>,
}
5 changes: 4 additions & 1 deletion crates/rspack_binding_values/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use napi_derive::napi;
use rspack_core::Module;

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

#[derive(Default)]
#[napi(object)]
Expand Down Expand Up @@ -86,8 +86,11 @@ impl ToJsModule for dyn Module + '_ {
#[napi(object)]
pub struct JsExecuteModuleArg {
pub entry: String,
pub request: String,
pub options: JsBuildTimeExecutionOption,
pub runtime_modules: Vec<String>,
pub codegen_results: JsCodegenerationResults,
pub id: u32,
}

#[derive(Default)]
Expand Down
Loading
Loading