Vue-svg-loader: svgRule is undefined with Nuxt 2.0

Created on 26 Sep 2018  路  5Comments  路  Source: visualfanatic/vue-svg-loader

I'm trying to use vue-svg-loader with Nuxt 2.0. I am using vue-svg-loader 0.10.0

In the nuxt.config.js I've added the recommended code:

    extend(config, ctx) {
      // Run ESLint on save
      const svgRule = config.module.rules.find(rule => rule.loader === 'url-loader');

      svgRule.test = /\.(png|jpe?g|gif)$/;

      config.module.rules.push({
        test: /\.svg$/,
        loader: 'vue-svg-loader',
      });

      if (ctx.isDev && ctx.isClient) {
        config.module.rules.push({
          enforce: 'pre',
          test: /\.(js|vue)$/,
          loader: 'eslint-loader',
          exclude: /(node_modules)/,
          options: {
            fix: true
          }
        })
      }
    }

But the svgRule is undefined...

TypeError: Cannot set property 'test' of undefined

I was using the same setup with Nuxt 1.0 and vue-svg-loader 0.8.0. That worked fine.

Most helpful comment

Instead of checking whether it's the URL loader or not, filter for all rules containing "svg":

           config.module.rules
                .filter(rule => rule.test && /svg/.test(rule.test.toString()))
                .forEach(rule => {
                    rule.test = /\.(png|jpe?g|gif)$/
                })

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

All 5 comments

+1

Instead of checking whether it's the URL loader or not, filter for all rules containing "svg":

           config.module.rules
                .filter(rule => rule.test && /svg/.test(rule.test.toString()))
                .forEach(rule => {
                    rule.test = /\.(png|jpe?g|gif)$/
                })

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

@leopoldkristjansson I updated all examples and tested them against Next.js 1.x and 2.x, everything should work now as expected on both major versions.

@manniL Thank you ! <3

https://github.com/Developmint/nuxt-svg-loader might help as well :relaxed:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

andreasvirkus picture andreasvirkus  路  5Comments

MattCCC picture MattCCC  路  6Comments

sashakaralchuk picture sashakaralchuk  路  6Comments

ryanrca picture ryanrca  路  5Comments

psulkava picture psulkava  路  3Comments