我的 webpack 配置有什么问题?

What is wrong in my webpack configurations?

我认为我做的一切都是正确的,但是当我 运行 webpack serve 命令时,我进入了一个列表目录而不是网页,我可能是我做错了什么使 webpack找不到 index.html 文件或可能是其他原因:

webpack.config.js 文件:

const path = require('path');
const HTMLplugin = require('html-webpack-plugin');

const htmlWebpackPlugin = new HTMLplugin({
    template: './public/index.html',
    filename: 'index.html'
}) 

const rules = [
    {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
            loader: 'babel-loader'
        }
    },
    {
        test: /\.css$/,
        exclude: /node_modules/,
        use: ['style-loader', 'css-loader']
    }
];

module.exports = {
    entry: path.join(__dirname, 'src', 'index.js'),
    output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname, './build')
    },
    module: {rules},
    plugins: [htmlWebpackPlugin]
}

package.json 文件:

{
  "name": "hh",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "webpack-dev-server --mode developent --open --hot",
    "build": "webpack --mode production"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@babel/cli": "^7.12.1",
    "@babel/core": "^7.12.3",
    "@babel/preset-env": "^7.12.1",
    "@babel/preset-react": "^7.12.1",
    "babel-loader": "^8.1.0",
    "bootstrap": "^4.5.3",
    "css-loader": "^5.0.0",
    "html-webpack-plugin": "^4.5.0",
    "react": "^17.0.1",
    "react-bootstrap": "^1.4.0",
    "react-dom": "^17.0.1",
    "style-loader": "^2.0.0",
    "webpack": "^5.3.1",
    "webpack-cli": "^4.1.0",
    "webpack-dev-server": "^3.11.0"
  }
}

.babelrc.json 文件:

{
   "presets" : ["@babel/preset-env", "@babel/preset-react];
}

这就是我在 运行 webpack serve 时得到的结果

我的配置有什么问题吗??..为什么我得到的是列表目录而不是我的主页??

您的 webpack.config.js 缺少 contentBase 参数。

此参数将告诉 Webpack 从何处提供内容。在您的例子中,这是 ./public 目录:

const path = require('path');

module.exports = {
  //...
  devServer: {
    contentBase: path.join(__dirname, 'public')
  }
};