0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

feat(next): better tsconfig (#11859)

Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com>
This commit is contained in:
Florian Lefebvre 2024-09-03 17:49:09 +02:00 committed by GitHub
parent 5a39da87d3
commit 38047119ff
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
80 changed files with 207 additions and 184 deletions

View file

@ -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"]
}
```

View file

@ -1,3 +1,5 @@
{
"extends": "astro/tsconfigs/base"
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}

View file

@ -1 +0,0 @@
/// <reference path="../.astro/types.d.ts" />

View file

@ -1,5 +1,7 @@
{
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"],
"compilerOptions": {
"strictNullChecks": true
}

View file

@ -1,5 +1,7 @@
{
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"],
"compilerOptions": {
"jsx": "preserve"
}

View file

@ -1,3 +1,5 @@
{
"extends": "astro/tsconfigs/base"
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}

View file

@ -1 +0,0 @@
/// <reference path="../.astro/types.d.ts" />

View file

@ -1,3 +1,5 @@
{
"extends": "astro/tsconfigs/base"
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}

View file

@ -1 +0,0 @@
/// <reference path="../.astro/types.d.ts" />

View file

@ -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"

View file

@ -1 +0,0 @@
/// <reference path="../.astro/types.d.ts" />

View file

@ -1,5 +1,7 @@
{
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"],
"compilerOptions": {
// Preact specific settings
"jsx": "react-jsx",

View file

@ -1 +0,0 @@
/// <reference path="../.astro/types.d.ts" />

View file

@ -1,5 +1,7 @@
{
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"],
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "react"

View file

@ -1 +0,0 @@
/// <reference path="../.astro/types.d.ts" />

View file

@ -1,5 +1,7 @@
{
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"],
"compilerOptions": {
// Solid specific settings
"jsx": "preserve",

View file

@ -1 +0,0 @@
/// <reference path="../.astro/types.d.ts" />

View file

@ -1,3 +1,5 @@
{
"extends": "astro/tsconfigs/base"
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}

View file

@ -1 +0,0 @@
/// <reference path="../.astro/types.d.ts" />

View file

@ -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"

View file

@ -1 +0,0 @@
/// <reference path="../.astro/types.d.ts" />

View file

@ -1,3 +1,5 @@
{
"extends": "astro/tsconfigs/base"
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}

View file

@ -1,4 +1,3 @@
/// <reference path="../.astro/types.d.ts" />
declare namespace App {
interface Locals {
user: {

View file

@ -1,3 +1,5 @@
{
"extends": "astro/tsconfigs/base"
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}

View file

@ -1 +0,0 @@
/// <reference path="../.astro/types.d.ts" />

View file

@ -1,3 +1,5 @@
{
"extends": "astro/tsconfigs/base"
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}

View file

@ -1 +0,0 @@
/// <reference path="../.astro/types.d.ts" />

View file

@ -1,3 +1,5 @@
{
"extends": "astro/tsconfigs/base"
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}

View file

@ -1 +0,0 @@
/// <reference path="../.astro/types.d.ts" />

View file

@ -1,3 +1,5 @@
{
"extends": "astro/tsconfigs/base"
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}

View file

@ -1 +0,0 @@
/// <reference path="../.astro/types.d.ts" />

View file

@ -1,3 +1,5 @@
{
"extends": "astro/tsconfigs/base"
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}

View file

@ -1,4 +1,5 @@
{
"extends": "astro/tsconfigs/strict",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}

View file

@ -1,5 +1,7 @@
{
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"],
"compilerOptions": {
"resolveJsonModule": true
}

View file

@ -1 +0,0 @@
/// <reference path="../.astro/types.d.ts" />

View file

@ -1,5 +1,7 @@
{
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"],
"compilerOptions": {
"strictNullChecks": true
}

View file

@ -1 +0,0 @@
/// <reference path="../.astro/types.d.ts" />

View file

@ -1,3 +1,5 @@
{
"extends": "astro/tsconfigs/base"
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}

View file

@ -1 +0,0 @@
/// <reference path="../.astro/types.d.ts" />

View file

@ -1,3 +1,5 @@
{
"extends": "astro/tsconfigs/base"
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}

View file

@ -1 +0,0 @@
/// <reference path="../.astro/types.d.ts" />

View file

@ -1,3 +1,5 @@
{
"extends": "astro/tsconfigs/base"
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}

View file

@ -1 +0,0 @@
/// <reference path="../.astro/types.d.ts" />

View file

@ -1,5 +1,7 @@
{
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"],
"compilerOptions": {
// Preact specific settings
"jsx": "react-jsx",

View file

@ -1 +0,0 @@
/// <reference path="../.astro/types.d.ts" />

View file

@ -1,3 +1,5 @@
{
"extends": "astro/tsconfigs/base"
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}

View file

@ -1,3 +1,5 @@
{
"extends": "astro/tsconfigs/base"
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}

View file

@ -4,5 +4,7 @@
"strictNullChecks": true,
"jsx": "react-jsx",
"jsxImportSource": "react"
}
},
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}

View file

@ -4,5 +4,7 @@
"strictNullChecks": true,
"jsx": "react-jsx",
"jsxImportSource": "react"
}
},
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}

View file

@ -1,5 +1,7 @@
{
"compilerOptions": {
"importsNotUsedAsValues": "error"
}
},
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}

View file

@ -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"]
}

View file

@ -1,3 +0,0 @@
/// <reference path="../../.astro/types.d.ts" />
/// <reference path="../.astro/types.d.ts" />
/// <reference types="astro/client" />

View file

@ -3,5 +3,7 @@
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "react"
}
},
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}

View file

@ -3,5 +3,7 @@
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "react"
}
},
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}

View file

@ -3,5 +3,7 @@
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "react"
}
},
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}

View file

@ -2,5 +2,7 @@
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "react"
}
},
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}

View file

@ -1,2 +0,0 @@
// TODO: use types.d.ts for backward compatibility. Use astro.d.ts in Astro 5.0
export const REFERENCE_FILE = './types.d.ts';

View file

@ -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 = {
/**
@ -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);
@ -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.
*

View file

@ -1,76 +0,0 @@
import type fsMod from 'node:fs';
import { dirname, relative } from 'node:path';
import { fileURLToPath } from 'node:url';
import { bold } from 'kleur/colors';
import { normalizePath } from 'vite';
import type { AstroSettings } from '../../types/astro.js';
import { AstroError, AstroErrorData } from '../errors/index.js';
import type { Logger } from '../logger/core.js';
import { REFERENCE_FILE } from './constants.js';
export async function writeFiles(settings: AstroSettings, fs: typeof fsMod, logger: Logger) {
try {
writeInjectedTypes(settings, fs);
await setUpEnvTs(settings, fs, logger);
} catch (e) {
throw new AstroError(AstroErrorData.UnknownFilesystemError, { cause: e });
}
}
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(REFERENCE_FILE, settings.dotAstroDir)),
astroDtsContent,
'utf-8',
);
}
async function setUpEnvTs(settings: AstroSettings, fs: typeof fsMod, logger: Logger) {
const envTsPath = fileURLToPath(new URL('env.d.ts', settings.config.srcDir));
const envTsPathRelativetoRoot = relative(fileURLToPath(settings.config.root), envTsPath);
const relativePath = normalizePath(
relative(
fileURLToPath(settings.config.srcDir),
fileURLToPath(new URL(REFERENCE_FILE, settings.dotAstroDir)),
),
);
const expectedTypeReference = getTsReference('path', relativePath);
if (fs.existsSync(envTsPath)) {
const initialEnvContents = await fs.promises.readFile(envTsPath, 'utf-8');
let typesEnvContents = initialEnvContents;
if (!typesEnvContents.includes(expectedTypeReference)) {
typesEnvContents = `${expectedTypeReference}\n${typesEnvContents}`;
}
if (initialEnvContents !== typesEnvContents) {
logger.info('types', `Updated ${bold(envTsPathRelativetoRoot)} type declarations.`);
await fs.promises.writeFile(envTsPath, typesEnvContents, 'utf-8');
}
} else {
// Otherwise, inject the `env.d.ts` file
await fs.promises.mkdir(settings.config.srcDir, { recursive: true });
await fs.promises.writeFile(envTsPath, expectedTypeReference, 'utf-8');
logger.info('types', `Added ${bold(envTsPathRelativetoRoot)} type declarations`);
}
}

View file

@ -24,10 +24,6 @@ const createFixture = () => {
return astroFixture.config;
},
clean() {
const envPath = new URL('env.d.ts', astroFixture.config.srcDir);
if (fs.existsSync(envPath)) {
fs.unlinkSync(new URL('env.d.ts', astroFixture.config.srcDir));
}
fs.rmSync(new URL('./.astro/', astroFixture.config.root), { force: true, recursive: true });
},
async whenSyncing() {
@ -108,40 +104,15 @@ describe('astro sync', () => {
fixture = createFixture();
});
describe('References', () => {
it('Writes `src/env.d.ts` if none exists', async () => {
await fixture.load('./fixtures/astro-basic/');
fixture.clean();
await fixture.whenSyncing();
fixture.thenFileShouldExist('src/env.d.ts');
fixture.thenFileContentShouldInclude(
'src/env.d.ts',
`/// <reference path="../.astro/types.d.ts" />`,
);
});
it('Updates `src/env.d.ts` if one exists', async () => {
const config = await fixture.load('./fixtures/astro-basic/');
fixture.clean();
fs.writeFileSync(new URL('./env.d.ts', config.srcDir), '// whatever', 'utf-8');
await fixture.whenSyncing();
fixture.thenFileShouldExist('src/env.d.ts');
fixture.thenFileContentShouldInclude(
'src/env.d.ts',
`/// <reference path="../.astro/types.d.ts" />`,
);
});
it('Writes `src/types.d.ts`', async () => {
await fixture.load('./fixtures/astro-basic/');
fixture.clean();
await fixture.whenSyncing();
fixture.thenFileShouldExist('.astro/types.d.ts');
fixture.thenFileContentShouldInclude(
'.astro/types.d.ts',
`/// <reference types="astro/client" />`,
);
});
it('Writes `.astro/types.d.ts`', async () => {
await fixture.load('./fixtures/astro-basic/');
fixture.clean();
await fixture.whenSyncing();
fixture.thenFileShouldExist('.astro/types.d.ts');
fixture.thenFileContentShouldInclude(
'.astro/types.d.ts',
`/// <reference types="astro/client" />`,
);
});
describe('Content collections', () => {

View file

@ -1,5 +1,7 @@
{
"compilerOptions": {
"baseUrl": "./src"
}
},
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}

View file

@ -13,5 +13,7 @@
"src/*"
]
}
}
},
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}

