首页 > 解决方案 > 带有 publicPath 的 Webpack 资产生成器不起作用?

问题描述

将 webpack 升级到 5.64.0 版本时,module.rule.generator.publicPath已停止正常工作。

module.exports={
  // ....,
  generator: {
    filename: devMode
      ? "../fonts/[name][ext][query]"
      : "fonts/[name][hash][ext][query]",
    publicPath: "fonts",
  },
}

我有这个错误:

ERROR in ./src/frontlib.sass
Module build failed (from ./node_modules/mini-css-extract-plugi/dist/loader.js):
HookWebpackError: Invalid URL at tryRunOrWebpackError 

现在即使我将 Webpack 降级到以前的版本,我仍然有同样的错误。

如何将生成器设置publicPath为新的 Webpack 版本?

标签: webpackgenerator

解决方案


我有同样的问题,但我得到了它的工作。生成器不支持 publicPath。您必须使用编译器的输出。

...

entry: {
    main: './src/inputFile.js'
},
output: {
    filename: 'outputName.js',
    path: path.resolve(__dirname, '../outputFolder')
}

...

{
    test: /.(ttf|otf|eot|svg|woff(2)?)(\?[a-z0-9]+)?$/,
    type: "asset/resource",
    generator: {
        filename: './fonts/[hash][ext]'
    }
}

我在这个仓库中有一个例子: https ://github.com/Vanillabacke/webpack-5-settings


推荐阅读