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

Nuxtjs + Vue-svg-loader #1332

Closed
danciupuliga opened this issue Aug 10, 2017 · 5 comments
Closed

Nuxtjs + Vue-svg-loader #1332

danciupuliga opened this issue Aug 10, 2017 · 5 comments
Labels

Comments

@danciupuliga
Copy link

danciupuliga commented Aug 10, 2017

I am trying to integrate https://www.npmjs.com/package/vue-svg-loader for loading SVG icons inline as vue components.

I have the following in my nuxt.config file:

{
    test: /\.(svg)$/,
    loader: 'vue-svg-loader', // `vue-svg` for webpack 1.x 
 }, {
    test: /\.(png|jpe?g|gif|svg)$/,
    loader: 'url-loader',
    exclude: /(\/icons)/,
    query: {
        limit: 10000, // 10KO
        name: 'img/[name].[hash].[ext]'
    }
}

It seems that the svg files are still being converted to base64 by the url-loader even though I have excluded the path to the svg directory.

Is there a different way to overwrite the url-loader to ignore svg files?

This question is available on Nuxt.js community (#c1182)
@josantana
Copy link

Hi, @danciupuliga.

I think you need to remove svg from this line, like so:

    test: /\.(png|jpe?g|gif)$/,

The second rule seems to be overwriting the first one.

@josantana
Copy link

@danciupuliga, another thought: Are you using nuxt 1.0.0-rc3 or later? If so, maybe the problem is that you are applying those loader rules on:

build: {
    loaders: [ ... ]
},

If you check RC3 breaking changes, you'll notice that the "build.loaders has been removed". You should place your custom rules directly on build.extend, instead. This is how I am applying my SVG rules right now:

build: {
    extend(config, ctx) {
        // Find default SVG rule
        let imgRuleIndex = null;
        for (let i = 0; i < config.module.rules.length; i += 1) {
            if (config.module.rules[i].test.toString().match('gif')) {
                imgRuleIndex = i;
                break;
            }
        }
        const imgRule = config.module.rules[imgRuleIndex];
        // Overwrite the default images rule, removing SVG from it
        config.module.rules[imgRuleIndex] = {
            test: /\.(png|jpe?g|gif)$/,
            loader: imgRule.loader,
            query: imgRule.query,
        };
        // Now, add your own SVG rule
        config.module.rules.push({
            test: /\.svg/,
            loader: <YOUR-FAVORITE-LOADER>, // 'vue-svg-loader', 'svg-sprite-loader', 'svgo-loader'... you choose. ;)
        });
    },
},

@KonstantinVlasov
Copy link

Or you can try this shorter variant:

  build: {
    extend (config) {
      const urlLoader = config.module.rules.find((rule) => rule.loader === 'url-loader')
      urlLoader.test = /\.(png|jpe?g|gif)$/

      config.module.rules.push({
        test: /\.svg$/,
        loader: 'svg-inline-loader',
        exclude: /node_modules/
      })
    }
  }

@danciupuliga
Copy link
Author

thanks @KonstantinVlasov that worked!

@lock
Copy link

lock bot commented Nov 4, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Nov 4, 2018
@danielroe danielroe added the 2.x label Jan 18, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants