Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove snapshot_module_load_cb #20043

Merged
merged 5 commits into from
Aug 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
refactor: remove snapshot_module_load_cb
  • Loading branch information
nayeemrmn committed Aug 3, 2023
commit 4ff716dc5fcda151b5448078859dcfde42d637ee
9 changes: 3 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ repository = "https://github.com/denoland/deno"
v8 = { version = "0.74.1", default-features = false }
deno_ast = { version = "0.27.0", features = ["transpiling"] }

deno_core = "0.199.0"
deno_core = { version = "0.199.0", git = "https://github.com/denoland/deno_core.git" }
deno_ops = "0.77.0"
serde_v8 = "0.110.0"

Expand Down
2 changes: 0 additions & 2 deletions cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,6 @@ mod ts {
.expect("snapshot compression failed"),
);
})),
snapshot_module_load_cb: None,
with_runtime_cb: None,
});
for path in output.files_loaded_during_snapshot {
Expand Down Expand Up @@ -377,7 +376,6 @@ fn create_cli_snapshot(snapshot_path: PathBuf) -> CreateSnapshotOutput {
startup_snapshot: Some(deno_runtime::js::deno_isolate_init()),
extensions,
compression_cb: None,
snapshot_module_load_cb: None,
with_runtime_cb: None,
})
}
Expand Down
54 changes: 29 additions & 25 deletions runtime/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,33 @@ mod startup_snapshot {
use deno_core::snapshot_util::*;
use deno_core::Extension;
use deno_core::ExtensionFileSource;
use deno_core::ModuleCode;
use deno_core::ExtensionFileSourceCode;
use deno_http::DefaultHttpPropertyExtractor;
use std::path::Path;

fn transpile_ts_for_snapshotting(
file_source: &ExtensionFileSource,
) -> Result<ModuleCode, AnyError> {
fn maybe_transpile_source(
source: &mut ExtensionFileSource,
) -> Result<(), AnyError> {
// Always transpile `node:` built-in modules, since they might be TypeScript.
let media_type = if file_source.specifier.starts_with("node:") {
let media_type = if source.specifier.starts_with("node:") {
MediaType::TypeScript
} else {
MediaType::from_path(Path::new(&file_source.specifier))
MediaType::from_path(Path::new(&source.specifier))
};

let should_transpile = match media_type {
MediaType::JavaScript => false,
MediaType::Mjs => false,
MediaType::TypeScript => true,
_ => {
panic!(
"Unsupported media type for snapshotting {media_type:?} for file {}",
file_source.specifier
)
}
};
let code = file_source.load()?;

if !should_transpile {
return Ok(code);
match media_type {
MediaType::TypeScript => {}
MediaType::JavaScript => return Ok(()),
MediaType::Mjs => return Ok(()),
_ => panic!(
"Unsupported media type for snapshotting {media_type:?} for file {}",
source.specifier
),
}
let code = source.load()?;

let parsed = deno_ast::parse_module(ParseParams {
specifier: file_source.specifier.to_string(),
specifier: source.specifier.to_string(),
text_info: SourceTextInfo::from_string(code.as_str().to_owned()),
media_type,
capture_tokens: false,
Expand All @@ -62,7 +56,9 @@ mod startup_snapshot {
..Default::default()
})?;

Ok(transpiled_source.text.into())
source.code =
ExtensionFileSourceCode::Computed(transpiled_source.text.into());
Ok(())
}

#[derive(Clone)]
Expand Down Expand Up @@ -312,7 +308,7 @@ mod startup_snapshot {
// NOTE(bartlomieju): ordering is important here, keep it in sync with
// `runtime/worker.rs`, `runtime/web_worker.rs` and `cli/build.rs`!
let fs = std::sync::Arc::new(deno_fs::RealFs);
let extensions: Vec<Extension> = vec![
let mut extensions: Vec<Extension> = vec![
deno_webidl::deno_webidl::init_ops_and_esm(),
deno_console::deno_console::init_ops_and_esm(),
deno_url::deno_url::init_ops_and_esm(),
Expand Down Expand Up @@ -356,13 +352,21 @@ mod startup_snapshot {
runtime_main::init_ops_and_esm(),
];

for extension in &mut extensions {
for source in extension.esm_files.to_mut() {
maybe_transpile_source(source).unwrap();
}
for source in extension.js_files.to_mut() {
maybe_transpile_source(source).unwrap();
}
}

let output = create_snapshot(CreateSnapshotOptions {
cargo_manifest_dir: env!("CARGO_MANIFEST_DIR"),
snapshot_path,
startup_snapshot: None,
extensions,
compression_cb: None,
snapshot_module_load_cb: Some(Box::new(transpile_ts_for_snapshotting)),
with_runtime_cb: None,
});
for path in output.files_loaded_during_snapshot {
Expand Down
Loading