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

Using ressourceQuery /blockType=script/ to only convert to vue component svg inside <script> #81

Open
mrleblanc101 opened this issue Aug 20, 2021 · 1 comment

Comments

@mrleblanc101
Copy link
Contributor

Hi,
Would it be possible to load svg inside script tag in SFC (import) as vue-component and all svg inside template using the default Nuxt loader ?

I feel like it would be possible using webpack ressource query but i wasn't actually able to make it work using ressourceQuery: /blockType=script/.

We use SVG in 3 way in our projet:

  1. Inline in template using vue-svg-inline-loader: <img inline src="@/assets/svg/pin.svg" width="14" height="14" />
    We have a SVGo config to strip the fill so we can change the svg color using CSS

  2. Import inside our JS file using @nuxtjs/svg or vue-svg-loader. This is mostly when we have dynamic data and we want to display the correct component without having multiple v-if/v-else-if/v-else in the template with <component :is="componentName" />

  3. Simple <img src="path/to/file.svg" /> but this is were this plugin come into conflict. We now need to use ?inline/?raw to make this work.

@mrleblanc101
Copy link
Contributor Author

mrleblanc101 commented Aug 20, 2021

Here what I tried:

const svgo = {
    plugins: [
        { prefixIds: true },
        { removeTitle: true },
        { removeDesc: true },
        { removeViewBox: false },
        { removeDimensions: true },
        {
            removeAttrs: {
                attrs: ['fill', 'opacity', 'stroke'],
            },
        },
    ],
};

...

build: {
    extend(config, ctx) {
        const svgRule = config.module.rules.find(rule => rule.test.test('.svg'));
        svgRule.test = /\.(png|jpe?g|gif|webp)$/;

        config.module.rules.push({
            test: /\\.svg$/,
            resourceQuery: /blockType=script/,
            use: [
            'vue-loader',
            {
                loader: 'vue-svg-loader',
                options: {
                    svgo,
                },
            },
            ],
        });

        config.module.rules.push({
            test: /\.vue$/,
            loader: 'vue-svg-inline-loader',
            options: {
                inlineKeyword: 'inline',
                inlineStrict: true,
                spriteKeyword: 'sprite',
                spriteStrict: true,
                removeAttributes: ['alt', 'src', 'inline'],
                xhtml: false,
                svgo,
            },
        });
    },
},

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

No branches or pull requests

1 participant