Webpack 开发服务器配置 - contentBase 在最新版本中不起作用

Webpack Dev Server Config - contentBase not working in latest version

当我将 webpack 升级到 4.0.0-beta.3 和 运行 npx webpack serve 我得到这个错误:

[webpack-cli] Invalid configuration object. Object has been initialized using a configuration object that does not match the API schema.
 - configuration has an unknown property 'contentBase'. These properties are valid:
   object { bonjour?, client?, compress?, devMiddleware?, firewall?, headers?, historyApiFallback?, host?, hot?, http2?, https?, liveReload?, onAfterSetupMiddleware?, onBeforeSetupMiddleware?, onListening?, open?, port?, proxy?, public?, setupExitSignals?, static?, transportMode?, watchFiles? }

这是我在 3.11.2 中工作的 webpack.config.js:

const path = require('path');
const ArcGISPlugin = require("@arcgis/webpack-plugin");

module.exports = {
  mode: 'development',
  entry: {
      main: './app/main.js'
  },
  plugins: [
      new ArcGISPlugin()
  ],
  devServer: {
      contentBase: './'
  }
}

我的 devDependencies 来自 package.json:

  "devDependencies": {
    "@arcgis/webpack-plugin": "^4.18.0",
    "dojo-typings": "^1.11.11",
    "webpack-cli": "^4.7.2",
    "webpack-dev-server": "^4.0.0-beta.3"

我需要如何更新我的配置才能使最新版本正常运行?当我取出开发服务器对象时,服务器将 运行,但从 ./public 中提供不存在的内容。

我是 webpack 的新手,所以我还不熟悉应用程序、配置和要求。

devServer: {
  static: './'
}

我应该更仔细地阅读错误。上面的对象使我的配置再次工作。

instead of contentBase we are using static



enter code here
const path = require("path");

module.exports = {
  entry: "./app/Main.js",
  output: {
    publicPath: "/",
    path: path.resolve(__dirname, "app"),
    filename: "bundled.js"
  },
  mode: "development",
  devtool: "source-map",
  devServer: {
    port: 3000,
    static: {
      directory: path.join(__dirname, "app")
    },

    hot: true,
    historyApiFallback: { index: "index.html" }
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /(node_modules)/,
        use: {
          loader: "babel-loader",
          options: {
            presets: ["@babel/preset-react", ["@babel/preset-env", { targets: { node: "12" } }]]
          }
        }
      }
    ]
  }
};
  devServer: {
    static: {
      directory: path.join(__dirname, "public")
    },

    compress: true,
    port: 3010, // default 8000
  },

使用static: './directory-name'代替contentBase: './directory-name'

示例:

devServer: {
  static: './dist'
}

将 clean 设置为 false 也很重要。它发生在我身上......

webpackConfig = {
  output: {
    clean: false
  }
}

使用 static 代替 contentBase 在最新的 Webpack v5

中被弃用
  devServer: {
    static: {
      directory: path.join(__dirname, "./")
    },

完整详情:https://webpack.js.org/configuration/dev-server/#devserver