react环境配置

前端之家收集整理的这篇文章主要介绍了react环境配置前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1.安装软件

(1) nodejs node下载链接

(2) npm包管理 (安装nodejs的时候已经安装了npm包管理)

2.更换npm库的源

由于国内网络环境问题,配置阿里的npm库源

npm config set registry https://registry.npm.taobao.org


3.构建项目

3.1 选择一个目录,用webstorm构建一个空白的项目


3.2 配置依赖库

打开控制台切到项目所在目录,在控制台里输入npm init 命令来初始化一个package.json文件,这个文件是一个类似于pom.xml的文件,用来描述需要的依赖库。

package.json内容

  1. {
  2. "name": "myfirstreact","version": "1.0.0","description": "\"我的第一个react程序\"","main": "index.js","dependencies": {
  3. "babel": "^6.5.2","webpack": "^1.14.0","webpack-dev-server": "^1.16.2"
  4. },"devDependencies": {
  5. "babel": "^6.5.2","babel-loader": "^6.2.10","babel-preset-es2015": "^6.18.0","babel-preset-react": "^6.16.0","babel-preset-stage-0": "^6.16.0","css-loader": "^0.26.1","eslint": "^3.13.1","eslint-plugin-react": "^6.9.0","file-loader": "^0.9.0","jquery": "^3.1.1","less-loader": "^2.2.3","react": "^15.4.2","react-dom": "^15.4.2","react-hot-loader": "^1.3.1","style-loader": "^0.13.1","url-loader": "^0.5.7","scripts": {
  6. "start": "node server.js","lint": "eslint src"
  7. },"author": "","license": "ISC"
  8. }


3.3 安装需要的文件

npm install webpack webpack-dev-server babel --save-dev

npm install react react-dom babel-loader less-loader css-loader style-loader url-loader file-loader babel-preset-es2015 babel-preset-react react-hot-loader jquery eslint eslint-plugin-react --save-dev

npminstallbabel-core

npm install --save-dev babel-preset-stage-0

--save-dev 会把下载包的相关信息写到package.json的devDependencies里面方便以后发布,其他人使用的时候只需要npm install就可以把相关的依赖下载到当前的项目里面。

在package.json里面包的版本之前的^表示可以安装类似2.x.x版本的组件但是不能安装3.x.x版本的软件
在package.json里面包的版本之前的~表示可以安装2.1.x的软件不能安装2.3.x的软件

3.4 新建一个webpack.config.js的文件

文件里面添加如下信息

  1. 'use strict';
  2. var webpack = require('webpack');
  3. var commonsPlugin = new webpack.optimize.CommonsChunkPlugin('common.js');
  4. var path = require('path');
  5.  
  6. module.exports = {
  7. devtool: 'eval',entry: [
  8. 'webpack-dev-server/client?http://localhost:3000','webpack/hot/only-dev-server','./src/js/entry.js'
  9. ],output: {
  10. path: path.join(__dirname,'dist'),filename: 'bundle.js',publicPath: '/static/'
  11. },plugins: [
  12. new webpack.HotModuleReplacementPlugin(),commonsPlugin
  13. ],module: {
  14. loaders: [
  15. {
  16. test: /\.css$/,loader: 'style-loader!css-loader'
  17. },{
  18. test: /\.jsx?$/,loaders: ['react-hot','babel'],exclude: /node_modules/,},{
  19. test: /\.(png|jpg)$/,loader: 'url-loader?limit=8192'
  20. },{
  21. test: /\.less$/,loader: 'style-loader!css-loader!less-loader'
  22. },{
  23. test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,loader: "url-loader?limit=10000&mimetype=application/font-woff"
  24. },{
  25. test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,loader: "file-loader"
  26. }
  27. ]
  28. },resolve: {
  29. root:'',extensions: ['','.js','.json','.less']
  30. }
  31. };

3.5新建一个.babelrc文件

里面的内容如下:

  1. {
  2. "presets": ["es2015","stage-0","react"]
  3. }

上面的是babel插件的相关信息

3.6 新建一个.eslintrc文件

里面的内容如下:

  1. {
  2. "ecmaFeatures": {
  3. "jsx": true,"modules": true
  4. },"env": {
  5. "browser": true,"node": true
  6. },"parser": "babel-eslint","rules": {
  7. "quotes": [2,"single"],"strict": [2,"never"],"react/jsx-uses-react": 2,"react/jsx-uses-vars": 2,"react/react-in-jsx-scope": 2
  8. },"plugins": [
  9. "react"
  10. ]
  11. }
这个配置是设置js的代码相关规范的

3.7新建server.js文件

添加以下内容

  1. var webpack = require('webpack');
  2. var WebpackDevServer = require('webpack-dev-server');
  3. var config = require('./webpack.config');
  4.  
  5. new WebpackDevServer(webpack(config),{
  6. publicPath: config.output.publicPath,hot: true,inline:true,historyApiFallback: true
  7. }).listen(3000,'localhost',function (err,result) {
  8. if (err) {
  9. return console.log(err);
  10. }
  11.  
  12. console.log('Listening at http://localhost:3000/');
  13. });

3.8创建index.html文件

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <Meta charset="UTF-8">
  5. <title>myFirstReact</title>
  6. </head>
  7. <body>
  8. <div id="root"></div>
  9. <script type="text/javascript" src="/static/common.js" charset="utf-8"></script>
  10. <script type="text/javascript" src="/static/bundle.js" charset="utf-8"></script>
  11. </body>
  12. </html>

3.9 创建src/js文件

3.9.1 创建src/js/helloworld.js
  1. import React,{ Component } from 'react';
  2.  
  3. export default class Hello extends Component {
  4. render() {
  5. return (
  6. <div>
  7. <h1>Hello,world.</h1>
  8. <p>this is a simple section</p>
  9. <p>终于搭建成功了</p>
  10. </div>
  11.  
  12. );
  13. }
  14. }


3.9.2 创建src/js/entry.js:
  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import Hello from './helloworld';
  4.  
  5. ReactDOM.render(<Hello />,document.getElementById('root'));

4 在控制台运行npm run start,


5 在浏览器中输入http://localhost:3000/查看到如下内容

参考 http://www.jianshu.com/p/bf6ca7cb7f8a

猜你在找的React相关文章