From df5073724d59eca8aa5da521b6a8734935d3f700 Mon Sep 17 00:00:00 2001 From: Bryce Russell Date: Mon, 9 Dec 2024 22:33:51 -0600 Subject: [PATCH] Migrate to native `injectTypes` function (#182) * Migrate to native `injectTypes` function * Add changeset * Remove test for type generation Type generation is handled by Astro now so this method does not work anymore --- .changeset/silly-olives-joke.md | 5 +++ package/src/index.ts | 65 +++++++++++++++++---------------- package/tests/theme.test.js | 16 -------- 3 files changed, 38 insertions(+), 48 deletions(-) create mode 100644 .changeset/silly-olives-joke.md diff --git a/.changeset/silly-olives-joke.md b/.changeset/silly-olives-joke.md new file mode 100644 index 00000000..946efbb6 --- /dev/null +++ b/.changeset/silly-olives-joke.md @@ -0,0 +1,5 @@ +--- +"astro-theme-provider": patch +--- + +Migrate away from the AIK utility `addDts` to the native utility `injectTypes` diff --git a/package/src/index.ts b/package/src/index.ts index 91fc1a9f..1bfee67e 100644 --- a/package/src/index.ts +++ b/package/src/index.ts @@ -1,7 +1,7 @@ import { existsSync } from "node:fs"; import { basename, extname, join, resolve } from "node:path"; import type { AstroIntegration } from "astro"; -import { addDts, addIntegration, addVitePlugin, watchDirectory } from "astro-integration-kit"; +import { addIntegration, addVitePlugin, watchDirectory } from "astro-integration-kit"; import { addPageDir } from "astro-pages"; import type { IntegrationOption as PageDirIntegrationOption } from "astro-pages"; import staticDir from "astro-public"; @@ -101,6 +101,33 @@ export default function ( ); } + let themeTypesBuffer = ` + type ThemeName = "${themeName}"; + + declare namespace AstroThemeProvider { + export interface Themes { + "${themeName}": true; + } + + export interface ThemeOptions { + "${themeName}": { + pages?: { [Pattern in keyof ThemeRoutes]?: string | boolean } & {} + overrides?: { + [Module in keyof ThemeExports]?: + ThemeExports[Module] extends Record + ? ThemeExports[Module] extends string[] + ? string[] + : { [Export in keyof ThemeExports[Module]]?: string } + : never + } & {} + integrations?: keyof ThemeIntegrationsResolved extends never + ? \`\$\{ThemeName\} is not injecting any integrations\` + : { [Name in keyof ThemeIntegrationsResolved]?: boolean } & {} + }; + } + } + `; + const themeIntegration: AstroIntegration = { name: themeName, hooks: { @@ -140,32 +167,6 @@ export default function ( ThemeIntegrationsResolved: "", }; - let themeTypesBuffer = ` - type ThemeName = "${themeName}"; - - declare namespace AstroThemeProvider { - export interface Themes { - "${themeName}": true; - } - - export interface ThemeOptions { - "${themeName}": { - pages?: { [Pattern in keyof ThemeRoutes]?: string | boolean } & {} - overrides?: { - [Module in keyof ThemeExports]?: - ThemeExports[Module] extends Record - ? ThemeExports[Module] extends string[] - ? string[] - : { [Export in keyof ThemeExports[Module]]?: string } - : never - } & {} - integrations?: keyof ThemeIntegrationsResolved extends never - ? \`\$\{ThemeName\} is not injecting any integrations\` - : { [Name in keyof ThemeIntegrationsResolved]?: boolean } & {} - }; - } - } - `; if (logLevel) { // Warn about issues with theme's `package.json` @@ -394,13 +395,13 @@ export default function ( } `; } - - // Write compiled types to .d.ts file - addDts(params, { - name: themeName, + }, + "astro:config:done": ({ injectTypes }) => { + injectTypes({ + filename: 'theme.d.ts', content: themeTypesBuffer, }); - }, + } }, }; diff --git a/package/tests/theme.test.js b/package/tests/theme.test.js index cdfec3f9..50b4c060 100644 --- a/package/tests/theme.test.js +++ b/package/tests/theme.test.js @@ -260,22 +260,6 @@ describe("defineTheme", () => { } } }); - - it("should generate types", () => { - const theme = defineTheme({})(); - const params = astroConfigSetupParamsStub(); - - theme.hooks["astro:config:setup"]?.(params); - - assert.equal( - readFileSync(resolve(projectSrc, "env.d.ts"), "utf-8").includes( - `/// `, - ), - true, - ); - - assert.equal(existsSync(resolve(projectRoot, `.astro/${packageName}.d.ts`)), true); - }); }); }); });