mirror of
https://github.com/denoland/deno.git
synced 2025-02-01 12:16:11 -05:00
parent
7b11670e13
commit
25e243ad4e
7 changed files with 97 additions and 79 deletions
|
@ -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"] }
|
||||
|
|
|
@ -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)'] }
|
||||
|
@ -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 }
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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<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
runtime/transpile.rs
Normal file
78
runtime/transpile.rs
Normal file
|
@ -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))
|
||||
}
|
|
@ -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),
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Add table
Reference in a new issue