使用 webpack 在输出目录中保留文件夹结构

Cha*_*had 6 javascript webpack

我有以下文件夹结构:

/index.html
/app/index.tsx
Run Code Online (Sandbox Code Playgroud)

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

/dist/index.html
/dist/app/bundle.js
/dist/app/bundle.js.map
Run Code Online (Sandbox Code Playgroud)

我有以下 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"
            })
        ]    
    }
]
Run Code Online (Sandbox Code Playgroud)

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

jan*_*mon 2

这类似于WebpackHtmlPlugin 问题 #234

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

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

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

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