Webpack file-loader 升级后 src= "[object Module]" 的解决方法

Webpack file-loader 升级后 src= "[object Module]" 的解决方法,当 file-loader 的版本是 4.3.0 及以上,则需要在 webpack.config.js 中手动配置属性 esModule 。


{
  test: /\.(jpg|jpeg|png|gif|svg)$/,
  loader: "file-loader",
  options: {
    esModule: false, // 默认值是 true,需要手动改成 false
  }
}




esModule

Type: Boolean Default: true

By default, file-loader generates JS modules that use the ES modules syntax. There are some cases in which using ES modules is beneficial, like in the case of module concatenation and tree shaking.


You can enable a CommonJS module syntax using:


webpack.config.js

module.exports = {
 module: {
  rules: [
   {
    test: /\.css$/,
    use: [
     {
      loader: 'file-loader',
      options: {
       esModule: false,
      },
     },
    ],
   },
  ],
 },
};





参考:

https://webpack.js.org/loaders/file-loader/#esmodule