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

chore: fix hmr build #27781

Merged
merged 2 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion cli/snapshot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
6 changes: 4 additions & 2 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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)'] }
Expand All @@ -39,6 +40,7 @@ path = "lib.rs"
[[example]]
name = "extension"
path = "examples/extension/main.rs"
required-features = ["transpile"]

[dependencies]
deno_ast = { workspace = true, optional = true }
Expand Down
4 changes: 3 additions & 1 deletion runtime/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
76 changes: 1 addition & 75 deletions runtime/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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<SourceMapData>), 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<SourceMapData> = transpiled_source
.source_map
.map(|sm| sm.into_bytes().into());
let source_text = transpiled_source.text;
Ok((source_text.into(), maybe_source_map))
}
78 changes: 78 additions & 0 deletions runtime/transpile.rs
Original file line number Diff line number Diff line change
@@ -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<SourceMapData>), 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<SourceMapData> = transpiled_source
.source_map
.map(|sm| sm.into_bytes().into());
let source_text = transpiled_source.text;
Ok((source_text.into(), maybe_source_map))
}
5 changes: 5 additions & 0 deletions runtime/web_worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "transpile")]
extension_transpiler: Some(Rc::new(|specifier, source| {
crate::transpile::maybe_transpile_source(specifier, source)
})),
#[cfg(not(feature = "transpile"))]
extension_transpiler: None,
inspector: true,
feature_checker: Some(services.feature_checker),
Expand Down
5 changes: 5 additions & 0 deletions runtime/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "transpile")]
extension_transpiler: Some(Rc::new(|specifier, source| {
crate::transpile::maybe_transpile_source(specifier, source)
})),
#[cfg(not(feature = "transpile"))]
extension_transpiler: None,
inspector: true,
is_main: true,
Expand Down
Loading