使用html-webpack-plugin在Webpack中生成index.html文件(使用vue-simple样板的项目)

时间:2018-04-01 06:08:06

标签: javascript webpack vue.js html-webpack-plugin

我每次在Webpack中构建应用程序时都尝试生成自己的index.html文件,为此,我安装了html-webpack-plugin

我理解为了在dist文件夹中生成index.html文件,我需要在webpack.config.js文件中包含以下内容:

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

使用上述设置,它应该生成以下内容,这是我想要的输出:

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

不幸的是,我一直在使用vue-simple Webpack boilerplate来构建我的VueJS学习项目,因此,它在输出部分中有一个publicPath条目:

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

通过上面的设置,html-webpack-plugin可以理解地在我的dist文件夹中的index.html文件中生成以下脚本标记,这不是我需要的,因为src现在指向&#34; / dist / build的.js&#34;

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

如果我从输出设置中删除了publicPath,我就无法从开发服务器加载我的页面,因为一切都中断了。我读过这个SO post about publicPath,但我仍然不确定我应该怎样做才能实现我的目标,因为所有内容都是由样板文件设置的。我应该如何编辑我的webpack.config.js文件,以便在构建时生成所需的index.html文件而不会破坏我的开发服务器上的任何内容?

以下是我的完整webpack.config设置:

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

以下是我的文件夹结构:

enter image description here

1 个答案:

答案 0 :(得分:0)

您也可以使用vue-cli进行脚手架。 (在这里阅读vue-cli的vue文档https://vuejs.org/2015/12/28/vue-cli/

以下内容将为您提供完整的预配置webpack配置:

$.ajax({ url: "<?php echo base_url();?>proposal/ajax_load", type: 'POST', data : {"account" : project_id}, success: function(result){ if(result==="true"){ $("#err").show(); $("#err").html("Project_id already exist"); $(".btn").prop('disabled', true); }else{ $("#err").hide(); $(".btn").prop('disabled', false); } } });

然后你可以使用vue init webpack project-namenpm run build来生成你的index.html&#34; dist&#34;夹。