-
-
Notifications
You must be signed in to change notification settings - Fork 618
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
258 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
use std::{ptr::NonNull, sync::Arc}; | ||
|
||
use napi::{Either, Env, JsString}; | ||
use napi_derive::napi; | ||
use rspack_core::{Compilation, ModuleGraph, RuntimeSpec}; | ||
use rustc_hash::FxHashSet; | ||
|
||
use crate::{JsDependency, JsModule, JsModuleWrapper}; | ||
|
||
#[napi] | ||
pub struct JsModuleGraph { | ||
compilation: NonNull<Compilation>, | ||
} | ||
|
||
impl JsModuleGraph { | ||
pub fn new(compilation: &Compilation) -> Self { | ||
#[allow(clippy::unwrap_used)] | ||
JsModuleGraph { | ||
compilation: NonNull::new(compilation as *const Compilation as *mut Compilation).unwrap(), | ||
} | ||
} | ||
|
||
fn as_ref(&self) -> napi::Result<(&'static Compilation, ModuleGraph<'static>)> { | ||
let compilation = unsafe { self.compilation.as_ref() }; | ||
let module_graph = compilation.get_module_graph(); | ||
|
||
Ok((compilation, module_graph)) | ||
} | ||
} | ||
|
||
#[napi] | ||
impl JsModuleGraph { | ||
#[napi(ts_return_type = "JsModule | null")] | ||
pub fn get_module(&self, js_dependency: &JsDependency) -> napi::Result<Option<JsModuleWrapper>> { | ||
let (compilation, module_graph) = self.as_ref()?; | ||
let module = module_graph.get_module_by_dependency_id(&js_dependency.dependency_id); | ||
let js_module = module | ||
.map(|module| JsModuleWrapper::new(module.as_ref(), compilation.id(), Some(compilation))); | ||
Ok(js_module) | ||
} | ||
|
||
#[napi] | ||
pub fn get_used_exports( | ||
&self, | ||
env: Env, | ||
js_module: &JsModule, | ||
js_runtime: Either<String, Vec<String>>, | ||
) -> napi::Result<Option<Either<bool, Vec<JsString>>>> { | ||
let (_, module_graph) = self.as_ref()?; | ||
|
||
let mut runtime: FxHashSet<Arc<str>> = FxHashSet::default(); | ||
match js_runtime { | ||
Either::A(s) => { | ||
runtime.insert(Arc::from(s)); | ||
} | ||
Either::B(vec) => { | ||
runtime.extend(vec.into_iter().map(Arc::from)); | ||
} | ||
}; | ||
let used_exports = | ||
module_graph.get_used_exports(&js_module.identifier, Some(&RuntimeSpec::new(runtime))); | ||
Ok(match used_exports { | ||
rspack_core::UsedExports::Null => None, | ||
rspack_core::UsedExports::Bool(b) => Some(Either::A(b)), | ||
rspack_core::UsedExports::Vec(vec) => Some(Either::B( | ||
vec | ||
.into_iter() | ||
.map(|atom| env.create_string(atom.as_str())) | ||
.collect::<napi::Result<Vec<_>>>()?, | ||
)), | ||
}) | ||
} | ||
|
||
#[napi(ts_return_type = "JsModule | null")] | ||
pub fn get_issuer(&self, module: &JsModule) -> napi::Result<Option<JsModuleWrapper>> { | ||
let (compilation, module_graph) = self.as_ref()?; | ||
let issuer = module_graph.get_issuer(&module.identifier); | ||
Ok( | ||
issuer | ||
.map(|module| JsModuleWrapper::new(module.as_ref(), compilation.id(), Some(compilation))), | ||
) | ||
} | ||
} |
Empty file.
1 change: 1 addition & 0 deletions
1
packages/rspack-test-tools/tests/configCases/module_graph/get_issuer/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import './foo'; |
32 changes: 32 additions & 0 deletions
32
packages/rspack-test-tools/tests/configCases/module_graph/get_issuer/rspack.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const { normalize, join } = require('path'); | ||
|
||
const PLUGIN_NAME = "Test"; | ||
|
||
class Plugin { | ||
/** | ||
* @param {import("@rspack/core").Compiler} compiler | ||
*/ | ||
apply(compiler) { | ||
compiler.hooks.compilation.tap(PLUGIN_NAME, compilation => { | ||
compilation.hooks.finishModules.tap(PLUGIN_NAME, () => { | ||
const fooModule = Array.from(compilation.modules.values()) | ||
.find(module => normalize(module.request) === normalize(join(__dirname, 'foo.js'))); | ||
const issuer = compilation.moduleGraph.getIssuer(fooModule); | ||
expect(normalize(issuer.request)).toBe(normalize(join(__dirname, 'index.js'))); | ||
}); | ||
}); | ||
|
||
} | ||
} | ||
|
||
/** @type {import("@rspack/core").Configuration} */ | ||
module.exports = { | ||
target: "web", | ||
node: { | ||
__dirname: false, | ||
__filename: false, | ||
}, | ||
plugins: [ | ||
new Plugin() | ||
] | ||
}; |
Empty file.
32 changes: 32 additions & 0 deletions
32
packages/rspack-test-tools/tests/configCases/module_graph/get_module/rspack.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const { normalize, join } = require('path'); | ||
|
||
const PLUGIN_NAME = "Test"; | ||
|
||
class Plugin { | ||
/** | ||
* @param {import("@rspack/core").Compiler} compiler | ||
*/ | ||
apply(compiler) { | ||
compiler.hooks.compilation.tap(PLUGIN_NAME, compilation => { | ||
compilation.hooks.finishModules.tap(PLUGIN_NAME, () => { | ||
const entry = Array.from(compilation.entries.values())[0]; | ||
const entryDependency = entry.dependencies[0]; | ||
const refModule = compilation.moduleGraph.getModule(entryDependency); | ||
expect(normalize(refModule.request)).toBe(normalize(join(__dirname, 'index.js'))); | ||
}); | ||
}); | ||
|
||
} | ||
} | ||
|
||
/** @type {import("@rspack/core").Configuration} */ | ||
module.exports = { | ||
target: "web", | ||
node: { | ||
__dirname: false, | ||
__filename: false, | ||
}, | ||
plugins: [ | ||
new Plugin() | ||
] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
0891a65
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 Benchmark detail: Open
0891a65
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 Ran ecosystem CI: Open