mirror of
https://github.com/denoland/deno.git
synced 2024-11-22 15:06:54 -05:00
fix: compile TS dependencies of JS files (#6000)
This commit fixes regression that caused TS dependencies not being compiled. Check was added that ensures TS compiler is run if any of dependencies in module graph is TS/TSX/JSX.
This commit is contained in:
parent
becbb56b19
commit
c813990806
4 changed files with 87 additions and 9 deletions
|
@ -5,8 +5,10 @@ use crate::flags;
|
||||||
use crate::http_cache;
|
use crate::http_cache;
|
||||||
use crate::import_map::ImportMap;
|
use crate::import_map::ImportMap;
|
||||||
use crate::lockfile::Lockfile;
|
use crate::lockfile::Lockfile;
|
||||||
|
use crate::module_graph::ModuleGraphFile;
|
||||||
use crate::module_graph::ModuleGraphLoader;
|
use crate::module_graph::ModuleGraphLoader;
|
||||||
use crate::msg;
|
use crate::msg;
|
||||||
|
use crate::msg::MediaType;
|
||||||
use crate::permissions::Permissions;
|
use crate::permissions::Permissions;
|
||||||
use crate::state::exit_unstable;
|
use crate::state::exit_unstable;
|
||||||
use crate::tsc::CompiledModule;
|
use crate::tsc::CompiledModule;
|
||||||
|
@ -140,16 +142,14 @@ impl GlobalState {
|
||||||
.fetch_cached_source_file(&module_specifier, permissions.clone())
|
.fetch_cached_source_file(&module_specifier, permissions.clone())
|
||||||
.expect("Source file not found");
|
.expect("Source file not found");
|
||||||
|
|
||||||
// Check if we need to compile files
|
// Check if we need to compile files.
|
||||||
let needs_compilation = match out.media_type {
|
let should_compile = needs_compilation(
|
||||||
msg::MediaType::TypeScript
|
self.ts_compiler.compile_js,
|
||||||
| msg::MediaType::TSX
|
out.media_type,
|
||||||
| msg::MediaType::JSX => true,
|
module_graph.values().collect::<Vec<_>>(),
|
||||||
msg::MediaType::JavaScript => self.ts_compiler.compile_js,
|
);
|
||||||
_ => false,
|
|
||||||
};
|
|
||||||
|
|
||||||
if needs_compilation {
|
if should_compile {
|
||||||
self
|
self
|
||||||
.ts_compiler
|
.ts_compiler
|
||||||
.compile_module_graph(
|
.compile_module_graph(
|
||||||
|
@ -241,8 +241,79 @@ impl GlobalState {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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,
|
||||||
|
module_graph_files: Vec<&ModuleGraphFile>,
|
||||||
|
) -> 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;
|
||||||
|
|
||||||
|
media_type == (MediaType::TypeScript as i32)
|
||||||
|
|| media_type == (MediaType::TSX as i32)
|
||||||
|
|| media_type == (MediaType::JSX as i32)
|
||||||
|
});
|
||||||
|
|
||||||
|
needs_compilation
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn thread_safe() {
|
fn thread_safe() {
|
||||||
fn f<S: Send + Sync>(_: S) {}
|
fn f<S: Send + Sync>(_: S) {}
|
||||||
f(GlobalState::mock(vec![]));
|
f(GlobalState::mock(vec![]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_needs_compilation() {
|
||||||
|
assert!(!needs_compilation(
|
||||||
|
false,
|
||||||
|
MediaType::JavaScript,
|
||||||
|
vec![&ModuleGraphFile {
|
||||||
|
specifier: "some/file.js".to_string(),
|
||||||
|
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![],
|
||||||
|
media_type: MediaType::JavaScript as i32,
|
||||||
|
source_code: "function foo() {}".to_string(),
|
||||||
|
}]
|
||||||
|
));
|
||||||
|
assert!(!needs_compilation(false, MediaType::JavaScript, vec![]));
|
||||||
|
assert!(needs_compilation(true, MediaType::JavaScript, vec![]));
|
||||||
|
assert!(needs_compilation(false, MediaType::TypeScript, vec![]));
|
||||||
|
assert!(needs_compilation(false, MediaType::JSX, vec![]));
|
||||||
|
assert!(needs_compilation(false, MediaType::TSX, vec![]));
|
||||||
|
assert!(needs_compilation(
|
||||||
|
false,
|
||||||
|
MediaType::JavaScript,
|
||||||
|
vec![&ModuleGraphFile {
|
||||||
|
specifier: "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![],
|
||||||
|
media_type: MediaType::TypeScript as i32,
|
||||||
|
source_code: "function foo() {}".to_string(),
|
||||||
|
}]
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
|
@ -1837,6 +1837,11 @@ itest!(cjs_imports {
|
||||||
output: "cjs_imports.ts.out",
|
output: "cjs_imports.ts.out",
|
||||||
});
|
});
|
||||||
|
|
||||||
|
itest!(ts_import_from_js {
|
||||||
|
args: "run --quiet --reload ts_import_from_js.js",
|
||||||
|
output: "ts_import_from_js.js.out",
|
||||||
|
});
|
||||||
|
|
||||||
itest!(proto_exploit {
|
itest!(proto_exploit {
|
||||||
args: "run proto_exploit.js",
|
args: "run proto_exploit.js",
|
||||||
output: "proto_exploit.js.out",
|
output: "proto_exploit.js.out",
|
||||||
|
|
1
cli/tests/ts_import_from_js.js
Normal file
1
cli/tests/ts_import_from_js.js
Normal file
|
@ -0,0 +1 @@
|
||||||
|
import "./005_more_imports.ts";
|
1
cli/tests/ts_import_from_js.js.out
Normal file
1
cli/tests/ts_import_from_js.js.out
Normal file
|
@ -0,0 +1 @@
|
||||||
|
Hello
|
Loading…
Reference in a new issue