From 52631839850c8c9c01730fb53d1a3be09994a22a Mon Sep 17 00:00:00 2001 From: David Sherret Date: Wed, 22 Jan 2025 12:19:28 -0500 Subject: [PATCH 1/2] chore: fix hmr build --- cli/snapshot/Cargo.toml | 2 +- runtime/Cargo.toml | 5 +-- runtime/lib.rs | 4 ++- runtime/snapshot.rs | 76 +-------------------------------------- runtime/transpile.rs | 78 +++++++++++++++++++++++++++++++++++++++++ runtime/web_worker.rs | 5 +++ runtime/worker.rs | 5 +++ 7 files changed, 96 insertions(+), 79 deletions(-) create mode 100644 runtime/transpile.rs diff --git a/cli/snapshot/Cargo.toml b/cli/snapshot/Cargo.toml index db3098d048400d..c023af04a3b17d 100644 --- a/cli/snapshot/Cargo.toml +++ b/cli/snapshot/Cargo.toml @@ -17,4 +17,4 @@ path = "lib.rs" disable = [] [build-dependencies] -deno_runtime = { workspace = true, features = ["include_js_files_for_snapshotting", "only_snapshotted_js_sources", "snapshotting"] } +deno_runtime = { workspace = true, features = ["include_js_files_for_snapshotting", "only_snapshotted_js_sources", "snapshot"] } diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml index 99a255b6cc51dc..9b580393d7b2cc 100644 --- a/runtime/Cargo.toml +++ b/runtime/Cargo.toml @@ -22,12 +22,13 @@ include_js_files_for_snapshotting = [ ] # A dev feature to disable creations and loading of snapshots in favor of # loading JS sources at runtime. -hmr = ["include_js_files_for_snapshotting"] +hmr = ["include_js_files_for_snapshotting", "transpile"] # Signal that only snapshotted JS sources should be used. This will # conditionally exclude the runtime source transpilation logic, and add an # assertion that a snapshot is provided. only_snapshotted_js_sources = ["include_js_files_for_snapshotting"] -snapshotting = ["deno_ast"] +snapshot = ["transpile"] +transpile = ["deno_ast"] [lints.rust] unexpected_cfgs = { level = "warn", check-cfg = ['cfg(tokio_unstable)'] } diff --git a/runtime/lib.rs b/runtime/lib.rs index a2f3e543531e49..92d6079f6d6bcb 100644 --- a/runtime/lib.rs +++ b/runtime/lib.rs @@ -36,9 +36,11 @@ pub mod inspector_server; pub mod js; pub mod ops; pub mod permissions; -#[cfg(feature = "snapshotting")] +#[cfg(feature = "snapshot")] pub mod snapshot; pub mod tokio_util; +#[cfg(feature = "transpile")] +pub mod transpile; pub mod web_worker; pub mod worker; diff --git a/runtime/snapshot.rs b/runtime/snapshot.rs index a2f0322763123c..80d90aab1ade75 100644 --- a/runtime/snapshot.rs +++ b/runtime/snapshot.rs @@ -7,17 +7,10 @@ use std::path::PathBuf; use std::rc::Rc; use std::sync::Arc; -use deno_ast::MediaType; -use deno_ast::ParseParams; -use deno_ast::SourceMapOption; use deno_cache::SqliteBackedCache; use deno_core::snapshot::*; use deno_core::v8; use deno_core::Extension; -use deno_core::ModuleCodeString; -use deno_core::ModuleName; -use deno_core::SourceMapData; -use deno_error::JsErrorBox; use deno_http::DefaultHttpPropertyExtractor; use deno_io::fs::FsError; use deno_permissions::PermissionCheckError; @@ -345,7 +338,7 @@ pub fn create_runtime_snapshot( startup_snapshot: None, extensions, extension_transpiler: Some(Rc::new(|specifier, source| { - maybe_transpile_source(specifier, source) + crate::transpile::maybe_transpile_source(specifier, source) })), with_runtime_cb: Some(Box::new(|rt| { let isolate = rt.v8_isolate(); @@ -376,70 +369,3 @@ pub fn create_runtime_snapshot( println!("cargo:rerun-if-changed={}", path.display()); } } - -deno_error::js_error_wrapper!( - deno_ast::ParseDiagnostic, - JsParseDiagnostic, - "Error" -); -deno_error::js_error_wrapper!( - deno_ast::TranspileError, - JsTranspileError, - "Error" -); - -pub fn maybe_transpile_source( - name: ModuleName, - source: ModuleCodeString, -) -> Result<(ModuleCodeString, Option), JsErrorBox> { - // Always transpile `node:` built-in modules, since they might be TypeScript. - let media_type = if name.starts_with("node:") { - MediaType::TypeScript - } else { - MediaType::from_path(Path::new(&name)) - }; - - match media_type { - MediaType::TypeScript => {} - MediaType::JavaScript => return Ok((source, None)), - MediaType::Mjs => return Ok((source, None)), - _ => panic!( - "Unsupported media type for snapshotting {media_type:?} for file {}", - name - ), - } - - let parsed = deno_ast::parse_module(ParseParams { - specifier: deno_core::url::Url::parse(&name).unwrap(), - text: source.into(), - media_type, - capture_tokens: false, - scope_analysis: false, - maybe_syntax: None, - }) - .map_err(|e| JsErrorBox::from_err(JsParseDiagnostic(e)))?; - let transpiled_source = parsed - .transpile( - &deno_ast::TranspileOptions { - imports_not_used_as_values: deno_ast::ImportsNotUsedAsValues::Remove, - ..Default::default() - }, - &deno_ast::TranspileModuleOptions::default(), - &deno_ast::EmitOptions { - source_map: if cfg!(debug_assertions) { - SourceMapOption::Separate - } else { - SourceMapOption::None - }, - ..Default::default() - }, - ) - .map_err(|e| JsErrorBox::from_err(JsTranspileError(e)))? - .into_source(); - - let maybe_source_map: Option = transpiled_source - .source_map - .map(|sm| sm.into_bytes().into()); - let source_text = transpiled_source.text; - Ok((source_text.into(), maybe_source_map)) -} diff --git a/runtime/transpile.rs b/runtime/transpile.rs new file mode 100644 index 00000000000000..9c60f2ecc907ee --- /dev/null +++ b/runtime/transpile.rs @@ -0,0 +1,78 @@ +// Copyright 2018-2025 the Deno authors. MIT license. + +use std::path::Path; + +use deno_ast::MediaType; +use deno_ast::ParseParams; +use deno_ast::SourceMapOption; +use deno_core::ModuleCodeString; +use deno_core::ModuleName; +use deno_core::SourceMapData; +use deno_error::JsErrorBox; + +deno_error::js_error_wrapper!( + deno_ast::ParseDiagnostic, + JsParseDiagnostic, + "Error" +); +deno_error::js_error_wrapper!( + deno_ast::TranspileError, + JsTranspileError, + "Error" +); + +pub fn maybe_transpile_source( + name: ModuleName, + source: ModuleCodeString, +) -> Result<(ModuleCodeString, Option), JsErrorBox> { + // Always transpile `node:` built-in modules, since they might be TypeScript. + let media_type = if name.starts_with("node:") { + MediaType::TypeScript + } else { + MediaType::from_path(Path::new(&name)) + }; + + match media_type { + MediaType::TypeScript => {} + MediaType::JavaScript => return Ok((source, None)), + MediaType::Mjs => return Ok((source, None)), + _ => panic!( + "Unsupported media type for snapshotting {media_type:?} for file {}", + name + ), + } + + let parsed = deno_ast::parse_module(ParseParams { + specifier: deno_core::url::Url::parse(&name).unwrap(), + text: source.into(), + media_type, + capture_tokens: false, + scope_analysis: false, + maybe_syntax: None, + }) + .map_err(|e| JsErrorBox::from_err(JsParseDiagnostic(e)))?; + let transpiled_source = parsed + .transpile( + &deno_ast::TranspileOptions { + imports_not_used_as_values: deno_ast::ImportsNotUsedAsValues::Remove, + ..Default::default() + }, + &deno_ast::TranspileModuleOptions::default(), + &deno_ast::EmitOptions { + source_map: if cfg!(debug_assertions) { + SourceMapOption::Separate + } else { + SourceMapOption::None + }, + ..Default::default() + }, + ) + .map_err(|e| JsErrorBox::from_err(JsTranspileError(e)))? + .into_source(); + + let maybe_source_map: Option = transpiled_source + .source_map + .map(|sm| sm.into_bytes().into()); + let source_text = transpiled_source.text; + Ok((source_text.into(), maybe_source_map)) +} diff --git a/runtime/web_worker.rs b/runtime/web_worker.rs index d7cf85bab932ed..595fe9506ce3a0 100644 --- a/runtime/web_worker.rs +++ b/runtime/web_worker.rs @@ -594,6 +594,11 @@ impl WebWorker { shared_array_buffer_store: services.shared_array_buffer_store, compiled_wasm_module_store: services.compiled_wasm_module_store, extensions, + #[cfg(feature = "hmr")] + extension_transpiler: Some(Rc::new(|specifier, source| { + crate::transpile::maybe_transpile_source(specifier, source) + })), + #[cfg(not(feature = "hmr"))] extension_transpiler: None, inspector: true, feature_checker: Some(services.feature_checker), diff --git a/runtime/worker.rs b/runtime/worker.rs index 270c8b5392ea04..6e4dad0157ae47 100644 --- a/runtime/worker.rs +++ b/runtime/worker.rs @@ -501,6 +501,11 @@ impl MainWorker { shared_array_buffer_store: services.shared_array_buffer_store.clone(), compiled_wasm_module_store: services.compiled_wasm_module_store.clone(), extensions, + #[cfg(feature = "hmr")] + extension_transpiler: Some(Rc::new(|specifier, source| { + crate::transpile::maybe_transpile_source(specifier, source) + })), + #[cfg(not(feature = "hmr"))] extension_transpiler: None, inspector: true, is_main: true, From e2c4f6098751f7c449ac3a6490adbd96d38d0f93 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Wed, 22 Jan 2025 12:21:52 -0500 Subject: [PATCH 2/2] fix --- runtime/Cargo.toml | 1 + runtime/web_worker.rs | 4 ++-- runtime/worker.rs | 4 ++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml index 9b580393d7b2cc..58154ebe6aa16a 100644 --- a/runtime/Cargo.toml +++ b/runtime/Cargo.toml @@ -40,6 +40,7 @@ path = "lib.rs" [[example]] name = "extension" path = "examples/extension/main.rs" +required-features = ["transpile"] [dependencies] deno_ast = { workspace = true, optional = true } diff --git a/runtime/web_worker.rs b/runtime/web_worker.rs index 595fe9506ce3a0..7d6c890cfe9c4d 100644 --- a/runtime/web_worker.rs +++ b/runtime/web_worker.rs @@ -594,11 +594,11 @@ impl WebWorker { shared_array_buffer_store: services.shared_array_buffer_store, compiled_wasm_module_store: services.compiled_wasm_module_store, extensions, - #[cfg(feature = "hmr")] + #[cfg(feature = "transpile")] extension_transpiler: Some(Rc::new(|specifier, source| { crate::transpile::maybe_transpile_source(specifier, source) })), - #[cfg(not(feature = "hmr"))] + #[cfg(not(feature = "transpile"))] extension_transpiler: None, inspector: true, feature_checker: Some(services.feature_checker), diff --git a/runtime/worker.rs b/runtime/worker.rs index 6e4dad0157ae47..3fa3e6828b2bc2 100644 --- a/runtime/worker.rs +++ b/runtime/worker.rs @@ -501,11 +501,11 @@ impl MainWorker { shared_array_buffer_store: services.shared_array_buffer_store.clone(), compiled_wasm_module_store: services.compiled_wasm_module_store.clone(), extensions, - #[cfg(feature = "hmr")] + #[cfg(feature = "transpile")] extension_transpiler: Some(Rc::new(|specifier, source| { crate::transpile::maybe_transpile_source(specifier, source) })), - #[cfg(not(feature = "hmr"))] + #[cfg(not(feature = "transpile"))] extension_transpiler: None, inspector: true, is_main: true,