From b683018be30d1cbafb1f598e809cff4023472187 Mon Sep 17 00:00:00 2001 From: Florian Lefebvre Date: Sat, 24 Aug 2024 17:13:01 +0200 Subject: [PATCH] feat: excludeOutDir --- packages/astro/src/core/config/schema.ts | 7 ++++++ packages/astro/src/core/sync/write-files.ts | 12 ++++++----- packages/astro/src/types/public/config.ts | 24 +++++++++++++++++++++ packages/astro/test/typescript.test.js | 18 ++++++++++++++-- 4 files changed, 54 insertions(+), 7 deletions(-) diff --git a/packages/astro/src/core/config/schema.ts b/packages/astro/src/core/config/schema.ts index 7cab519f4a..fe9885ab3a 100644 --- a/packages/astro/src/core/config/schema.ts +++ b/packages/astro/src/core/config/schema.ts @@ -95,6 +95,9 @@ export const ASTRO_CONFIG_DEFAULTS = { serverIslands: false, contentIntellisense: false, contentLayer: false, + typescript: { + excludeOutDir: true, + }, }, } satisfies AstroUserConfig & { server: { open: boolean } }; @@ -544,6 +547,10 @@ export const AstroConfigSchema = z.object({ .object({ include: z.array(z.string()).optional(), exclude: z.array(z.string()).optional(), + excludeOutDir: z + .boolean() + .optional() + .default(ASTRO_CONFIG_DEFAULTS.experimental.typescript.excludeOutDir), }) .optional(), }) diff --git a/packages/astro/src/core/sync/write-files.ts b/packages/astro/src/core/sync/write-files.ts index ba82058e7e..90730cea10 100644 --- a/packages/astro/src/core/sync/write-files.ts +++ b/packages/astro/src/core/sync/write-files.ts @@ -82,6 +82,8 @@ async function setupEnvDts(settings: AstroSettings, fs: typeof fsMod, logger: Lo } async function setupTsconfig(settings: AstroSettings, fs: typeof fsMod, logger: Logger) { + const typescript = settings.config.experimental.typescript!; + const tsconfigPath = normalizePath(fileURLToPath(new URL('tsconfig.json', settings.dotAstroDir))); let relativeDtsPath = normalizePath( relative( @@ -97,8 +99,8 @@ async function setupTsconfig(settings: AstroSettings, fs: typeof fsMod, logger: ); const include = [relativeDtsPath]; - if (settings.config.experimental.typescript?.include) { - for (const value of settings.config.experimental.typescript.include) { + if (typescript.include) { + for (const value of typescript.include) { if (startsWithDotSlash(value) || startsWithDotDotSlash(value)) { include.push( normalizePath( @@ -113,9 +115,9 @@ async function setupTsconfig(settings: AstroSettings, fs: typeof fsMod, logger: } } } - const exclude = [relativeOutDirPath]; - if (settings.config.experimental.typescript?.exclude) { - for (const value of settings.config.experimental.typescript.exclude) { + const exclude = typescript.excludeOutDir ? [relativeOutDirPath] : []; + if (typescript.exclude) { + for (const value of typescript.exclude) { if (startsWithDotSlash(value) || startsWithDotDotSlash(value)) { exclude.push( normalizePath( diff --git a/packages/astro/src/types/public/config.ts b/packages/astro/src/types/public/config.ts index f494672214..11028d1676 100644 --- a/packages/astro/src/types/public/config.ts +++ b/packages/astro/src/types/public/config.ts @@ -2067,6 +2067,30 @@ export interface AstroUserConfig { * ``` */ exclude?: Array; + + /** + * @docs + * @name experimental.typescript.excludeOutDir + * @type {boolean} + * @default `true` + * @version 5.0.0 + * @description + * + * By default, `outDir` will be added to excluded typescript files. You can opt-out of this behavior by setting this option to `false`: + * + * ```js title="astro.config.*" ins={6} + * import { defineConfig } from 'astro/config' + * + * export default defineConfig({ + * experimental: { + * typescript: { + * excludeOutDir: false + * } + * } + * }) + * ``` + */ + excludeOutDir?: boolean; }; }; } diff --git a/packages/astro/test/typescript.test.js b/packages/astro/test/typescript.test.js index 69019d1252..e2642d920c 100644 --- a/packages/astro/test/typescript.test.js +++ b/packages/astro/test/typescript.test.js @@ -126,15 +126,29 @@ describe('experimental.typescript', () => { } }); - it('should add outDir to .astro/tsconfig.json', async () => { + it('should add outDir to .astro/tsconfig.json if excludeOutDir is enabled', async () => { for (const outDir of ['dist', 'custom']) { const fixture = await createFixture({ experimental: { typescript: {} }, outDir }); await fixture.sync(); - const tsconfig = JSON.parse(await fixture.readFile(GENERATED_TSCONFIG_PATH)); + const raw = await fixture.readFile(GENERATED_TSCONFIG_PATH); + const tsconfig = JSON.parse(raw); assert.equal(tsconfig.exclude.includes(`../${outDir}`), true); } }); + it('should not add outDir to .astro/tsconfig.json if excludeOutDir is disabled', async () => { + for (const outDir of ['dist', 'custom']) { + const fixture = await createFixture({ + experimental: { typescript: { excludeOutDir: false } }, + outDir, + }); + await fixture.sync(); + const raw = await fixture.readFile(GENERATED_TSCONFIG_PATH); + const tsconfig = JSON.parse(raw); + assert.equal(tsconfig.exclude.includes(`../${outDir}`), false); + } + }); + it('should handle include/exclude relative paths', async () => { const dirs = [ { outDir: 'dist', exclude: '../dist' },