Skip to content

Commit

Permalink
refactor: remove snapshot_module_load_cb (denoland#20043)
Browse files Browse the repository at this point in the history
  • Loading branch information
nayeemrmn committed Aug 5, 2023
1 parent 85a2b28 commit b96f283
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 36 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

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

4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +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_ops = "0.77.0"
serde_v8 = "0.110.0"
deno_core = "0.200.0"

deno_runtime = { version = "0.122.0", path = "./runtime" }
napi_sym = { version = "0.44.0", path = "./cli/napi/sym" }
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

0 comments on commit b96f283

Please sign in to comment.