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 Dec 20, 2023
1 parent 7f8321d commit 84806a3
Show file tree
Hide file tree
Showing 38 changed files with 1,397 additions and 319 deletions.
11 changes: 11 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 @@ -277,6 +278,15 @@ export interface JsExecuteModuleArg {
codegenResults: JsCodegenerationResults
}

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

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

export interface JsModule {
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
61 changes: 61 additions & 0 deletions crates/rspack_binding_values/src/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,67 @@ 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(),
exports: res.exports,
})
.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 exports: String,
}

impl JsCompilation {
Expand Down
Loading

0 comments on commit 84806a3

Please sign in to comment.