Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ impl Default for ModuleExecutor {
}

impl ModuleExecutor {
pub fn is_running(&self) -> bool {
self.event_sender.is_some()
}

pub async fn before_build_module_graph(&mut self, compilation: &Compilation) -> Result<()> {
let mut make_artifact = self.make_artifact.steal();
let mut exports_info_artifact = self.exports_info_artifact.steal();
Expand Down
39 changes: 34 additions & 5 deletions crates/rspack_core/src/compilation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,9 +652,20 @@ impl Compilation {
self.add_entry(entry, options).await?;
}

// ensure module executor is running so loaders can call `importModule`
let started_executor = match &self.module_executor {
Some(me) if !me.is_running() => {
let mut module_executor = self.module_executor.take().expect("checked above");
module_executor.before_build_module_graph(self).await?;
self.module_executor = Some(module_executor);
true
}
_ => false,
};

let make_artifact = self.build_module_graph_artifact.steal();
let exports_info_artifact = self.exports_info_artifact.steal();
let (make_artifact, exports_info_artifact) = update_module_graph(
let build_result = update_module_graph(
self,
make_artifact,
exports_info_artifact,
Expand All @@ -668,11 +679,29 @@ impl Compilation {
.collect(),
)],
)
.await?;
self.build_module_graph_artifact = make_artifact.into();
self.exports_info_artifact = exports_info_artifact.into();
.await;

// restore artifacts before teardown — after_build_module_graph reads them
let build_result = match build_result {
Ok((make_artifact, exports_info_artifact)) => {
self.build_module_graph_artifact = make_artifact.into();
self.exports_info_artifact = exports_info_artifact.into();
Ok(())
}
Err(e) => {
self.build_module_graph_artifact = StealCell::new(BuildModuleGraphArtifact::new());
self.exports_info_artifact = StealCell::new(ExportsInfoArtifact::default());
Err(e)
}
};

Ok(())
// tear down the executor so build_module_graph_pass can set it up fresh
if started_executor && let Some(mut module_executor) = self.module_executor.take() {
module_executor.after_build_module_graph(self).await?;
self.module_executor = Some(module_executor);
}

build_result
}

pub async fn add_include(&mut self, args: Vec<(BoxDependency, EntryOptions)>) -> Result<()> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default "hello from imported module";
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import data from "./data.js";
export default data;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
it("should have built the dynamically added entry", () => {
expect(__STATS__.compilation.namedChunks).toHaveProperty("dynamic");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/** @type {import("@rspack/core").PitchLoaderDefinitionFunction} */
exports.pitch = async function (remaining) {
const result = await this.importModule(
this.resourcePath + ".webpack[javascript/auto]" + "!=!" + remaining
);
return result.default || result;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const path = require("path");

const PLUGIN_NAME = "AddEntryWithImportModulePlugin";

class AddEntryWithImportModulePlugin {
/**
* @param {import("@rspack/core").Compiler} compiler
*/
apply(compiler) {
const { EntryPlugin } = compiler.rspack;

compiler.hooks.make.tapPromise(PLUGIN_NAME, compilation => {
return new Promise((resolve, reject) => {
compilation.addEntry(
compiler.context,
EntryPlugin.createDependency(
path.resolve(__dirname, "entry.js")
),
{ name: "dynamic" },
(err) => {
if (err) reject(err);
else resolve();
}
);
});
});
}
}

/**@type {import("@rspack/core").Configuration}*/
module.exports = {
output: {
filename: "[name].js"
},
module: {
rules: [
{
test: /entry\.js$/,
use: [{ loader: "./loader", options: {} }],
type: "asset/source"
}
]
},
plugins: [new AddEntryWithImportModulePlugin()]
};
Loading