NPM搭建React开发环境
前提是安装了npm和node
npm或cnpm命令创建项目:
cnpm install -g create-react-app ===>>( npm install -g create-react-app )
create-react-app my-app (my-app是你自己的项目名称 自定义)
创建成功
跟着步骤走:
cd my-app
npm start
也有可能会失败
这时候呢不要方,清理一下缓存再安装 (亲测已解决)
npm cache clean --force
打包发布
npm run build
npm install -g serve
serve build
实现组件的按需打包
· 1.下载依赖模块
yarn add react-app-rewired customize-cra babel-plugin-import
2.定义加载配置的js模块:
在项目根目录下创建 config-overrides.js 文件并添加以下代码
const { override, fixBabeLImports } = require(' customize-cra');
module.exports = override(
// 针对antd实现按需打包
fixBabeLImports(' import ', {
libraryName: 'antd ',
libraryDirectory: 'es ',
style: 'css', //自动打包相关的样式
}),
);
注意*:
react-app-rewired2.0之后,“injectbabelplugin”助手已被弃用 如果是最新版的会报错
react-app-rewired降到2.0的方法
执行以下两条命令
1.npm uninstall react-app-rewired 删除原来的react-app-rewired
2.npm i react-app-rewired@2.0.2-next.0 安装2.0版本
个人笔记专用