Skip to content
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
66 changes: 44 additions & 22 deletions crates/node_binding/src/compilation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use rspack_napi::napi::bindgen_prelude::*;
use rspack_napi::NapiResultExt;
use rspack_napi::OneShotRef;
use rspack_plugin_runtime::RuntimeModuleFromJs;
use rustc_hash::FxHashMap;

use super::{JsFilename, PathWithInfo};
use crate::entry::JsEntryOptions;
Expand All @@ -38,6 +39,7 @@ use crate::JsStatsOptimizationBailout;
use crate::LocalJsFilename;
use crate::RawDependency;
use crate::ToJsCompatSource;
use crate::COMPILER_REFERENCES;
use crate::{AssetInfo, JsAsset, JsPathData, JsStats};
use crate::{JsRspackDiagnostic, JsRspackError};

Expand Down Expand Up @@ -700,23 +702,52 @@ impl JsCompilation {
) -> napi::Result<()> {
let compilation = self.as_mut()?;

let Some(compiler_reference) = COMPILER_REFERENCES.with(|ref_cell| {
let references = ref_cell.borrow();
references.get(&compilation.compiler_id()).cloned()
}) else {
return Err(napi::Error::from_reason(
"Unable to addInclude now. The Compiler has been garbage collected by JavaScript.",
));
};
let Some(js_compiler) = compiler_reference.get() else {
return Err(napi::Error::from_reason(
"Unable to addInclude now. The Compiler has been garbage collected by JavaScript.",
));
};
let include_dependencies_map = &js_compiler.include_dependencies_map;

let args = js_args
.into_iter()
.map(|(js_context, js_dependency, js_options)| {
let layer = match &js_options {
Some(options) => options.layer.clone(),
None => None,
};
let dependency = Box::new(EntryDependency::new(
js_dependency.request,
js_context.into(),
layer,
false,
)) as BoxDependency;
let options = match js_options {
Some(js_opts) => js_opts.into(),
None => EntryOptions::default(),
};
let dependency = if let Some(map) = include_dependencies_map.get(&js_dependency.request)
&& let Some(dependency) = map.get(&options)
{
dependency.clone()
} else {
let dependency: BoxDependency = Box::new(EntryDependency::new(
js_dependency.request.clone(),
js_context.into(),
layer,
false,
));
if let Some(mut map) = include_dependencies_map.get_mut(&js_dependency.request) {
map.insert(options.clone(), dependency.clone());
} else {
let mut map = FxHashMap::default();
map.insert(options.clone(), dependency.clone());
include_dependencies_map.insert(js_dependency.request, map);
}
dependency
};
(dependency, options)
})
.collect::<Vec<(BoxDependency, EntryOptions)>>();
Expand All @@ -736,24 +767,15 @@ impl JsCompilation {
.into_iter()
.map(|dependency_id| {
let module_graph = compilation.get_module_graph();
match module_graph.module_graph_module_by_dependency_id(&dependency_id) {
Some(module) => match module_graph.module_by_identifier(&module.module_identifier) {
Some(module) => {
let js_module =
JsModuleWrapper::new(module.identifier(), None, compilation.compiler_id());
(Either::B(()), Either::B(js_module))
}
None => (
Either::A(format!(
"Module created by {:#?} cannot be found",
dependency_id
)),
Either::A(()),
),
},
match module_graph.get_module_by_dependency_id(&dependency_id) {
Some(module) => {
let js_module =
JsModuleWrapper::new(module.identifier(), None, compilation.compiler_id());
(Either::B(()), Either::B(js_module))
}
None => (
Either::A(format!(
"Module created by {:#?} cannot be found",
"Module created by {:?} cannot be found",
dependency_id
)),
Either::A(()),
Expand Down
8 changes: 7 additions & 1 deletion crates/node_binding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ use std::sync::{Arc, Mutex};
use compiler::{Compiler, CompilerState, CompilerStateGuard};
use napi::{bindgen_prelude::*, CallContext};
use rspack_collections::UkeyMap;
use rspack_core::{Compilation, CompilerId, ModuleIdentifier, PluginExt};
use rspack_core::{
BoxDependency, Compilation, CompilerId, EntryOptions, ModuleIdentifier, PluginExt,
};
use rspack_error::Diagnostic;
use rspack_fs::IntermediateFileSystem;
use rspack_fs_node::{NodeFileSystem, ThreadsafeNodeFS};
Expand Down Expand Up @@ -85,7 +87,9 @@ use resolver_factory::*;
pub use resource_data::*;
pub use rsdoctor::*;
use rspack_tracing::{ChromeTracer, OtelTracer, StdoutTracer, Tracer};
use rspack_util::fx_hash::FxDashMap;
pub use runtime::*;
use rustc_hash::FxHashMap;
pub use source::*;
pub use stats::*;
use swc_core::common::util::take::Take;
Expand All @@ -112,6 +116,7 @@ pub struct JsCompiler {
js_hooks_plugin: JsHooksAdapterPlugin,
compiler: Pin<Box<Compiler>>,
state: CompilerState,
include_dependencies_map: FxDashMap<String, FxHashMap<EntryOptions, BoxDependency>>,
}

#[napi]
Expand Down Expand Up @@ -187,6 +192,7 @@ impl JsCompiler {
compiler: Box::pin(Compiler::from(rspack)),
state: CompilerState::init(),
js_hooks_plugin,
include_dependencies_map: Default::default(),
})
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
STATE.foo = 42;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
__webpack_require__("./foo.js");

it("should addInclude foo.js", () => {
expect(STATE.foo).toBe(42);
const builtModules = Object.fromEntries(__STATS__.modules.map(m => [m.name, m.built]));
expect(builtModules).toEqual({ "./index.js": true, "./foo.js": true });
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
__webpack_require__("./foo.js");

it("should addInclude foo.js", () => {
expect(STATE.foo).toBe(42);
const builtModules = Object.fromEntries(__STATS__.modules.map(m => [m.name, m.built]));
expect(builtModules).toEqual({ "./index.js": true, "./foo.js": false });
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
let step = 0;
let factorizeRequests = [];

/** @type {import("@rspack/core").Configuration} */
module.exports = {
plugins: [
function (compiler) {
const PLUGIN_NAME = "TEST_PLUGIN";
const { EntryPlugin } = compiler.webpack;
compiler.hooks.finishMake.tapPromise(PLUGIN_NAME, compilation => {
return new Promise((resolve, reject) => {
compilation.addInclude(
compiler.context,
EntryPlugin.createDependency("./foo.js"),
{},
err => {
if (err) return reject(err);
return resolve();
}
);
});
});

compiler.hooks.compilation.tap(
PLUGIN_NAME,
(compilation, { normalModuleFactory }) => {
normalModuleFactory.hooks.factorize.tap(PLUGIN_NAME, data => {
factorizeRequests.push(data.request);
});
}
);
compiler.hooks.done.tap(PLUGIN_NAME, () => {
if (step === 0) {
expect(factorizeRequests.length).toBe(2);
expect(factorizeRequests.includes("./index.js")).toBe(true);
expect(factorizeRequests.includes("./foo.js")).toBe(true);
} else if (step === 1) {
expect(factorizeRequests.length).toBe(1);
expect(factorizeRequests.includes("./index.js")).toBe(true);
} else {
throw new Error("Unexpected step");
}
step += 1;
factorizeRequests = [];
});
}
]
};
Loading