Vue Cli 3 is not allowing me to process SVG's

2020-03-17 04:10发布

Vue Cli defaults to file-loader for SVG assets, but I want to use svg-sprite-loader (as well as a few others) instead.

I updated the vue.config.js file to do this and it still seems to use file-loader. Almost as though it's not picking up my config at all.

vue.config.js

module.exports = {
  configureWebpack: {
    module: {
      rules: [
        {
          test: /\.(svg)(\?.*)?$/,
          use: [
            {
              loader: 'svg-sprite-loader',
              options: {
                name: '[name]-[hash:7]',
                prefixize: true
              }
            },
            'svg-fill-loader',
            'svgo-loader'
          ]
        }
      ]
    }
  }
}

Is there anything wrong with my setup?

I'm still getting SVG files imported into my component as a URL string / path when it should be an object with properties.

Many thanks.

4条回答
我只想做你的唯一
2楼-- · 2020-03-17 04:46

Vue CLI docs for version 3.x in webpack section suggests to use something like this:

// vue.config.js
module.exports = {
  chainWebpack: config => {
    const svgRule = config.module.rule('svg')

    // clear all existing loaders.
    // if you don't do this, the loader below will be appended to
    // existing loaders of the rule.
    svgRule.uses.clear()

    // add replacement loader(s)
    svgRule
      .use('vue-svg-loader')
      .loader('vue-svg-loader')
  }
}

Even vue-svg-loader configuration guide suggests same approach.

查看更多
Deceive 欺骗
3楼-- · 2020-03-17 04:49

The Webpack docs for Vue CLI 3.0 beta got updated with an example on how to replace an existing Base Loader. For svg-sprite-loader this means that you'll have to add the following configuration to your vue.config.js:

chainWebpack: config => {
  config.module
    .rule('svg')
    .use('file-loader')
    .loader('svg-sprite-loader')
}
查看更多
我想做一个坏孩纸
4楼-- · 2020-03-17 04:56

This took me a while to find a work around. Basically you need to stop file-loader matching on .svg. The best way I have found to do this is using chainWebpack and returning false from the test method on file-loader. I have included my working config.

module.exports = {
  lintOnSave: false,
  configureWebpack: {
    module: {
      rules: [
        {
          test: /\.(svg)(\?.*)?$/,
          use: [
            {
              loader: 'svg-inline-loader',
              options: {
                limit: 10000,
                name: 'assets/img/[name].[hash:7].[ext]'
              }
            }
          ]
        }
      ]
    }
  },
  chainWebpack: config => {
    config.module
      .rule('svg')
      .test(() => false)
      .use('file-loader')
  }
}

查看更多
相关推荐>>
5楼-- · 2020-03-17 05:11

I'm using Vue CLI 3.0.3 and this config works for me

查看更多
登录 后发表回答