html-webpack-plugin 和 webpack 2:没有起始斜杠“/"

问题描述:

我有一个正在运行的 webpack 1 项目,我想将其迁移到 webpack 2.

I have a working webpack 1 project that I want to migrate to webpack 2.

它几乎可以工作了,我剩下的主要问题是 html-webpack-plugin:

It is almost working, my main remaining problem is with html-webpack-plugin:

当我在 webpack 2 中使用它时,生成的脚本标签是这样的:

when I use it in webpack 2, the generated script tag is of the form:

<script type="text/javascript" src="static/js/bundle.js"></script>

代替:

<script type="text/javascript" src="/static/js/bundle.js"></script>

注意:使用相同的插件选项,它在 Webpack 1 中按预期工作.

Note: with the same plugin options, it works as expected in Webpack 1.

以下是 webpack conf 的相关部分:

Here are the relevant parts of webpack conf:

  entry: [
    paths.appIndexJs,
    publicPath,
  ],
  output: {
    pathinfo: true,
    path: paths.appBuild,
    filename: 'static/js/bundle.js',
  },

  // (...)

  plugins: [
    new HtmlWebpackPlugin({
      inject: true,
      template: paths.appHtml,
    }),

我做错了什么?

尝试显式设置 output.publicPath:

output : {
  filename   : 'static/js/bundle.js',
  path       : paths.appBuild,
  publicPath : '/'
}