2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-11-04 10:38:52 -05:00
|
|
|
use crate::deno_dir;
|
|
|
|
use crate::file_fetcher::SourceFileFetcher;
|
|
|
|
use crate::flags;
|
2020-02-19 08:17:13 -05:00
|
|
|
use crate::http_cache;
|
2020-05-29 10:32:15 -04:00
|
|
|
use crate::import_map::ImportMap;
|
2019-11-04 10:38:52 -05:00
|
|
|
use crate::lockfile::Lockfile;
|
2020-06-01 15:01:51 -04:00
|
|
|
use crate::module_graph::ModuleGraphFile;
|
2020-05-29 10:32:15 -04:00
|
|
|
use crate::module_graph::ModuleGraphLoader;
|
2019-11-04 10:38:52 -05:00
|
|
|
use crate::msg;
|
2020-06-01 15:01:51 -04:00
|
|
|
use crate::msg::MediaType;
|
2020-06-15 11:53:05 -04:00
|
|
|
use crate::op_error::OpError;
|
2020-05-04 14:10:59 -04:00
|
|
|
use crate::permissions::Permissions;
|
2020-05-29 10:32:15 -04:00
|
|
|
use crate::state::exit_unstable;
|
2020-05-08 10:18:00 -04:00
|
|
|
use crate::tsc::CompiledModule;
|
|
|
|
use crate::tsc::TargetLib;
|
|
|
|
use crate::tsc::TsCompiler;
|
2020-01-05 11:56:18 -05:00
|
|
|
use deno_core::ErrBox;
|
|
|
|
use deno_core::ModuleSpecifier;
|
2019-11-04 10:38:52 -05:00
|
|
|
use std::env;
|
|
|
|
use std::ops::Deref;
|
2020-02-11 11:23:40 -05:00
|
|
|
use std::sync::atomic::AtomicUsize;
|
2019-11-04 10:38:52 -05:00
|
|
|
use std::sync::Arc;
|
|
|
|
use std::sync::Mutex;
|
2020-02-06 21:24:51 -05:00
|
|
|
use tokio::sync::Mutex as AsyncMutex;
|
2019-11-04 10:38:52 -05:00
|
|
|
|
|
|
|
/// Holds state of the program and can be accessed by V8 isolate.
|
2020-02-06 21:24:51 -05:00
|
|
|
#[derive(Clone)]
|
2020-02-06 23:05:02 -05:00
|
|
|
pub struct GlobalState(Arc<GlobalStateInner>);
|
2019-11-04 10:38:52 -05:00
|
|
|
|
|
|
|
/// This structure represents state of single "deno" program.
|
|
|
|
///
|
|
|
|
/// It is shared by all created workers (thus V8 isolates).
|
2020-02-06 23:05:02 -05:00
|
|
|
pub struct GlobalStateInner {
|
2019-11-04 10:38:52 -05:00
|
|
|
/// Flags parsed from `argv` contents.
|
2020-02-26 05:52:15 -05:00
|
|
|
pub flags: flags::Flags,
|
2019-11-04 10:38:52 -05:00
|
|
|
/// Permissions parsed from `flags`.
|
2020-05-04 14:10:59 -04:00
|
|
|
pub permissions: Permissions,
|
2019-11-04 10:38:52 -05:00
|
|
|
pub dir: deno_dir::DenoDir,
|
|
|
|
pub file_fetcher: SourceFileFetcher,
|
|
|
|
pub ts_compiler: TsCompiler,
|
|
|
|
pub lockfile: Option<Mutex<Lockfile>>,
|
2020-02-11 11:23:40 -05:00
|
|
|
pub compiler_starts: AtomicUsize,
|
2020-05-29 10:32:15 -04:00
|
|
|
pub maybe_import_map: Option<ImportMap>,
|
2020-02-06 21:24:51 -05:00
|
|
|
compile_lock: AsyncMutex<()>,
|
2019-11-04 10:38:52 -05:00
|
|
|
}
|
|
|
|
|
2020-02-06 23:05:02 -05:00
|
|
|
impl Deref for GlobalState {
|
|
|
|
type Target = GlobalStateInner;
|
2019-11-04 10:38:52 -05:00
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-06 23:05:02 -05:00
|
|
|
impl GlobalState {
|
2020-02-26 05:52:15 -05:00
|
|
|
pub fn new(flags: flags::Flags) -> Result<Self, ErrBox> {
|
2019-11-04 10:38:52 -05:00
|
|
|
let custom_root = env::var("DENO_DIR").map(String::into).ok();
|
|
|
|
let dir = deno_dir::DenoDir::new(custom_root)?;
|
2020-02-19 08:17:13 -05:00
|
|
|
let deps_cache_location = dir.root.join("deps");
|
2020-05-07 08:32:57 -04:00
|
|
|
let http_cache = http_cache::HttpCache::new(&deps_cache_location);
|
2019-11-04 10:38:52 -05:00
|
|
|
|
|
|
|
let file_fetcher = SourceFileFetcher::new(
|
2020-02-19 08:17:13 -05:00
|
|
|
http_cache,
|
2019-11-04 10:38:52 -05:00
|
|
|
!flags.reload,
|
2020-06-13 13:09:39 -04:00
|
|
|
flags.cache_blocklist.clone(),
|
2019-12-03 17:48:53 -05:00
|
|
|
flags.no_remote,
|
|
|
|
flags.cached_only,
|
2020-02-17 11:59:51 -05:00
|
|
|
flags.ca_file.clone(),
|
2019-11-04 10:38:52 -05:00
|
|
|
)?;
|
|
|
|
|
|
|
|
let ts_compiler = TsCompiler::new(
|
|
|
|
file_fetcher.clone(),
|
|
|
|
dir.gen_cache.clone(),
|
|
|
|
!flags.reload,
|
|
|
|
flags.config_path.clone(),
|
|
|
|
)?;
|
|
|
|
|
|
|
|
// Note: reads lazily from disk on first call to lockfile.check()
|
|
|
|
let lockfile = if let Some(filename) = &flags.lock {
|
|
|
|
Some(Mutex::new(Lockfile::new(filename.to_string())))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2020-05-29 10:32:15 -04:00
|
|
|
let maybe_import_map: Option<ImportMap> =
|
|
|
|
match flags.import_map_path.as_ref() {
|
|
|
|
None => None,
|
|
|
|
Some(file_path) => {
|
|
|
|
if !flags.unstable {
|
|
|
|
exit_unstable("--importmap")
|
|
|
|
}
|
|
|
|
Some(ImportMap::load(file_path)?)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-02-06 23:05:02 -05:00
|
|
|
let inner = GlobalStateInner {
|
2019-11-04 10:38:52 -05:00
|
|
|
dir,
|
2020-05-04 14:10:59 -04:00
|
|
|
permissions: Permissions::from_flags(&flags),
|
2019-11-04 10:38:52 -05:00
|
|
|
flags,
|
|
|
|
file_fetcher,
|
|
|
|
ts_compiler,
|
|
|
|
lockfile,
|
2020-05-29 10:32:15 -04:00
|
|
|
maybe_import_map,
|
2020-02-11 11:23:40 -05:00
|
|
|
compiler_starts: AtomicUsize::new(0),
|
2020-02-06 21:24:51 -05:00
|
|
|
compile_lock: AsyncMutex::new(()),
|
2019-11-04 10:38:52 -05:00
|
|
|
};
|
2020-02-06 23:05:02 -05:00
|
|
|
Ok(GlobalState(Arc::new(inner)))
|
2019-11-04 10:38:52 -05:00
|
|
|
}
|
|
|
|
|
2020-05-29 10:32:15 -04:00
|
|
|
/// This function is called when new module load is
|
|
|
|
/// initialized by the EsIsolate. Its resposibility is to collect
|
|
|
|
/// all dependencies and if it is required then also perform TS typecheck
|
|
|
|
/// and traspilation.
|
|
|
|
pub async fn prepare_module_load(
|
2019-12-14 17:12:34 -05:00
|
|
|
&self,
|
2020-02-03 18:08:44 -05:00
|
|
|
module_specifier: ModuleSpecifier,
|
2019-11-25 09:33:23 -05:00
|
|
|
maybe_referrer: Option<ModuleSpecifier>,
|
2020-01-29 12:54:23 -05:00
|
|
|
target_lib: TargetLib,
|
2020-05-11 07:13:27 -04:00
|
|
|
permissions: Permissions,
|
2020-05-18 06:59:29 -04:00
|
|
|
is_dyn_import: bool,
|
2020-05-29 10:32:15 -04:00
|
|
|
maybe_import_map: Option<ImportMap>,
|
|
|
|
) -> Result<(), ErrBox> {
|
|
|
|
let module_specifier = module_specifier.clone();
|
|
|
|
|
|
|
|
// TODO(ry) Try to lift compile_lock as high up in the call stack for
|
|
|
|
// sanity.
|
|
|
|
let compile_lock = self.compile_lock.lock().await;
|
|
|
|
|
|
|
|
let mut module_graph_loader = ModuleGraphLoader::new(
|
|
|
|
self.file_fetcher.clone(),
|
|
|
|
maybe_import_map,
|
|
|
|
permissions.clone(),
|
|
|
|
is_dyn_import,
|
|
|
|
false,
|
|
|
|
);
|
|
|
|
module_graph_loader
|
|
|
|
.add_to_graph(&module_specifier, maybe_referrer)
|
|
|
|
.await?;
|
|
|
|
let module_graph = module_graph_loader.get_graph();
|
|
|
|
|
|
|
|
let out = self
|
|
|
|
.file_fetcher
|
|
|
|
.fetch_cached_source_file(&module_specifier, permissions.clone())
|
|
|
|
.expect("Source file not found");
|
|
|
|
|
2020-06-01 15:01:51 -04:00
|
|
|
// Check if we need to compile files.
|
2020-06-10 10:02:41 -04:00
|
|
|
let module_graph_files = module_graph.values().collect::<Vec<_>>();
|
2020-06-01 15:01:51 -04:00
|
|
|
let should_compile = needs_compilation(
|
|
|
|
self.ts_compiler.compile_js,
|
|
|
|
out.media_type,
|
2020-06-10 10:02:41 -04:00
|
|
|
&module_graph_files,
|
2020-06-01 15:01:51 -04:00
|
|
|
);
|
2020-06-10 10:02:41 -04:00
|
|
|
let allow_js = should_allow_js(&module_graph_files);
|
2020-05-29 10:32:15 -04:00
|
|
|
|
2020-06-01 15:01:51 -04:00
|
|
|
if should_compile {
|
2020-05-29 10:32:15 -04:00
|
|
|
self
|
|
|
|
.ts_compiler
|
|
|
|
.compile_module_graph(
|
|
|
|
self.clone(),
|
|
|
|
&out,
|
|
|
|
target_lib,
|
|
|
|
permissions,
|
|
|
|
module_graph,
|
2020-06-10 10:02:41 -04:00
|
|
|
allow_js,
|
2020-05-29 10:32:15 -04:00
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
|
|
|
|
drop(compile_lock);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(bartlomieju): this method doesn't need to be async anymore
|
|
|
|
/// This method is used after `prepare_module_load` finishes and EsIsolate
|
|
|
|
/// starts loading source and executing source code. This method shouldn't
|
|
|
|
/// perform any IO (besides $DENO_DIR) and only operate on sources collected
|
|
|
|
/// during `prepare_module_load`.
|
|
|
|
pub async fn fetch_compiled_module(
|
|
|
|
&self,
|
|
|
|
module_specifier: ModuleSpecifier,
|
|
|
|
_maybe_referrer: Option<ModuleSpecifier>,
|
2020-02-03 18:08:44 -05:00
|
|
|
) -> Result<CompiledModule, ErrBox> {
|
2019-11-04 10:38:52 -05:00
|
|
|
let state1 = self.clone();
|
|
|
|
let state2 = self.clone();
|
2020-02-03 18:08:44 -05:00
|
|
|
let module_specifier = module_specifier.clone();
|
2019-11-04 10:38:52 -05:00
|
|
|
|
2020-02-03 18:08:44 -05:00
|
|
|
let out = self
|
2019-11-04 10:38:52 -05:00
|
|
|
.file_fetcher
|
2020-05-29 10:32:15 -04:00
|
|
|
.fetch_cached_source_file(&module_specifier, Permissions::allow_all())
|
|
|
|
.expect("Cached source file doesn't exist");
|
2020-02-06 21:24:51 -05:00
|
|
|
|
|
|
|
// TODO(ry) Try to lift compile_lock as high up in the call stack for
|
|
|
|
// sanity.
|
|
|
|
let compile_lock = self.compile_lock.lock().await;
|
|
|
|
|
2020-05-29 10:32:15 -04:00
|
|
|
// Check if we need to compile files
|
|
|
|
let was_compiled = match out.media_type {
|
2020-02-03 18:08:44 -05:00
|
|
|
msg::MediaType::TypeScript
|
|
|
|
| msg::MediaType::TSX
|
2020-05-29 10:32:15 -04:00
|
|
|
| msg::MediaType::JSX => true,
|
|
|
|
msg::MediaType::JavaScript => self.ts_compiler.compile_js,
|
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
|
|
|
|
let compiled_module = if was_compiled {
|
2020-06-15 11:53:05 -04:00
|
|
|
state1
|
|
|
|
.ts_compiler
|
|
|
|
.get_compiled_module(&out.url)
|
|
|
|
.map_err(|e| {
|
|
|
|
let msg = e.to_string();
|
|
|
|
OpError::other(format!(
|
|
|
|
"Failed to get compiled source code of {}.\nReason: {}",
|
|
|
|
out.url, msg
|
|
|
|
))
|
|
|
|
})?
|
2020-05-29 10:32:15 -04:00
|
|
|
} else {
|
|
|
|
CompiledModule {
|
2020-05-11 17:48:36 -04:00
|
|
|
code: String::from_utf8(out.source_code.clone())?,
|
2020-05-08 10:18:00 -04:00
|
|
|
name: out.url.to_string(),
|
2020-05-29 10:32:15 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-02-06 21:24:51 -05:00
|
|
|
drop(compile_lock);
|
2020-02-03 18:08:44 -05:00
|
|
|
|
|
|
|
if let Some(ref lockfile) = state2.lockfile {
|
|
|
|
let mut g = lockfile.lock().unwrap();
|
|
|
|
if state2.flags.lock_write {
|
2020-05-11 17:48:36 -04:00
|
|
|
g.insert(&out.url, out.source_code);
|
2020-02-03 18:08:44 -05:00
|
|
|
} else {
|
2020-05-11 17:48:36 -04:00
|
|
|
let check = match g.check(&out.url, out.source_code) {
|
2020-02-03 18:08:44 -05:00
|
|
|
Err(e) => return Err(ErrBox::from(e)),
|
|
|
|
Ok(v) => v,
|
|
|
|
};
|
|
|
|
if !check {
|
|
|
|
eprintln!(
|
|
|
|
"Subresource integrity check failed --lock={}\n{}",
|
|
|
|
g.filename, compiled_module.name
|
|
|
|
);
|
|
|
|
std::process::exit(10);
|
2019-11-04 10:38:52 -05:00
|
|
|
}
|
2019-12-14 17:12:34 -05:00
|
|
|
}
|
|
|
|
}
|
2020-02-03 18:08:44 -05:00
|
|
|
Ok(compiled_module)
|
2019-11-04 10:38:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2020-02-06 23:05:02 -05:00
|
|
|
pub fn mock(argv: Vec<String>) -> GlobalState {
|
2020-02-26 05:52:15 -05:00
|
|
|
GlobalState::new(flags::Flags {
|
2020-02-06 23:05:02 -05:00
|
|
|
argv,
|
2020-02-26 05:52:15 -05:00
|
|
|
..flags::Flags::default()
|
2020-02-06 23:05:02 -05:00
|
|
|
})
|
2019-11-04 10:38:52 -05:00
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-10 10:02:41 -04:00
|
|
|
/// Determine if TS compiler should be run with `allowJs` setting on. This
|
2020-06-15 11:53:05 -04:00
|
|
|
/// is the case when there's either:
|
|
|
|
/// - a JavaScript file with non-JavaScript import
|
|
|
|
/// - JSX import
|
2020-06-10 10:02:41 -04:00
|
|
|
fn should_allow_js(module_graph_files: &[&ModuleGraphFile]) -> bool {
|
|
|
|
module_graph_files.iter().any(|module_file| {
|
2020-06-19 06:27:15 -04:00
|
|
|
if module_file.media_type == MediaType::JSX {
|
2020-06-15 11:53:05 -04:00
|
|
|
true
|
2020-06-19 06:27:15 -04:00
|
|
|
} else if module_file.media_type == MediaType::JavaScript {
|
2020-06-10 10:02:41 -04:00
|
|
|
module_file.imports.iter().any(|import_desc| {
|
|
|
|
let import_file = module_graph_files
|
|
|
|
.iter()
|
|
|
|
.find(|f| {
|
|
|
|
f.specifier == import_desc.resolved_specifier.to_string().as_str()
|
|
|
|
})
|
|
|
|
.expect("Failed to find imported file");
|
|
|
|
let media_type = import_file.media_type;
|
2020-06-19 06:27:15 -04:00
|
|
|
media_type == MediaType::TypeScript
|
|
|
|
|| media_type == MediaType::TSX
|
|
|
|
|| media_type == MediaType::JSX
|
2020-06-10 10:02:41 -04:00
|
|
|
})
|
2020-06-15 11:53:05 -04:00
|
|
|
} else {
|
|
|
|
false
|
2020-06-10 10:02:41 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-06-01 15:01:51 -04:00
|
|
|
// Compilation happens if either:
|
|
|
|
// - `checkJs` is set to true in TS config
|
|
|
|
// - entry point is a TS file
|
|
|
|
// - any dependency in module graph is a TS file
|
|
|
|
fn needs_compilation(
|
|
|
|
compile_js: bool,
|
|
|
|
media_type: MediaType,
|
2020-06-10 10:02:41 -04:00
|
|
|
module_graph_files: &[&ModuleGraphFile],
|
2020-06-01 15:01:51 -04:00
|
|
|
) -> bool {
|
|
|
|
let mut needs_compilation = match media_type {
|
|
|
|
msg::MediaType::TypeScript | msg::MediaType::TSX | msg::MediaType::JSX => {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
msg::MediaType::JavaScript => compile_js,
|
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
|
|
|
|
needs_compilation |= module_graph_files.iter().any(|module_file| {
|
|
|
|
let media_type = module_file.media_type;
|
|
|
|
|
2020-06-19 06:27:15 -04:00
|
|
|
media_type == (MediaType::TypeScript)
|
|
|
|
|| media_type == (MediaType::TSX)
|
|
|
|
|| media_type == (MediaType::JSX)
|
2020-06-01 15:01:51 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
needs_compilation
|
|
|
|
}
|
|
|
|
|
2019-11-04 10:38:52 -05:00
|
|
|
#[test]
|
|
|
|
fn thread_safe() {
|
|
|
|
fn f<S: Send + Sync>(_: S) {}
|
2020-02-25 14:42:00 -05:00
|
|
|
f(GlobalState::mock(vec![]));
|
2019-11-04 10:38:52 -05:00
|
|
|
}
|
2020-06-01 15:01:51 -04:00
|
|
|
|
|
|
|
#[test]
|
2020-06-10 10:02:41 -04:00
|
|
|
fn test_should_allow_js() {
|
2020-06-19 06:27:15 -04:00
|
|
|
use crate::doc::Location;
|
2020-06-10 10:02:41 -04:00
|
|
|
use crate::module_graph::ImportDescriptor;
|
|
|
|
|
|
|
|
assert!(should_allow_js(&[
|
|
|
|
&ModuleGraphFile {
|
|
|
|
specifier: "file:///some/file.ts".to_string(),
|
|
|
|
url: "file:///some/file.ts".to_string(),
|
|
|
|
redirect: None,
|
|
|
|
filename: "some/file.ts".to_string(),
|
|
|
|
imports: vec![],
|
|
|
|
referenced_files: vec![],
|
|
|
|
lib_directives: vec![],
|
|
|
|
types_directives: vec![],
|
|
|
|
type_headers: vec![],
|
2020-06-19 06:27:15 -04:00
|
|
|
media_type: MediaType::TypeScript,
|
2020-06-10 10:02:41 -04:00
|
|
|
source_code: "function foo() {}".to_string(),
|
|
|
|
},
|
|
|
|
&ModuleGraphFile {
|
|
|
|
specifier: "file:///some/file1.js".to_string(),
|
|
|
|
url: "file:///some/file1.js".to_string(),
|
|
|
|
redirect: None,
|
|
|
|
filename: "some/file1.js".to_string(),
|
|
|
|
imports: vec![ImportDescriptor {
|
|
|
|
specifier: "./file.ts".to_string(),
|
|
|
|
resolved_specifier: ModuleSpecifier::resolve_url(
|
|
|
|
"file:///some/file.ts",
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
type_directive: None,
|
|
|
|
resolved_type_directive: None,
|
2020-06-19 06:27:15 -04:00
|
|
|
location: Location {
|
|
|
|
filename: "file:///some/file1.js".to_string(),
|
|
|
|
line: 0,
|
|
|
|
col: 0,
|
|
|
|
},
|
2020-06-10 10:02:41 -04:00
|
|
|
}],
|
|
|
|
referenced_files: vec![],
|
|
|
|
lib_directives: vec![],
|
|
|
|
types_directives: vec![],
|
|
|
|
type_headers: vec![],
|
2020-06-19 06:27:15 -04:00
|
|
|
media_type: MediaType::JavaScript,
|
2020-06-10 10:02:41 -04:00
|
|
|
source_code: "function foo() {}".to_string(),
|
|
|
|
},
|
|
|
|
],));
|
|
|
|
|
2020-06-15 11:53:05 -04:00
|
|
|
assert!(should_allow_js(&[
|
|
|
|
&ModuleGraphFile {
|
|
|
|
specifier: "file:///some/file.jsx".to_string(),
|
|
|
|
url: "file:///some/file.jsx".to_string(),
|
|
|
|
redirect: None,
|
|
|
|
filename: "some/file.jsx".to_string(),
|
|
|
|
imports: vec![],
|
|
|
|
referenced_files: vec![],
|
|
|
|
lib_directives: vec![],
|
|
|
|
types_directives: vec![],
|
|
|
|
type_headers: vec![],
|
2020-06-19 06:27:15 -04:00
|
|
|
media_type: MediaType::JSX,
|
2020-06-15 11:53:05 -04:00
|
|
|
source_code: "function foo() {}".to_string(),
|
|
|
|
},
|
|
|
|
&ModuleGraphFile {
|
|
|
|
specifier: "file:///some/file.ts".to_string(),
|
|
|
|
url: "file:///some/file.ts".to_string(),
|
|
|
|
redirect: None,
|
|
|
|
filename: "some/file.ts".to_string(),
|
|
|
|
imports: vec![ImportDescriptor {
|
|
|
|
specifier: "./file.jsx".to_string(),
|
|
|
|
resolved_specifier: ModuleSpecifier::resolve_url(
|
|
|
|
"file:///some/file.jsx",
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
type_directive: None,
|
|
|
|
resolved_type_directive: None,
|
2020-06-19 06:27:15 -04:00
|
|
|
location: Location {
|
|
|
|
filename: "file:///some/file1.ts".to_string(),
|
|
|
|
line: 0,
|
|
|
|
col: 0,
|
|
|
|
},
|
2020-06-15 11:53:05 -04:00
|
|
|
}],
|
|
|
|
referenced_files: vec![],
|
|
|
|
lib_directives: vec![],
|
|
|
|
types_directives: vec![],
|
|
|
|
type_headers: vec![],
|
2020-06-19 06:27:15 -04:00
|
|
|
media_type: MediaType::TypeScript,
|
2020-06-15 11:53:05 -04:00
|
|
|
source_code: "function foo() {}".to_string(),
|
|
|
|
},
|
|
|
|
]));
|
|
|
|
|
2020-06-10 10:02:41 -04:00
|
|
|
assert!(!should_allow_js(&[
|
|
|
|
&ModuleGraphFile {
|
|
|
|
specifier: "file:///some/file.js".to_string(),
|
2020-06-01 15:01:51 -04:00
|
|
|
url: "file:///some/file.js".to_string(),
|
|
|
|
redirect: None,
|
|
|
|
filename: "some/file.js".to_string(),
|
|
|
|
imports: vec![],
|
|
|
|
referenced_files: vec![],
|
|
|
|
lib_directives: vec![],
|
|
|
|
types_directives: vec![],
|
|
|
|
type_headers: vec![],
|
2020-06-19 06:27:15 -04:00
|
|
|
media_type: MediaType::JavaScript,
|
2020-06-01 15:01:51 -04:00
|
|
|
source_code: "function foo() {}".to_string(),
|
2020-06-10 10:02:41 -04:00
|
|
|
},
|
|
|
|
&ModuleGraphFile {
|
|
|
|
specifier: "file:///some/file1.js".to_string(),
|
|
|
|
url: "file:///some/file1.js".to_string(),
|
|
|
|
redirect: None,
|
|
|
|
filename: "some/file1.js".to_string(),
|
|
|
|
imports: vec![ImportDescriptor {
|
|
|
|
specifier: "./file.js".to_string(),
|
|
|
|
resolved_specifier: ModuleSpecifier::resolve_url(
|
|
|
|
"file:///some/file.js",
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
type_directive: None,
|
|
|
|
resolved_type_directive: None,
|
2020-06-19 06:27:15 -04:00
|
|
|
location: Location {
|
|
|
|
filename: "file:///some/file.js".to_string(),
|
|
|
|
line: 0,
|
|
|
|
col: 0,
|
|
|
|
},
|
2020-06-10 10:02:41 -04:00
|
|
|
}],
|
|
|
|
referenced_files: vec![],
|
|
|
|
lib_directives: vec![],
|
|
|
|
types_directives: vec![],
|
|
|
|
type_headers: vec![],
|
2020-06-19 06:27:15 -04:00
|
|
|
media_type: MediaType::JavaScript,
|
2020-06-10 10:02:41 -04:00
|
|
|
source_code: "function foo() {}".to_string(),
|
|
|
|
},
|
|
|
|
],));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_needs_compilation() {
|
|
|
|
assert!(!needs_compilation(
|
2020-06-01 15:01:51 -04:00
|
|
|
false,
|
|
|
|
MediaType::JavaScript,
|
2020-06-10 10:02:41 -04:00
|
|
|
&[&ModuleGraphFile {
|
|
|
|
specifier: "some/file.js".to_string(),
|
|
|
|
url: "file:///some/file.js".to_string(),
|
2020-06-01 15:01:51 -04:00
|
|
|
redirect: None,
|
2020-06-10 10:02:41 -04:00
|
|
|
filename: "some/file.js".to_string(),
|
2020-06-01 15:01:51 -04:00
|
|
|
imports: vec![],
|
|
|
|
referenced_files: vec![],
|
|
|
|
lib_directives: vec![],
|
|
|
|
types_directives: vec![],
|
|
|
|
type_headers: vec![],
|
2020-06-19 06:27:15 -04:00
|
|
|
media_type: MediaType::JavaScript,
|
2020-06-01 15:01:51 -04:00
|
|
|
source_code: "function foo() {}".to_string(),
|
2020-06-10 10:02:41 -04:00
|
|
|
}],
|
|
|
|
));
|
|
|
|
|
|
|
|
assert!(!needs_compilation(false, MediaType::JavaScript, &[]));
|
|
|
|
assert!(needs_compilation(true, MediaType::JavaScript, &[]));
|
|
|
|
assert!(needs_compilation(false, MediaType::TypeScript, &[]));
|
|
|
|
assert!(needs_compilation(false, MediaType::JSX, &[]));
|
|
|
|
assert!(needs_compilation(false, MediaType::TSX, &[]));
|
|
|
|
assert!(needs_compilation(
|
|
|
|
false,
|
|
|
|
MediaType::JavaScript,
|
|
|
|
&[
|
|
|
|
&ModuleGraphFile {
|
|
|
|
specifier: "file:///some/file.ts".to_string(),
|
|
|
|
url: "file:///some/file.ts".to_string(),
|
|
|
|
redirect: None,
|
|
|
|
filename: "some/file.ts".to_string(),
|
|
|
|
imports: vec![],
|
|
|
|
referenced_files: vec![],
|
|
|
|
lib_directives: vec![],
|
|
|
|
types_directives: vec![],
|
|
|
|
type_headers: vec![],
|
2020-06-19 06:27:15 -04:00
|
|
|
media_type: MediaType::TypeScript,
|
2020-06-10 10:02:41 -04:00
|
|
|
source_code: "function foo() {}".to_string(),
|
|
|
|
},
|
|
|
|
&ModuleGraphFile {
|
|
|
|
specifier: "file:///some/file1.js".to_string(),
|
|
|
|
url: "file:///some/file1.js".to_string(),
|
|
|
|
redirect: None,
|
|
|
|
filename: "some/file1.js".to_string(),
|
|
|
|
imports: vec![],
|
|
|
|
referenced_files: vec![],
|
|
|
|
lib_directives: vec![],
|
|
|
|
types_directives: vec![],
|
|
|
|
type_headers: vec![],
|
2020-06-19 06:27:15 -04:00
|
|
|
media_type: MediaType::JavaScript,
|
2020-06-10 10:02:41 -04:00
|
|
|
source_code: "function foo() {}".to_string(),
|
|
|
|
},
|
|
|
|
],
|
2020-06-01 15:01:51 -04:00
|
|
|
));
|
|
|
|
}
|