Skip to content

Commit

Permalink
fix: type generation with astro check
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Stramel committed Jul 14, 2023
1 parent e66e3ad commit 8e6d2f8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Expand Up @@ -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 }}
Expand Down
7 changes: 4 additions & 3 deletions demo/package.json
Expand Up @@ -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",
Expand Down
40 changes: 22 additions & 18 deletions packages/core/src/vite-plugin-astro-icon.ts
Expand Up @@ -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) {
Expand All @@ -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<Pick<IntegrationOptions, 'include' | 'iconDir'>>, { root }: Pick<AstroConfig, "root">) {
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,
Expand All @@ -71,7 +74,7 @@ async function generateIconTypeDefinitions(
.flat(1)
.join("")
: "never"
};\n
};
}`
);
}
Expand All @@ -83,3 +86,4 @@ async function ensureDir(path: URL): Promise<void> {
await mkdir(path);
}
}

0 comments on commit 8e6d2f8

Please sign in to comment.