From 8e6d2f8d2169ed58f66ca2c23f7b6d3a42acd69a Mon Sep 17 00:00:00 2001 From: Michael Stramel Date: Thu, 13 Jul 2023 23:04:00 -0500 Subject: [PATCH] fix: type generation with astro check --- .github/workflows/ci.yml | 4 +++ demo/package.json | 7 ++-- packages/core/src/vite-plugin-astro-icon.ts | 40 +++++++++++---------- 3 files changed, 30 insertions(+), 21 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 29e3610..3656a7a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -89,6 +89,10 @@ jobs: - name: Install dependencies run: pnpm install + - name: Check + run: pnpm run check + working-directory: ${{ matrix.target }} + - name: Build run: pnpm build working-directory: ${{ matrix.target }} diff --git a/demo/package.json b/demo/package.json index 05d95e5..1286d2c 100644 --- a/demo/package.json +++ b/demo/package.json @@ -3,10 +3,11 @@ "version": "0.0.1", "private": true, "scripts": { - "dev": "astro dev", - "start": "astro dev", "build": "astro build", - "preview": "astro preview" + "check": "astro check", + "dev": "astro dev", + "preview": "astro preview", + "start": "astro dev" }, "dependencies": { "@iconify-json/bi": "^1.1.15", diff --git a/packages/core/src/vite-plugin-astro-icon.ts b/packages/core/src/vite-plugin-astro-icon.ts index a449955..29b404c 100644 --- a/packages/core/src/vite-plugin-astro-icon.ts +++ b/packages/core/src/vite-plugin-astro-icon.ts @@ -15,10 +15,9 @@ export async function createPlugin( const virtualModuleId = "virtual:astro-icon"; const resolvedVirtualModuleId = "\0" + virtualModuleId; - // Load provided Iconify collections - const collections = await loadIconifyCollections(include); - await generateIconTypeDefinitions(Object.values(collections), root); - + // Load collections + const collections = await loadCollections({ include, iconDir }, { root }) + return { name: "astro-icon", resolveId(id) { @@ -28,24 +27,28 @@ export async function createPlugin( }, async load(id) { if (id === resolvedVirtualModuleId) { - try { - // Attempt to create local collection - const local = await loadLocalCollection(iconDir); - collections["local"] = local; - } catch (ex) { - // Failed to load the local collection - } - await generateIconTypeDefinitions(Object.values(collections), root); - - return `import.meta.glob('/src/icons/**/*.svg'); - - export default ${JSON.stringify(collections)};\n - export const config = ${JSON.stringify({ include })}`; + return `export default ${JSON.stringify(collections)};\nexport const config = ${JSON.stringify({ include })}`; } }, }; } +async function loadCollections({ include, iconDir }: Required>, { root }: Pick) { + const collections = await loadIconifyCollections(include); + + try { + // Attempt to create local collection + const local = await loadLocalCollection(iconDir); + collections["local"] = local; + } catch (ex) { + // Failed to load the local collection + } + + await generateIconTypeDefinitions(Object.values(collections), root); + + return collections +} + async function generateIconTypeDefinitions( collections: IconCollection[], rootDir: URL, @@ -71,7 +74,7 @@ async function generateIconTypeDefinitions( .flat(1) .join("") : "never" - };\n + }; }` ); } @@ -83,3 +86,4 @@ async function ensureDir(path: URL): Promise { await mkdir(path); } } +