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

乍得

我具有以下文件夹结构:

/index.html
/app/index.tsx

与webpack捆绑后,我想将下面的ouput目录与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应该这样做。我究竟做错了什么?

詹蒂蒙

这类似于WebpackHtmlPlugin问题#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"
    }),
  ],
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

展平多级文件夹树结构,仅在输出中保留“文件”类型的项目

使用数据工厂复制文件时如何在 blob 存储中保留文件夹结构

压缩文件夹及其所有内容,而无需在Python中保留目录结构

从文件夹和子文件夹复制文件,但不保留目录结构

Babel使用通配符编译路径,但保留文件夹结构

在conlp中使用concat()保留文件夹结构

如何删除文件和文件夹但保留目录结构并留下空文件而不是文件本身?

使用powershell脚本复制保留文件夹结构的文件夹内容

Powershell 复制,保留文件夹结构

使用Webpack将src文件夹结构保存到dist文件夹中?

如何移动一些保留预定义目录结构的特定文件夹?

C#-从ZIP提取特定目录,保留文件夹结构

使用文件夹属性生成的文件夹图标在注册表中保留在哪里?

介子复制/安装头文件到输出目录并保持文件夹结构

webpack 将同名目录下的所有文件输出到不同的文件夹中

从所有子目录删除文件,保留一个子目录以外的文件夹结构

当我在TFS中排队新的Build时,能否在我的项目目录中保留文件夹的内容?

使用保留的文件夹树结构复制具有指定扩展名的文件

tar整个子文件夹和另一个子文件夹中的单个文件,保留目录结构吗?

Gulp:在目标位置保留单个文件的文件夹结构

在保留文件夹结构的同时复制大量文件

在保留文件夹结构的同时仅压缩文件夹中的特定文件

压缩内容而不保留文件夹结构

保留子文件夹结构的递归 ffmpeg 脚本

使用IoC在项目中推荐的目录文件夹结构是什么

Visual Studio:如何在不复制文件夹结构的情况下“复制到输出目录”?

将一组特定文件从一个目录复制到另一个目录,同时保留文件夹结构

使用webpack将资产文件夹推送到公共目录

Web项目的文件夹/目录结构-最佳做法