Skip to content

Commit

Permalink
feat: createCodegenDir
Browse files Browse the repository at this point in the history
  • Loading branch information
florian-lefebvre committed Sep 23, 2024
1 parent 0c30ceb commit 46fd026
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
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

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>`.

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.

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:

```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

0 comments on commit 46fd026

Please sign in to comment.