-
-
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
8 changed files
with
157 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
use std::{ptr::NonNull, sync::Arc}; | ||
|
||
use napi::Either; | ||
use napi_derive::napi; | ||
use rspack_core::{Compilation, ExportsInfo, ModuleGraph, RuntimeSpec}; | ||
|
||
use crate::JsRuntimeSpec; | ||
|
||
#[napi] | ||
pub struct JsExportsInfo { | ||
exports_info: ExportsInfo, | ||
compilation: NonNull<Compilation>, | ||
} | ||
|
||
impl JsExportsInfo { | ||
pub fn new(exports_info: ExportsInfo, compilation: &Compilation) -> Self { | ||
#[allow(clippy::unwrap_used)] | ||
Self { | ||
exports_info, | ||
compilation: NonNull::new(compilation as *const Compilation as *mut Compilation).unwrap(), | ||
} | ||
} | ||
|
||
fn as_ref(&self) -> napi::Result<ModuleGraph<'static>> { | ||
let compilation = unsafe { self.compilation.as_ref() }; | ||
let module_graph = compilation.get_module_graph(); | ||
Ok(module_graph) | ||
} | ||
|
||
fn as_mut(&mut self) -> napi::Result<ModuleGraph<'static>> { | ||
let compilation = unsafe { self.compilation.as_mut() }; | ||
let module_graph = compilation.get_module_graph_mut(); | ||
Ok(module_graph) | ||
} | ||
} | ||
|
||
#[napi] | ||
impl JsExportsInfo { | ||
#[napi(ts_args_type = "runtime: string | string[] | undefined")] | ||
pub fn is_used(&self, js_runtime: Option<JsRuntimeSpec>) -> napi::Result<bool> { | ||
let module_graph = self.as_ref()?; | ||
let runtime: Option<RuntimeSpec> = js_runtime.map(|js_rt| match js_rt { | ||
Either::A(str) => vec![str].into_iter().map(Arc::from).collect(), | ||
Either::B(vec) => vec.into_iter().map(Arc::from).collect(), | ||
}); | ||
Ok(self.exports_info.is_used(&module_graph, runtime.as_ref())) | ||
} | ||
|
||
#[napi(ts_args_type = "runtime: string | string[] | undefined")] | ||
pub fn is_module_used(&self, js_runtime: Option<JsRuntimeSpec>) -> napi::Result<bool> { | ||
let module_graph = self.as_ref()?; | ||
let runtime: Option<RuntimeSpec> = js_runtime.map(|js_rt| match js_rt { | ||
Either::A(str) => vec![str].into_iter().map(Arc::from).collect(), | ||
Either::B(vec) => vec.into_iter().map(Arc::from).collect(), | ||
}); | ||
Ok( | ||
self | ||
.exports_info | ||
.is_module_used(&module_graph, runtime.as_ref()), | ||
) | ||
} | ||
|
||
#[napi(ts_args_type = "runtime: string | string[] | undefined")] | ||
pub fn set_used_in_unknown_way( | ||
&mut self, | ||
js_runtime: Option<JsRuntimeSpec>, | ||
) -> napi::Result<bool> { | ||
let mut module_graph = self.as_mut()?; | ||
let runtime: Option<RuntimeSpec> = js_runtime.map(|js_rt| match js_rt { | ||
Either::A(str) => vec![str].into_iter().map(Arc::from).collect(), | ||
Either::B(vec) => vec.into_iter().map(Arc::from).collect(), | ||
}); | ||
Ok( | ||
self | ||
.exports_info | ||
.set_used_in_unknown_way(&mut module_graph, runtime.as_ref()), | ||
) | ||
} | ||
} |
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,31 @@ | ||
import { JsExportsInfo } from "@rspack/binding"; | ||
|
||
export type RuntimeSpec = string | string[] | undefined; | ||
|
||
export class ExportsInfo { | ||
#inner: JsExportsInfo; | ||
|
||
static __from_binding(binding: JsExportsInfo) { | ||
return new ExportsInfo(binding); | ||
} | ||
|
||
static __to_binding(module: ExportsInfo): JsExportsInfo { | ||
return module.#inner; | ||
} | ||
|
||
private constructor(binding: JsExportsInfo) { | ||
this.#inner = binding; | ||
} | ||
|
||
isUsed(runtime: RuntimeSpec): boolean { | ||
return this.#inner.isUsed(runtime); | ||
} | ||
|
||
isModuleUsed(runtime: RuntimeSpec): boolean { | ||
return this.#inner.isModuleUsed(runtime); | ||
} | ||
|
||
setUsedInUnknownWay(runtime: RuntimeSpec): boolean { | ||
return this.#inner.setUsedInUnknownWay(runtime); | ||
} | ||
} |
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