1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 07:14:47 -05:00

refactor: Remove cli::compilers module (#5138)

This PR removes "cli/compilers/" directory.

"cli/compilers/ts.rs" has been renamed to "cli/tsc.rs"
This commit is contained in:
Bartek Iwańczuk 2020-05-08 16:18:00 +02:00 committed by GitHub
parent 6b73e0caff
commit f9f10229a4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 70 additions and 129 deletions

View file

@ -1,66 +0,0 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
use crate::ops;
use crate::state::State;
use crate::web_worker::WebWorker;
use core::task::Context;
use deno_core::ErrBox;
use deno_core::StartupData;
use futures::future::Future;
use futures::future::FutureExt;
use std::ops::Deref;
use std::ops::DerefMut;
use std::pin::Pin;
use std::task::Poll;
/// This worker is used to host TypeScript and WASM compilers.
///
/// It provides minimal set of ops that are necessary to facilitate
/// compilation.
///
/// NOTE: This worker is considered priveleged, because it may
/// access file system without permission check.
///
/// At the moment this worker is meant to be single-use - after
/// performing single compilation/bundling it should be destroyed.
///
/// TODO(bartlomieju): add support to reuse the worker - or in other
/// words support stateful TS compiler
pub struct CompilerWorker(WebWorker);
impl CompilerWorker {
pub fn new(name: String, startup_data: StartupData, state: State) -> Self {
let state_ = state.clone();
let mut worker = WebWorker::new(name, startup_data, state_, false);
{
let isolate = &mut worker.isolate;
ops::compiler::init(isolate, &state);
// TODO(bartlomieju): CompilerWorker should not
// depend on those ops
ops::os::init(isolate, &state);
ops::fs::init(isolate, &state);
}
Self(worker)
}
}
impl Deref for CompilerWorker {
type Target = WebWorker;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for CompilerWorker {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl Future for CompilerWorker {
type Output = Result<(), ErrBox>;
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
let inner = self.get_mut();
inner.0.poll_unpin(cx)
}
}

View file

@ -1,21 +0,0 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
use crate::compilers::CompiledModule;
use crate::file_fetcher::SourceFile;
use deno_core::ErrBox;
use std::str;
pub struct JsCompiler {}
impl JsCompiler {
pub async fn compile(
&self,
source_file: SourceFile,
) -> Result<CompiledModule, ErrBox> {
Ok(CompiledModule {
code: str::from_utf8(&source_file.source_code)
.unwrap()
.to_string(),
name: source_file.url.to_string(),
})
}
}

View file

@ -1,25 +0,0 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
use crate::ops::JsonResult;
use deno_core::ErrBox;
use futures::Future;
mod compiler_worker;
mod js;
mod ts;
pub use js::JsCompiler;
pub use ts::runtime_compile;
pub use ts::runtime_transpile;
pub use ts::TargetLib;
pub use ts::TsCompiler;
pub type CompilationResultFuture = dyn Future<Output = JsonResult>;
#[derive(Debug, Clone)]
pub struct CompiledModule {
pub code: String,
pub name: String,
}
pub type CompiledModuleFuture =
dyn Future<Output = Result<CompiledModule, ErrBox>>;

View file

@ -1,8 +1,4 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
use crate::compilers::CompiledModule;
use crate::compilers::JsCompiler;
use crate::compilers::TargetLib;
use crate::compilers::TsCompiler;
use crate::deno_dir;
use crate::file_fetcher::SourceFileFetcher;
use crate::flags;
@ -10,6 +6,9 @@ use crate::http_cache;
use crate::lockfile::Lockfile;
use crate::msg;
use crate::permissions::Permissions;
use crate::tsc::CompiledModule;
use crate::tsc::TargetLib;
use crate::tsc::TsCompiler;
use deno_core::ErrBox;
use deno_core::ModuleSpecifier;
use std::env;
@ -33,7 +32,6 @@ pub struct GlobalStateInner {
pub permissions: Permissions,
pub dir: deno_dir::DenoDir,
pub file_fetcher: SourceFileFetcher,
pub js_compiler: JsCompiler,
pub ts_compiler: TsCompiler,
pub lockfile: Option<Mutex<Lockfile>>,
pub compiler_starts: AtomicUsize,
@ -84,7 +82,6 @@ impl GlobalState {
flags,
file_fetcher,
ts_compiler,
js_compiler: JsCompiler {},
lockfile,
compiler_starts: AtomicUsize::new(0),
compile_lock: AsyncMutex::new(()),
@ -140,10 +137,16 @@ impl GlobalState {
.ok();
};
state1.js_compiler.compile(out).await
Ok(CompiledModule {
code: String::from_utf8(out.source_code)?,
name: out.url.to_string(),
})
}
}
_ => state1.js_compiler.compile(out).await,
_ => Ok(CompiledModule {
code: String::from_utf8(out.source_code)?,
name: out.url.to_string(),
}),
}?;
drop(compile_lock);

View file

@ -23,7 +23,6 @@ extern crate url;
mod checksum;
pub mod colors;
pub mod compilers;
pub mod deno_dir;
pub mod diagnostics;
mod disk_cache;
@ -57,6 +56,7 @@ mod swc_util;
mod test_runner;
pub mod test_util;
mod tokio_util;
mod tsc;
mod upgrade;
pub mod version;
mod web_worker;
@ -66,7 +66,6 @@ pub use dprint_plugin_typescript::swc_common;
pub use dprint_plugin_typescript::swc_ecma_ast;
pub use dprint_plugin_typescript::swc_ecma_parser;
use crate::compilers::TargetLib;
use crate::doc::parser::DocFileLoader;
use crate::file_fetcher::SourceFile;
use crate::file_fetcher::SourceFileFetcher;
@ -76,6 +75,7 @@ use crate::op_error::OpError;
use crate::ops::io::get_stdio;
use crate::state::DebugType;
use crate::state::State;
use crate::tsc::TargetLib;
use crate::worker::MainWorker;
use deno_core::v8_set_flags;
use deno_core::ErrBox;

View file

@ -1,4 +1,4 @@
use crate::compilers::CompiledModule;
use crate::tsc::CompiledModule;
use serde_json::json;
pub use serde_json::Value;
use std::collections::HashMap;

View file

@ -1,10 +1,10 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
use super::dispatch_json::{Deserialize, JsonOp, Value};
use crate::compilers::runtime_compile;
use crate::compilers::runtime_transpile;
use crate::futures::FutureExt;
use crate::op_error::OpError;
use crate::state::State;
use crate::tsc::runtime_compile;
use crate::tsc::runtime_transpile;
use deno_core::CoreIsolate;
use deno_core::ZeroCopyBuf;
use std::collections::HashMap;

View file

@ -1,5 +1,4 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
use crate::compilers::TargetLib;
use crate::file_fetcher::SourceFileFetcher;
use crate::global_state::GlobalState;
use crate::global_timer::GlobalTimer;
@ -9,6 +8,7 @@ use crate::op_error::OpError;
use crate::ops::JsonOp;
use crate::ops::MinimalOp;
use crate::permissions::Permissions;
use crate::tsc::TargetLib;
use crate::web_worker::WebWorkerHandle;
use deno_core::Buf;
use deno_core::ErrBox;

View file

@ -1,7 +1,5 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
use super::compiler_worker::CompilerWorker;
use crate::colors;
use crate::compilers::CompiledModule;
use crate::diagnostics::Diagnostic;
use crate::diagnostics::DiagnosticItem;
use crate::disk_cache::DiskCache;
@ -12,16 +10,23 @@ use crate::fs as deno_fs;
use crate::global_state::GlobalState;
use crate::msg;
use crate::op_error::OpError;
use crate::ops;
use crate::source_maps::SourceMapGetter;
use crate::startup_data;
use crate::state::State;
use crate::state::*;
use crate::tokio_util;
use crate::version;
use crate::web_worker::WebWorker;
use crate::web_worker::WebWorkerHandle;
use crate::worker::WorkerEvent;
use core::task::Context;
use deno_core::Buf;
use deno_core::ErrBox;
use deno_core::ModuleSpecifier;
use deno_core::StartupData;
use futures::future::Future;
use futures::future::FutureExt;
use log::info;
use regex::Regex;
use serde::Deserialize;
@ -33,13 +38,58 @@ use std::fs;
use std::hash::BuildHasher;
use std::io;
use std::ops::Deref;
use std::ops::DerefMut;
use std::path::PathBuf;
use std::pin::Pin;
use std::str;
use std::sync::atomic::Ordering;
use std::sync::Arc;
use std::sync::Mutex;
use std::task::Poll;
use url::Url;
#[derive(Debug, Clone)]
pub struct CompiledModule {
pub code: String,
pub name: String,
}
pub struct CompilerWorker(WebWorker);
impl CompilerWorker {
pub fn new(name: String, startup_data: StartupData, state: State) -> Self {
let state_ = state.clone();
let mut worker = WebWorker::new(name, startup_data, state_, false);
{
let isolate = &mut worker.isolate;
ops::compiler::init(isolate, &state);
}
Self(worker)
}
}
impl Deref for CompilerWorker {
type Target = WebWorker;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for CompilerWorker {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl Future for CompilerWorker {
type Output = Result<(), ErrBox>;
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
let inner = self.get_mut();
inner.0.poll_unpin(cx)
}
}
lazy_static! {
static ref CHECK_JS_RE: Regex =
Regex::new(r#""checkJs"\s*?:\s*?true"#).unwrap();
@ -805,7 +855,7 @@ mod tests {
url: specifier.as_url().clone(),
filename: PathBuf::from(p.to_str().unwrap().to_string()),
media_type: msg::MediaType::TypeScript,
source_code: include_bytes!("../tests/002_hello.ts").to_vec(),
source_code: include_bytes!("./tests/002_hello.ts").to_vec(),
types_url: None,
};
let mock_state =