summaryrefslogtreecommitdiff
path: root/core/snapshot_util.rs
diff options
context:
space:
mode:
authorLeo Kettmeir <crowlkats@toaxl.com>2023-02-07 20:22:46 +0100
committerGitHub <noreply@github.com>2023-02-07 20:22:46 +0100
commitb4aa1530970f7b9cc4e6f2f27e077852c4e178d3 (patch)
tree3d008912affe8550692183bd2697a386db5e3c79 /core/snapshot_util.rs
parent65500f36e870b4ada3996b06aa287e30177d21a3 (diff)
refactor: Use ES modules for internal runtime code (#17648)
This PR refactors all internal js files (except core) to be written as ES modules. `__bootstrap`has been mostly replaced with static imports in form in `internal:[path to file from repo root]`. To specify if files are ESM, an `esm` method has been added to `Extension`, similar to the `js` method. A new ModuleLoader called `InternalModuleLoader` has been added to enable the loading of internal specifiers, which is used in all situations except when a snapshot is only loaded, and not a new one is created from it. --------- Co-authored-by: Bartek IwaƄczuk <biwanczuk@gmail.com>
Diffstat (limited to 'core/snapshot_util.rs')
-rw-r--r--core/snapshot_util.rs29
1 files changed, 28 insertions, 1 deletions
diff --git a/core/snapshot_util.rs b/core/snapshot_util.rs
index 8e397e262..8daaa9866 100644
--- a/core/snapshot_util.rs
+++ b/core/snapshot_util.rs
@@ -1,10 +1,12 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
+use anyhow::Context;
use std::path::Path;
use std::path::PathBuf;
use crate::Extension;
use crate::JsRuntime;
+use crate::ModuleSpecifier;
use crate::RuntimeOptions;
use crate::Snapshot;
@@ -17,6 +19,7 @@ pub struct CreateSnapshotOptions {
pub extensions: Vec<Extension>,
pub extensions_with_js: Vec<Extension>,
pub additional_files: Vec<PathBuf>,
+ pub additional_esm_files: Vec<PathBuf>,
pub compression_cb: Option<Box<CompressionCb>>,
}
@@ -44,6 +47,27 @@ pub fn create_snapshot(create_snapshot_options: CreateSnapshotOptions) {
)
.unwrap();
}
+ for file in create_snapshot_options.additional_esm_files {
+ let display_path = file.strip_prefix(display_root).unwrap_or(&file);
+ let display_path_str = display_path.display().to_string();
+
+ let filename =
+ &("internal:".to_string() + &display_path_str.replace('\\', "/"));
+
+ futures::executor::block_on(async {
+ let id = js_runtime
+ .load_side_module(
+ &ModuleSpecifier::parse(filename)?,
+ Some(std::fs::read_to_string(&file)?),
+ )
+ .await?;
+ let receiver = js_runtime.mod_evaluate(id);
+ js_runtime.run_event_loop(false).await?;
+ receiver.await?
+ })
+ .with_context(|| format!("Couldn't execute '{}'", file.display()))
+ .unwrap();
+ }
let snapshot = js_runtime.snapshot();
let snapshot_slice: &[u8] = &snapshot;
@@ -79,9 +103,12 @@ pub fn create_snapshot(create_snapshot_options: CreateSnapshotOptions) {
);
}
+pub type FilterFn = Box<dyn Fn(&PathBuf) -> bool>;
+
pub fn get_js_files(
cargo_manifest_dir: &'static str,
directory: &str,
+ filter: Option<FilterFn>,
) -> Vec<PathBuf> {
let manifest_dir = Path::new(cargo_manifest_dir);
let mut js_files = std::fs::read_dir(directory)
@@ -92,7 +119,7 @@ pub fn get_js_files(
})
.filter(|path| {
path.extension().unwrap_or_default() == "js"
- && !path.ends_with("99_main.js")
+ && filter.as_ref().map(|filter| filter(path)).unwrap_or(true)
})
.collect::<Vec<PathBuf>>();
js_files.sort();