【问题标题】:webpack dev server throws 404webpack 开发服务器抛出 404
【发布时间】:2022-01-09 16:28:45
【问题描述】:

我无法使用 webpack 开发服务器来处理我当前的配置。我不断收到 404,没有任何错误或提示,为什么或是什么导致了错误。 我的项目源码目录结构

.src
├── client
│   ├── client.ts
│   ├── index.html
│   └── tsconfig.json
└── server
    ├── server.ts
    └── tsconfig.json

我的 webpack.config.js

const path = require("path");
const copyPlugin = require("copy-webpack-plugin");

module.exports = {
  entry: "./src/client/client.ts",
  mode: "development",
  devServer: {
    watchFiles: ["src/**/*"],
    //port: 3000,
    hot: true,
    compress: true,
    open: "Chrome",
  },
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        use: "ts-loader",
        exclude: /node_modules/,
      },
      {
        test: /\.css$/i,
        include: path.resolve(__dirname, "src"),
        use: ["style-loader", "css-loader", "postcss-loader"],
      },
    ],
  },
  resolve: {
    extensions: [".tsx", ".ts", ".js"],
  },
  plugins: [
    new copyPlugin({
      patterns: [{ from: "./src/client/index.html", to: "/client/index.html" }],
    }),
  ],
  output: {
    filename: "bundle.js",
    path: path.resolve(__dirname, "./dist"),
    publicPath: "/",
    clean: true,
  },
};

我使用"start": "webpack serve --mode=development --open", 作为我的启动脚本。 谢谢

【问题讨论】:

    标签: webpack webpack-dev-server


    【解决方案1】:

    在配置中的 webpack devServer 对象下添加 static: path.join(__dirname, "./src/client"), 解决了我的问题。 完整的配置,以防它帮助别人

    const path = require("path");
    const copyPlugin = require("copy-webpack-plugin");
    
    module.exports = {
      entry: "./src/client/client.ts",
      mode: "development",
      devServer: {
        watchFiles: ["src/**/*"],
        port: 3000,
        hot: true,
        compress: true,
        open: "Chrome",
        static: path.join(__dirname, "./src/client"),
      },
      module: {
        rules: [
          {
            test: /\.tsx?$/,
            use: "ts-loader",
            exclude: /node_modules/,
          },
          {
            test: /\.css$/i,
            include: path.resolve(__dirname, "src"),
            use: ["style-loader", "css-loader", "postcss-loader"],
          },
        ],
      },
      resolve: {
        extensions: [".tsx", ".ts", ".js"],
      },
      plugins: [
        new copyPlugin({
          patterns: [{ from: "src/client/index.html", to: "/client/index.html" }],
        }),
      ],
      output: {
        filename: "bundle.js",
        path: path.resolve(__dirname, "./dist"),
        publicPath: "/",
        clean: true,
      },
    };
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-20
      • 2015-10-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多