Skip to content

Commit

Permalink
feat(compilation): add pluginImport and executeModule
Browse files Browse the repository at this point in the history
  • Loading branch information
JSerFeng committed Jan 22, 2024
1 parent 84a6c7c commit 1617ae3
Show file tree
Hide file tree
Showing 40 changed files with 1,293 additions and 174 deletions.
17 changes: 17 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,10 +308,20 @@ export interface JsCompatSource {

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

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

export interface JsHooks {
processAssetsStageAdditional: (...args: any[]) => any
processAssetsStagePreProcess: (...args: any[]) => any
Expand Down Expand Up @@ -392,6 +408,7 @@ export interface JsLoaderContext {
* @internal
*/
diagnosticsExternal: ExternalObject<'Diagnostic[]'>
_moduleIdentifier: string
}

export interface JsModule {
Expand Down
20 changes: 14 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,18 +833,25 @@ 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>> {
) -> 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())
Expand Down Expand Up @@ -1031,7 +1039,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
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,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 +319,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
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
66 changes: 66 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,76 @@ 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(),
})
.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>,
}

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>,
}
4 changes: 3 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,6 +86,8 @@ 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,
}
Expand Down
Loading

0 comments on commit 1617ae3

Please sign in to comment.