Skip to content

Commit

Permalink
refactor(ts-next): support multiple configuration files in extends
Browse files Browse the repository at this point in the history
  • Loading branch information
unicornware committed Feb 6, 2023
1 parent 392a866 commit 64cfb1a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 22 deletions.
1 change: 1 addition & 0 deletions .dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ pkgs
pnpm
preid
syncer
tsconfigs
vates
yarnrc
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"@flex-development/errnode": "1.5.0",
"@flex-development/mlly": "1.0.0-alpha.9",
"@flex-development/pathe": "1.0.3",
"@flex-development/tsconfig-types": "2.0.3",
"@flex-development/tsconfig-types": "3.0.0",
"@flex-development/tutils": "6.0.0-alpha.10",
"merge-anything": "5.1.4",
"sort-keys": "5.0.0",
Expand Down
37 changes: 23 additions & 14 deletions src/utils/load-tsconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import type {
TSConfig
} from '@flex-development/tsconfig-types'
import {
isEmptyString,
isNIL,
isObjectPlain,
isString,
type Nilable,
Expand Down Expand Up @@ -102,16 +102,25 @@ const loadTsconfig = (
throw new ERR_INVALID_RETURN_VALUE('a plain object', 'parseJSON', tsconfig)
}

// try merging tsconfig and base tsconfig
if (isString(tsconfig.extends) && !isEmptyString(tsconfig.extends.trim())) {
/**
* Configuration files to inherit from.
*
* @const {string[]} bases
*/
const bases: string[] = isNIL(tsconfig.extends)
? []
: [tsconfig.extends].flat().filter(extend => !!extend.trim())

// try merging tsconfig and base tsconfigs
for (const extend of bases) {
/**
* Absolute path to base tsconfig file.
*
* @const {string} basepath
*/
const basepath: string = pathe.join(
pathe.dirname(id.pathname),
(tsconfig.extends + '.json').replace(/(\.json\.json)$/, '.json')
(extend + '.json').replace(/(\.json\.json)$/, '.json')
)

/**
Expand All @@ -124,13 +133,13 @@ const loadTsconfig = (
// merge tsconfig objects if base tsconfig object was found
if (base) {
/**
* Compares values from a base tsconfig file and inheriting tsconfig file
* to determine how the values should be merged.
* Compares values from a base tsconfig file and inheriting tsconfig
* file to determine how the values should be merged.
*
* @param {Nilable<CompilerOptions | CompilerOptionsValue>} b - Value from
* base tsconfig object, {@linkcode base}
* @param {Nilable<CompilerOptions | CompilerOptionsValue>} t - Value from
* inheriting tsconfig object, {@linkcode tsconfig}
* @param {Nilable<CompilerOptions | CompilerOptionsValue>} b - Value
* from base tsconfig object, {@linkcode base}
* @param {Nilable<CompilerOptions | CompilerOptionsValue>} t - Value
* from inheriting tsconfig object, {@linkcode tsconfig}
* @param {string | symbol} key - Object key being evaluated
* @return {Nilable<CompilerOptions | CompilerOptionsValue>} Merge value
*/
Expand All @@ -148,17 +157,17 @@ const loadTsconfig = (

// determine how to merge values
switch (true) {
// relative paths should be interpreted as relative to base tsconfig,
// but they need also need to be relative to inheriting tsconfig
// relative paths should be interpreted as relative to base, but
// they need also need to be relative to inheriting tsconfig
case key === 'baseUrl' && isString(b):
case key === 'outDir' && isString(b):
case key === 'rootDir' && isString(b):
const { extends: extend = '' } = tsconfig!
if (b === t) merged = pathe.join(pathe.dirname(extend), b as string)
break
// recursively merge compilerOptions
case key === 'compilerOptions':
merged = sortKeys(mergeAndCompare(compare, b, t) as CompilerOptions)
merged = mergeAndCompare(compare, b, t) as CompilerOptions
merged = sortKeys(merged)
break
// exclude, files, and include properties from inheriting tsconfig
// file should overwrite those from base tsconfig file.
Expand Down
15 changes: 8 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1299,12 +1299,13 @@ __metadata:
languageName: node
linkType: hard

"@flex-development/tsconfig-types@npm:2.0.3":
version: 2.0.3
resolution: "@flex-development/tsconfig-types@npm:2.0.3::__archiveUrl=https%3A%2F%2Fnpm.pkg.github.com%2Fdownload%2F%40flex-development%2Ftsconfig-types%2F2.0.3%2F7b5cdde69b2fa08162f254f97b49945271945b1d"
peerDependencies:
"@flex-development/tutils": ">=6.0.0-alpha.7"
checksum: a0c471bc5b9212d3228a3aa037d0acf51426aa6aa5a0e104b92e93329a05f16c7dab13c3c5cdb6a9af7d0f6004ad010de0a51e180f4261c1a9b818903d68c84d
"@flex-development/tsconfig-types@npm:3.0.0":
version: 3.0.0
resolution: "@flex-development/tsconfig-types@npm:3.0.0::__archiveUrl=https%3A%2F%2Fnpm.pkg.github.com%2Fdownload%2F%40flex-development%2Ftsconfig-types%2F3.0.0%2Ffcd2cf1f02f3377a624b44c74aad985ee804121c"
dependencies:
"@flex-development/pkg-types": "npm:2.0.0"
"@flex-development/tutils": "npm:6.0.0-alpha.10"
checksum: ad6d09fd4ac41349d928f10a77bf83d23c02215c0036a4794b49d825a693b310c043204440e47ac2bb3aa10a64c26f729a97ef6d1240d50daaa1a8f25af51fae
languageName: node
linkType: hard

Expand All @@ -1319,7 +1320,7 @@ __metadata:
"@flex-development/mkbuild": "npm:1.0.0-alpha.9"
"@flex-development/mlly": "npm:1.0.0-alpha.9"
"@flex-development/pathe": "npm:1.0.3"
"@flex-development/tsconfig-types": "npm:2.0.3"
"@flex-development/tsconfig-types": "npm:3.0.0"
"@flex-development/tutils": "npm:6.0.0-alpha.10"
"@graphql-eslint/eslint-plugin": "npm:3.15.0"
"@types/chai": "npm:4.3.4"
Expand Down

0 comments on commit 64cfb1a

Please sign in to comment.