【问题标题】:webpack set hash in publicPath cause error when include the chunkhash in filename?webpack set hash in publicPath 在文件名中包含 chunkhash 时导致错误?
【发布时间】:2018-10-22 10:35:28
【问题描述】:

webpack打包项目时,需要将dist文件上传到cdn,然后提高访问速度。

我需要将之前的包保存在 cdn 中,所以我使用构建哈希来唯一化包。

但是我发现在publicPath 中设置[hash] 导致chunkhashfilename 中出现错误?

我知道删除filename中的chunkhash可以解决问题,但我还是想知道为什么?

这是我的小型 webpack 配置:

const path = require("path");

module.exports = {
  entry: "./src/index.js",
  output: {
    path: path.resolve(__dirname, "./dist"),
    filename: "[chunkhash].js",
    publicPath: "https://my.cdn/[hash]/"
  }
};

错误信息:

块主 [entry] 中的错误 [chunkhash].js '[chunkhash].js' 中的块不能使用 [chunkhash] 或 [contenthash](改用 [hash])

【问题讨论】:

    标签: javascript node.js webpack


    【解决方案1】:

    您误解了publicPath 的概念。公共路径是从文件所在的本地域的根目录开始,webpack 必须请求获取必要的其他块的位置。例如,如果是/,webpack 将请求${CURRENT_DOMAIN}/dj834j9384j123.js。所以 publicPath 必须是一条路径。

    另外,如果你使用 webpack-dev-server 或类似的,不应该使用 chunkhash,只能使用 hash。

    【讨论】:

    • 如文档所述,可以使用publicPath 设置cdn 路径。如果我将filename 更改为[hash].js 没有错误,您无法解释。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-21
    • 1970-01-01
    • 2012-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多