Webpack开发服务器找不到index.html文件。

3

我有以下的webpack配置:

webpack.common.js:

const path = require('path');

module.exports = {
  entry: './src/index.js',
  watchOptions: {
    ignored: [
      path.resolve(__dirname, '.git'),
      path.resolve(__dirname, 'node_modules'),
    ]
  },
  module: {
    rules: [],
  },
  resolve: {
    extensions: ['.js'],
  },
  output: {
    filename: 'bundle.js',
    path: path.join(__dirname, 'dist'),
    publicPath: "/dist/",
  },
};

webpack.dev.js:

const { merge } = require('webpack-merge');
const common = require('./webpack.common.js');
const HtmlWebpackPlugin = require('html-webpack-plugin')

module.exports = merge(common, {
  mode: 'development',
  devtool: 'inline-source-map',
  devServer: {
    open: true,
    compress: false,
    port: 9000,
    http2: false,
    https: false,
    liveReload: true,
    watchFiles: ['src/**/*'],
    client: {
      overlay: false,
    },
    static: {
      directory: __dirname,
    }
  },
  plugins: [
    new HtmlWebpackPlugin({
        template: './src/index.html',
    })
  ]
});

我的文件夹结构:

webpack.common.js
webpack.dev.js
src/
├─ index.html
├─ index.js

工具版本:

"html-webpack-plugin": "^5.5.0"
"webpack": "^5.52.0"
"webpack-dev-server": "^4.1.0"
"webpack-merge": "^5.8.0"

问题:在根目录下运行webpack-dev-server --config webpack.dev.js时,访问localhost:9000/index.html时出现index.html的404错误。难道不应该在内存中创建index.html吗?

启动服务器时,日志中会显示以下内容:

asset bundle.js 901 KiB [emitted] (name: main)
asset index.html 5 KiB [emitted]

如何避免出现404错误并访问存储在内存中的index.html?我是否漏掉了什么?

2个回答

1
也许这个webpack配置可以解决您的问题,index.html文件可能在一个名为"public"而不是"src"的文件夹中,并且启用了在index.html中注入代码。
请尝试以下配置:
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

module.exports ={
  mode: 'development',
  entry: './src/index.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'index.js'
  },
  resolve: {
    extensions: ['.js','.jsx']
  },
  module: {
    rules: [
        {
            test: /\.(js|jsx)$/,
            exclude: /node_modules/,
            use:{
                loader: 'babel-loader'
            }
        },
        {
            test: /\.html$/,
            use:{
                loader: 'html-loader'
            }
        },
        {
            test: /\.(css|scss)$/,
            use:[
                "style-loader",
                "css-loader",
                "sass-loader"
            ]
        },
        {
            test: /\.svg$/,
            use:{
                loader: "svg-url-loader"
            }
        },
        {
            test: /\.png$/,
            use: [
                "file-loader",
                {
                    loader: "image-webpack-loader"
                }
            ]
        }
      ]
   },
  plugins: [
    new HtmlWebpackPlugin({
        template: "./public/index.html",
        inject:true,
        filename: 'index.html'
    }),
    new MiniCssExtractPlugin(),
  ],
  devServer:{ 
    static: {
        publicPath: path.join(__dirname, 'dist')
    },
    open:true,
    compress: true,
    port: 3000
    }
}

0

我的问题出在webpack.common.js文件的这一行:

publicPath: "/dist/"

通过移除它,它就可以工作了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接