Skip to content

Commit 018a909

Browse files
authored
fix: template variable not replaced in baseUrl of tsconfig (#61)
1 parent cc908e8 commit 018a909

File tree

4 files changed

+38
-8
lines changed

4 files changed

+38
-8
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "../../tsconfig_template_variable_with_base_url.json"
3+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"compilerOptions": {
3+
"baseUrl": "${configDir}",
4+
"paths": {
5+
"foo": ["${configDir}/foo.js"]
6+
}
7+
}
8+
}

src/tests/tsconfig_paths.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,9 @@ async fn test_template_variable() {
237237
let pass = [
238238
(f2.clone(), "tsconfig1.json", "foo", f2.join("foo.js")),
239239
(f2.clone(), "tsconfig2.json", "foo", f2.join("foo.js")),
240+
(f2.clone(), "tsconfig3.json", "foo", f2.join("foo.js")),
240241
(f.clone(), "tsconfig_template_variable.json", "foo", f.join("foo.js")),
242+
(f.clone(), "tsconfig_template_variable_with_base_url.json", "foo", f.join("foo.js")),
241243
];
242244

243245
for (dir, tsconfig, request, expected) in pass {

src/tsconfig.rs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1+
use indexmap::IndexMap;
2+
use rustc_hash::FxHasher;
3+
use serde::Deserialize;
14
use std::{
25
hash::BuildHasherDefault,
36
path::{Path, PathBuf},
47
sync::Arc,
58
};
69

7-
use indexmap::IndexMap;
8-
use rustc_hash::FxHasher;
9-
use serde::Deserialize;
10-
1110
use crate::PathUtil;
1211

1312
pub type CompilerOptionsPathsMap = IndexMap<String, Vec<String>, BuildHasherDefault<FxHasher>>;
@@ -19,6 +18,8 @@ pub enum ExtendsField {
1918
Multiple(Vec<String>),
2019
}
2120

21+
const TEMPLATE_VARIABLE: &str = "${configDir}";
22+
2223
#[derive(Debug, Deserialize)]
2324
#[serde(rename_all = "camelCase")]
2425
pub struct TsConfig {
@@ -85,8 +86,11 @@ impl TsConfig {
8586
tsconfig.root = root;
8687
tsconfig.path = path.to_path_buf();
8788
let directory = tsconfig.directory().to_path_buf();
88-
if let Some(base_url) = tsconfig.compiler_options.base_url {
89-
tsconfig.compiler_options.base_url = Some(directory.normalize_with(base_url));
89+
if let Some(base_url) = &tsconfig.compiler_options.base_url {
90+
// keep the `${configDir}` template variable in the baseUrl
91+
if !base_url.starts_with(TEMPLATE_VARIABLE) {
92+
tsconfig.compiler_options.base_url = Some(directory.normalize_with(base_url));
93+
}
9094
}
9195
if tsconfig.compiler_options.paths.is_some() {
9296
tsconfig.compiler_options.paths_base =
@@ -106,6 +110,16 @@ impl TsConfig {
106110
}
107111
}
108112
}
113+
114+
let mut p = self.compiler_options.paths_base.to_string_lossy().to_string();
115+
Self::substitute_template_variable(&dir, &mut p);
116+
self.compiler_options.paths_base = p.into();
117+
118+
if let Some(base_url) = self.compiler_options.base_url.as_mut() {
119+
let mut p = base_url.to_string_lossy().to_string();
120+
Self::substitute_template_variable(&dir, &mut p);
121+
*base_url = p.into();
122+
}
109123
}
110124
self
111125
}
@@ -221,9 +235,12 @@ impl TsConfig {
221235
///
222236
/// See <https://github.com/microsoft/TypeScript/pull/58042>
223237
fn substitute_template_variable(directory: &Path, path: &mut String) {
224-
const TEMPLATE_VARIABLE: &str = "${configDir}/";
225238
if let Some(stripped_path) = path.strip_prefix(TEMPLATE_VARIABLE) {
226-
*path = directory.join(stripped_path).to_string_lossy().to_string();
239+
if let Some(unleashed_path) = stripped_path.strip_prefix("/") {
240+
*path = directory.join(unleashed_path).to_string_lossy().to_string();
241+
} else {
242+
*path = directory.join(stripped_path).to_string_lossy().to_string();
243+
}
227244
}
228245
}
229246
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy