Skip to content

Commit

Permalink
feat(next): better tsconfig (#11859)
Browse files Browse the repository at this point in the history
Co-authored-by: Sarah Rainsberger <[email protected]>
Co-authored-by: Bjorn Lu <[email protected]>
  • Loading branch information
3 people authored Sep 3, 2024
1 parent 5a39da8 commit 3804711
Show file tree
Hide file tree
Showing 80 changed files with 207 additions and 184 deletions.
19 changes: 19 additions & 0 deletions .changeset/old-zebras-teach.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
'astro': major
---

Changes the default `tsconfig.json` with better defaults, and makes `src/env.d.ts` optional

Astro's default `tsconfig.json` in starter examples has been updated to include generated types and exclude your build output. This means that `src/env.d.ts` is only necessary if you have added custom type declarations or if you're not using a `tsconfig.json` file.

Additionally, running `astro sync` no longer creates, nor updates, `src/env.d.ts` as it is not required for type-checking standard Astro projects.

To update your project to Astro's recommended TypeScript settings, please add the following `include` and `exclude` properties to `tsconfig.json`:

```diff
{
"extends": "astro/tsconfigs/base",
+ "include": ["**/*", ".astro/types.d.ts"],
+ "exclude": ["dist"]
}
```
4 changes: 3 additions & 1 deletion examples/basics/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"extends": "astro/tsconfigs/base"
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}
1 change: 0 additions & 1 deletion examples/blog/src/env.d.ts

This file was deleted.

2 changes: 2 additions & 0 deletions examples/blog/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"],
"compilerOptions": {
"strictNullChecks": true
}
Expand Down
2 changes: 2 additions & 0 deletions examples/component/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"],
"compilerOptions": {
"jsx": "preserve"
}
Expand Down
4 changes: 3 additions & 1 deletion examples/container-with-vitest/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"extends": "astro/tsconfigs/base"
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}
1 change: 0 additions & 1 deletion examples/framework-alpine/src/env.d.ts

This file was deleted.

4 changes: 3 additions & 1 deletion examples/framework-alpine/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"extends": "astro/tsconfigs/base"
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}
1 change: 0 additions & 1 deletion examples/framework-multiple/src/env.d.ts

This file was deleted.

2 changes: 2 additions & 0 deletions examples/framework-multiple/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"],
"compilerOptions": {
// Needed for TypeScript intellisense in the template inside Vue files
"jsx": "preserve"
Expand Down
1 change: 0 additions & 1 deletion examples/framework-preact/src/env.d.ts

This file was deleted.

2 changes: 2 additions & 0 deletions examples/framework-preact/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"],
"compilerOptions": {
// Preact specific settings
"jsx": "react-jsx",
Expand Down
1 change: 0 additions & 1 deletion examples/framework-react/src/env.d.ts

This file was deleted.

2 changes: 2 additions & 0 deletions examples/framework-react/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"],
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "react"
Expand Down
1 change: 0 additions & 1 deletion examples/framework-solid/src/env.d.ts

This file was deleted.

2 changes: 2 additions & 0 deletions examples/framework-solid/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"],
"compilerOptions": {
// Solid specific settings
"jsx": "preserve",
Expand Down
1 change: 0 additions & 1 deletion examples/framework-svelte/src/env.d.ts

This file was deleted.

4 changes: 3 additions & 1 deletion examples/framework-svelte/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"extends": "astro/tsconfigs/base"
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}
1 change: 0 additions & 1 deletion examples/framework-vue/src/env.d.ts

This file was deleted.

2 changes: 2 additions & 0 deletions examples/framework-vue/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"],
"compilerOptions": {
// Needed for TypeScript intellisense in the template inside Vue files
"jsx": "preserve"
Expand Down
1 change: 0 additions & 1 deletion examples/hackernews/src/env.d.ts

This file was deleted.

4 changes: 3 additions & 1 deletion examples/hackernews/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"extends": "astro/tsconfigs/base"
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}
1 change: 0 additions & 1 deletion examples/middleware/src/env.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/// <reference path="../.astro/types.d.ts" />
declare namespace App {
interface Locals {
user: {
Expand Down
4 changes: 3 additions & 1 deletion examples/middleware/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"extends": "astro/tsconfigs/base"
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}
1 change: 0 additions & 1 deletion examples/minimal/src/env.d.ts

This file was deleted.

4 changes: 3 additions & 1 deletion examples/minimal/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"extends": "astro/tsconfigs/base"
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}
1 change: 0 additions & 1 deletion examples/non-html-pages/src/env.d.ts

This file was deleted.

4 changes: 3 additions & 1 deletion examples/non-html-pages/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"extends": "astro/tsconfigs/base"
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}
1 change: 0 additions & 1 deletion examples/portfolio/src/env.d.ts

This file was deleted.

4 changes: 3 additions & 1 deletion examples/portfolio/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"extends": "astro/tsconfigs/base"
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}
1 change: 0 additions & 1 deletion examples/ssr/src/env.d.ts

This file was deleted.

4 changes: 3 additions & 1 deletion examples/ssr/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"extends": "astro/tsconfigs/base"
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}
1 change: 1 addition & 0 deletions examples/starlog/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"extends": "astro/tsconfigs/strict",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}
2 changes: 2 additions & 0 deletions examples/view-transitions/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"],
"compilerOptions": {
"resolveJsonModule": true
}
Expand Down
1 change: 0 additions & 1 deletion examples/with-markdoc/src/env.d.ts

