-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Content collections] Move generated types to
.astro
directory (#5786)
* feat: change cacheDir to `.astro` * feat: write reference in env.d.ts if none exists * chore: update with-content types * test: env.d.ts transform * nit: setUp -> add * refactor: content.d.ts -> types.d.ts * chore: update confirmation log * chore: changeset * feat: inject env.d.ts if none exists * feat: set up env.d.ts on `astro sync` * chore: duplicate envTsPathRelative * docs: update changeset * fix: make srcDir if none exists * fix: types.generated -> .astro in gitignore * feat: add env.d.ts to test gitignore * chore: remove env.d.ts from content-collections * test: move sync tests to `astro sync`, add file write test * refactor: simplify test gitignore to base * fix: add / to `.astro` bc that scares me
- Loading branch information
1 parent
813073a
commit c218074
Showing
21 changed files
with
263 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
'astro': minor | ||
--- | ||
|
||
Move generated content collection types to a `.astro` directory. This replaces the previously generated `src/content/types.generated.d.ts` file. | ||
|
||
If you're using Git for version control, we recommend ignoring this generated directory by adding `.astro` to your .gitignore. | ||
|
||
Astro will also generate the [TypeScript reference path](https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html#-reference-path-) to include `.astro` types in your project. This will update your project's `src/env.d.ts` file, or write one if none exists. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
/// <reference path="../.astro/types.d.ts" /> | ||
/// <reference types="astro/client" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import type { AstroSettings } from '../@types/astro.js'; | ||
import type fsMod from 'node:fs'; | ||
import { normalizePath, Plugin } from 'vite'; | ||
import path from 'node:path'; | ||
import { getContentPaths, getDotAstroTypeReference } from '../content/index.js'; | ||
import { info, LogOptions } from '../core/logger/core.js'; | ||
import { fileURLToPath } from 'node:url'; | ||
import { bold } from 'kleur/colors'; | ||
|
||
export function getEnvTsPath({ srcDir }: { srcDir: URL }) { | ||
return new URL('env.d.ts', srcDir); | ||
} | ||
|
||
export function astroInjectEnvTsPlugin({ | ||
settings, | ||
logging, | ||
fs, | ||
}: { | ||
settings: AstroSettings; | ||
logging: LogOptions; | ||
fs: typeof fsMod; | ||
}): Plugin { | ||
return { | ||
name: 'astro-inject-env-ts', | ||
// Use `post` to ensure project setup is complete | ||
// Ex. `.astro` types have been written | ||
enforce: 'post', | ||
async config() { | ||
await setUpEnvTs({ settings, logging, fs }); | ||
}, | ||
}; | ||
} | ||
|
||
export async function setUpEnvTs({ | ||
settings, | ||
logging, | ||
fs, | ||
}: { | ||
settings: AstroSettings; | ||
logging: LogOptions; | ||
fs: typeof fsMod; | ||
}) { | ||
const envTsPath = getEnvTsPath(settings.config); | ||
const dotAstroDir = getContentPaths(settings.config).cacheDir; | ||
const dotAstroTypeReference = getDotAstroTypeReference(settings.config); | ||
const envTsPathRelativetoRoot = normalizePath( | ||
path.relative(fileURLToPath(settings.config.root), fileURLToPath(envTsPath)) | ||
); | ||
|
||
if (fs.existsSync(envTsPath)) { | ||
// Add `.astro` types reference if none exists | ||
if (!fs.existsSync(dotAstroDir)) return; | ||
|
||
let typesEnvContents = await fs.promises.readFile(envTsPath, 'utf-8'); | ||
const expectedTypeReference = getDotAstroTypeReference(settings.config); | ||
|
||
if (!typesEnvContents.includes(expectedTypeReference)) { | ||
typesEnvContents = `${expectedTypeReference}\n${typesEnvContents}`; | ||
await fs.promises.writeFile(envTsPath, typesEnvContents, 'utf-8'); | ||
info(logging, 'content', `Added ${bold(envTsPathRelativetoRoot)} types`); | ||
} | ||
} else { | ||
// Otherwise, inject the `env.d.ts` file | ||
let referenceDefs: string[] = []; | ||
if (settings.config.integrations.find((i) => i.name === '@astrojs/image')) { | ||
referenceDefs.push('/// <reference types="@astrojs/image/client" />'); | ||
} else { | ||
referenceDefs.push('/// <reference types="astro/client" />'); | ||
} | ||
|
||
if (fs.existsSync(dotAstroDir)) { | ||
referenceDefs.push(dotAstroTypeReference); | ||
} | ||
|
||
await fs.promises.mkdir(settings.config.srcDir, { recursive: true }); | ||
await fs.promises.writeFile(envTsPath, referenceDefs.join('\n'), 'utf-8'); | ||
info(logging, 'astro', `Added ${bold(envTsPathRelativetoRoot)} types`); | ||
} | ||
} |
Oops, something went wrong.