Webpack 开发服务器配置 - contentBase 不工作
Webpack Dev Server Config - contentBase not working
我正在尝试设置一个 webpack 开发服务器,但由于某种原因,我 运行 遇到了错误。
[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?, compress?, devMiddleware?, headers?, historyApiFallback?, host?, hot?, http2?, https?, ipc?, liveReload?, magicHtml?, onAfterSetupMiddleware?, onBeforeSetupMiddleware?, onListening?, open?, port?, proxy?, server?, setupExitSignals?, static?, watchFiles?, webSocketServer? }
我确实在全局安装了所有需要的包并尝试了一些其他建议,但我无法让它工作。
这是配置:
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,
contentBase: 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' } }],
],
},
},
},
],
},
};
我的文件:
enter image description here
期待您的回答!谢谢
我可以假设错误是在迁移到最新版本 Webpack/DevServer 后出现的,他们做了一些重大更改,包括 devServer 设置。
特别是对于这个问题,请尝试使用此代码而不是 contentBase
:
devServer: {
static: {
directory: path.resolve(__dirname, 'app'),
},
...
这是可以提供帮助的完整迁移指南https://github.com/webpack/webpack-dev-server/blob/master/migration-v4.md
我正在尝试设置一个 webpack 开发服务器,但由于某种原因,我 运行 遇到了错误。
[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?, compress?, devMiddleware?, headers?, historyApiFallback?, host?, hot?, http2?, https?, ipc?, liveReload?, magicHtml?, onAfterSetupMiddleware?, onBeforeSetupMiddleware?, onListening?, open?, port?, proxy?, server?, setupExitSignals?, static?, watchFiles?, webSocketServer? }
我确实在全局安装了所有需要的包并尝试了一些其他建议,但我无法让它工作。
这是配置:
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,
contentBase: 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' } }],
],
},
},
},
],
},
};
我的文件:
enter image description here
期待您的回答!谢谢
我可以假设错误是在迁移到最新版本 Webpack/DevServer 后出现的,他们做了一些重大更改,包括 devServer 设置。
特别是对于这个问题,请尝试使用此代码而不是 contentBase
:
devServer: {
static: {
directory: path.resolve(__dirname, 'app'),
},
...
这是可以提供帮助的完整迁移指南https://github.com/webpack/webpack-dev-server/blob/master/migration-v4.md