我试图以几种不同的方式解决这个问题,所以我必须从头开始.
我有一个名为webpack.dev.js的配置文件,如下图所示:@H_502_3@
const path = require("path"); const HtmlWebpackPlugin = require("html-webpack-plugin"); const ExtractTextPlugin = require("extract-text-webpack-plugin"); const CopyWebpackPlugin = require("copy-webpack-plugin"); module.exports = { entry: "./src/script.js",output: { filename: "bundle.js",path: path.resolve(__dirname,"dist") },devtool: "inline-source-map",devServer: { contentBase: path.join(__dirname,module: { rules: [ { test: /\.js$/,exclude: /node_modules/,use: { loader: "babel-loader",options: { presets: ["env"] } } },{ test: /\.scss$/,use: ExtractTextPlugin.extract({ fallback: "style-loader",use: ["css-loader","postcss-loader","sass-loader"] }) } ] },plugins: [ new HtmlWebpackPlugin({template: path.join("src","index.html")}),new ExtractTextPlugin("style.css"),new CopyWebpackPlugin([{from: "src/images",to: "images"}]) ] };
所以,我在package.json中设置了一个启动脚本来启动dev服务器@H_502_3@
“开始”:“webpack-dev-server –config webpack.dev.js”@H_502_3@
现在是问题的开始.当我运行脚本时,我收到以下错误@H_502_3@
Invalid configuration object. webpack-dev-server has been initialized using a configuration object that does not match the API schema. - configuration has an unknown property 'error'. These properties are valid: object { hot?,hotOnly?,lazy?,bonjour?,host?,allowedHosts?,filename?,publicPath?,port?,socket?,watchOptions?,headers?,clientLogLevel?,overlay?,progress?,key?,cert?,ca?,pfx?,pfxPassphrase?,requestCert?,inline?,disableHostCheck?,public?,https?,contentBase?,watchContentBase?,open?,useLocalIp?,openPage?,features?,compress?,proxy?,historyApiFallback?,staticOptions?,setup?,stats?,reporter?,noInfo?,quiet?,serverSideRender?,index?,log?,warn? }
如您所见,此错误非常令人困惑,因为配置文件中没有任何错误属性@H_502_3@
在尝试了不同的方法来解决这个问题后,我尝试删除devServer属性并使用默认设置启动dev服务器.@H_502_3@
但现在是时候变得奇怪了.如果Web服务器启动了两次,输出看起来如下:@H_502_3@
Project is running at http://localhost:8080/ webpack output is served from / Project is running at http://localhost:8081/ webpack output is served from /
之后,它记录了几个警告,其中有多个模块的名称只有套管不同@H_502_3@
然后经过一些谷歌搜索后,我发现其他人也有这个未知的属性“错误”问题,并且发生在他身上的原因是他在背景中运行了http服务器.@H_502_3@
所以现在,我的理论是,由于某种原因,webpack-dev-server并行运行两次,并且会产生竞争条件或错误,从而触发此未知属性的“错误”问题.@H_502_3@
我只发现另外两个有类似问题的人,他们通过向HtmlWebpackPlugin的配置对象添加inject:false来修复它们.这样做并没有使错误消失,并且在没有devServer配置的情况下运行时它只是从页面中删除了所有js和css,因为它没有注入< link>和< script>标签到html中.@H_502_3@