问题:
在 webpack.config.js 配置了devServer,通过contentBase 配置了静态资源的路径,但是报错了。报错如下:
[webpack-cli] Invalid options object. Dev Server has been initialized using an options object that does not match the API schema.
- options has an unknown property 'contentBase'. These properties are valid:
object { allowedHosts?, bonjour?, client?, ***press?, devMiddleware?, headers?, historyApiFallback?, host?, hot?, http2?, https?, ipc?, liveReload?, magicHtml?, onAfterSetupMiddleware?, onBeforeSetupMiddleware?, onListening?, open?, port?, proxy?, server?, setupExitSignals?, setupMiddlewares?, static?, watchFiles?, webSocketServer? }
原因及解析:
错误信息中提示contentBase 不是有效的配置项(可以看到contentBase不在罗列的有效配置项中)。因为Weback 5 将 contentBase的配置修改为 static
解决
把 contentBase
改为 static
const path = require('path')
module.exports = {
mode: "development", // 指定构建模式
entry: "./src/main.js", // 指定构建入口文件
output: {
path: path.resolve(__dirname, 'main'), // 指定构建生成的文件名
filename: "build.js" // 指定构建生产的文件名
},
devServer: {
static: path.resolve(__dirname, 'dist') // 开发服务器启动路径
}
}