使用webpack

时间:2016-03-21 04:06:52

标签: javascript webpack

我有以下文件夹结构:

/index.html
/app/index.tsx

在与webpack捆绑后,我希望将以下的输出目录与bundle.js一起注入index.html

/dist/index.html
/dist/app/bundle.js
/dist/app/bundle.js.map

我有以下webpack配置

var webpack = require("webpack")
var HtmlWebpackPlugin = require("html-webpack-plugin");

module.exports = [
    {
        entry: "./app/index",
        output: {
            path: "./dist/app",
            filename: "bundle.js"
        },
        devtool: "source-map",
        resolve: {
            extensions: ["", ".tsx", ".ts", ".js"]
        },
        plugins: [
            new webpack.optimize.UglifyJsPlugin(),
            new HtmlWebpackPlugin({
                "filename": "./index.html",
                "template": "html!./index.html"
            })
        ],
        module: {
            loaders: [
            { test: /\.tsx?$/, loader: "ts-loader" },
            ]
        }
    }, 
    {
        output: {
            path: "./dist",
            filename: "bundle.js"
        },
        plugins: [
            new HtmlWebpackPlugin({
                "filename": "./index.html",
                "template": "html!./index.html"
            })
        ]    
    }
]

它似乎工作正常,但脚本bundle.js没有按预期注入index.html。 HtmlWebpackPlugin应该这样做。我做错了什么?

1 个答案:

答案 0 :(得分:2)

这类似于WebpackHtmlPlugin issue #234

来自https://github.com/webpack/docs/wiki/configuration#outputpublicpath

  

output.publicPath publicPath指定公共URL地址   在浏览器中引用时的输出文件。

您可以做的是将'app'文件夹名称添加到输出文件名:

module.exports = {
  output: {
    filename: "app/[name]-[hash].js",
    path: "dist",
    publicPath: ""
  },
  plugins: [
    new HtmlWebpackPlugin({
       "template": "html!./index.html"
    }),
  ],
}