javascriptvue.jssvgwebpackwebpack-loader

Adding SVG Loader to Vue-cli project for a specific directory only


I am using Vue 2.6.10 in a vue-cli project. I want to use a SVG Loader in this project for SVG files from a specific directory only. These are my current rules in vue.config.js

  chainWebpack: (config) => {
    const svgRule = config.module.rule('svg')
    svgRule.uses.clear()
    svgRule
      .use('vue-svg-loader')
      .loader('vue-svg-loader')
  },

This works - but runs on all SVG files.

I've seen some include and exclude rules for webpack, but I am not sure how you would apply them in this context.


Solution

  • https://github.com/neutrinojs/webpack-chain is in use here. The issue that I have is that after adding a restriction for vue-svg-loader I had to define the file-loader rule for all the other svg files.

    I have two folders, /icons/inline for vue-svg-loader and /icons/plain for the default svgs that have to be served as files. This one works for me:

    chainWebpack: (config) => {
      config.module.rule('svg').uses.clear()
    
      config.module
        .rule('svg')
        .test(/\/icons\/inline\/.*\.svg$/)
        .use('babel-loader')
          .loader('babel-loader')
          .end()
        .use('vue-svg-loader')
          .loader('vue-svg-loader')
          .end()
    
      config.module
        .rule('svg-file')
        .test(/\/icons\/plain\/.*\.svg$/)
        .use('file-loader')
          .loader('file-loader')
          .end()
    }