View file

@ -1,3 +1,5 @@
{
"extends": "astro/tsconfigs/base"
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}

View file

@ -1,3 +1,5 @@
{
"extends": "astro/tsconfigs/base"
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}

View file

@ -1,3 +1,5 @@
{
"extends": "astro/tsconfigs/base"
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}

View file

@ -1,3 +1,5 @@
{
"extends": "astro/tsconfigs/base"
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}

View file

@ -1,3 +1,5 @@
{
"extends": "astro/tsconfigs/base"
"extends": "astro/tsconfigs/base",
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}

View file

@ -7,5 +7,7 @@
"src/assets/*"
]
},
}
},
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}

View file

@ -5,5 +5,7 @@
"paths": {
"~/assets/*": ["src/assets/*"]
},
}
},
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}

View file

@ -2,5 +2,7 @@
"extends": "astro/tsconfigs/base",
"compilerOptions": {
"baseUrl": ".",
}
},
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}

View file

@ -2,5 +2,7 @@
"extends": "astro/tsconfigs/base",
"compilerOptions": {
"baseUrl": ".",
}
},
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}

View file

@ -5,5 +5,7 @@
"paths": {
"~/assets/*": ["src/assets/*"]
},
}
},
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}

View file

@ -5,5 +5,7 @@
"paths": {
"~/assets/*": ["src/assets/*"]
},
}
},
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}

View file

@ -7,5 +7,7 @@
"src/assets/*"
]
},
}
},
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}

View file

@ -3,5 +3,7 @@
"strict": true,
"jsxImportSource": "solid-js",
"types": ["astro/client"]
}
},
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}

View file

@ -3,5 +3,7 @@
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "react"
}
},
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}

View file

@ -3,5 +3,7 @@
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "react"
}
},
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}

View file

@ -5,5 +5,7 @@
"paths": {
"~/assets/*": ["src/assets/*"]
},
}
},
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}

View file

@ -3,5 +3,7 @@
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "preact"
}
},
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}

View file

@ -5,5 +5,7 @@
"paths": {
"~/assets/*": ["src/assets/*"]
},
}
},
"include": ["**/*", ".astro/types.d.ts"],
"exclude": ["dist"]
}