From 832428945331e7ca3649fd3934726de6a10eff32 Mon Sep 17 00:00:00 2001 From: Princesseuh Date: Tue, 10 Oct 2023 18:51:34 +0200 Subject: [PATCH] feat: use tsconfck instead of tsconfig-resolver --- packages/astro/package.json | 2 +- packages/astro/src/@types/astro.ts | 4 +- packages/astro/src/cli/add/index.ts | 26 ++-- packages/astro/src/config/index.ts | 2 +- .../astro/src/content/server-listeners.ts | 16 +-- packages/astro/src/core/build/index.ts | 2 +- packages/astro/src/core/config/settings.ts | 18 ++- packages/astro/src/core/config/tsconfig.ts | 125 +++++++++++++----- packages/astro/src/core/dev/restart.ts | 4 +- packages/astro/src/core/preview/index.ts | 2 +- packages/astro/src/core/sync/index.ts | 2 +- .../src/vite-plugin-config-alias/index.ts | 3 +- .../test/units/config/config-tsconfig.test.js | 49 +++---- pnpm-lock.yaml | 24 +++- 14 files changed, 177 insertions(+), 102 deletions(-) diff --git a/packages/astro/package.json b/packages/astro/package.json index 33bf6eb4f..936ac2c36 100644 --- a/packages/astro/package.json +++ b/packages/astro/package.json @@ -167,7 +167,7 @@ "shiki": "^0.14.3", "string-width": "^6.1.0", "strip-ansi": "^7.1.0", - "tsconfig-resolver": "^3.0.1", + "tsconfck": "3.0.0-next.9", "unist-util-visit": "^4.1.2", "vfile": "^5.3.7", "vite": "^4.4.9", diff --git a/packages/astro/src/@types/astro.ts b/packages/astro/src/@types/astro.ts index 2217e76f2..45a82b355 100644 --- a/packages/astro/src/@types/astro.ts +++ b/packages/astro/src/@types/astro.ts @@ -11,13 +11,13 @@ import type * as babel from '@babel/core'; import type { OutgoingHttpHeaders } from 'node:http'; import type { AddressInfo } from 'node:net'; import type * as rollup from 'rollup'; -import type { TsConfigJson } from 'tsconfig-resolver'; import type * as vite from 'vite'; import type { RemotePattern } from '../assets/utils/remotePattern.js'; import type { SerializedSSRManifest } from '../core/app/types.js'; import type { PageBuildData } from '../core/build/types.js'; import type { AstroConfigType } from '../core/config/index.js'; import type { AstroTimer } from '../core/config/timer.js'; +import type { TSConfig } from '../core/config/tsconfig.js'; import type { AstroCookies } from '../core/cookies/index.js'; import type { ResponseWithEncoding } from '../core/endpoint/index.js'; import type { AstroIntegrationLogger, Logger, LoggerLevel } from '../core/logger/core.js'; @@ -1503,7 +1503,7 @@ export interface AstroSettings { * Map of directive name (e.g. `load`) to the directive script code */ clientDirectives: Map; - tsConfig: TsConfigJson | undefined; + tsConfig: TSConfig | undefined; tsConfigPath: string | undefined; watchFiles: string[]; timer: AstroTimer; diff --git a/packages/astro/src/cli/add/index.ts b/packages/astro/src/cli/add/index.ts index 859a95c0a..d47db0e99 100644 --- a/packages/astro/src/cli/add/index.ts +++ b/packages/astro/src/cli/add/index.ts @@ -849,25 +849,31 @@ async function updateTSConfig( return UpdateResult.none; } - const inputConfig = loadTSConfig(cwd, false); - const configFileName = inputConfig.exists ? inputConfig.path.split('/').pop() : 'tsconfig.json'; + let inputConfig = await loadTSConfig(cwd); + let inputConfigText = ''; - if (inputConfig.reason === 'invalid-config') { + if (inputConfig === 'invalid-config') { return UpdateResult.failure; + } else if (inputConfig === 'missing-config' || inputConfig === 'unknown-error') { + logger.debug('add', "Couldn't find tsconfig.json or jsconfig.json, generating one"); + inputConfig = { + tsconfig: defaultTSConfig, + tsconfigFile: path.join(cwd, 'tsconfig.json'), + rawConfig: { tsconfig: defaultTSConfig, tsconfigFile: path.join(cwd, 'tsconfig.json') }, + }; + } else { + inputConfigText = JSON.stringify(inputConfig.rawConfig.tsconfig, null, 2); } - if (inputConfig.reason === 'not-found') { - logger.debug('add', "Couldn't find tsconfig.json or jsconfig.json, generating one"); - } + const configFileName = inputConfig.tsconfigFile.split('/').pop(); const outputConfig = updateTSConfigForFramework( - inputConfig.exists ? inputConfig.config : defaultTSConfig, + inputConfig.rawConfig.tsconfig, firstIntegrationWithTSSettings ); - const input = inputConfig.exists ? JSON.stringify(inputConfig.config, null, 2) : ''; const output = JSON.stringify(outputConfig, null, 2); - const diff = getDiffContent(input, output); + const diff = getDiffContent(inputConfigText, output); if (!diff) { return UpdateResult.none; @@ -907,7 +913,7 @@ async function updateTSConfig( } if (await askToContinue({ flags })) { - await fs.writeFile(inputConfig?.path ?? path.join(cwd, 'tsconfig.json'), output, { + await fs.writeFile(inputConfig.tsconfigFile, output, { encoding: 'utf-8', }); logger.debug('add', `Updated ${configFileName} file`); diff --git a/packages/astro/src/config/index.ts b/packages/astro/src/config/index.ts index 8625140ea..fc45ae983 100644 --- a/packages/astro/src/config/index.ts +++ b/packages/astro/src/config/index.ts @@ -35,7 +35,7 @@ export function getViteConfig(inlineConfig: UserConfig) { level: 'info', }); const { astroConfig: config } = await resolveConfig({}, cmd); - const settings = createSettings(config, inlineConfig.root); + const settings = await createSettings(config, inlineConfig.root); await runHookConfigSetup({ settings, command: cmd, logger }); const viteConfig = await createVite( { diff --git a/packages/astro/src/content/server-listeners.ts b/packages/astro/src/content/server-listeners.ts index c5e3da2c4..699d5f271 100644 --- a/packages/astro/src/content/server-listeners.ts +++ b/packages/astro/src/content/server-listeners.ts @@ -32,7 +32,7 @@ export async function attachContentServerListeners({ contentPaths.contentDir.href.replace(settings.config.root.href, '') )} for changes` ); - const maybeTsConfigStats = getTSConfigStatsWhenAllowJsFalse({ contentPaths, settings }); + const maybeTsConfigStats = await getTSConfigStatsWhenAllowJsFalse({ contentPaths, settings }); if (maybeTsConfigStats) warnAllowJsIsFalse({ ...maybeTsConfigStats, logger }); await attachListeners(); } else { @@ -96,7 +96,7 @@ See ${bold('https://www.typescriptlang.org/tsconfig#allowJs')} for more informat ); } -function getTSConfigStatsWhenAllowJsFalse({ +async function getTSConfigStatsWhenAllowJsFalse({ contentPaths, settings, }: { @@ -108,15 +108,15 @@ function getTSConfigStatsWhenAllowJsFalse({ ); if (!isContentConfigJsFile) return; - const inputConfig = loadTSConfig(fileURLToPath(settings.config.root), false); - const tsConfigFileName = inputConfig.exists && inputConfig.path.split(path.sep).pop(); + const inputConfig = await loadTSConfig(fileURLToPath(settings.config.root)); + if (typeof inputConfig === 'string') return; + + const tsConfigFileName = inputConfig.tsconfigFile.split(path.sep).pop(); if (!tsConfigFileName) return; const contentConfigFileName = contentPaths.config.url.pathname.split(path.sep).pop()!; - const allowJSOption = inputConfig?.config?.compilerOptions?.allowJs; - const hasAllowJs = - allowJSOption === true || (tsConfigFileName === 'jsconfig.json' && allowJSOption !== false); - if (hasAllowJs) return; + const allowJSOption = inputConfig.tsconfig.compilerOptions?.allowJs; + if (allowJSOption) return; return { tsConfigFileName, contentConfigFileName }; } diff --git a/packages/astro/src/core/build/index.ts b/packages/astro/src/core/build/index.ts index 5f5ae69a1..797a29a25 100644 --- a/packages/astro/src/core/build/index.ts +++ b/packages/astro/src/core/build/index.ts @@ -59,7 +59,7 @@ export default async function build( const { userConfig, astroConfig } = await resolveConfig(inlineConfig, 'build'); telemetry.record(eventCliSession('build', userConfig)); - const settings = createSettings(astroConfig, fileURLToPath(astroConfig.root)); + const settings = await createSettings(astroConfig, fileURLToPath(astroConfig.root)); const builder = new AstroBuilder(settings, { ...options, diff --git a/packages/astro/src/core/config/settings.ts b/packages/astro/src/core/config/settings.ts index 8b70f6e96..2c32de2b4 100644 --- a/packages/astro/src/core/config/settings.ts +++ b/packages/astro/src/core/config/settings.ts @@ -102,18 +102,22 @@ export function createBaseSettings(config: AstroConfig): AstroSettings { }; } -export function createSettings(config: AstroConfig, cwd?: string): AstroSettings { - const tsconfig = loadTSConfig(cwd); +export async function createSettings(config: AstroConfig, cwd?: string): Promise { + const tsconfig = await loadTSConfig(cwd); const settings = createBaseSettings(config); - const watchFiles = tsconfig?.exists ? [tsconfig.path, ...tsconfig.extendedPaths] : []; - + let watchFiles = []; if (cwd) { watchFiles.push(fileURLToPath(new URL('./package.json', pathToFileURL(cwd)))); } - settings.tsConfig = tsconfig?.config; - settings.tsConfigPath = tsconfig?.path; - settings.watchFiles = watchFiles; + if (typeof tsconfig !== 'string') { + watchFiles = [tsconfig.tsconfigFile, ...(tsconfig.extended ?? []).map((e) => e.tsconfigFile)]; + + settings.watchFiles = watchFiles; + settings.tsConfig = tsconfig.tsconfig; + settings.tsConfigPath = tsconfig.tsconfigFile; + } + return settings; } diff --git a/packages/astro/src/core/config/tsconfig.ts b/packages/astro/src/core/config/tsconfig.ts index a0c78f08c..408c2a1a4 100644 --- a/packages/astro/src/core/config/tsconfig.ts +++ b/packages/astro/src/core/config/tsconfig.ts @@ -1,13 +1,19 @@ -import { existsSync } from 'node:fs'; import { join } from 'node:path'; -import * as tsr from 'tsconfig-resolver'; +import { + TSConfckParseError, + find, + parse, + type TSConfckParseOptions, + type TSConfckParseResult, +} from 'tsconfck'; +import type { CompilerOptions, TypeAcquisition } from 'typescript'; -export const defaultTSConfig: tsr.TsConfigJson = { extends: 'astro/tsconfigs/base' }; +export const defaultTSConfig: TSConfig = { extends: 'astro/tsconfigs/base' }; export type frameworkWithTSSettings = 'vue' | 'react' | 'preact' | 'solid-js'; // The following presets unfortunately cannot be inside the specific integrations, as we need // them even in cases where the integrations are not installed -export const presets = new Map([ +export const presets = new Map([ [ 'vue', // Settings needed for template intellisense when using Volar { @@ -45,52 +51,74 @@ export const presets = new Map([ ], ]); +// eslint-disable-next-line @typescript-eslint/ban-types +type TSConfigResult = Promise< + (TSConfckParseResult & T) | 'invalid-config' | 'missing-config' | 'unknown-error' +>; + /** * Load a tsconfig.json or jsconfig.json is the former is not found * @param cwd Directory to start from * @param resolve Determine if the function should go up directories like TypeScript would */ -export function loadTSConfig(cwd: string | undefined, resolve = true): tsr.TsConfigResult { - cwd = cwd ?? process.cwd(); - let config = tsr.tsconfigResolverSync({ - cwd, - filePath: resolve ? undefined : cwd, - ignoreExtends: !resolve, - }); +export async function loadTSConfig( + cwd: string | undefined +): Promise> { + const safeCwd = cwd ?? process.cwd(); - // When a direct filepath is provided to `tsconfigResolver`, it'll instead return invalid-config even when - // the file does not exists. We'll manually handle this so we can provide better errors to users - if (!resolve && config.reason === 'invalid-config' && !existsSync(join(cwd, 'tsconfig.json'))) { - config = { reason: 'not-found', path: undefined, exists: false }; - } + const [jsconfig, tsconfig] = await Promise.all( + ['jsconfig.json', 'tsconfig.json'].map((configName) => + // `tsconfck` expects its first argument to be a file path, not a directory path, so we fake one + find(join(safeCwd, './dummy.txt'), { root: cwd, configName: configName }) + ) + ); - // If we couldn't find a tsconfig.json, try to load a jsconfig.json instead - if (config.reason === 'not-found') { - const jsconfig = tsr.tsconfigResolverSync({ - cwd, - filePath: resolve ? undefined : cwd, - searchName: 'jsconfig.json', - ignoreExtends: !resolve, - }); + // If we have both files, prefer tsconfig.json + if (tsconfig) { + const parsedConfig = await safeParse(tsconfig, { root: cwd }); - if ( - !resolve && - jsconfig.reason === 'invalid-config' && - !existsSync(join(cwd, 'jsconfig.json')) - ) { - return { reason: 'not-found', path: undefined, exists: false }; + if (typeof parsedConfig === 'string') { + return parsedConfig; } - return jsconfig; + return { ...parsedConfig, rawConfig: parsedConfig.extended?.[0] ?? parsedConfig.tsconfig }; } - return config; + if (jsconfig) { + const parsedConfig = await safeParse(jsconfig, { root: cwd }); + + if (typeof parsedConfig === 'string') { + return parsedConfig; + } + + return { ...parsedConfig, rawConfig: parsedConfig.extended?.[0] ?? parsedConfig.tsconfig }; + } + + return 'missing-config'; +} + +async function safeParse(tsconfigPath: string, options: TSConfckParseOptions = {}): TSConfigResult { + try { + const parseResult = await parse(tsconfigPath, options); + + if (parseResult.tsconfig == null) { + return 'missing-config'; + } + + return parseResult; + } catch (e) { + if (e instanceof TSConfckParseError) { + return 'invalid-config'; + } + + return 'unknown-error'; + } } export function updateTSConfigForFramework( - target: tsr.TsConfigJson, + target: TSConfig, framework: frameworkWithTSSettings -): tsr.TsConfigJson { +): TSConfig { if (!presets.has(framework)) { return target; } @@ -120,3 +148,32 @@ function deepMergeObjects>(a: T, b: T): T { return merged; } + +// The code below is adapted from `pkg-types` +// `pkg-types` offer more types and utilities, but since we only want the TSConfig type, we'd rather avoid adding a dependency. +// https://github.com/unjs/pkg-types/blob/78328837d369d0145a8ddb35d7fe1fadda4bfadf/src/types/tsconfig.ts +// See https://github.com/unjs/pkg-types/blob/78328837d369d0145a8ddb35d7fe1fadda4bfadf/LICENSE for license information + +export type StripEnums> = { + [K in keyof T]: T[K] extends boolean + ? T[K] + : T[K] extends string + ? T[K] + : T[K] extends object + ? T[K] + : T[K] extends Array + ? T[K] + : T[K] extends undefined + ? undefined + : any; +}; + +export interface TSConfig { + compilerOptions?: StripEnums; + compileOnSave?: boolean; + extends?: string; + files?: string[]; + include?: string[]; + exclude?: string[]; + typeAcquisition?: TypeAcquisition; +} diff --git a/packages/astro/src/core/dev/restart.ts b/packages/astro/src/core/dev/restart.ts index 2d6ba75f3..f03db39cf 100644 --- a/packages/astro/src/core/dev/restart.ts +++ b/packages/astro/src/core/dev/restart.ts @@ -64,7 +64,7 @@ export async function restartContainer(container: Container): Promise { const { tsConfig, tsConfigPath } = settings; if (!tsConfig || !tsConfigPath || !tsConfig.compilerOptions) return null; - const { baseUrl, paths } = tsConfig.compilerOptions; + const { baseUrl, paths } = tsConfig.compilerOptions as CompilerOptions; if (!baseUrl) return null; // resolve the base url from the configuration file directory diff --git a/packages/astro/test/units/config/config-tsconfig.test.js b/packages/astro/test/units/config/config-tsconfig.test.js index 4b87793a9..09556d083 100644 --- a/packages/astro/test/units/config/config-tsconfig.test.js +++ b/packages/astro/test/units/config/config-tsconfig.test.js @@ -1,51 +1,40 @@ import { expect } from 'chai'; +import * as path from 'node:path'; import { fileURLToPath } from 'node:url'; import { loadTSConfig, updateTSConfigForFramework } from '../../../dist/core/config/index.js'; -import * as path from 'node:path'; -import * as tsr from 'tsconfig-resolver'; const cwd = fileURLToPath(new URL('../../fixtures/tsconfig-handling/', import.meta.url)); describe('TSConfig handling', () => { - beforeEach(() => { - // `tsconfig-resolver` has a weird internal cache that only vaguely respect its own rules when not resolving - // so we need to clear it before each test or we'll get false positives. This should only be relevant in tests. - tsr.clearCache(); - }); - describe('tsconfig / jsconfig loading', () => { - it('can load tsconfig.json', () => { - const config = loadTSConfig(cwd); + it('can load tsconfig.json', async () => { + const config = await loadTSConfig(cwd); - expect(config.exists).to.equal(true); - expect(config.config.files).to.deep.equal(['im-a-test']); + expect(config).to.not.be.undefined; }); - it('can resolve tsconfig.json up directories', () => { - const config = loadTSConfig(path.join(cwd, 'nested-folder')); + it('can resolve tsconfig.json up directories', async () => { + const config = await loadTSConfig(path.join(cwd, 'nested-folder')); - expect(config.exists).to.equal(true); - expect(config.path).to.equal(path.join(cwd, 'tsconfig.json')); - expect(config.config.files).to.deep.equal(['im-a-test']); + expect(config).to.not.be.undefined; + expect(config.tsconfigFile).to.equal(path.join(cwd, 'tsconfig.json')); + expect(config.tsconfig.files).to.deep.equal(['im-a-test']); }); - it('can fallback to jsconfig.json if tsconfig.json does not exists', () => { - const config = loadTSConfig(path.join(cwd, 'jsconfig'), false); + it('can fallback to jsconfig.json if tsconfig.json does not exists', async () => { + const config = await loadTSConfig(path.join(cwd, 'jsconfig')); - expect(config.exists).to.equal(true); - expect(config.path).to.equal(path.join(cwd, 'jsconfig', 'jsconfig.json')); - expect(config.config.files).to.deep.equal(['im-a-test-js']); + expect(config).to.not.be.undefined; + expect(config.tsconfigFile).to.equal(path.join(cwd, 'jsconfig', 'jsconfig.json')); + expect(config.tsconfig.files).to.deep.equal(['im-a-test-js']); }); - it('properly return errors when not resolving', () => { - const invalidConfig = loadTSConfig(path.join(cwd, 'invalid'), false); - const missingConfig = loadTSConfig(path.join(cwd, 'missing'), false); + it('properly return errors when not resolving', async () => { + const invalidConfig = await loadTSConfig(path.join(cwd, 'invalid')); + const missingConfig = await loadTSConfig(path.join(cwd, 'missing')); - expect(invalidConfig.exists).to.equal(false); - expect(invalidConfig.reason).to.equal('invalid-config'); - - expect(missingConfig.exists).to.equal(false); - expect(missingConfig.reason).to.equal('not-found'); + expect(invalidConfig).to.equal('invalid-config'); + expect(missingConfig).to.equal('missing-config'); }); }); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c39875566..9a49bbf35 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -625,9 +625,9 @@ importers: strip-ansi: specifier: ^7.1.0 version: 7.1.0 - tsconfig-resolver: - specifier: ^3.0.1 - version: 3.0.1 + tsconfck: + specifier: 3.0.0-next.9 + version: 3.0.0-next.9(typescript@5.1.6) unist-util-visit: specifier: ^4.1.2 version: 4.1.2 @@ -8834,6 +8834,7 @@ packages: /@types/json5@0.0.30: resolution: {integrity: sha512-sqm9g7mHlPY/43fcSNrCYfOeX9zkTTK+euO5E6+CVijSMm5tTjkVdwdqRkY3ljjIAf8679vps5jKUoJBCLsMDA==} + dev: true /@types/katex@0.16.0: resolution: {integrity: sha512-hz+S3nV6Mym5xPbT9fnO8dDhBFQguMYpY0Ipxv06JMi1ORgnEM4M1ymWDUhUNer3ElLmT583opRo4RzxKmh9jw==} @@ -8983,6 +8984,7 @@ packages: /@types/resolve@1.20.2: resolution: {integrity: sha512-60BCwRFOZCQhDncwQdxxeOEEkbc5dIMccYLwbxsS4TUNeVECQ/pBJ0j09mrHOl/JJvpRPGwO9SvE4nR2Nb/a4Q==} + dev: true /@types/sax@1.2.4: resolution: {integrity: sha512-pSAff4IAxJjfAXUG6tFkO7dsSbTmf8CtUpfhhZ5VhkRpC4628tJhh3+V6H1E+/Gs9piSzYKT5yzHO5M4GG9jkw==} @@ -16595,6 +16597,7 @@ packages: /strip-bom@4.0.0: resolution: {integrity: sha512-3xurFv5tEgii33Zi8Jtp55wEIILR9eh34FAW00PZf+JnSsTmV/ioewSgQl97JHvgjoRGwPShsWm+IdrxB35d0w==} engines: {node: '>=8'} + dev: true /strip-comments@2.0.1: resolution: {integrity: sha512-ZprKx+bBLXv067WTCALv8SSz5l2+XhpYCsVtSqlMnkAXMWDq+/ekVbl1ghqP9rUHTzv6sm/DwCOiYutU/yp1fw==} @@ -17017,6 +17020,19 @@ packages: code-block-writer: 12.0.0 dev: true + /tsconfck@3.0.0-next.9(typescript@5.1.6): + resolution: {integrity: sha512-bgVlu3qcRUZpm9Au1IHiPDkb8XU+72bRkXrBaJsiAjIlixtkbKLe4q1odrrqG0rVHvh0Q4R3adT/nh1FwzftXA==} + engines: {node: ^18 || >=20} + hasBin: true + peerDependencies: + typescript: ^5.0.0 + peerDependenciesMeta: + typescript: + optional: true + dependencies: + typescript: 5.1.6 + dev: false + /tsconfig-resolver@3.0.1: resolution: {integrity: sha512-ZHqlstlQF449v8glscGRXzL6l2dZvASPCdXJRWG4gHEZlUVx2Jtmr+a2zeVG4LCsKhDXKRj5R3h0C/98UcVAQg==} dependencies: @@ -17026,6 +17042,7 @@ packages: resolve: 1.22.4 strip-bom: 4.0.0 type-fest: 3.0.0 + dev: true /tsconfig@7.0.0: resolution: {integrity: sha512-vZXmzPrL+EmC4T/4rVlT2jNVMWCi/O4DIiSj3UHg1OE5kCKbk4mfrXc6dZksLgRM/TZlKnousKH9bbTazUWRRw==} @@ -17176,6 +17193,7 @@ packages: /type-fest@3.0.0: resolution: {integrity: sha512-MINvUN5ug9u+0hJDzSZNSnuKXI8M4F5Yvb6SQZ2CYqe7SgKXKOosEcU5R7tRgo85I6eAVBbkVF7TCvB4AUK2xQ==} engines: {node: '>=14.16'} + dev: true /type-is@1.6.18: resolution: {integrity: sha512-TkRKr9sUTxEH8MdfuCSP7VizJyzRNMjj2J2do2Jr3Kym598JVdEksuzPQCnlFPW4ky9Q+iA+ma9BGm06XQBy8g==}