diff --git a/.changeset/lazy-tips-lie.md b/.changeset/lazy-tips-lie.md new file mode 100644 index 0000000..e901c47 --- /dev/null +++ b/.changeset/lazy-tips-lie.md @@ -0,0 +1,5 @@ +--- +"astro-icon": minor +--- + +`astro-icon` is now compatible with Astro's `--experimental-static-build` flag diff --git a/.changeset/tall-needles-admire.md b/.changeset/tall-needles-admire.md new file mode 100644 index 0000000..04ba925 --- /dev/null +++ b/.changeset/tall-needles-admire.md @@ -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" }) + ``` diff --git a/demo/package.json b/demo/package.json index 1a64066..269c523 100644 --- a/demo/package.json +++ b/demo/package.json @@ -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": { diff --git a/demo/src/icons/heroicons.ts b/demo/src/icons/heroicons.ts index ed4de37..fb091c5 100644 --- a/demo/src/icons/heroicons.ts +++ b/demo/src/icons/heroicons.ts @@ -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" }) diff --git a/demo/src/icons/radix.ts b/demo/src/icons/radix.ts index 66bd334..f846c8b 100644 --- a/demo/src/icons/radix.ts +++ b/demo/src/icons/radix.ts @@ -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/", diff --git a/packages/core/index.ts b/packages/core/index.ts index dd4ba7a..3f869c2 100644 --- a/packages/core/index.ts +++ b/packages/core/index.ts @@ -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) => { @@ -29,6 +27,5 @@ export { Spritesheet, SpriteSheet, SpriteProvider, - Sprite, - createIconPack, + Sprite }; diff --git a/packages/core/lib/Icon.astro b/packages/core/lib/Icon.astro index 8577b91..cfd0d9c 100644 --- a/packages/core/lib/Icon.astro +++ b/packages/core/lib/Icon.astro @@ -25,4 +25,4 @@ ${e}`) } --- -{title ? ({title}) : ''}{innerHTML} +${title}` : '') + innerHTML} /> diff --git a/packages/core/lib/Spritesheet.astro b/packages/core/lib/Spritesheet.astro index dbfba28..7271ef4 100644 --- a/packages/core/lib/Spritesheet.astro +++ b/packages/core/lib/Spritesheet.astro @@ -25,9 +25,5 @@ ${e}`); --- - {icons.map(icon => ( - - {icon.innerHTML} - - ))} + {icons.map(icon => ())} diff --git a/packages/core/lib/createIconPack.ts b/packages/core/lib/createIconPack.ts index 85c741e..e9d73ec 100644 --- a/packages/core/lib/createIconPack.ts +++ b/packages/core/lib/createIconPack.ts @@ -1,8 +1,6 @@ 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; @@ -10,14 +8,14 @@ export interface CreateIconPackOptions { 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) ); diff --git a/packages/core/lib/utils.ts b/packages/core/lib/utils.ts index 09399d3..fbfc565 100644 --- a/packages/core/lib/utils.ts +++ b/packages/core/lib/utils.ts @@ -1,3 +1,4 @@ +/// import { SPRITESHEET_NAMESPACE } from "./constants"; import { Props, Optimize } from "./Props"; import getFromService from "./resolver"; @@ -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!` @@ -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 (!/=3.0.0 <4.0.0" immutable "^4.0.0" + source-map-js ">=0.6.2 <2.0.0" scheduler@^0.20.2: version "0.20.2" @@ -4376,6 +4328,13 @@ send@^0.17.1: range-parser "~1.2.1" statuses "~1.5.0" +serialize-javascript@^6.0.0: + version "6.0.0" + resolved "https://registry.yarnpkg.com/serialize-javascript/-/serialize-javascript-6.0.0.tgz#efae5d88f45d7924141da8b5c3a7a7e663fefeb8" + integrity sha512-Qr3TosvguFt8ePWqsvRfrKyQXIiW+nGbYpy8XK24NQHE83caxWt+mIymTT19DGFbNWNLfEwsrkSmN64lVWB9ag== + dependencies: + randombytes "^2.1.0" + set-blocking@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/set-blocking/-/set-blocking-2.0.0.tgz#045f9782d011ae9a6803ddd382b24392b3d890f7" @@ -4398,10 +4357,10 @@ shebang-regex@^1.0.0: resolved "https://registry.yarnpkg.com/shebang-regex/-/shebang-regex-1.0.0.tgz#da42f49740c0b42db2ca9728571cb190c98efea3" integrity sha1-2kL0l0DAtC2yypcoVxyxkMmO/qM= -shiki@^0.9.10: - version "0.9.14" - resolved "https://registry.yarnpkg.com/shiki/-/shiki-0.9.14.tgz#6b3e369edf76049ae7ad7c2b0498c35c200b8dd7" - integrity sha512-uLHjjyJdNsMzF9GOF8vlOuZ8BwigiYPraMN5yjC826k8K7Xu90JQcC5GUNrzRibLgT2EOk9597I1IX+jRdA8nw== +shiki@^0.10.0: + version "0.10.0" + resolved "https://registry.yarnpkg.com/shiki/-/shiki-0.10.0.tgz#85f21ecfa95b377ff64db6c71442c22c220e9540" + integrity sha512-iczxaIYeBFHTFrQPb9DVy2SKgYxC4Wo7Iucm7C17cCh2Ge/refnvHscUOxM85u57MfLoNOtjoEFUWt9gBexblA== dependencies: jsonc-parser "^3.0.0" vscode-oniguruma "^1.6.1" @@ -4457,6 +4416,11 @@ sorcery@^0.10.0: sander "^0.5.0" sourcemap-codec "^1.3.0" +"source-map-js@>=0.6.2 <2.0.0", source-map-js@^1.0.2: + version "1.0.2" + resolved "https://registry.yarnpkg.com/source-map-js/-/source-map-js-1.0.2.tgz#adbc361d9c62df380125e7f161f71c826f1e490c" + integrity sha512-R0XvVJ9WusLiqTCEiGCmICCMplcCkIwwR11mOSD9CR5u+IXYdiseeEuXCVAjS54zqwkLcPNnmU4OeJ6tUrWhDw== + source-map-js@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/source-map-js/-/source-map-js-1.0.1.tgz#a1741c131e3c77d048252adfa24e23b908670caf" @@ -4654,7 +4618,7 @@ strip-indent@^3.0.0: dependencies: min-indent "^1.0.0" -strnum@^1.0.4, strnum@^1.0.5: +strnum@^1.0.5: version "1.0.5" resolved "https://registry.yarnpkg.com/strnum/-/strnum-1.0.5.tgz#5c4e829fe15ad4ff0d20c3db5ac97b73c9b072db" integrity sha512-J8bbNyKKXl5qYcR36TIO8W3mVGVHrmmxsd5PAItGkmyzwJvybiw2IVq5nqd0i4LSNSkB/sx9VHllbfFdr9k1JA== @@ -4687,15 +4651,20 @@ supports-esm@^1.0.0: dependencies: has-package-exports "^1.1.0" -svelte-hmr@^0.14.7: - version "0.14.7" - resolved "https://registry.yarnpkg.com/svelte-hmr/-/svelte-hmr-0.14.7.tgz#7fa8261c7b225d9409f0a86f3b9ea5c3ca6f6607" - integrity sha512-pDrzgcWSoMaK6AJkBWkmgIsecW0GChxYZSZieIYfCP0v2oPyx2CYU/zm7TBIcjLVUPP714WxmViE9Thht4etog== +supports-preserve-symlinks-flag@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/supports-preserve-symlinks-flag/-/supports-preserve-symlinks-flag-1.0.0.tgz#6eda4bd344a3c94aea376d4cc31bc77311039e09" + integrity sha512-ot0WnXS9fgdkgIcePe6RHNk1WA8+muPa6cSjeR3V8K27q9BB1rTE3R1p7Hv0z1ZyAc8s6Vvv8DIyWf681MAt0w== + +svelte-hmr@^0.14.9: + version "0.14.9" + resolved "https://registry.yarnpkg.com/svelte-hmr/-/svelte-hmr-0.14.9.tgz#35f277efc789e1a6230185717347cddb2f8e9833" + integrity sha512-bKE9+4qb4sAnA+TKHiYurUl970rjA0XmlP9TEP7K/ncyWz3m81kA4HOgmlZK/7irGK7gzZlaPDI3cmf8fp/+tg== -svelte-preprocess@^4.9.8: - version "4.9.8" - resolved "https://registry.yarnpkg.com/svelte-preprocess/-/svelte-preprocess-4.9.8.tgz#fd40afebfb352f469beab289667485ebf0d811da" - integrity sha512-EQS/oRZzMtYdAprppZxY3HcysKh11w54MgA63ybtL+TAZ4hVqYOnhw41JVJjWN9dhPnNjjLzvbZ2tMhTsla1Og== +svelte-preprocess@^4.10.2: + version "4.10.3" + resolved "https://registry.yarnpkg.com/svelte-preprocess/-/svelte-preprocess-4.10.3.tgz#9aac89a8abc3889fa5740fb34f7dd74f3c578e13" + integrity sha512-ttw17lJfb/dx2ZJT9sesaXT5l7mPQ9Apx1H496Kli3Hkk7orIRGpOw6rCPkRNzr6ueVPqb4vzodS5x7sBFhKHw== dependencies: "@types/pug" "^2.0.4" "@types/sass" "^1.16.0" @@ -4704,10 +4673,10 @@ svelte-preprocess@^4.9.8: sorcery "^0.10.0" strip-indent "^3.0.0" -svelte@^3.44.2: - version "3.44.2" - resolved "https://registry.yarnpkg.com/svelte/-/svelte-3.44.2.tgz#3e69be2598308dfc8354ba584cec54e648a50f7f" - integrity sha512-jrZhZtmH3ZMweXg1Q15onb8QlWD+a5T5Oca4C1jYvSURp2oD35h4A5TV6t6MEa93K4LlX6BkafZPdQoFjw/ylA== +svelte@^3.46.4: + version "3.46.4" + resolved "https://registry.yarnpkg.com/svelte/-/svelte-3.46.4.tgz#0c46bc4a3e20a2617a1b7dc43a722f9d6c084a38" + integrity sha512-qKJzw6DpA33CIa+C/rGp4AUdSfii0DOTCzj/2YpSKKayw5WGSS624Et9L1nU1k2OVRS9vaENQXp2CVZNU+xvIg== svgo@^2.8.0: version "2.8.0" @@ -4848,11 +4817,16 @@ typescript@4.3.4: resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.3.4.tgz#3f85b986945bcf31071decdd96cf8bfa65f9dcbc" integrity sha512-uauPG7XZn9F/mo+7MrsRjyvbxFpzemRjKEZXS4AK83oP2KKOJPvb+9cO/gmnv8arWZvhnjVOXz7B49m1l0e9Ew== -typescript@^4.3.5, typescript@^4.5.1-rc: +typescript@^4.3.5: version "4.5.2" resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.5.2.tgz#8ac1fba9f52256fdb06fb89e4122fa6a346c2998" integrity sha512-5BlMof9H1yGt0P8/WF+wPNw6GfctgGjXp5hkblpyT+8rkASSmkUKMXrxR0Xg8ThVCi/JnHQiKXeBaEwCeQwMFw== +typescript@^4.5.4: + version "4.5.4" + resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.5.4.tgz#a17d3a0263bf5c8723b9c52f43c5084edf13c2e8" + integrity sha512-VgYs2A2QIRuGphtzFV7aQJduJ2gyfTljngLzjpfW9FoYZF6xuw1W0vW9ghCKLfcWrCFxK81CSGRAvS1pn4fIUg== + unbox-primitive@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/unbox-primitive/-/unbox-primitive-1.0.1.tgz#085e215625ec3162574dc8859abee78a59b14471" @@ -4983,11 +4957,6 @@ universalify@^0.1.0: resolved "https://registry.yarnpkg.com/universalify/-/universalify-0.1.2.tgz#b646f69be3942dabcecc9d6639c80dc105efaa66" integrity sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg== -unpipe@~1.0.0: - version "1.0.0" - resolved "https://registry.yarnpkg.com/unpipe/-/unpipe-1.0.0.tgz#b2bf4ee8514aae6165b4817829d21b2ef49904ec" - integrity sha1-sr9O6FFKrmFltIF4KdIbLvSZBOw= - util@^0.12.0: version "0.12.4" resolved "https://registry.yarnpkg.com/util/-/util-0.12.4.tgz#66121a31420df8f01ca0c464be15dfa1d1850253" @@ -5000,11 +4969,6 @@ util@^0.12.0: safe-buffer "^5.1.2" which-typed-array "^1.1.2" -utils-merge@1.0.1: - version "1.0.1" - resolved "https://registry.yarnpkg.com/utils-merge/-/utils-merge-1.0.1.tgz#9f95710f50a267947b2ccc124741c1028427e713" - integrity sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM= - uvu@^0.5.0: version "0.5.2" resolved "https://registry.yarnpkg.com/uvu/-/uvu-0.5.2.tgz#c145e7f4b5becf80099cf22fd8a4a05f0112b2c0" @@ -5050,15 +5014,15 @@ vfile@^5.0.0: unist-util-stringify-position "^3.0.0" vfile-message "^3.0.0" -vite@^2.6.10, vite@~2.6.10: - version "2.6.14" - resolved "https://registry.yarnpkg.com/vite/-/vite-2.6.14.tgz#35c09a15e4df823410819a2a239ab11efb186271" - integrity sha512-2HA9xGyi+EhY2MXo0+A2dRsqsAG3eFNEVIo12olkWhOmc8LfiM+eMdrXf+Ruje9gdXgvSqjLI9freec1RUM5EA== +vite@^2.8.0: + version "2.8.1" + resolved "https://registry.yarnpkg.com/vite/-/vite-2.8.1.tgz#4b2a42c1d01d6786a2a4c278cd0ba7223b8ec15c" + integrity sha512-Typ8qjUnW0p53gBsJpisrKcZlEbUPZATja9BG6Z09QZjg9YrnEn/htkr/VH4WhnH7eNUQeSD+wKI1lHzQRWskw== dependencies: - esbuild "^0.13.2" - postcss "^8.3.8" - resolve "^1.20.0" - rollup "^2.57.0" + esbuild "^0.14.14" + postcss "^8.4.6" + resolve "^1.22.0" + rollup "^2.59.0" optionalDependencies: fsevents "~2.3.2" @@ -5154,16 +5118,16 @@ vscode-uri@^3.0.2: resolved "https://registry.yarnpkg.com/vscode-uri/-/vscode-uri-3.0.2.tgz#ecfd1d066cb8ef4c3a208decdbab9a8c23d055d0" integrity sha512-jkjy6pjU1fxUvI51P+gCsxg1u2n8LSt0W6KrCNQceaziKzff74GoWmjVG46KieVzybO1sttPQmYfrwSHey7GUA== -vue@^3.2.22: - version "3.2.23" - resolved "https://registry.yarnpkg.com/vue/-/vue-3.2.23.tgz#fe17e4a98bee1afe2aed351a0a80e052728f9ce2" - integrity sha512-MGp9JZC37lzGhwSu6c1tQxrQbXbw7XKFqtYh7SFwNrNK899FPxGAHwSHMZijMChTSC3uZrD2BGO/3EHOgMJ0cw== +vue@^3.2.30: + version "3.2.30" + resolved "https://registry.yarnpkg.com/vue/-/vue-3.2.30.tgz#47de3039631ac22cab2fd26b427575260199b8bb" + integrity sha512-ZmTFWVJUX2XADkuOB8GcLTuxnBLogjJBTNVrM7WsTnjqRQ+VR8bLNrvNsbn8vj/LaP5+0WFAPrpngOYE2x+e+Q== dependencies: - "@vue/compiler-dom" "3.2.23" - "@vue/compiler-sfc" "3.2.23" - "@vue/runtime-dom" "3.2.23" - "@vue/server-renderer" "3.2.23" - "@vue/shared" "3.2.23" + "@vue/compiler-dom" "3.2.30" + "@vue/compiler-sfc" "3.2.30" + "@vue/runtime-dom" "3.2.30" + "@vue/server-renderer" "3.2.30" + "@vue/shared" "3.2.30" wcwidth@^1.0.1: version "1.0.1" @@ -5231,7 +5195,7 @@ which-typed-array@^1.1.2: has-tostringtag "^1.0.0" is-typed-array "^1.1.7" -which@^1.2.9: +which@^1.2.14, which@^1.2.9: version "1.3.1" resolved "https://registry.yarnpkg.com/which/-/which-1.3.1.tgz#a45043d54f5805316da8d62f9f50918d3da70b0a" integrity sha512-HxJdYWq1MTIQbJ3nw0cqssHoTNU267KlrDuGZ1WYlxDStUtKUhOaJmh112/TZmHxxUfuJqPXSOm7tDyas0OSIQ== @@ -5274,6 +5238,11 @@ yallist@^4.0.0: resolved "https://registry.yarnpkg.com/yallist/-/yallist-4.0.0.tgz#9bb92790d9c0effec63be73519e11a35019a3a72" integrity sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A== +yaml@^1.10.2: + version "1.10.2" + resolved "https://registry.yarnpkg.com/yaml/-/yaml-1.10.2.tgz#2301c5ffbf12b467de8da2333a459e29e7920e4b" + integrity sha512-r3vXyErRCYJ7wg28yvBY5VSoAF8ZvlcW9/BwUzEtUsjvX/DKs24dIkuwjtuprwJJHsbyUbLApepYTR1BN4uHrg== + yargs-parser@^18.1.2, yargs-parser@^18.1.3: version "18.1.3" resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-18.1.3.tgz#be68c4975c6b2abf469236b0c870362fab09a7b0" @@ -5282,11 +5251,6 @@ yargs-parser@^18.1.2, yargs-parser@^18.1.3: camelcase "^5.0.0" decamelize "^1.2.0" -yargs-parser@^20.2.9: - version "20.2.9" - resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-20.2.9.tgz#2eb7dc3b0289718fc295f362753845c41a0c94ee" - integrity sha512-y11nGElTIV+CT3Zv9t7VKl+Q3hTQoT9a1Qzezhhl6Rp21gJ/IVTW7Z3y9EWXhuUBC2Shnf+DX0antecpAwSP8w== - yargs-parser@^21.0.0: version "21.0.0" resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-21.0.0.tgz#a485d3966be4317426dd56bdb6a30131b281dc55"