Skip to content

Commit

Permalink
Migrate to native injectTypes function (#182)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
BryceRussell authored Dec 10, 2024
1 parent d94482d commit df50737
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 48 deletions.
5 changes: 5 additions & 0 deletions .changeset/silly-olives-joke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro-theme-provider": patch
---

Migrate away from the AIK utility `addDts` to the native utility `injectTypes`
65 changes: 33 additions & 32 deletions package/src/index.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -101,6 +101,33 @@ export default function <ThemeName extends string, Schema extends z.ZodTypeAny>(
);
}

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<string, any>
? 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: {
Expand Down Expand Up @@ -140,32 +167,6 @@ export default function <ThemeName extends string, Schema extends z.ZodTypeAny>(
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<string, any>
? 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`
Expand Down Expand Up @@ -394,13 +395,13 @@ export default function <ThemeName extends string, Schema extends z.ZodTypeAny>(
}
`;
}

// Write compiled types to .d.ts file
addDts(params, {
name: themeName,
},
"astro:config:done": ({ injectTypes }) => {
injectTypes({
filename: 'theme.d.ts',
content: themeTypesBuffer,
});
},
}
},
};

Expand Down
16 changes: 0 additions & 16 deletions package/tests/theme.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
`/// <reference types="../.astro/${packageName}.d.ts" />`,
),
true,
);

assert.equal(existsSync(resolve(projectRoot, `.astro/${packageName}.d.ts`)), true);
});
});
});
});

0 comments on commit df50737

Please sign in to comment.