devServer
- 安装webpack-dev-server
cnpm i webpack-dev-server -D
- 在webpack.config.js中新增配置
...其他配置项devServer:{port:8080,open:true //自动打开浏览器}
- 在package.json中新增dev命令
"scripts": {"build": "webpack --config webpack.config.js","dev": "webpack-dev-server --config webpack.config.js"},
- 运行dev命令,测试效果
npm run dev
- 报错解决
Error: Cannot find module ‘webpack-cli/bin/config-yargs’ 解决方案,降级webpack与webpack-cli版本
cnpm i webpack@4 webpack-cli@3 -D
在webpack环境中,运行React项目
- 将React项目代码迁移至自定义webpack环境
- 安装依赖,启动项目
cnpm inpm run dev
- 语法报错
handleSend = async(ev)=>{ //新增任务| ^
babel-plugin的使用
解决方案
cnpm install --save-dev @babel/plugin-proposal-class-properties在.babelrc中配置plugins{"presets": ["@babel/preset-react"],"plugins": ["@babel/plugin-proposal-class-properties"]}
- 样式文件解析报错
You may need an appropriate loader to handle this file type,currently no loaders are configured to process this file.| html,> body {| margin: 0;| padding: 0;
解决方案
css-loader
- 安装样式解析相关loader
cnpm i style-loader css-loader -D
- 在webpack.config.js中的module.rules中配置新的规则
{test: /\.css$/,use: [ 'style-loader', 'css-loader' ]}
