Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

webpack - How do I use webpack5's 'asset/resource' and `ImageMinimizerPlugin` to mirror source directory's file structure?

This is my config

{
plugins: [
    new ImageMinimizerPlugin({
      minimizerOptions: {
        plugins: ['pngquant'],
      },
    }),
    new ImageMinimizerPlugin({
      deleteOriginalAssets: false,
      filename: '[path][name].webp',
      minimizerOptions: {
        plugins: ['imagemin-webp'],
      },
    })
]
}
// ...
appConfig.output.assetModuleFilename = '[path][name][ext]'

Now I want to add the rule

{
    test: /.(jpe?g|png|svg|gif|webp)$/i,
    type: 'asset/resource',
    generator: {
      filename: (source) => {
        return source.filename.replace('assets/', '')
      },
    },
  }

manifest.json


  "build/app.webp": "/build/assets/images/image-error.webp",
  "build/app.jpg": "/build/images/image-error.jpg",

How do I configure webpack so that the webp images end up next to the original images? This rule will only work for the original images, but the webp images will keep the 'assets/' prefix.

Sure I can remove the filename function, but I don't want them to end up in assets. I need them to end-up one folder down due to legacy code.

question from:https://stackoverflow.com/questions/65904582/how-do-i-use-webpack5s-asset-resource-and-imageminimizerplugin-to-mirror-so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...