2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-08-28 17:30:46 -04:00
|
|
|
// Utilities shared between `build.rs` and the rest of the crate.
|
|
|
|
|
|
|
|
use deno_ast::MediaType;
|
|
|
|
use deno_ast::ParseParams;
|
|
|
|
use deno_ast::SourceTextInfo;
|
|
|
|
use deno_core::error::AnyError;
|
|
|
|
use deno_core::extension;
|
|
|
|
use deno_core::Extension;
|
2024-03-04 20:17:39 -05:00
|
|
|
use deno_core::ModuleCodeString;
|
|
|
|
use deno_core::ModuleName;
|
|
|
|
use deno_core::SourceMapData;
|
2023-08-28 17:30:46 -04:00
|
|
|
use std::path::Path;
|
|
|
|
|
|
|
|
extension!(runtime,
|
|
|
|
deps = [
|
|
|
|
deno_webidl,
|
|
|
|
deno_console,
|
|
|
|
deno_url,
|
|
|
|
deno_tls,
|
|
|
|
deno_web,
|
|
|
|
deno_fetch,
|
|
|
|
deno_cache,
|
|
|
|
deno_websocket,
|
|
|
|
deno_webstorage,
|
|
|
|
deno_crypto,
|
|
|
|
deno_broadcast_channel,
|
|
|
|
deno_node,
|
|
|
|
deno_ffi,
|
|
|
|
deno_net,
|
|
|
|
deno_napi,
|
|
|
|
deno_http,
|
|
|
|
deno_io,
|
|
|
|
deno_fs
|
|
|
|
],
|
|
|
|
esm_entry_point = "ext:runtime/90_deno_ns.js",
|
|
|
|
esm = [
|
|
|
|
dir "js",
|
|
|
|
"01_errors.js",
|
|
|
|
"01_version.ts",
|
|
|
|
"06_util.js",
|
|
|
|
"10_permissions.js",
|
|
|
|
"11_workers.js",
|
|
|
|
"13_buffer.js",
|
|
|
|
"30_os.js",
|
|
|
|
"40_fs_events.js",
|
|
|
|
"40_http.js",
|
|
|
|
"40_process.js",
|
|
|
|
"40_signals.js",
|
|
|
|
"40_tty.js",
|
|
|
|
"41_prompt.js",
|
|
|
|
"90_deno_ns.js",
|
2024-01-06 08:40:26 -05:00
|
|
|
"98_global_scope_shared.js",
|
|
|
|
"98_global_scope_window.js",
|
|
|
|
"98_global_scope_worker.js"
|
2023-08-28 17:30:46 -04:00
|
|
|
],
|
|
|
|
customizer = |ext: &mut Extension| {
|
|
|
|
#[cfg(not(feature = "exclude_runtime_main_js"))]
|
|
|
|
{
|
2024-03-04 20:17:39 -05:00
|
|
|
use deno_core::ascii_str_include;
|
|
|
|
use deno_core::ExtensionFileSource;
|
|
|
|
ext.esm_files.to_mut().push(ExtensionFileSource::new("ext:runtime_main/js/99_main.js", ascii_str_include!("./js/99_main.js")));
|
2023-08-28 17:30:46 -04:00
|
|
|
ext.esm_entry_point = Some("ext:runtime_main/js/99_main.js");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
pub fn maybe_transpile_source(
|
2024-03-04 20:17:39 -05:00
|
|
|
name: ModuleName,
|
|
|
|
source: ModuleCodeString,
|
|
|
|
) -> Result<(ModuleCodeString, Option<SourceMapData>), AnyError> {
|
2023-08-28 17:30:46 -04:00
|
|
|
// Always transpile `node:` built-in modules, since they might be TypeScript.
|
2024-03-04 20:17:39 -05:00
|
|
|
let media_type = if name.starts_with("node:") {
|
2023-08-28 17:30:46 -04:00
|
|
|
MediaType::TypeScript
|
|
|
|
} else {
|
2024-03-04 20:17:39 -05:00
|
|
|
MediaType::from_path(Path::new(&name))
|
2023-08-28 17:30:46 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
match media_type {
|
|
|
|
MediaType::TypeScript => {}
|
2024-03-04 20:17:39 -05:00
|
|
|
MediaType::JavaScript => return Ok((source, None)),
|
|
|
|
MediaType::Mjs => return Ok((source, None)),
|
2023-08-28 17:30:46 -04:00
|
|
|
_ => panic!(
|
|
|
|
"Unsupported media type for snapshotting {media_type:?} for file {}",
|
2024-03-04 20:17:39 -05:00
|
|
|
name
|
2023-08-28 17:30:46 -04:00
|
|
|
),
|
|
|
|
}
|
|
|
|
|
|
|
|
let parsed = deno_ast::parse_module(ParseParams {
|
2024-03-04 20:17:39 -05:00
|
|
|
specifier: deno_core::url::Url::parse(&name).unwrap(),
|
|
|
|
text_info: SourceTextInfo::from_string(source.as_str().to_owned()),
|
2023-08-28 17:30:46 -04:00
|
|
|
media_type,
|
|
|
|
capture_tokens: false,
|
|
|
|
scope_analysis: false,
|
|
|
|
maybe_syntax: None,
|
|
|
|
})?;
|
|
|
|
let transpiled_source = parsed.transpile(&deno_ast::EmitOptions {
|
|
|
|
imports_not_used_as_values: deno_ast::ImportsNotUsedAsValues::Remove,
|
|
|
|
inline_source_map: false,
|
|
|
|
..Default::default()
|
|
|
|
})?;
|
|
|
|
|
2024-03-04 20:17:39 -05:00
|
|
|
Ok((transpiled_source.text.into(), None))
|
2023-08-28 17:30:46 -04:00
|
|
|
}
|