nuxt / scripts

Third-Party Scripts Meets Nuxt Developer Experience.
https://scripts.nuxt.com
MIT License
290 stars 41 forks source link

Add proper exports when auto-import is turned off #288

Open lukasaric opened 2 weeks ago

lukasaric commented 2 weeks ago

🆒 Your use case

If Nuxt project has autoImports turned off, when you want to import some composable from nuxt/scripts you need to do something like this:

import { useScriptGoogleTagManager } from '@nuxt/scripts/dist/runtime/registry/google-tag-manager'

Which also does not work and it leads to this error:

Image

🆕 The solution you'd like

Add Nuxt aliases or something similar so the developer can easily import composable or components explicitly.

Example:

// re-export everything through the root index barrel file
import { useScriptGoogleTagManager } from '@nuxt/scripts

// re-export just for certain folder index barrel file
import { useScriptGoogleTagManager } from '@nuxt/scripts/registry

// or avoid using barrel files due to performance issues and use at least Nuxt alias and directly import file
import useScriptGoogleTagManager from '@nuxt/scripts/registry/google-tag-manager.ts

🔍 Alternatives you've considered

No response

ℹī¸ Additional info

No response

harlan-zw commented 4 days ago

Hi, I believe the correct usage here is to just import explicitly from #imports

import { useScriptGoogleTagManager } from '#imports'

I'm open to adding a domain-specific alias for the registry scripts as I'm already using a few, but would need a valid use case.

lukasaric commented 4 days ago

Hi, I believe the correct usage here is to just import explicitly from #imports

import { useScriptGoogleTagManager } from '#imports' I'm open to adding a domain-specific alias for the registry scripts as I'm already using a few, but would need a valid use case.

  • #nuxt-scripts -> types
  • #nuxt-scripts-utils -> utils
  • #nuxt-scripts-validator -> valibot or mock

When you import it from #imports it imports the wrong one 🙈 . Other than that, there are multiple issues in general with #imports (e.g go to definition etc) so there are a lot reasons why it would be good improve imports and add aliases as you suggested. @harlan-zw

Image