Skip to content

Commit 52ec884

Browse files
authored
refactor: Support specifying specifiers for extension files (#5)
1 parent 4ada523 commit 52ec884

File tree

3 files changed

+42
-10
lines changed

3 files changed

+42
-10
lines changed

core/extensions.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ macro_rules! extension {
190190
$(, ops = [ $( $(#[$m:meta])* $( $op:ident )::+ $( < $( $op_param:ident ),* > )? ),+ $(,)? ] )?
191191
$(, esm_entry_point = $esm_entry_point:literal )?
192192
$(, esm = [ $( dir $dir_esm:literal , )? $( $esm:literal ),* $(,)? ] )?
193+
$(, esm_with_specifiers = [ $( dir $dir_esm2:literal , )? $( ($esm_specifier:literal, $esm_file:literal) ),* $(,)? ] )?
193194
$(, js = [ $( dir $dir_js:literal , )? $( $js:literal ),* $(,)? ] )?
194195
$(, options = { $( $options_id:ident : $options_type:ty ),* $(,)? } )?
195196
$(, middleware = $middleware_fn:expr )?
@@ -218,6 +219,9 @@ macro_rules! extension {
218219
$( ext.esm(
219220
$crate::include_js_files!( $name $( dir $dir_esm , )? $( $esm , )* )
220221
); )?
222+
$( ext.esm(
223+
$crate::include_js_files_with_specifiers!( $name $( dir $dir_esm2 , )? $( ( $esm_specifier, $esm_file) , )* )
224+
); )?
221225
$(
222226
ext.esm_entry_point($esm_entry_point);
223227
)?
@@ -643,3 +647,33 @@ macro_rules! include_js_files {
643647
]
644648
};
645649
}
650+
651+
#[cfg(not(feature = "include_js_files_for_snapshotting"))]
652+
#[macro_export]
653+
macro_rules! include_js_files_with_specifiers {
654+
($name:ident dir $dir:literal, $( ( $specifier:literal , $file:literal ),)+) => {
655+
vec![
656+
$($crate::ExtensionFileSource {
657+
specifier: $specifier,
658+
code: $crate::ExtensionFileSourceCode::IncludedInBinary(
659+
include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/", $dir, "/", $file))
660+
),
661+
},)+
662+
]
663+
};
664+
}
665+
666+
#[cfg(feature = "include_js_files_for_snapshotting")]
667+
#[macro_export]
668+
macro_rules! include_js_files_with_specifiers {
669+
($name:ident dir $dir:literal, $( ( $specifier:literal , $file:literal ),)+) => {
670+
vec![
671+
$($crate::ExtensionFileSource {
672+
specifier: $specifier,
673+
code: $crate::ExtensionFileSourceCode::LoadedFromFsDuringSnapshot(
674+
std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join($dir).join($file)
675+
),
676+
},)+
677+
]
678+
};
679+
}

core/modules/map.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -888,14 +888,10 @@ impl ModuleMap {
888888
/// Clear the module map, meant to be used after initializing extensions.
889889
/// Optionally pass a list of exceptions `(old_name, new_name)` representing
890890
/// specifiers which will be renamed and preserved in the module map.
891-
pub fn clear_module_map(
892-
&mut self,
893-
exceptions: impl Iterator<Item = (&'static str, &'static str)>,
894-
) {
891+
pub fn clear_module_map(&mut self, exceptions: &'static [&'static str]) {
895892
let handles = exceptions
896-
.map(|(old_name, new_name)| {
897-
(self.get_handle_by_name(old_name).unwrap(), new_name)
898-
})
893+
.iter()
894+
.map(|mod_name| (self.get_handle_by_name(mod_name).unwrap(), mod_name))
899895
.collect::<Vec<_>>();
900896
self.clear();
901897
for (handle, new_name) in handles {

core/runtime/jsruntime.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ pub struct RuntimeOptions {
404404
/// If provided, the module map will be cleared and left only with the specifiers
405405
/// in this list, with the new names provided. If not provided, the module map is
406406
/// left intact.
407-
pub rename_modules: Option<Vec<(&'static str, &'static str)>>,
407+
pub preserve_snapshotted_modules: Option<&'static [&'static str]>,
408408

409409
/// V8 snapshot that should be loaded on startup.
410410
pub startup_snapshot: Option<Snapshot>,
@@ -711,11 +711,13 @@ impl JsRuntime {
711711
.unwrap();
712712

713713
// If the user has requested that we rename modules
714-
if let Some(rename_modules) = options.rename_modules {
714+
if let Some(preserve_snapshotted_modules) =
715+
options.preserve_snapshotted_modules
716+
{
715717
js_runtime
716718
.module_map
717719
.borrow_mut()
718-
.clear_module_map(rename_modules.into_iter());
720+
.clear_module_map(preserve_snapshotted_modules);
719721
}
720722

721723
js_runtime

0 commit comments

Comments
 (0)