首页 > 解决方案 > localhost webpack 显示的文件目录?

问题描述

当我运行 webpack 开发服务器时,它给了我这样的 localhost 链接,http://localhost:8080/当我进入时,我得到了我的文件目录,如下图所示。

只有当我单击我的html目录(在我的情况下为构建文件夹)时,我才会重定向我的index.html页面。

如果我index.html构建中删除它可以工作。但是我需要这个目录分发,因为我将html在当前项目中有几个文件。

我的 webpack 版本

"webpack": "^4.8.3"

我该如何解决这个问题请帮忙?

在此处输入图像描述

我的webpack.config.js

const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const webpack = require('webpack');

let conf = {
    entry:{
        index:  "./src/main/index.js"

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

    devServer: {
        overlay:true
    },
    module: {
        rules: [
            {
                test: /\.js/,
                loader:"babel-loader",
                //exclude: "/node_modules/"
            },
            {
                test:/\.scss$/,
                use: ExtractTextPlugin.extract({
                    fallback: "style-loader",
                    use: [
                        {
                            loader: 'css-loader',
                            options: {
                                // If you are having trouble with urls not resolving add this setting.
                                // See https://github.com/webpack-contrib/css-loader#url
                                url: false,
                                minimize: true,
                                sourceMap: true
                            }
                        }, 
                        {
                            loader: 'sass-loader',
                            options: {
                                sourceMap: true
                            }
                        }
                      ]
                })
            }
        ]
    },
    plugins: [
        new ExtractTextPlugin({
            filename:"[name].css"
        }),
        new HtmlWebpackPlugin({
            filename:"index.html",
            template:"build/index.html",
            hash:true,
            chunks:["index"]
        }),

        new webpack.ProvidePlugin({
            '$': "jquery",
            'jQuery': "jquery",
            'Popper': 'popper.js',
            "Bootstrap":"bootstrap.js"
        })
    ]
};

module.exports = (env, options) => {

    let production = options.mode === "production";

    conf.devtool = production ? false : "eval-sourcemap";

    return conf;
} 

标签: webpack

解决方案


通过将此配置添加到我的devServer解决了问题

 devServer: {
        overlay:true,
        contentBase: "./build",
        hot: true
    },

推荐阅读