Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions fixtures/tsconfig/cases/extends-imports/default.tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"target": "ES2022"
}
}
5 changes: 5 additions & 0 deletions fixtures/tsconfig/cases/extends-imports/node.tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"target": "ES2015"
}
}
10 changes: 10 additions & 0 deletions fixtures/tsconfig/cases/extends-imports/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "extends-imports",
"imports": {
"#string": "./string.tsconfig.json",
"#conditional": {
"node": "./node.tsconfig.json",
"default": "./default.tsconfig.json"
}
}
}
5 changes: 5 additions & 0 deletions fixtures/tsconfig/cases/extends-imports/string.tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"target": "ES2020"
}
}
3 changes: 3 additions & 0 deletions fixtures/tsconfig/cases/extends-imports/tsconfig-missing.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "#missing"
}
3 changes: 3 additions & 0 deletions fixtures/tsconfig/cases/extends-imports/tsconfig-string.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "#string"
}
3 changes: 3 additions & 0 deletions fixtures/tsconfig/cases/extends-imports/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "#conditional"
}
25 changes: 25 additions & 0 deletions src/tests/tsconfig_extends.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,31 @@ fn test_extend_package() {
}
}

#[test]
fn test_extend_imports() {
let f = super::fixture_root().join("tsconfig/cases/extends-imports");

let resolver = Resolver::new(ResolveOptions {
tsconfig: Some(TsconfigDiscovery::Manual(TsconfigOptions {
config_file: f.join("tsconfig.json"),
references: TsconfigReferences::Auto,
})),
..ResolveOptions::default()
});

let resolution = resolver.resolve_tsconfig(f.join("tsconfig-string.json")).expect("resolved");
assert_eq!(resolution.compiler_options.target, Some("ES2020".to_string()));

let resolution = resolver.resolve_tsconfig(&f).expect("resolved");
assert_eq!(resolution.compiler_options.target, Some("ES2015".to_string()));

let result = resolver.resolve_tsconfig(f.join("tsconfig-missing.json"));
assert!(
matches!(&result, Err(crate::ResolveError::TsconfigNotFound(_))),
"expected TsconfigNotFound for an undefined `#` import, got {result:?}",
);
}

fn assert_extends_symlink_resolves_to_canonical(config_file: &Path) {
let f = super::fixture_root().join("tsconfig/cases/extends-symlink");
let resolver = Resolver::new(ResolveOptions {
Expand Down
48 changes: 37 additions & 11 deletions src/tsconfig_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,25 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
Ok(None)
}

/// Lookup configuration shared by tsconfig `extends` targets that resolve
/// through `node_modules`: bare package specifiers (via the package
/// `exports` field) and `#`-prefixed subpath imports (via the package
/// `imports` field). Both use the same conditions so that, for example,
/// `extends: "pkg"` and `extends: "#pkg"` agree on which condition wins.
fn tsconfig_extends_resolver(&self) -> Self {
self.clone_with_options(ResolveOptions {
tsconfig: None,
condition_names: vec!["node".into(), "import".into()],
Comment thread
Boshen marked this conversation as resolved.
extensions: vec![".json".into()],
main_files: vec!["tsconfig".into()],
#[cfg(feature = "yarn_pnp")]
yarn_pnp: self.options.yarn_pnp,
#[cfg(feature = "yarn_pnp")]
cwd: self.options.cwd.clone(),
..ResolveOptions::default()
})
}

fn get_extended_tsconfig_path(
&self,
directory: &CachedPath,
Expand All @@ -406,18 +425,25 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
None => Err(ResolveError::Specifier(SpecifierError::Empty(specifier.to_string()))),
Some(b'/') => Ok(PathBuf::from(specifier)),
Some(b'.') => Ok(tsconfig.directory().normalize_with(specifier)),
_ => self
.clone_with_options(ResolveOptions {
tsconfig: None,
condition_names: vec!["node".into(), "import".into()],
extensions: vec![".json".into()],
main_files: vec!["tsconfig".into()],
#[cfg(feature = "yarn_pnp")]
yarn_pnp: self.options.yarn_pnp,
#[cfg(feature = "yarn_pnp")]
cwd: self.options.cwd.clone(),
..ResolveOptions::default()
// Node.js subpath imports, e.g. `extends: "#config"`, resolved
// through the nearest `package.json` `imports` field — the same
// path the resolver takes for `require("#config")`.
Some(b'#') => self
.tsconfig_extends_resolver()
.load_package_imports(directory, specifier, Some(tsconfig), &mut Ctx::default())
Comment thread
Boshen marked this conversation as resolved.
.map_err(|err| match err {
ResolveError::PackageImportNotDefined(..) | ResolveError::NotFound(..) => {
ResolveError::TsconfigNotFound(PathBuf::from(specifier))
}
_ => err,
})
.and_then(|resolved| {
resolved
.map(|p| p.path().to_path_buf())
.ok_or_else(|| ResolveError::TsconfigNotFound(PathBuf::from(specifier)))
}),
_ => self
.tsconfig_extends_resolver()
.load_package_self_or_node_modules(directory, specifier, None, &mut Ctx::default())
.map(|p| p.to_path_buf())
.map_err(|err| match err {
Expand Down
Loading