codefeathers / rollup-plugin-svelte-svg

Import SVG files as Svelte Components
MIT License
76 stars 12 forks source link

Not working in SvelteKit #32

Closed Dan503 closed 1 year ago

Dan503 commented 2 years ago

This is SvelteKit: https://kit.svelte.dev/

This is my svelte.config.js file

import adapter from '@sveltejs/adapter-auto'
import preprocess from 'svelte-preprocess'
import { svelteSVG } from 'rollup-plugin-svelte-svg'

/** @type {import('@sveltejs/kit').Config} */
const config = {
    // Consult https://github.com/sveltejs/svelte-preprocess
    // for more information about preprocessors
    preprocess: preprocess(),

    kit: {
        adapter: adapter(),
        vite: () => ({
            plugins: [svelteSVG({ svgo: {}, enforce: 'pre' })],
        }),
    },
}

export default config

I'm importing it like this in a svelte file:

<script>
    import ArrowRight from '../../icons/arrow-right.svg'
</script>

I am getting this error though:

<ArrowRight> is not a valid SSR component. You may need to review your build config to ensure that dependencies are compiled, rather than imported as pre-compiled modules

MKRhere commented 2 years ago

I was able to reproduce it, but still investigating why this could happen.

notsidney commented 1 year ago

It works for me on SvelteKit 1.0 by adding it to the Vite config instead:

import { sveltekit } from '@sveltejs/kit/vite';
import { svelteSVG } from 'rollup-plugin-svelte-svg';

/** @type {import('vite').UserConfig} */
const config = {
    plugins: [
        sveltekit(),
        svelteSVG({
            svgo: {
                plugins: [
                    {
                        name: 'preset-default',
                        params: {
                            overrides: {
                                removeViewBox: false,
                            },
                        },
                    },
                    'removeXMLNS',
                ],
            },
            // https://vitejs.dev/guide/api-plugin.html#plugin-ordering
            enforce: 'pre',
        }),
    ],
};

export default config;
MKRhere commented 1 year ago

Thank you @notsidney! I'll update the README.