refactor(cli): remove 'js' module, simplify compiler snapshot (#9020)

This commit removes "js" module from "cli".

It contained stuff related to TypeScript compiler (snapshot,
declaration files) and thus it was moved to "tsc" module.
This commit is contained in:
Bartek Iwańczuk 2021-01-06 02:38:23 +01:00 committed by GitHub
parent 4c4791b589
commit bb88418221
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 63 additions and 80 deletions

View File

@ -1,40 +0,0 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
use deno_core::Snapshot;
pub const TS_VERSION: &str = env!("TS_VERSION");
pub static COMPILER_SNAPSHOT: &[u8] =
include_bytes!(concat!(env!("OUT_DIR"), "/COMPILER_SNAPSHOT.bin"));
pub static DENO_NS_LIB: &str = include_str!("dts/lib.deno.ns.d.ts");
pub static DENO_WEB_LIB: &str = include_str!(env!("DENO_WEB_LIB_PATH"));
pub static DENO_FETCH_LIB: &str = include_str!(env!("DENO_FETCH_LIB_PATH"));
pub static SHARED_GLOBALS_LIB: &str =
include_str!("dts/lib.deno.shared_globals.d.ts");
pub static WINDOW_LIB: &str = include_str!("dts/lib.deno.window.d.ts");
pub static UNSTABLE_NS_LIB: &str = include_str!("dts/lib.deno.unstable.d.ts");
pub fn compiler_isolate_init() -> Snapshot {
debug!("Deno compiler isolate init with snapshots.");
let data = COMPILER_SNAPSHOT;
Snapshot::Static(data)
}
#[test]
fn compiler_snapshot() {
let mut js_runtime = deno_core::JsRuntime::new(deno_core::RuntimeOptions {
startup_snapshot: Some(compiler_isolate_init()),
..Default::default()
});
js_runtime
.execute(
"<anon>",
r#"
if (!(startup)) {
throw Error("bad");
}
console.log(`ts version: ${ts.version}`);
"#,
)
.unwrap();
}

View File

@ -5,7 +5,6 @@ use super::language_server::StateSnapshot;
use super::text;
use super::utils;
use crate::js;
use crate::media_type::MediaType;
use crate::tokio_util::create_basic_runtime;
use crate::tsc;
@ -1025,7 +1024,7 @@ fn set_asset(state: &mut State, args: Value) -> Result<Value, AnyError> {
/// server.
pub fn start(debug: bool) -> Result<JsRuntime, AnyError> {
let mut runtime = JsRuntime::new(RuntimeOptions {
startup_snapshot: Some(js::compiler_isolate_init()),
startup_snapshot: Some(tsc::compiler_snapshot()),
..Default::default()
});

View File

@ -25,7 +25,6 @@ mod http_cache;
mod http_util;
mod import_map;
mod info;
mod js;
mod lockfile;
mod lsp;
mod media_type;
@ -277,15 +276,15 @@ fn print_cache_info(
fn get_types(unstable: bool) -> String {
let mut types = format!(
"{}\n{}\n{}\n{}\n{}",
crate::js::DENO_NS_LIB,
crate::js::DENO_WEB_LIB,
crate::js::DENO_FETCH_LIB,
crate::js::SHARED_GLOBALS_LIB,
crate::js::WINDOW_LIB,
crate::tsc::DENO_NS_LIB,
crate::tsc::DENO_WEB_LIB,
crate::tsc::DENO_FETCH_LIB,
crate::tsc::SHARED_GLOBALS_LIB,
crate::tsc::WINDOW_LIB,
);
if unstable {
types.push_str(&format!("\n{}", crate::js::UNSTABLE_NS_LIB,));
types.push_str(&format!("\n{}", crate::tsc::UNSTABLE_NS_LIB,));
}
types

View File

@ -13,7 +13,6 @@ use crate::info::ModuleGraphInfo;
use crate::info::ModuleInfo;
use crate::info::ModuleInfoMap;
use crate::info::ModuleInfoMapItem;
use crate::js;
use crate::lockfile::Lockfile;
use crate::media_type::MediaType;
use crate::specifier_handler::CachedModule;
@ -855,17 +854,14 @@ impl Graph {
vec![config.as_bytes(), version::deno().as_bytes().to_owned()];
let graph = Arc::new(Mutex::new(self));
let response = tsc::exec(
js::compiler_isolate_init(),
tsc::Request {
config: config.clone(),
debug: options.debug,
graph: graph.clone(),
hash_data,
maybe_tsbuildinfo,
root_names,
},
)?;
let response = tsc::exec(tsc::Request {
config: config.clone(),
debug: options.debug,
graph: graph.clone(),
hash_data,
maybe_tsbuildinfo,
root_names,
})?;
let mut graph = graph.lock().unwrap();
graph.maybe_tsbuildinfo = response.maybe_tsbuildinfo;
@ -983,17 +979,14 @@ impl Graph {
let hash_data =
vec![config.as_bytes(), version::deno().as_bytes().to_owned()];
let graph = Arc::new(Mutex::new(self));
let response = tsc::exec(
js::compiler_isolate_init(),
tsc::Request {
config: config.clone(),
debug: options.debug,
graph: graph.clone(),
hash_data,
maybe_tsbuildinfo: None,
root_names,
},
)?;
let response = tsc::exec(tsc::Request {
config: config.clone(),
debug: options.debug,
graph: graph.clone(),
hash_data,
maybe_tsbuildinfo: None,
root_names,
})?;
let graph = graph.lock().unwrap();
match options.bundle_type {

View File

@ -25,6 +25,23 @@ use std::path::PathBuf;
use std::sync::Arc;
use std::sync::Mutex;
// Declaration files
pub static DENO_NS_LIB: &str = include_str!("dts/lib.deno.ns.d.ts");
pub static DENO_WEB_LIB: &str = include_str!(env!("DENO_WEB_LIB_PATH"));
pub static DENO_FETCH_LIB: &str = include_str!(env!("DENO_FETCH_LIB_PATH"));
pub static SHARED_GLOBALS_LIB: &str =
include_str!("dts/lib.deno.shared_globals.d.ts");
pub static WINDOW_LIB: &str = include_str!("dts/lib.deno.window.d.ts");
pub static UNSTABLE_NS_LIB: &str = include_str!("dts/lib.deno.unstable.d.ts");
pub static COMPILER_SNAPSHOT: &[u8] =
include_bytes!(concat!(env!("OUT_DIR"), "/COMPILER_SNAPSHOT.bin"));
pub fn compiler_snapshot() -> Snapshot {
Snapshot::Static(COMPILER_SNAPSHOT)
}
/// Provide static assets that are not preloaded in the compiler snapshot.
pub fn get_asset(asset: &str) -> Option<&'static str> {
macro_rules! inc {
@ -357,12 +374,9 @@ fn respond(state: &mut State, args: Value) -> Result<Value, AnyError> {
/// Execute a request on the supplied snapshot, returning a response which
/// contains information, like any emitted files, diagnostics, statistics and
/// optionally an updated TypeScript build info.
pub fn exec(
snapshot: Snapshot,
request: Request,
) -> Result<Response, AnyError> {
pub fn exec(request: Request) -> Result<Response, AnyError> {
let mut runtime = JsRuntime::new(RuntimeOptions {
startup_snapshot: Some(snapshot),
startup_snapshot: Some(compiler_snapshot()),
..Default::default()
});
// tsc cannot handle root specifiers that don't have one of the "acceptable"
@ -442,7 +456,6 @@ mod tests {
use super::*;
use crate::diagnostics::Diagnostic;
use crate::diagnostics::DiagnosticCategory;
use crate::js;
use crate::module_graph::tests::MockSpecifierHandler;
use crate::module_graph::GraphBuilder;
use crate::tsc_config::TsConfig;
@ -512,7 +525,26 @@ mod tests {
maybe_tsbuildinfo: None,
root_names: vec![(specifier.clone(), MediaType::TypeScript)],
};
exec(js::compiler_isolate_init(), request)
exec(request)
}
#[test]
fn test_compiler_snapshot() {
let mut js_runtime = deno_core::JsRuntime::new(deno_core::RuntimeOptions {
startup_snapshot: Some(compiler_snapshot()),
..Default::default()
});
js_runtime
.execute(
"<anon>",
r#"
if (!(startup)) {
throw Error("bad");
}
console.log(`ts version: ${ts.version}`);
"#,
)
.unwrap();
}
#[tokio::test]

View File

@ -1,7 +1,7 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
pub const GIT_COMMIT_HASH: &str = env!("GIT_COMMIT_HASH");
pub const TYPESCRIPT: &str = crate::js::TS_VERSION;
pub const TYPESCRIPT: &str = env!("TS_VERSION");
pub fn deno() -> String {
let semver = env!("CARGO_PKG_VERSION");