-
Notifications
You must be signed in to change notification settings - Fork 35
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
Not work on nuxt 3 #86
Comments
Thanks @dacoto97 for reporting this. |
Solution: npm install vite-svg-loader --save-dev import { defineNuxtConfig } from "nuxt3"
import svgLoader from "vite-svg-loader"
export default defineNuxtConfig({
vite: {
plugins: [svgLoader()]
}
}) |
I reopen the issue because we need to add the support for Vite to this module |
@manuelodelain anything new on this? |
Hi @niklasfjeldberg Unfortunately not ! I would like to do something really soon but had no time to check Vite / Nuxt3. Any help would be appreciated. |
This solution appears to break Nuxt 3's asset resolution Have you found a workaround for this ? |
I appreciate you sharing this! However the problem with this solution is that vite-svg-loader does not work with "~" paths, meaning you need to rewrite all your SVG paths to "../assets/icon.svg" pattern. Sounds a bit limiting when you want to keep using location-independent paths in your project |
In Nuxt 3, you don't need <template>
<span v-if="icon" class="h-[1em] w-[1em]" v-html="icon" />
</template>
<script setup lang="ts">
const props = defineProps<{
name?: string
}>()
// Auto-load icons
const icons = Object.fromEntries(
Object.entries(import.meta.glob('~/assets/images/*.svg', { as: 'raw' })).map(
([key, value]) => {
const filename = key.split('/').pop()!.split('.').shift()
return [filename, value]
},
),
)
// Lazily load the icon
const icon = props.name && (await icons?.[props.name]?.())
</script> |
I wish I knew that before... Although vite-svg-loader offers SVGO optimization, is there any way to use svgo with vite glob import? |
Nope, no SVGO for now. But I will create a module for that. |
Your solution is super interesting but I am still a bit confused. Is there a way to avoid the |
@BenjaminOddou You should be able to get rid of the span by generating a VNode. Something like (untested): const VNode = () => h('svg', { innerHTML: /* your content */ }) |
Hello @madc and thank you very much for your response. I finally succeeded to get rid of the <!-- TheSVG.vue -->
<script setup lang="ts">
const props = defineProps<{ name: string }>()
const icon = defineAsyncComponent(() => import(`../assets/svgs/${props.name}.svg?component`))
</script>
<template>
<component :is="icon" />
</template> <!-- SomePage.vue -->
<template>
<TheSVG id="big-circle" name="big-circle" />
</template> but I still struggle to interact with inline elements inside svg when they are imported. In a nutshell, I remarked that my inner svg elements aren't accessible I created a discussion about it here. Don't hesitate to leave a message or suggestion 😃 |
I defined these two lines in the main.js file
Then I ran npm run build and the icon is displayed |
An alternative to this solution that does not throw an error:
|
use vite-svg-loader
|
The text was updated successfully, but these errors were encountered: