Skip to content
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

feat(next): codegenDir and update .astro paths #11963

Merged
merged 22 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
a60023e
feat: update paths
florian-lefebvre Sep 10, 2024
a28f00e
chore: changeset
florian-lefebvre Sep 10, 2024
74174b4
fix: tests
florian-lefebvre Sep 10, 2024
eb0c144
Update test-utils.js
florian-lefebvre Sep 10, 2024
a2616d8
Merge branch 'next' into feat/dotastro-move
florian-lefebvre Sep 11, 2024
540d2a9
Discard changes to packages/astro/test/units/dev/restart.test.js
florian-lefebvre Sep 18, 2024
e963c87
Discard changes to packages/astro/test/test-utils.js
florian-lefebvre Sep 18, 2024
7438ef7
Discard changes to packages/astro/test/data-collections-schema.test.js
florian-lefebvre Sep 18, 2024
575a774
Discard changes to packages/astro/test/content-layer.test.js
florian-lefebvre Sep 18, 2024
7de2901
Discard changes to packages/astro/test/content-intellisense.test.js
florian-lefebvre Sep 18, 2024
37b064b
Merge branch 'next' into feat/dotastro-move
florian-lefebvre Sep 18, 2024
186aa60
chore: remove changeset
florian-lefebvre Sep 18, 2024
9109991
feat: adjust
florian-lefebvre Sep 18, 2024
92bdac7
feat: move dts to root
florian-lefebvre Sep 18, 2024
a850394
chore: remove duplicate folder creation
florian-lefebvre Sep 18, 2024
091dbe5
feat: fix test and add codegenDir
florian-lefebvre Sep 18, 2024
3edbe68
chore: changeset
florian-lefebvre Sep 18, 2024
0c30ceb
Update three-olives-reflect.md
florian-lefebvre Sep 18, 2024
46fd026
feat: createCodegenDir
florian-lefebvre Sep 23, 2024
d077e47
Merge branch 'next' into feat/dotastro-move
florian-lefebvre Sep 23, 2024
635abe3
Update .changeset/three-olives-reflect.md
florian-lefebvre Sep 23, 2024
1400894
Apply suggestions from code review
florian-lefebvre Sep 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .changeset/three-olives-reflect.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@
'astro': minor
---

Adds a new `codegenDir` URL to integrations `astro:config:setup` hook
Adds a new `createCodegenDir()` function to integrations `astro:config:setup` hook
florian-lefebvre marked this conversation as resolved.
Show resolved Hide resolved

In 4.14, we introduced the `injectTypes` utility on the `astro:config:done` hook. It allows to create `.d.ts` files and make their types available to users projects automatically. Under the hood, it creates a file in `<root>/.astro/integrations/<normalized_integration_name>`.
florian-lefebvre marked this conversation as resolved.
Show resolved Hide resolved

While `.astro` has always been the preferred place to write code generated files, it has also been prone to mistakes. For example, you can write a `.astro/types.d.ts` file, breaking Astro types. Or you can create a file that overrides a file created by another integration.
florian-lefebvre marked this conversation as resolved.
Show resolved Hide resolved

In this release, `<root>/.astro/integrations/<normalized_integration_name>` is now exposed in the `astro:config:setup` hook as `codegenDir`. It allows you to have a dedicated folder, avoiding conflicts with another integration or Astro. This directory is always created before any hook runs so it's safe to write files to it directly:
In this release, `<root>/.astro/integrations/<normalized_integration_name>` can now be retrieved in the `astro:config:setup` hook by calling `createCodegenDir()`. It allows you to have a dedicated folder, avoiding conflicts with another integration or Astro. This directory is created by calling thos function so it's safe to write files to it directly:
florian-lefebvre marked this conversation as resolved.
Show resolved Hide resolved

```js
import { writeFileSync } from 'node:fs'

const integration = {
name: 'my-integration',
hooks: {
'astro:config:setup': ({ codegenDir }) => {
'astro:config:setup': ({ createCodegenDir }) => {
const codegenDir = createCodegenDir()
writeFileSync(new URL('cache.json', codegenDir), '{}', 'utf-8')
}
}
Expand Down
9 changes: 5 additions & 4 deletions packages/astro/src/integrations/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,6 @@ export async function runHookConfigSetup({
for (let i = 0; i < updatedConfig.integrations.length; i++) {
const integration = updatedConfig.integrations[i];

const codegenDir = new URL(normalizeCodegenDir(integration.name), settings.dotAstroDir);
await fs.promises.mkdir(codegenDir, { recursive: true });

/**
* By making integration hooks optional, Astro can now ignore null or undefined Integrations
* instead of giving an internal error most people can't read
Expand All @@ -177,7 +174,6 @@ export async function runHookConfigSetup({
config: updatedConfig,
command,
isRestart,
codegenDir,
addRenderer(renderer: AstroRenderer) {
if (!renderer.name) {
throw new Error(`Integration ${bold(integration.name)} has an unnamed renderer.`);
Expand Down Expand Up @@ -242,6 +238,11 @@ export async function runHookConfigSetup({
);
updatedSettings.middlewares[order].push(entrypoint);
},
createCodegenDir: () => {
const codegenDir = new URL(normalizeCodegenDir(integration.name), settings.dotAstroDir);
fs.mkdirSync(codegenDir, { recursive: true });
return codegenDir;
},
logger: integrationLogger,
};

Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/types/public/integrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ export interface BaseIntegrationHooks {
config: AstroConfig;
command: 'dev' | 'build' | 'preview' | 'sync';
isRestart: boolean;
codegenDir: URL;
updateConfig: (newConfig: DeepPartial<AstroConfig>) => AstroConfig;
addRenderer: (renderer: AstroRenderer) => void;
addWatchFile: (path: URL | string) => void;
Expand All @@ -177,6 +176,7 @@ export interface BaseIntegrationHooks {
addClientDirective: (directive: ClientDirectiveConfig) => void;
addDevToolbarApp: (entrypoint: DevToolbarAppEntry) => void;
addMiddleware: (mid: AstroIntegrationMiddleware) => void;
createCodegenDir: () => URL;
logger: AstroIntegrationLogger;
}) => void | Promise<void>;
'astro:config:done': (options: {
Expand Down
Loading