natemoo-re / astro-icon

Inline and sprite-based SVGs in Astro made easy!
https://astroicon.dev
Other
990 stars 57 forks source link

Automatically include all statically-referenced icons #191

Open mikestopcontinues opened 5 months ago

mikestopcontinues commented 5 months ago

What version of astro-icon are you using?

v1.0.2

Astro Info

Astro                    v4.1.1
Node                     v18.19.0
System                   macOS (arm64)
Package Manager          pnpm
Output                   server
Adapter                  @astrojs/vercel/serverless
Integrations             auto-import
                         @astrojs/mdx
                         @astrojs/preact
                         @astrojs/alpinejs
                         astro-icon
                         @astrojs/tailwind
                         @astrojs/partytown

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

I use quite a few icons from my preferred icon set, but I don't use them all. However, every single icon can be inferred from static analysis of my ./src directory.

What's the expected result?

Automatically including all icons that match the following criteria:

  1. If an icon pack is installed @iconify-json/[iconset]
  2. And the pattern /(iconset):(?<icon>[a-z-]+)/ appears anywhere within ./src
  3. And [icon] is exported from @iconify-json/[iconset]
  4. Include it in the bundle.

Also include any icons explicitly listed in astro.config.ts.

What do you think?

Link to Minimal Reproducible Example

N/A

natemoo-re commented 5 months ago

This is an interesting idea! It could definitely work, but I'm obviously hesitant to try to write code that handles all the possible edge cases here and can operate on every possible file type. For example, Astro gets a lot of support questions about Tailwind not supporting dynamic expressions because of the similar strategy they use. It's not the most intuitive behavior IMO.

A similar effort by @antfu called PurgeIcon has since been abandoned, I assume due to the maintenance burden of such complex behavior.

In my opinion, explicit opt-in is almost always a better choice than implicit magic.

hrvstr commented 2 months ago

This would be a really neat feature but I understand that there are too many edge cases to build something like this.

How are you handling explicitly listing icons on larger projects? I have quite a long list of icons in my projects, and maintaining it is a lot of effort. This is especially hard when coming back to an older project. I have to go through all the components and check what icons are used.

Maybe we can do this the other way around and create a script that lists which icons are required to build the project. Let's say I remove all icons from the config file and run the build command. The build fails and I get a list of all missing icons that I can then explicitly list in my config.