配置React开发环境教程

这是一篇新手向文章,主要是记录一下使用过程,但愿能给予别人一些帮助和提示html

用 Yarn 作包管理
用 Babel 作jsx和es6语法编译器
Webpack 作模块管理和打包node

教程是基于macOS的,Nodejs得提早安装好。个人Nodejs和npm的版本以下react

node -v
v6.9.2

npm -v
3.10.9

Yarn安装和配置

咱们在 macOS 下能够经过brew去安装,以下webpack

brew update
brew install yarn

Yarn 下载的包或者模块都是跟npm一个源的,由于某些缘由,下载速度很是慢,难受,因此咱们得换源git

Yarn 换源和npm的源是一致的,都是共享.npmrc的配置信息,因此修改给 npm 换源就是等于给 Yarn 换源,以下es6

npm set registry https://registry.npm.taobao.org
npm set disturl https://npm.taobao.org/dist
npm cache clean

经过查看.npmrc文件检查是否更换源成功github

vim ~/.npmrc

项目初始化

打开你的终端,新建文件夹而后进入该文件夹,用yarn init去作初始化,过程相似npm init,会有几个选项须要你填写,你能够根据你的须要去填写,这里我就直接一路回车就能够了。web

mkdir react-demo
cd react-demo
yarn init

init完以后,就会提示success Saved package.json,说明初始化成功,咱们能够查看一下package.json有什么东西npm

vim package.json
{
    "name": "react-demo",
    "version": "1.0.0",
    "main": "index.js",
    "license": "MIT"
}

webpack安装和配置

yarn add webpack webpack-dev-server path

安装完毕,你会发现当前目录多了个yarn.lock,这个文件是Yarn用来锁定当前的依赖,不用担忧json

如今,咱们已经安装好webpack了,咱们须要一个配置文件用来执行,以下

touch webpack.config.js

而后更新该文件内容为以下

const path = require('path');
module.exports = {
  entry: './client/index.js',
  output: {
    path: path.resolve('dist'),
    filename: 'index_bundle.js'
  },
  module: {
    loaders: [
      { test: /\.js$/, loader: 'babel-loader', exclude: /node_modules/ },
      { test: /\.jsx$/, loader: 'babel-loader', exclude: /node_modules/ }
    ]
  }
}

从配置文件内容能够看出,为了让webpack运行,咱们须要一个入口entry和一个输出output

为了能让JSX代码或者是ES6的代码也能正常在浏览器运行,咱们须要loaders去装载babel-loader

更多的loaders咱们能够查看 webpack文档

Babel安装和配置

yarn add babel-loader babel-core babel-preset-es2015 babel-preset-react --dev

在webpack配置过程当中,咱们用到了babel-loader,除了这个外,咱们一样须要babel的其余依赖

babel-preset-es2015babel-preset-react这两个是 Babel 的插件,告诉Babel将es2015react的代码编译为Vanilla JS

安装完毕,咱们还须要去配置Babel,新建一个文件为.babelrc

touch .babelrc

而后更新该文件内容为以下

{
    "presets":[
        "es2015", "react"
    ]
}

webpack中的loader的 babel-loader 就是根据这个去执行

配置入口文件

如今咱们的目录结构以下

|-- node_modules
|-- .babelrc
|-- package.json
|-- webpack.config.js
|-- yarn.lock

咱们须要建立新的文件夹,同时在新文件夹内新建index.jsindex.html文件

mkdir client
cd client
touch index.js
touch index.html

而后咱们更新一下index.js的内容为

console.log('Hello world!')

一样地,咱们也要更新一下index.html内容为

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>React App Setup</title>
  </head>
  <body>
    <div id="root">

    </div>
  </body>
</html>

index.html是咱们react组件运行在浏览器上的载体,react组件编写是jsx,同时也用到了es6,因为大多数浏览器是不支持es6和jsx,因此咱们必须经过Babel编译这些代码,而后绑定输出显示在index.html上。

同时咱们还须要html-webpack-plugin包为咱们生成html

cd ..
yarn add html-webpack-plugin

安装完成后,打开webpack.config.js而后添加下面配置信息

const path = require('path');

const HtmlWebpackPlugin = require('html-webpack-plugin');
const HtmlWebpackPluginConfig = new HtmlWebpackPlugin({
  template: './client/index.html',
  filename: 'index.html',
  inject: 'body'
})

module.exports = {

    ...

    module: {
        loaders: [
            ...
        ]
    },

    plugins: [HtmlWebpackPluginConfig]
}

咱们引入html-webpack-plugin,而后建立它的实例,而后配置templatefilenameinject,其中inject: 'body'是告诉插件添加JavaScript到页尾,在闭合body标签前

为了能够运行它,咱们须要配置package.json,在"dependencies": {}代码块前插入以下代码

"scripts": {
    "start": "webpack-dev-server"
},

而后咱们就能够运行了

yarn start

终端出现以下内容

Project is running at http://localhost:8080/

咱们打开浏览器,输入http://localhost:8080/,在开发者工具的Console,发现有一段信息为Hello world!

react安装与配置

yarn add react react-dom

而后进入client目录,建立组件

cd client
mkdir components
cd components
touch App.jsx
cd ../..

如今咱们项目目录结构以下

|-- node_modules
|-- client
     |-- components
         |-- App.jsx
     |-- index.html
     |-- index.js
|-- .babelrc
|-- package.json
|-- webpack.config.js
|-- yarn.lock

而后咱们更新一下App.jsx文件的内容以下

import React from 'react';

export default class App extends React.Component {
  render() {
    return (
     <div style={{textAlign: 'center'}}>
        <h1>Hello World Again</h1>
      </div>);
  }
}

咱们还须要修改一下咱们的入口文件index.js,替换内容为以下

import React from 'react';
import ReactDOM from 'react-dom';
import App from './components/App.jsx';

ReactDOM.render(<App />, document.getElementById('root'));

而后在终端输入yarn start

刷新http://localhost:8080/,就能看到Hello World Again

至此,经过 Yarn 包管理,配置webpack和Babel,去搭建编写react组件的开发环境的新手向教程就此完毕

欢迎访问个人博客~ https://www.linpx.com/

相关文章
相关标签/搜索