This file was deleted.

2 changes: 2 additions & 0 deletions examples/with-markdoc/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"],
"compilerOptions": {
"strictNullChecks": true
}
Expand Down
1 change: 0 additions & 1 deletion examples/with-markdown-plugins/src/env.d.ts

This file was deleted.

4 changes: 3 additions & 1 deletion examples/with-markdown-plugins/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"extends": "astro/tsconfigs/base"
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}
1 change: 0 additions & 1 deletion examples/with-markdown-shiki/src/env.d.ts

This file was deleted.

4 changes: 3 additions & 1 deletion examples/with-markdown-shiki/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"extends": "astro/tsconfigs/base"
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}
1 change: 0 additions & 1 deletion examples/with-mdx/src/env.d.ts

This file was deleted.

4 changes: 3 additions & 1 deletion examples/with-mdx/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"extends": "astro/tsconfigs/base"
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}
1 change: 0 additions & 1 deletion examples/with-nanostores/src/env.d.ts

This file was deleted.

2 changes: 2 additions & 0 deletions examples/with-nanostores/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"],
"compilerOptions": {
// Preact specific settings
"jsx": "react-jsx",
Expand Down
1 change: 0 additions & 1 deletion examples/with-tailwindcss/src/env.d.ts

This file was deleted.

4 changes: 3 additions & 1 deletion examples/with-tailwindcss/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"extends": "astro/tsconfigs/base"
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}
4 changes: 3 additions & 1 deletion examples/with-vitest/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"extends": "astro/tsconfigs/base"
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}
4 changes: 3 additions & 1 deletion packages/astro/e2e/fixtures/actions-blog/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@
"strictNullChecks": true,
"jsx": "react-jsx",
"jsxImportSource": "react"
}
},
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}
4 changes: 3 additions & 1 deletion packages/astro/e2e/fixtures/actions-react-19/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@
"strictNullChecks": true,
"jsx": "react-jsx",
"jsxImportSource": "react"
}
},
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}
4 changes: 3 additions & 1 deletion packages/astro/e2e/fixtures/client-only/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"compilerOptions": {
"importsNotUsedAsValues": "error"
}
},
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
// This is only needed because we link Astro locally.
"preserveSymlinks": true
},
"include": ["./src/**/*"]
"include": ["./src/**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}
3 changes: 0 additions & 3 deletions packages/astro/performance/fixtures/md/src/env.d.ts

This file was deleted.

4 changes: 3 additions & 1 deletion packages/astro/performance/fixtures/md/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "react"
}
},
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}
4 changes: 3 additions & 1 deletion packages/astro/performance/fixtures/mdoc/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "react"
}
},
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}
4 changes: 3 additions & 1 deletion packages/astro/performance/fixtures/mdx/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "react"
}
},
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}
4 changes: 3 additions & 1 deletion packages/astro/performance/fixtures/utils/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "react"
}
},
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}
2 changes: 0 additions & 2 deletions packages/astro/src/core/sync/constants.ts

This file was deleted.

32 changes: 30 additions & 2 deletions packages/astro/src/core/sync/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ import {
import type { Logger } from '../logger/core.js';
import { formatErrorMessage } from '../messages.js';
import { ensureProcessNodeEnv } from '../util.js';
import { writeFiles } from './write-files.js';
import { dirname, relative } from 'node:path';
import { normalizePath } from 'vite';

export type SyncOptions = {
/**
Expand Down Expand Up @@ -135,7 +136,7 @@ export async function syncInternal({
}
syncAstroEnv(settings);

await writeFiles(settings, fs, logger);
writeInjectedTypes(settings, fs);
logger.info('types', `Generated ${dim(getTimeStat(timerStart, performance.now()))}`);
} catch (err) {
const error = createSafeError(err);
Expand All @@ -148,6 +149,33 @@ export async function syncInternal({
}
}

function getTsReference(type: 'path' | 'types', value: string) {
return `/// <reference ${type}=${JSON.stringify(value)} />`;
}

const CLIENT_TYPES_REFERENCE = getTsReference('types', 'astro/client');

function writeInjectedTypes(settings: AstroSettings, fs: typeof fsMod) {
const references: Array<string> = [];

for (const { filename, content } of settings.injectedTypes) {
const filepath = fileURLToPath(new URL(filename, settings.dotAstroDir));
fs.mkdirSync(dirname(filepath), { recursive: true });
fs.writeFileSync(filepath, content, 'utf-8');
references.push(normalizePath(relative(fileURLToPath(settings.dotAstroDir), filepath)));
}

const astroDtsContent = `${CLIENT_TYPES_REFERENCE}\n${references.map((reference) => getTsReference('path', reference)).join('\n')}`;
if (references.length === 0) {
fs.mkdirSync(settings.dotAstroDir, { recursive: true });
}
fs.writeFileSync(
fileURLToPath(new URL('./types.d.ts', settings.dotAstroDir)),
astroDtsContent,
'utf-8',
);
}

/**
* Generate content collection types, and then returns the process exit signal.
*
Expand Down
Loading

0 comments on commit 3804711

Please sign in to comment.