Skip to content

Commit 44a7564

Browse files
hardfistCopilot
andauthored
fix: report error when access module_graph in loader (#12639)
* fix: report error when access module_graph in loader * Apply suggestions from code review Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: Copilot <[email protected]>
1 parent edce5bd commit 44a7564

File tree

5 files changed

+29
-0
lines changed

5 files changed

+29
-0
lines changed

crates/rspack_binding_api/src/module_graph.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ impl JsModuleGraph {
2828

2929
fn as_ref(&self) -> napi::Result<(&'static Compilation, &'static ModuleGraph)> {
3030
let compilation = unsafe { self.compilation.as_ref() };
31+
if compilation.build_module_graph_artifact.is_none() {
32+
return Err(napi::Error::from_reason(
33+
"ModuleGraph is not available during module graph building phase".to_string(),
34+
));
35+
}
3136
let module_graph = compilation.get_module_graph();
3237

3338
Ok((compilation, module_graph))
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = function loader(content) {
2+
3+
const moduleGraph = this._compilation.moduleGraph;
4+
let issuer = moduleGraph.getIssuer(this._module);
5+
console.log(issuer);
6+
return content;
7+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = [/ModuleGraph is not available during module graph building phase/]

tests/rspack-test/configCases/loader/access-module-graph/index.js

Whitespace-only changes.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
/**
3+
* @type {import('@rspack/core').RspackOptions}
4+
*/
5+
module.exports = {
6+
context: __dirname,
7+
entry: './index.js',
8+
module: {
9+
rules: [
10+
{
11+
test: /index.js/,
12+
use: [{ loader: "./access-mg-loader.js" }]
13+
}
14+
]
15+
}
16+
};

0 commit comments

Comments
 (0)