Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatibility with the static build #26

Merged
merged 6 commits into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/lazy-tips-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro-icon": minor
---

`astro-icon` is now compatible with Astro's `--experimental-static-build` flag
18 changes: 18 additions & 0 deletions .changeset/tall-needles-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
"astro-icon": minor
---

# Breaking Changes

- `astro-icon@0.6.0` is compatible with `astro@0.23.x` and up, but will no longer work in lower versions.

- The `createIconPack` export has been moved from `astro-icon` to `astro-icon/pack`.

You will likely see a Vite error that `createIconPack` is not defined until you update your import statement.

```diff
- import { createIconPack } from "astro-icon";
+ import { createIconPack } from "astro-icon/pack";

export default createIconPack({ package: "heroicons", dir: "outline" })
```
8 changes: 4 additions & 4 deletions demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
"version": "0.0.1",
"private": true,
"scripts": {
"dev": "astro dev",
"start": "astro dev",
"build": "astro build",
"dev": "astro dev --experimental-static-build",
"start": "astro dev --experimental-static-build",
"build": "astro build --experimental-static-build",
"preview": "astro preview"
},
"devDependencies": {
"astro": "^0.21.10",
"astro": "^0.23.0-next.4",
"astro-icon": "0.5.3"
},
"dependencies": {
Expand Down
4 changes: 2 additions & 2 deletions demo/src/icons/heroicons.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { createIconPack } from "astro-icon";
import { createIconPack } from "astro-icon/pack";

export default createIconPack({ package: "heroicons", dir: "outline" });
export default createIconPack({ package: "heroicons", dir: "outline" })
2 changes: 1 addition & 1 deletion demo/src/icons/radix.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createIconPack } from "astro-icon";
import { createIconPack } from "astro-icon/pack";

export default createIconPack({
url: "https://raw.githubusercontent.com/radix-ui/icons/master/packages/radix-icons/icons/",
Expand Down
5 changes: 1 addition & 4 deletions packages/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import Icon from "./lib/Icon.astro";
import SpriteProvider from "./lib/SpriteProvider.astro";
import SpriteComponent from "./lib/Sprite.astro";
import createIconPack from "./lib/createIconPack.ts";

import Sheet from "./lib/Spritesheet.astro";

const deprecate = (component: any, message: string) => {
Expand All @@ -29,6 +27,5 @@ export {
Spritesheet,
SpriteSheet,
SpriteProvider,
Sprite,
createIconPack,
Sprite
};
2 changes: 1 addition & 1 deletion packages/core/lib/Icon.astro
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ ${e}`)
}
---

<svg {...props} astro-icon={name}>{title ? (<title>{title}</title>) : ''}{innerHTML}</svg>
<svg {...props} astro-icon={name} set:html={(title ? `<title>${title}</title>` : '') + innerHTML} />
6 changes: 1 addition & 5 deletions packages/core/lib/Spritesheet.astro
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,5 @@ ${e}`);
---

<svg style={`display: none; ${style ?? ''}`.trim()} {...{ 'aria-hidden': true, ...props }} astro-icon-spritesheet>
{icons.map(icon => (
<symbol {...icon.props} id={`${SPRITESHEET_NAMESPACE}:${icon.name}`}>
{icon.innerHTML}
</symbol>
))}
{icons.map(icon => (<symbol {...icon.props} id={`${SPRITESHEET_NAMESPACE}:${icon.name}`} set:html={icon.innerHTML} />))}
</svg>
8 changes: 3 additions & 5 deletions packages/core/lib/createIconPack.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import { statSync, promises as fs } from "fs";
import { fileURLToPath, pathToFileURL } from "url";
import { createRequire } from "module";

const require = createRequire(import.meta.url);
import resolvePackage from "resolve-pkg";

export interface CreateIconPackOptions {
package?: string;
dir?: string;
url?: string;
}

export default function createIconPack({
export function createIconPack({
package: pkg,
dir,
url,
}: CreateIconPackOptions) {
if (pkg) {
const baseUrl = pathToFileURL(require.resolve(`${pkg}/package.json`));
return async (name: string) => {
const baseUrl = new URL(pathToFileURL(resolvePackage(pkg)) + "/");
const path = fileURLToPath(
new URL(dir ? `${dir}/${name}.svg` : `${name}.svg`, baseUrl)
);
Expand Down
22 changes: 20 additions & 2 deletions packages/core/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/// <reference types="vite/client" />
import { SPRITESHEET_NAMESPACE } from "./constants";
import { Props, Optimize } from "./Props";
import getFromService from "./resolver";
Expand Down Expand Up @@ -145,7 +146,14 @@ export default async function load(
filepath = `/src/icons/${pack}`;
let get;
try {
const mod = await import(`${filepath}`);
const files = import.meta.globEager(`/src/icons/**/*.{js,ts,cjs,mjc,cts,mts}`);
const keys = Object.fromEntries(Object.keys(files).map(key => [key.replace(/\.[cm]?[jt]s$/, ''), key]))

if (!(filepath in keys)) {
throw new Error(`Could not find the file "${filepath}"`);
}

const mod = files[keys[filepath]];
if (typeof mod.default !== "function") {
throw new Error(
`[astro-icon] "${filepath}" did not export a default function!`
Expand Down Expand Up @@ -177,7 +185,17 @@ ${contents}`
filepath = `/src/icons/${name}.svg`;

try {
const { default: contents } = await import(`${filepath}?raw`);
const files = import.meta.globEager(`/src/icons/**/*.svg`, {
assert: {
type: 'raw'
}
});

if(!(filepath in files)) {
throw new Error(`Could not find the file "${filepath}"`);
}

const contents = files[filepath];
if (!/<svg/gim.test(contents)) {
throw new Error(
`Unable to process "${filepath}" because it is not an SVG!
Expand Down
1 change: 1 addition & 0 deletions packages/core/pack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './lib/createIconPack';
4 changes: 3 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"version": "0.5.3",
"type": "module",
"exports": {
".": "./index.ts"
".": "./index.ts",
"./pack": "./lib/createIconPack.ts"
},
"files": [
"lib",
Expand Down Expand Up @@ -32,6 +33,7 @@
"homepage": "https://github.com/natemoo-re/astro-icon#readme",
"dependencies": {
"node-fetch": "^3.1.0",
"resolve-pkg": "^2.0.0",
"svgo": "^2.8.0"
}
}
2 changes: 1 addition & 1 deletion packages/www/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"preview": "astro preview"
},
"dependencies": {
"astro": "^0.22.3",
"astro": "^0.23.0-next.4",
"astro-icon": "0.5.3"
}
}
Loading