mirror of
https://github.com/swc-project/swc.git
synced 2024-11-24 02:06:08 +03:00
fix(es/loader): Support fully-specified ESM import specifiers (#3050)
This commit is contained in:
parent
189b1e286d
commit
d5c7fb898f
@ -167,10 +167,39 @@ impl NodeResolver {
|
||||
return Ok(path.to_path_buf());
|
||||
}
|
||||
|
||||
for ext in EXTENSIONS {
|
||||
let ext_path = path.with_extension(ext);
|
||||
if ext_path.is_file() {
|
||||
return Ok(ext_path);
|
||||
if let Some(name) = path.file_name() {
|
||||
let mut ext_path = path.to_path_buf();
|
||||
let name = name.to_string_lossy();
|
||||
for ext in EXTENSIONS {
|
||||
ext_path.set_file_name(format!("{}.{}", name, ext));
|
||||
if ext_path.is_file() {
|
||||
return Ok(ext_path);
|
||||
}
|
||||
}
|
||||
|
||||
// TypeScript-specific behavior: if the extension is ".js" or ".jsx",
|
||||
// try replacing it with ".ts" or ".tsx".
|
||||
ext_path.set_file_name(name.into_owned());
|
||||
let old_ext = path.extension().and_then(|ext| ext.to_str());
|
||||
|
||||
if let Some(old_ext) = old_ext {
|
||||
let extensions = match old_ext {
|
||||
// Note that the official compiler code always tries ".ts" before
|
||||
// ".tsx" even if the original extension was ".jsx".
|
||||
"js" => ["ts", "tsx"].as_slice(),
|
||||
"jsx" => ["ts", "tsx"].as_slice(),
|
||||
"mjs" => ["mts"].as_slice(),
|
||||
"cjs" => ["cts"].as_slice(),
|
||||
_ => [].as_slice(),
|
||||
};
|
||||
|
||||
for ext in extensions {
|
||||
ext_path.set_extension(ext);
|
||||
|
||||
if ext_path.is_file() {
|
||||
return Ok(ext_path);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,2 @@
|
||||
import { two } from "./two.js";
|
||||
two();
|
@ -0,0 +1 @@
|
||||
export function two() {}
|
@ -0,0 +1,3 @@
|
||||
function two() {
|
||||
}
|
||||
two();
|
@ -0,0 +1,3 @@
|
||||
function two() {
|
||||
}
|
||||
two();
|
@ -0,0 +1,2 @@
|
||||
import { result } from "./foo.js";
|
||||
console.log(result);
|
@ -0,0 +1 @@
|
||||
export const result = "foo.js.ts";
|
@ -0,0 +1 @@
|
||||
export const result = "foo.ts";
|
@ -0,0 +1,2 @@
|
||||
const result = "foo.js.ts";
|
||||
console.log(result);
|
@ -0,0 +1,2 @@
|
||||
const result = "foo.js.ts";
|
||||
console.log(result);
|
@ -0,0 +1,2 @@
|
||||
import { result } from "./foo.mjs";
|
||||
console.log(result);
|
@ -0,0 +1 @@
|
||||
export const result = "foo.mts";
|
@ -0,0 +1 @@
|
||||
export const result = "foo.ts";
|
@ -0,0 +1,2 @@
|
||||
const result = "foo.mts";
|
||||
console.log(result);
|
@ -0,0 +1,2 @@
|
||||
const result = "foo.mts";
|
||||
console.log(result);
|
@ -115,6 +115,31 @@ impl NodeModulesResolver {
|
||||
return Ok(Some(ext_path));
|
||||
}
|
||||
}
|
||||
|
||||
// TypeScript-specific behavior: if the extension is ".js" or ".jsx",
|
||||
// try replacing it with ".ts" or ".tsx".
|
||||
ext_path.set_file_name(name.into_owned());
|
||||
let old_ext = path.extension().and_then(|ext| ext.to_str());
|
||||
|
||||
if let Some(old_ext) = old_ext {
|
||||
let extensions = match old_ext {
|
||||
// Note that the official compiler code always tries ".ts" before
|
||||
// ".tsx" even if the original extension was ".jsx".
|
||||
"js" => ["ts", "tsx"].as_slice(),
|
||||
"jsx" => ["ts", "tsx"].as_slice(),
|
||||
"mjs" => ["mts"].as_slice(),
|
||||
"cjs" => ["cts"].as_slice(),
|
||||
_ => [].as_slice(),
|
||||
};
|
||||
|
||||
for ext in extensions {
|
||||
ext_path.set_extension(ext);
|
||||
|
||||
if ext_path.is_file() {
|
||||
return Ok(Some(ext_path));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bail!("file not found: {}", path.display())
|
||||
|
Loading…
Reference in New Issue
Block a user