-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Content collections] Move generated types to .astro
directory
#5786
Merged
+263
−97
Merged
Changes from 11 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
ce09a99
feat: change cacheDir to `.astro`
bholmesdev c4fef65
feat: write reference in env.d.ts if none exists
bholmesdev f5736b8
chore: update with-content types
bholmesdev bb145a4
test: env.d.ts transform
bholmesdev 919018b
nit: setUp -> add
bholmesdev fd7538a
refactor: content.d.ts -> types.d.ts
bholmesdev 45c50b7
chore: update confirmation log
bholmesdev 3023f2e
chore: changeset
bholmesdev b2b7e4d
feat: inject env.d.ts if none exists
bholmesdev 07ff9c8
feat: set up env.d.ts on `astro sync`
bholmesdev 481f977
chore: duplicate envTsPathRelative
bholmesdev aa1842a
docs: update changeset
bholmesdev eb0f5a7
fix: make srcDir if none exists
bholmesdev c1914de
fix: types.generated -> .astro in gitignore
bholmesdev 79ab58a
feat: add env.d.ts to test gitignore
bholmesdev 668cae8
chore: remove env.d.ts from content-collections
bholmesdev fdfff8f
test: move sync tests to `astro sync`, add file write test
bholmesdev a1ffc5b
refactor: simplify test gitignore to base
bholmesdev 4984511
fix: add / to `.astro` bc that scares me
bholmesdev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,16 @@ | ||
--- | ||
'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. | ||
|
||
#### Migration | ||
|
||
You will need a [TypeScript reference path](https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html#-reference-path-) to include `.astro` types in your project. Running `astro dev`, `astro build`, or `astro sync` will configure this automatically if your project has a `src/env.d.ts` file. Otherwise, you can add a `src/env.d.ts` file manually with the following contents: | ||
|
||
```diff | ||
/// <reference path="astro/client" /> | ||
+ /// <reference types="../.astro/types.d.ts" /> | ||
``` |
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,78 @@ | ||
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.writeFile(envTsPath, referenceDefs.join('\n')); | ||
info(logging, 'astro', `Added ${bold(envTsPathRelativetoRoot)} types`); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this mean you don't get types without manually creating a file? Or was this already required? Is there anything we can do about that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will be automatically generated if a
src/env.d.ts
is present! Otherwise, we assume you've deleted or moved this file to manage ambient types yourself. Since this file is home to theastro/client
types, I'd assume most users have this file in their Astro project.