diff --git a/.changeset/light-ants-divide.md b/.changeset/light-ants-divide.md new file mode 100644 index 0000000..b5d0bdf --- /dev/null +++ b/.changeset/light-ants-divide.md @@ -0,0 +1,5 @@ +--- +'tsconfck': minor +--- + +add support for typescript 5.5 ${configDir} replacement diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index fb098ae..5ffd6d9 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -19,7 +19,7 @@ jobs: strategy: matrix: # pseudo-matrix for convenience, NEVER use more than a single combination - node: [18] + node: [20] os: [ubuntu-latest] steps: - name: checkout diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index bc3cd63..53173b1 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -14,7 +14,7 @@ jobs: strategy: matrix: # pseudo-matrix for convenience, NEVER use more than a single combination - node: [18] + node: [20] os: [ubuntu-latest] steps: - uses: actions/checkout@v4 @@ -57,11 +57,19 @@ jobs: strategy: fail-fast: false matrix: - node: [18] + node: [20] os: [ubuntu-latest, macos-latest, windows-latest] + ts: ['current'] include: + - node: 18 + os: ubuntu-latest + ts: 'current' + - node: 22 + os: ubuntu-latest + ts: 'current' - node: 20 os: ubuntu-latest + ts: 'beta' steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 @@ -81,5 +89,9 @@ jobs: - name: install run: | pnpm install --frozen-lockfile --prefer-offline --ignore-scripts + - id: install_ts_beta + name: install ts beta + if: matrix.ts == 'beta' + run: pnpm --dir packages/tsconfck install -D typescript@beta - name: run tests run: pnpm test diff --git a/packages/tsconfck/src/parse-native.js b/packages/tsconfck/src/parse-native.js index fcac7a0..5146527 100644 --- a/packages/tsconfck/src/parse-native.js +++ b/packages/tsconfck/src/parse-native.js @@ -5,7 +5,8 @@ import { native2posix, resolveReferencedTSConfigFiles, resolveSolutionTSConfig, - resolveTSConfigJson + resolveTSConfigJson, + replaceTokens } from './util.js'; import { findNative } from './find-native.js'; @@ -102,17 +103,20 @@ function parseFile(tsconfigFile, ts, options, skipCache) { undefined, posixTSConfigFile ); + checkErrors(nativeResult, tsconfigFile); /** @type {import('./public.d.ts').TSConfckParseNativeResult} */ const result = { tsconfigFile, - tsconfig: result2tsconfig(nativeResult, ts), + tsconfig: result2tsconfig(nativeResult, ts, tsconfigFile), result: nativeResult }; + if (!skipCache) { cache?.setParseResult(tsconfigFile, Promise.resolve(result)); } + return result; } @@ -166,9 +170,10 @@ function checkErrors(nativeResult, tsconfigFile) { * * @param {any} result * @param {any} ts typescript + * @param {string} tsconfigFile * @returns {object} tsconfig with merged compilerOptions and enums restored to their string form */ -function result2tsconfig(result, ts) { +function result2tsconfig(result, ts, tsconfigFile) { // dereference result.raw so changes below don't modify original const tsconfig = JSON.parse(JSON.stringify(result.raw)); // for some reason the extended compilerOptions are not available in result.raw but only in result.options @@ -243,7 +248,8 @@ function result2tsconfig(result, ts) { // delete if it is false to match content of tsconfig delete tsconfig.compileOnSave; } - return tsconfig; + // ts itself has not replaced all tokens at this point, make sure they are + return replaceTokens(tsconfig, path.dirname(tsconfigFile)); } export class TSConfckParseNativeError extends Error { diff --git a/packages/tsconfck/src/parse.js b/packages/tsconfck/src/parse.js index 4483ad9..48ac090 100644 --- a/packages/tsconfck/src/parse.js +++ b/packages/tsconfck/src/parse.js @@ -7,6 +7,7 @@ import { makePromise, native2posix, resolve2posix, + replaceTokens, resolveReferencedTSConfigFiles, resolveSolutionTSConfig, resolveTSConfigJson @@ -136,7 +137,8 @@ function normalizeTSConfig(tsconfig, dir) { if (tsconfig.compilerOptions?.baseUrl && !path.isAbsolute(tsconfig.compilerOptions.baseUrl)) { tsconfig.compilerOptions.baseUrl = resolve2posix(dir, tsconfig.compilerOptions.baseUrl); } - return tsconfig; + + return replaceTokens(tsconfig, dir); } /** diff --git a/packages/tsconfck/src/util.js b/packages/tsconfck/src/util.js index 4ff3175..2bdb092 100644 --- a/packages/tsconfck/src/util.js +++ b/packages/tsconfck/src/util.js @@ -294,3 +294,17 @@ function pattern2regex(resolvedPattern, allowJs) { return new RegExp(regexStr); } + +/** + * replace tokens like ${configDir} + * @param {any} tsconfig + * @param {string} configDir + * @returns {any} + */ +export function replaceTokens(tsconfig, configDir) { + return JSON.parse( + JSON.stringify(tsconfig) + // replace ${configDir}, accounting for rebaseRelative emitted ../${configDir} + .replaceAll(/"(?:\.\.\/)*\${configDir}/g, `"${native2posix(configDir)}`) + ); +} diff --git a/packages/tsconfck/tests/fixtures/parse/valid/configDir/a/src/foo.ts b/packages/tsconfck/tests/fixtures/parse/valid/configDir/a/src/foo.ts new file mode 100644 index 0000000..b071191 --- /dev/null +++ b/packages/tsconfck/tests/fixtures/parse/valid/configDir/a/src/foo.ts @@ -0,0 +1,3 @@ +export function foo() { + return 'foo'; +} diff --git a/packages/tsconfck/tests/fixtures/parse/valid/configDir/a/tsconfig.json b/packages/tsconfck/tests/fixtures/parse/valid/configDir/a/tsconfig.json new file mode 100644 index 0000000..4d4c4a7 --- /dev/null +++ b/packages/tsconfck/tests/fixtures/parse/valid/configDir/a/tsconfig.json @@ -0,0 +1,3 @@ +{ + "extends": ["../tsconfig.base.json"] +} \ No newline at end of file diff --git a/packages/tsconfck/tests/fixtures/parse/valid/configDir/b/src/foo.ts b/packages/tsconfck/tests/fixtures/parse/valid/configDir/b/src/foo.ts new file mode 100644 index 0000000..b071191 --- /dev/null +++ b/packages/tsconfck/tests/fixtures/parse/valid/configDir/b/src/foo.ts @@ -0,0 +1,3 @@ +export function foo() { + return 'foo'; +} diff --git a/packages/tsconfck/tests/fixtures/parse/valid/configDir/b/tsconfig.json b/packages/tsconfck/tests/fixtures/parse/valid/configDir/b/tsconfig.json new file mode 100644 index 0000000..4d4c4a7 --- /dev/null +++ b/packages/tsconfck/tests/fixtures/parse/valid/configDir/b/tsconfig.json @@ -0,0 +1,3 @@ +{ + "extends": ["../tsconfig.base.json"] +} \ No newline at end of file diff --git a/packages/tsconfck/tests/fixtures/parse/valid/configDir/src/foo.ts b/packages/tsconfck/tests/fixtures/parse/valid/configDir/src/foo.ts new file mode 100644 index 0000000..b071191 --- /dev/null +++ b/packages/tsconfck/tests/fixtures/parse/valid/configDir/src/foo.ts @@ -0,0 +1,3 @@ +export function foo() { + return 'foo'; +} diff --git a/packages/tsconfck/tests/fixtures/parse/valid/configDir/tsconfig.base.json b/packages/tsconfck/tests/fixtures/parse/valid/configDir/tsconfig.base.json new file mode 100644 index 0000000..c1a48dc --- /dev/null +++ b/packages/tsconfck/tests/fixtures/parse/valid/configDir/tsconfig.base.json @@ -0,0 +1,10 @@ +{ + "include": ["${configDir}/**/*"], + "exclude": ["${configDir}/src/**/*.spec.ts"], + "compilerOptions": { + "paths": { + "$src": ["${configDir}/src/**/*"] + }, + "outDir": "${configDir}/build" + } +} \ No newline at end of file diff --git a/packages/tsconfck/tests/parse-native.js b/packages/tsconfck/tests/parse-native.js index 3a5f79c..deea123 100644 --- a/packages/tsconfck/tests/parse-native.js +++ b/packages/tsconfck/tests/parse-native.js @@ -12,6 +12,9 @@ import { transform as esbuildTransform } from 'esbuild'; import ts from 'typescript'; import { TSConfckCache } from '../src/cache.js'; +const ts_ver = ts.version.slice(3).split('.'); +const IS_TS_5_5 = ts_ver[0] >= 5 && ts_ver[1] >= 5; + describe('parse', () => { it('should be a function', () => { expect(parseNative).toBeTypeOf('function'); @@ -51,6 +54,9 @@ describe('parse', () => { it('should resolve with expected valid tsconfig.json', async () => { const samples = await globFixtures('parse/valid/**/tsconfig.json'); for (const filename of samples) { + if (!IS_TS_5_5 && filename.includes('configDir')) { + continue; // configDir doesn't work in 5.4 + } const actual = await parseNative(filename); expect(actual.tsconfigFile).toBe(filename); await expectToMatchSnap(actual.tsconfig, `input: ${filename}`, filename, 'parse-native'); diff --git a/packages/tsconfck/tests/snapshots/parse/invalid/bom/tsconfig.json.error.node22.parse-native.txt b/packages/tsconfck/tests/snapshots/parse/invalid/bom/tsconfig.json.error.node22.parse-native.txt new file mode 100644 index 0000000..d5d81fe --- /dev/null +++ b/packages/tsconfck/tests/snapshots/parse/invalid/bom/tsconfig.json.error.node22.parse-native.txt @@ -0,0 +1 @@ +TS 1005: ',' expected. \ No newline at end of file diff --git a/packages/tsconfck/tests/snapshots/parse/invalid/bom/tsconfig.json.error.node22.parse.txt b/packages/tsconfck/tests/snapshots/parse/invalid/bom/tsconfig.json.error.node22.parse.txt new file mode 100644 index 0000000..aa587eb --- /dev/null +++ b/packages/tsconfck/tests/snapshots/parse/invalid/bom/tsconfig.json.error.node22.parse.txt @@ -0,0 +1,4 @@ +parsing /parse/invalid/bom/tsconfig.json failed: SyntaxError: Unexpected token 'x', "{ + "extends": x"" +} +" is not valid JSON \ No newline at end of file diff --git a/packages/tsconfck/tests/snapshots/parse/invalid/comma/tsconfig.json.error.node22.parse-native.txt b/packages/tsconfck/tests/snapshots/parse/invalid/comma/tsconfig.json.error.node22.parse-native.txt new file mode 100644 index 0000000..4816a75 --- /dev/null +++ b/packages/tsconfck/tests/snapshots/parse/invalid/comma/tsconfig.json.error.node22.parse-native.txt @@ -0,0 +1 @@ +TS 1136: Property assignment expected. \ No newline at end of file diff --git a/packages/tsconfck/tests/snapshots/parse/invalid/comma/tsconfig.json.error.node22.parse.txt b/packages/tsconfck/tests/snapshots/parse/invalid/comma/tsconfig.json.error.node22.parse.txt new file mode 100644 index 0000000..9784048 --- /dev/null +++ b/packages/tsconfck/tests/snapshots/parse/invalid/comma/tsconfig.json.error.node22.parse.txt @@ -0,0 +1 @@ +parsing /parse/invalid/comma/tsconfig.json failed: SyntaxError: Expected double-quoted property name in JSON at position 69 (line 5 column 6) \ No newline at end of file diff --git a/packages/tsconfck/tests/snapshots/parse/invalid/comments/tsconfig.json.error.node22.parse-native.txt b/packages/tsconfck/tests/snapshots/parse/invalid/comments/tsconfig.json.error.node22.parse-native.txt new file mode 100644 index 0000000..d5d81fe --- /dev/null +++ b/packages/tsconfck/tests/snapshots/parse/invalid/comments/tsconfig.json.error.node22.parse-native.txt @@ -0,0 +1 @@ +TS 1005: ',' expected. \ No newline at end of file diff --git a/packages/tsconfck/tests/snapshots/parse/invalid/comments/tsconfig.json.error.node22.parse.txt b/packages/tsconfck/tests/snapshots/parse/invalid/comments/tsconfig.json.error.node22.parse.txt new file mode 100644 index 0000000..e91e240 --- /dev/null +++ b/packages/tsconfck/tests/snapshots/parse/invalid/comments/tsconfig.json.error.node22.parse.txt @@ -0,0 +1 @@ +parsing /parse/invalid/comments/tsconfig.json failed: SyntaxError: Expected ',' or '}' after property value in JSON at position 236 (line 14 column 7) \ No newline at end of file diff --git a/packages/tsconfck/tests/snapshots/parse/invalid/extends-array-circular/tsconfig.json.error.node22.parse-native.txt b/packages/tsconfck/tests/snapshots/parse/invalid/extends-array-circular/tsconfig.json.error.node22.parse-native.txt new file mode 100644 index 0000000..a1029cc --- /dev/null +++ b/packages/tsconfck/tests/snapshots/parse/invalid/extends-array-circular/tsconfig.json.error.node22.parse-native.txt @@ -0,0 +1 @@ +TS 18000: Circularity detected while resolving configuration: /parse/invalid/extends-array-circular/tsconfig.json -> /parse/invalid/extends-array-circular/tsconfig.a.json -> /parse/invalid/extends-array-circular/tsconfig.a.b.json -> /parse/invalid/extends-array-circular/tsconfig.a.json \ No newline at end of file diff --git a/packages/tsconfck/tests/snapshots/parse/invalid/extends-array-circular/tsconfig.json.error.node22.parse.txt b/packages/tsconfck/tests/snapshots/parse/invalid/extends-array-circular/tsconfig.json.error.node22.parse.txt new file mode 100644 index 0000000..046ed16 --- /dev/null +++ b/packages/tsconfck/tests/snapshots/parse/invalid/extends-array-circular/tsconfig.json.error.node22.parse.txt @@ -0,0 +1 @@ +Circular dependency in "extends": /parse/invalid/extends-array-circular/tsconfig.json -> /parse/invalid/extends-array-circular/tsconfig.b.json -> /parse/invalid/extends-array-circular/tsconfig.a.b.json -> /parse/invalid/extends-array-circular/tsconfig.a.json -> /parse/invalid/extends-array-circular/tsconfig.a.b.json \ No newline at end of file diff --git a/packages/tsconfck/tests/snapshots/parse/invalid/extends-circular/tsconfig.json.error.node22.parse-native.txt b/packages/tsconfck/tests/snapshots/parse/invalid/extends-circular/tsconfig.json.error.node22.parse-native.txt new file mode 100644 index 0000000..bb748d5 --- /dev/null +++ b/packages/tsconfck/tests/snapshots/parse/invalid/extends-circular/tsconfig.json.error.node22.parse-native.txt @@ -0,0 +1 @@ +TS 18000: Circularity detected while resolving configuration: /parse/invalid/extends-circular/tsconfig.json -> /parse/invalid/extends-circular/tsconfig.circular.json -> /parse/invalid/extends-circular/deep/tsconfig.circular2.json -> /parse/invalid/extends-circular/tsconfig.json \ No newline at end of file diff --git a/packages/tsconfck/tests/snapshots/parse/invalid/extends-circular/tsconfig.json.error.node22.parse.txt b/packages/tsconfck/tests/snapshots/parse/invalid/extends-circular/tsconfig.json.error.node22.parse.txt new file mode 100644 index 0000000..d5acc88 --- /dev/null +++ b/packages/tsconfck/tests/snapshots/parse/invalid/extends-circular/tsconfig.json.error.node22.parse.txt @@ -0,0 +1 @@ +Circular dependency in "extends": /parse/invalid/extends-circular/tsconfig.json -> /parse/invalid/extends-circular/tsconfig.circular.json -> /parse/invalid/extends-circular/deep/tsconfig.circular2.json -> /parse/invalid/extends-circular/tsconfig.json \ No newline at end of file diff --git a/packages/tsconfck/tests/snapshots/parse/invalid/extends-fallback-not-found/tsconfig.json.error.node22.parse-native.txt b/packages/tsconfck/tests/snapshots/parse/invalid/extends-fallback-not-found/tsconfig.json.error.node22.parse-native.txt new file mode 100644 index 0000000..9fcd2b2 --- /dev/null +++ b/packages/tsconfck/tests/snapshots/parse/invalid/extends-fallback-not-found/tsconfig.json.error.node22.parse-native.txt @@ -0,0 +1 @@ +TS 6053: File './dir' not found. \ No newline at end of file diff --git a/packages/tsconfck/tests/snapshots/parse/invalid/extends-fallback-not-found/tsconfig.json.error.node22.parse.txt b/packages/tsconfck/tests/snapshots/parse/invalid/extends-fallback-not-found/tsconfig.json.error.node22.parse.txt new file mode 100644 index 0000000..ed44ca0 --- /dev/null +++ b/packages/tsconfck/tests/snapshots/parse/invalid/extends-fallback-not-found/tsconfig.json.error.node22.parse.txt @@ -0,0 +1 @@ +failed to resolve "extends":"./dir" in /parse/invalid/extends-fallback-not-found/tsconfig.json \ No newline at end of file diff --git a/packages/tsconfck/tests/snapshots/parse/invalid/extends-not-found/tsconfig.json.error.node22.parse-native.txt b/packages/tsconfck/tests/snapshots/parse/invalid/extends-not-found/tsconfig.json.error.node22.parse-native.txt new file mode 100644 index 0000000..ab236e5 --- /dev/null +++ b/packages/tsconfck/tests/snapshots/parse/invalid/extends-not-found/tsconfig.json.error.node22.parse-native.txt @@ -0,0 +1 @@ +TS 5083: Cannot read file '/parse/invalid/tsconfig.doesnotexist.json'. \ No newline at end of file diff --git a/packages/tsconfck/tests/snapshots/parse/invalid/extends-not-found/tsconfig.json.error.node22.parse.txt b/packages/tsconfck/tests/snapshots/parse/invalid/extends-not-found/tsconfig.json.error.node22.parse.txt new file mode 100644 index 0000000..9112231 --- /dev/null +++ b/packages/tsconfck/tests/snapshots/parse/invalid/extends-not-found/tsconfig.json.error.node22.parse.txt @@ -0,0 +1 @@ +failed to resolve "extends":"../tsconfig.doesnotexist.json" in /parse/invalid/extends-not-found/tsconfig.json \ No newline at end of file diff --git a/packages/tsconfck/tests/snapshots/parse/invalid/mixed/tsconfig.json.error.node22.parse-native.txt b/packages/tsconfck/tests/snapshots/parse/invalid/mixed/tsconfig.json.error.node22.parse-native.txt new file mode 100644 index 0000000..d5d81fe --- /dev/null +++ b/packages/tsconfck/tests/snapshots/parse/invalid/mixed/tsconfig.json.error.node22.parse-native.txt @@ -0,0 +1 @@ +TS 1005: ',' expected. \ No newline at end of file diff --git a/packages/tsconfck/tests/snapshots/parse/invalid/mixed/tsconfig.json.error.node22.parse.txt b/packages/tsconfck/tests/snapshots/parse/invalid/mixed/tsconfig.json.error.node22.parse.txt new file mode 100644 index 0000000..f024b60 --- /dev/null +++ b/packages/tsconfck/tests/snapshots/parse/invalid/mixed/tsconfig.json.error.node22.parse.txt @@ -0,0 +1 @@ +parsing /parse/invalid/mixed/tsconfig.json failed: SyntaxError: Expected ',' or '}' after property value in JSON at position 297 (line 16 column 5) \ No newline at end of file diff --git a/packages/tsconfck/tests/snapshots/parse/valid/configDir/a/tsconfig.json.parse-native.json b/packages/tsconfck/tests/snapshots/parse/valid/configDir/a/tsconfig.json.parse-native.json new file mode 100644 index 0000000..b26f32a --- /dev/null +++ b/packages/tsconfck/tests/snapshots/parse/valid/configDir/a/tsconfig.json.parse-native.json @@ -0,0 +1,19 @@ +{ + "extends": [ + "../tsconfig.base.json" + ], + "include": [ + "/parse/valid/configDir/a/**/*" + ], + "exclude": [ + "/parse/valid/configDir/a/src/**/*.spec.ts" + ], + "compilerOptions": { + "paths": { + "$src": [ + "/parse/valid/configDir/a/src/**/*" + ] + }, + "outDir": "/parse/valid/configDir/a/build" + } +} \ No newline at end of file diff --git a/packages/tsconfck/tests/snapshots/parse/valid/configDir/a/tsconfig.json.parse.json b/packages/tsconfck/tests/snapshots/parse/valid/configDir/a/tsconfig.json.parse.json new file mode 100644 index 0000000..58a641e --- /dev/null +++ b/packages/tsconfck/tests/snapshots/parse/valid/configDir/a/tsconfig.json.parse.json @@ -0,0 +1,19 @@ +{ + "extends": [ + "../tsconfig.base.json" + ], + "include": [ + "/parse/valid/configDir/**/*" + ], + "exclude": [ + "/parse/valid/configDir/src/**/*.spec.ts" + ], + "compilerOptions": { + "paths": { + "$src": [ + "/parse/valid/configDir/src/**/*" + ] + }, + "outDir": "/parse/valid/configDir/build" + } +} \ No newline at end of file diff --git a/packages/tsconfck/tests/snapshots/parse/valid/configDir/a/tsconfig.json.to-json.json b/packages/tsconfck/tests/snapshots/parse/valid/configDir/a/tsconfig.json.to-json.json new file mode 100644 index 0000000..4d4c4a7 --- /dev/null +++ b/packages/tsconfck/tests/snapshots/parse/valid/configDir/a/tsconfig.json.to-json.json @@ -0,0 +1,3 @@ +{ + "extends": ["../tsconfig.base.json"] +} \ No newline at end of file diff --git a/packages/tsconfck/tests/snapshots/parse/valid/configDir/b/tsconfig.json.parse-native.json b/packages/tsconfck/tests/snapshots/parse/valid/configDir/b/tsconfig.json.parse-native.json new file mode 100644 index 0000000..de0ba58 --- /dev/null +++ b/packages/tsconfck/tests/snapshots/parse/valid/configDir/b/tsconfig.json.parse-native.json @@ -0,0 +1,19 @@ +{ + "extends": [ + "../tsconfig.base.json" + ], + "include": [ + "/parse/valid/configDir/b/**/*" + ], + "exclude": [ + "/parse/valid/configDir/b/src/**/*.spec.ts" + ], + "compilerOptions": { + "paths": { + "$src": [ + "/parse/valid/configDir/b/src/**/*" + ] + }, + "outDir": "/parse/valid/configDir/b/build" + } +} \ No newline at end of file diff --git a/packages/tsconfck/tests/snapshots/parse/valid/configDir/b/tsconfig.json.parse.json b/packages/tsconfck/tests/snapshots/parse/valid/configDir/b/tsconfig.json.parse.json new file mode 100644 index 0000000..58a641e --- /dev/null +++ b/packages/tsconfck/tests/snapshots/parse/valid/configDir/b/tsconfig.json.parse.json @@ -0,0 +1,19 @@ +{ + "extends": [ + "../tsconfig.base.json" + ], + "include": [ + "/parse/valid/configDir/**/*" + ], + "exclude": [ + "/parse/valid/configDir/src/**/*.spec.ts" + ], + "compilerOptions": { + "paths": { + "$src": [ + "/parse/valid/configDir/src/**/*" + ] + }, + "outDir": "/parse/valid/configDir/build" + } +} \ No newline at end of file diff --git a/packages/tsconfck/tests/snapshots/parse/valid/configDir/b/tsconfig.json.to-json.json b/packages/tsconfck/tests/snapshots/parse/valid/configDir/b/tsconfig.json.to-json.json new file mode 100644 index 0000000..4d4c4a7 --- /dev/null +++ b/packages/tsconfck/tests/snapshots/parse/valid/configDir/b/tsconfig.json.to-json.json @@ -0,0 +1,3 @@ +{ + "extends": ["../tsconfig.base.json"] +} \ No newline at end of file diff --git a/packages/tsconfck/types/index.d.ts.map b/packages/tsconfck/types/index.d.ts.map index d6f7c95..a4bb954 100644 --- a/packages/tsconfck/types/index.d.ts.map +++ b/packages/tsconfck/types/index.d.ts.map @@ -38,5 +38,5 @@ null, null ], - "mappings": ";;;;;;;;iBAUsBA,IAAIA;;;;;;;;iBCYJC,OAAOA;;;;;;;iBCTbC,MAAMA;;;;;;;;;;iBCDAC,UAAUA;cCXnBC,aAAaA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBC0BJC,KAAKA;cA2VdC,kBAAkBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBC3VTC,WAAWA;cA6NpBC,wBAAwBA;;;;;;;;;;;;;;;;;;;;;;;;;WCtPpBC,mBAAmBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;WAkCnBC,oBAAoBA;;;;WAIpBC,sBAAsBA;;;;;;;;;;;;;;;WAetBC,mBAAmBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;WA6BnBC,0BAA0BA;;;;;;;;;;;;WAY1BC,yBAAyBA" + "mappings": ";;;;;;;;iBAUsBA,IAAIA;;;;;;;;iBCYJC,OAAOA;;;;;;;iBCTbC,MAAMA;;;;;;;;;;iBCDAC,UAAUA;cCXnBC,aAAaA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBC2BJC,KAAKA;cA4VdC,kBAAkBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBC5VTC,WAAWA;cAkOpBC,wBAAwBA;;;;;;;;;;;;;;;;;;;;;;;;;WC5PpBC,mBAAmBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;WAkCnBC,oBAAoBA;;;;WAIpBC,sBAAsBA;;;;;;;;;;;;;;;WAetBC,mBAAmBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;WA6BnBC,0BAA0BA;;;;;;;;;;;;WAY1BC,yBAAyBA" } \ No newline at end of file diff --git a/scripts/profile.js b/scripts/profile.js new file mode 100644 index 0000000..0e3d24b --- /dev/null +++ b/scripts/profile.js @@ -0,0 +1,91 @@ +// simpler version of bench.js that can be used with node profilers and cpupro +import { parse, parseNative, TSConfckCache } from 'tsconfck'; +import { execSync } from 'node:child_process'; +import glob from 'tiny-glob/sync.js'; +import { transform } from 'esbuild'; + +import fs from 'node:fs'; +import { fileURLToPath } from 'node:url'; +// version of typescript source to download. must be an existing release version +// downloaded and extracted with curl + tar -xzf +const TS_VERSION = '5.2.2'; + +const args = process.argv.slice(2); +const forceInit = args.includes('--init'); +const ioLoad = args.includes('--io-load'); +const cpuLoad = args.includes('--cpu-load'); +const useNative = args.includes('--use-native'); +const noCache = args.includes('--no-cache'); + +const profileDir = fileURLToPath(new URL('../.bench', import.meta.url)); +const srcDir = `${profileDir}/src`; + +async function prepare() { + if (forceInit && fs.existsSync(profileDir)) { + fs.rmSync(profileDir, { recursive: true }); + } + if (!fs.existsSync(profileDir)) { + console.log(`preparing ${srcDir}: download and extract typescript ${TS_VERSION} src`); + fs.mkdirSync(profileDir); + execSync( + `curl https://github.com/microsoft/TypeScript/archive/refs/tags/v${TS_VERSION}.tar.gz -L -o ${profileDir}/ts.tgz` + ); + try { + execSync( + `tar --transform s/TypeScript-${TS_VERSION}// -xzf ts.tgz TypeScript-${TS_VERSION}/src`, + { cwd: profileDir } + ); + } catch (e) { + if (!fs.existsSync(srcDir)) { + throw e; // on windows git-bash tar might still error even after successfully extracting it + } + } + + if (!fs.existsSync(srcDir)) { + throw new Error( + `failed to extract ${profileDir}/ts.tgz TypeScript-${TS_VERSION}/src to ${srcDir}` + ); + } + if (fs.existsSync(`${profileDir}/ts.tgz`)) { + fs.rmSync(`${profileDir}/ts.tgz`); + } + if (fs.existsSync(`${srcDir}/loc`)) { + fs.rmSync(`${srcDir}/loc`, { recursive: true }); // localization data + } + console.log('preparation done'); + throw new Error('restart process so that init does not skew results'); + } +} + +await prepare(); +const allTSFiles = glob(`${srcDir}/**/*.ts`); +for (let i = 0; i < 10; i++) { + const cache = new TSConfckCache(); + await Promise.all( + allTSFiles.map((f) => + Promise.all([ + (useNative ? parseNative : parse)(f, noCache ? {} : { cache }) + .then((r) => r.tsconfig) + .catch((e) => { + console.log(`parse for ${f} failed`, e); + return {}; + }), + ioLoad || cpuLoad ? fs.promises.readFile(f, 'utf-8') : Promise.resolve('') + ]).then(([config, input]) => { + if (config) { + config.compilerOptions = { ...config.compilerOptions }; // make sure it's used + } + return cpuLoad + ? transform(input, { + loader: 'ts', + tsconfigRaw: { compilerOptions: {} } + }).catch((e) => { + console.log(`transform for ${f} failed`, e); + }) + : Promise.resolve({ code: '' }); + }) + ) + ); +} + +console.log('done');