0

I'm trying to generate my own index.html file every time I build my application in Webpack and to do so, I installed html-webpack-plugin.

I understand that in order generate an index.html file in my dist folder, I need to have the following in my webpack.config.js file:

output: {
    path: path.resolve(__dirname, './dist'),
    filename: '[name].js',
},
plugins: [
    new HtmlWebpackPlugin(), // creates an index.html file
],

With the above settings, it should generate the following, which is my desired output:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Webpack App</title>
  </head>
  <body>
  <script type="text/javascript" src="build.js"></script></body>
</html>

Unfortunately, I've been using the vue-simple Webpack boilerplate to build my VueJS learning project and as a result, it has a publicPath entry in the output section:

output: {
    path: path.resolve(__dirname, './dist'),
    publicPath: '/dist/',
    filename: '[name].js',
}

With the above settings, the html-webpack-plugin understandably generates the following script tag in the index.html file in my dist folder which is NOT what I need since src is now pointing to "/dist/build.js".

<script type="text/javascript" src="/dist/build.js"></script></body>

If I remove publicPath from my output settings, I can't load my page from my development server since everything breaks. I read this SO post about publicPath but I'm still unsure of what I should do to achieve my goals since everything was set up by the boilerplate. How should I edit my webpack.config.js file in order to generate my desired index.html file when I build without breaking anything on my dev server?

Below is my full webpack.config settings:

const path = require('path');
const webpack = require('webpack');
require("babel-polyfill"); // for async await
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
    // babel-polyfill for async await
    // entry: ["babel-polyfill", "./src/main.js"],
    entry: {
        build: ["babel-polyfill", "./src/main.js"]
    },
    output: {
        path: path.resolve(__dirname, './dist'),
        publicPath: '/dist/',
        filename: '[name].js', // this will output as build.js
    },
    module: {
        rules: [{
            test: /\.css$/,
            use: [
                'vue-style-loader',
                'css-loader'
            ],
        }, {
            test: /\.scss$/,
            use: [
                'vue-style-loader',
                'css-loader',
                'sass-loader'
            ],
        }, {
            test: /\.sass$/,
            use: [
                'vue-style-loader',
                'css-loader',
                'sass-loader?indentedSyntax'
            ],
        }, {
            test: /\.vue$/,
            loader: 'vue-loader',
            options: {
                loaders: {
                    // Since sass-loader (weirdly) has SCSS as its default parse mode, we map
                    // the "scss" and "sass" values for the lang attribute to the right configs here.
                    // other preprocessors should work out of the box, no loader config like this necessary.
                    'scss': [
                        'vue-style-loader',
                        'css-loader',
                        'sass-loader'
                    ],
                    'sass': [
                        'vue-style-loader',
                        'css-loader',
                        'sass-loader?indentedSyntax'
                    ]
                }
                // other vue-loader options go here
            }
        }, {
            test: /\.js$/,
            loader: 'babel-loader',
            exclude: /node_modules/
        }, {
            test: /\.(png|jpg|gif|svg)$/,
            loader: 'file-loader',
            include: '/src/assets/images',
            options: {
                name: '[name].[ext]?[hash]'
            }
        }]
    },
    resolve: {
        alias: {
            'vue$': 'vue/dist/vue.esm.js'
        },
        extensions: ['*', '.js', '.vue', '.json']
    },
    devServer: {
        historyApiFallback: true,
        noInfo: true,
        overlay: true
    },
    performance: {
        hints: false
    },
    plugins: [
        new webpack.ProvidePlugin({ // this injects the following into .vue files
            _: "lodash",
            math: "mathjs",
            moment: "moment",
            axios: "axios",
            Chart: "chart.js",
            firebase: "firebase",
        }),
        new HtmlWebpackPlugin(), // creates an index.html file in dist
    ],
    devtool: '#eval-source-map'
};

if (process.env.NODE_ENV === 'production') {
    module.exports.devtool = '#source-map'
    // http://vue-loader.vuejs.org/en/workflow/production.html
    module.exports.plugins = (module.exports.plugins || []).concat([
        new webpack.DefinePlugin({
            'process.env': {
                NODE_ENV: '"production"'
            }
        }),
        new webpack.optimize.UglifyJsPlugin({
            sourceMap: true,
            compress: {
                warnings: false
            }
        }),
        new webpack.LoaderOptionsPlugin({
            minimize: true
        })
    ]);
}

Below is the folder structure I have:

enter image description here

ptk
  • 6,835
  • 14
  • 45
  • 91

1 Answers1

0

You can alternatively use vue-cli for scaffolding. (Read the vue documentation for vue-cli here https://vuejs.org/2015/12/28/vue-cli/)

The following will give you a full preconfigured webpack config :

vue init webpack project-name

Then you can use npm run build OR yarn build which will generate your index.html in the "dist" folder.

nizantz
  • 1,561
  • 1
  • 13
  • 17
  • Thanks for the suggestion! I realised that this would be a possibility but I thought it might be a slight pain to migrate everything I've done over and risk breaking a lot of things... Do you know how I can fix my problem within the vue-simple scaffold or do you think I should just move everything to the standard vue Webpack template? – ptk Apr 01 '18 at 06:56
  • I would recommend migrating. However , the following should resolve your issue -> output: { path: path.resolve(__dirname, './dist'), publicPath: '', filename: '[name].js', // this will output as build.js } – nizantz Apr 01 '18 at 08:06