如何搭建react项目
手工搭建
npm init -y 初始化package.json
配置webpack.config.js
需要安装的
{ "name": "byme", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "dev": "webpack-dev-server --open --port 8888", "dev-slider": "webpack-dev-server --open --port 8888 --config webpack.config.slider.js"//用其他文件打包 }, "keywords": [], "author": "", "license": "ISC", "devDependencies": { "babel-core": "^6.26.0", "babel-loader": "^7.1.2", "babel-preset-es2015": "^6.24.1", "babel-preset-react": "^6.24.1", "babel-preset-stage-0": "^6.24.1", "css-loader": "^0.28.7", "file-loader": "^1.1.5", "html-webpack-plugin": "^2.30.1", "less": "^2.7.3", "less-loader": "^4.0.5", "style-loader": "^0.19.0", "url-loader": "^0.6.2", "webpack": "^3.8.1", "webpack-dev-server": "^2.9.4" }, "dependencies": { "react": "^16.0.0", "react-dom": "^16.0.0" } }
babelrc:
{ "presets": ["es2015","stage-0","react" ] }
react需要react转译器
安装脚手架
npm install -g create-react-app(项目名)
create-react-app my-app
cd my-app
npm start
安装搭建好的项目,使用内置的webpack打包
在src下建index.jsx文件
该脚手架安装额外的插件需要用yarn add 命令,用npm install会覆盖已经安装的模块