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

feat: support @nuxt/components auto-import #63

Closed
wants to merge 1 commit into from

Conversation

antfu
Copy link
Member

@antfu antfu commented Sep 15, 2021

// Auto import for components
const resolver = IconResolver(options.resolver)
this.nuxt.hook('components:extend', async(components: Component[]) => {
const files = await fg(['**/*.vue'], {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Components module is supposed to to glob on added directories. Why are you doing it manually? 🙈

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In https://github.com/antfu/unplugin-vue-components we are scanning for components usages on each request, meaning that if you use a component like this:

// pages/index.vue
<template>
  <mdi-alarm />
</template>

In that case, we will know that the Alarm icon from MDI is used, and inject the import statement to it. While with @nuxt/components, we need to search for the components usage beforehand as we register them globally on dev. This is indeed an workaround, but do you see any better solution for that?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hook is components:dirs

components:extend was mainly added to fine-tune scanned components (IE if generated names need a change) and also exposing multiple components from same export (which i guess you have 1 file per component)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For what I need is kinda another way around, I need to scan the usage of the components used instead of the components to be imported. components:dirs might not work for me as the icons aren't actually exist in the filesystem.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case, we will know that the Alarm icon from MDI is used, and inject the import statement to it. While with @nuxt/components, we need to search for the components usage beforehand as we register them globally on dev. This is indeed a workaround, but do you see any better solution for that?

It is the same for nuxt build. Loader that scans usage is intentionally disabled on development as the cost of this scanning step was proven to be more and not scalable as the number of .vue files and components increases. One can force this behavior in dev by setting loader: true in the components option.

Can't we simply register all unplugin-vue components beforehand to Components module and let it lazy-load them in dev with isAsync: true ?

Copy link

@pi0 pi0 Sep 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another point: There is runtime usage where the scanner cannot basically detect them by usage. Markdown (docus/content) is one example that is rendered after build step :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we simply register all unplugin-vue components beforehand to Components module and let it lazy-load them in dev with isAsync: true ?

While in this case, Iconify has 10,000+ icons from over 100 icon sets. We certainly don't want to register them all 😅 and that's why we go with this on-demand approach.

Loader

Yes, this is indeed we are doing the same thing with the loader and I understand the tradeoff in dev here. If you don't mind to have an additional hook in the loader, we could have the on-demand resolution for loader, and fallback to this solution when the loader is not enabled.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enabling loader if one of dirs requires it, would be nice idea indeed :) Would you please make an Issue+PR?

@jd-solanki
Copy link

Is this PR abandoned?

This was referenced Nov 18, 2021
@antfu antfu marked this pull request as draft March 20, 2022 13:09
@Xezard
Copy link

Xezard commented Aug 18, 2022

Any progress? I wanted to use unplugin icons in my project (nuxt 2 bridge), but after errors in resolving icon components, I realized that it was not worth it. The documentation doesn't say anything about the lack of icon component auto-import in nuxt, which is confusing.

@antfu antfu closed this Dec 16, 2023
@dargmuesli
Copy link

Hey @antfu, just out of curiosity, why was this PR closed? Is there any other fix or workaround available by now? 💡

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants