Home > Software design >  How to change filename inside a rule? (Vue CLI 5)
How to change filename inside a rule? (Vue CLI 5)

Time:11-25

I have this rule, using vue inspect

      /* config.module.rule('svg') */
          {
            test: /\.(svg)(\?.*)?$/,
            type: 'asset/resource',
            generator: {
              filename: 'img/[name][ext]'
            }
          },

I need to change filename for example to "[contenthash][ext]" but I cannot do it using

    module.exports = defineConfig({
    chainWebpack: (config) => {
    config.module.rule("svg").generator.filename = "[contenthash][ext]";
      },
    }) 

because I cannot set generator ,

I can rewrite all rules using

    module.exports = defineConfig({
    configureWebpack: (config) => {
    config.module.rules = [
          {
            test: /\.(svg)(\?.*)?$/,
            use: [
              {
                loader: "svg-inline-loader",
                options: {
                  name: "[contenthash].[ext]",
                },
              },
            ],
          },
        ];
    },
  })

but I need other rules to exist... so how can I change fileName of svg?

CodePudding user response:

Try this:

chainWebpack: (config) => {
  config.module.rule("svg")
    .set('generator', {
      filename: "[contenthash][ext]"
    })
}

Source

  • Related