Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preserving folder structure in output directory with webpack

I have the following folder structure:

/index.html
/app/index.tsx

After bundling with webpack, I would like to have the following ouput directory with the bundle.js injected into the index.html

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

I have the following webpack configuration

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"
            })
        ]    
    }
]

It seems to work fine, but the script bundle.js is not injected into the index.html as expected. The HtmlWebpackPlugin is supposed to do this. What am I doing wrong?

like image 889
Chad Avatar asked Mar 21 '16 04:03

Chad


1 Answers

This is similar to WebpackHtmlPlugin issue #234.

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

output.publicPath The publicPath specifies the public URL address of the output files when referenced in a browser.

What you can do is to add the 'app' folder name to the output filename:

module.exports = {
  output: {
    filename: "app/[name]-[hash].js",
    path: "dist",
    publicPath: ""
  },
  plugins: [
    new HtmlWebpackPlugin({
       "template": "html!./index.html"
    }),
  ],
}
like image 52
jantimon Avatar answered Oct 07 '22 20:10

jantimon