首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Webpack没有生成index.html

Webpack没有生成index.html
EN

Stack Overflow用户
提问于 2018-08-12 05:48:35
回答 1查看 1.3K关注 0票数 1

我使用的是vue和firebase。

当我尝试使用"npm run build“来部署firebase主机服务器时,我得到了这个文件

build.js、build.js.map、logo.png、profile.jpg

我想知道为什么它不生成index.html

所以我试了试HtmlWebpackPlugin,但它也不能工作。

这是我的webpack.config.js

谢谢!

代码语言:javascript
复制
var path = require('path')
var webpack = require('webpack')
var HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
    entry: './src/main.js',
    output: {
    path: path.resolve(__dirname, './dist'),
    publicPath: '/dist/',
    filename: 'build.js'
},
module: {
    rules: [{
            test: /\.css$/,
            use: [
                'vue-style-loader',
                'css-loader'
            ],
        }, {
            test: /\.vue$/,
            loader: 'vue-loader',
            options: {
                loaders: {}
                // other vue-loader options go here
            }
        },
        {
            test: /\.js$/,
            loader: 'babel-loader',
            exclude: /node_modules/
        },
        {
            test: /\.(png|jpg|gif|svg)$/,
            loader: 'file-loader',
            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
},
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
    })
])
}
// webpack.config.js
plugins: [
    new webpack.ProvidePlugin({
        Vue: ['vue/dist/vue.esm.js', 'default'],
        jQuery: 'jquery',
        'window.jQuery': 'jquery',
        $: 'jquery',
        moment: 'moment',
}),
new HtmlWebpackPlugin({
       hash: true,
       filename: './dist/index.html' //relative to root of the application
   })
]

这是我的package.json

代码语言:javascript
复制
 "build": "cross-env NODE_ENV=production webpack --progress --hide-modules"
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-12 06:39:37

看起来你没有导出你的最后两个插件和指定dist的html文件名,因为HtmlWebpackPlugin应该生成与你的path相关的文件,下面是固定的配置:

代码语言:javascript
复制
    var path = require('path')
    var webpack = require('webpack')
    var HtmlWebpackPlugin = require('html-webpack-plugin');

    module.exports = {
        entry: './src/main.js',
        output: {
            path: path.resolve(__dirname, './dist'),
            publicPath: '/dist/',
            filename: 'build.js'
        },
        module: {
            rules: [{
                    test: /\.css$/,
                    use: [
                        'vue-style-loader',
                        'css-loader'
                    ],
                }, {
                    test: /\.vue$/,
                    loader: 'vue-loader',
                    options: {
                        loaders: {}
                        // other vue-loader options go here
                    }
                },
                {
                    test: /\.js$/,
                    loader: 'babel-loader',
                    exclude: /node_modules/
                },
                {
                    test: /\.(png|jpg|gif|svg)$/,
                    loader: 'file-loader',
                    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
        },
        devtool: '#eval-source-map',
        plugins: [
            new webpack.ProvidePlugin({
                Vue: ['vue/dist/vue.esm.js', 'default'],
                jQuery: 'jquery',
                'window.jQuery': 'jquery',
                $: 'jquery',
                moment: 'moment',
            }),
            new HtmlWebpackPlugin({
                hash: true,
                filename: 'index.html'
            })
        ]
    }

    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
            })
        ])
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51804014

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档