首页 文章

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

提问于
浏览
0

每次我在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现在指向“/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

1 回答

  • 0

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

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

    vue init webpack project-name

    然后你可以使用 npm run buildyarn build ,这将在"dist"文件夹中生成index.html .

相关问题