自己动手搭建React开发环境之五Conclusion
2017-07-20 10:11
701 查看
导读:React作为近年来大红大紫的view层框架,以其高效、灵活性、强大的社区环境而受到广泛关注。但react也不是直接就能拿来使用的,它必须通过配置相应环境才能更好的开发项目。虽然你可以使用官方推荐的构建React环境方式Create React App,但有时候也想知道到底它是怎么配置的,所以自己动手搭建React环境也是很必要的学习过程。本系列分为5章,翻译自codecademy关于React搭建的教程。本篇原文地址:React Setup, Part V: Conclusion。
做得好!你目前已安装并配置好了Node、npm、React、ReactDOM、Babel和Webpack。那也是所有需要安装的东西。
To make these installed parts actually run, you will be using npm command-line scripts. You’ve already used some of these, such as
为了使那些安装的东西能实际运行,你会用到npm脚本命令。你之前也用到过一些,像
You will find that as your applications become more complex, you will need to use more and more verbose command-line scripts. This can become a serious pain!
随着应用越来越复杂,你会发现自己需要更多冗长的脚本命令。这是一个痛点。
Fortunately,
幸运的是,这里
Inside of your package.json file, find the
在package.json文件里,找到
Let’s say that you need to use the script
假如说你需要
(译者注:原文存在错误,这里scripts、deploy属性都要加双引号,以符合json语法。)
After that, you only need to type
这样做后,你只需敲入
In package.json, replace the
在package.json中,替换
译者注:原文存在错误,这里build、start属性同样要加双引号。记住所有package.json文件中的属性都要加双引号。
在根目录下,创建一个叫’app’的目录。在app中创建两个新文件:app/index.js 和app/index.html。
In app/index.html, copy the following code:
在app/index.html,键入如下代码:
In app/index.js, copy the following code:
在 app/index.js中,键入如下代码:
Inside of the
在
In app/components/App.js, write a component class. This component class can render whatever you want. Don’t forget to require React at the top of the file, and to set
在app/components/App.js中,写个组件类。这个组件可以渲染任何你想要的。别忘了在文件顶部要引入React,在底部设置
In the terminal, type:
在终端,键入:
(译者注:若运行出现以下错误:
Failed to parse json
Unexpected token ‘b’ at 7:5
build: “webpack”,
^
File: E:\html\react\package.json
Failed to parse package.json data.
则可能是因为package.json中属性没有加双引号导致的)
Check for a newly created
检查根目录下新建的
In the terminal, type:
在终端,键入:
Open a new browser tab and navigate to
打开一个新浏览器标签,导航到
In app/components/App.js, make a change to your component class’s render function. Refresh the browser tab and see your change appear on the screen!
在app/components/App.js中,改一下组建的render方法,刷新页面,你会看到更改在显示器上呈现出来。
看起来建立一个简单地app并让它运行起来要做大量的工作。
Perhaps it was, but that was in large part due to the fact that we slowly explained every step. Executing the steps by rote is much faster.
可能如此,但在很大程度上是因为我们对每一个步骤解释得很慢。通过死记硬背执行这些步骤就很快了。
Here’s a condensed version of how to get a React app up and running:
下面是对如何建立并运行React app的简洁版本:
In the terminal, create a new directory.
在终端,创建一个新目录,并
Type the following command-line scripts:
键入以下命令行脚本:
In your root directory, create a file named .babelrc. Write this inside:
在根目录下,新建.babelrc文件,并编写:
In your root directory, create another file named webpack.config.js. Write (or copy) this inside:
在根目录下,创建另一个webpack.config.js文件,并编写:
In package.json, replace the
在package.json中,将
Create a directory inside of your root directory named
在根目录下新建目录
Create three new files: app/index.js, app/index.html, and app/components/App.js.
新建app/index.js文件、app/index.html文件和app/components/App.js文件。
Copy the code from “Run a Local React App” into app/index.js and app/index.html. Make app/components/App.js the outermost component class of your new app.
复制”Run a Local React App”部分中的代码到app/index.js和app/index.html。app/components/App.js作为app的最外层组件类。
In the terminal, type these two commands:
在终端,键入以下命令:
Enjoy programming in React!
祝您React编程愉快!
(完)
npm scripts
Great work! You’ve installed and configured Node, npm, React, ReactDOM, Babel, and Webpack. That’s all of the installation that you need!做得好!你目前已安装并配置好了Node、npm、React、ReactDOM、Babel和Webpack。那也是所有需要安装的东西。
To make these installed parts actually run, you will be using npm command-line scripts. You’ve already used some of these, such as
npm initand
npm install --save react.
为了使那些安装的东西能实际运行,你会用到npm脚本命令。你之前也用到过一些,像
npm init和
npm install --save react。
You will find that as your applications become more complex, you will need to use more and more verbose command-line scripts. This can become a serious pain!
随着应用越来越复杂,你会发现自己需要更多冗长的脚本命令。这是一个痛点。
Fortunately,
npm scriptsare here to help.
幸运的是,这里
npm scripts可以提供帮助。
Inside of your package.json file, find the
scriptsobject. This object lets you give your command-line scripts easier names, and look them up whenever you forget them!
在package.json文件里,找到
scripts对象。这个对象允许给脚本命令取一个较短的名字,能让你忘记命令时可以查询到。
Let’s say that you need to use the script
npm run build && npm run git-commit && npm run git-push. That’s way too much to remember. In package.json, you could add:
假如说你需要
npm run build && npm run git-commit && npm run git-push这段命令。这太难记住了。不过,在package.json中,你可以添加以下语句:
(译者注:原文存在错误,这里scripts、deploy属性都要加双引号,以符合json语法。)
After that, you only need to type
npm run deploy, and it will execute the command saved as
deploy’s value. And even better, you can look up the command in package.json if you forget.
这样做后,你只需敲入
npm run deploy,就会执行保存为deploy值的命令。更好的是,如果你忘记了,你可以在 package.json中查找。
In package.json, replace the
scriptsobject with this:
在package.json中,替换
scripts对象成下面这样:
译者注:原文存在错误,这里build、start属性同样要加双引号。记住所有package.json文件中的属性都要加双引号。
npm run buildwill make webpack perform its transformations.
npm run build会使webpack执行转变。
npm run startwill start a local server!
npm run startwill also give you the ability to change your app and see the changes automatically, without having to restart the server for each new change.
npm run start会启动本地服务器。
npm run start使你在更新之后能直接看到结果,而不必每次做改变时都要重启服务器。
Run a Local React App
Inside of your root directory, create a new directory namedapp. Create two new files inside of
app: app/index.js and app/index.html.
在根目录下,创建一个叫’app’的目录。在app中创建两个新文件:app/index.js 和app/index.html。
In app/index.html, copy the following code:
在app/index.html,键入如下代码:
In app/index.js, copy the following code:
在 app/index.js中,键入如下代码:
Inside of the
appfolder, create a new folder named
components. Create a new file inside of app/components named app/components/App.js.
在
app文件夹內,新建一个
components的文件夹。在里面创建一个新文件:app/components/App.js。
In app/components/App.js, write a component class. This component class can render whatever you want. Don’t forget to require React at the top of the file, and to set
module.exportsequal to your component class at the bottom of the file.
在app/components/App.js中,写个组件类。这个组件可以渲染任何你想要的。别忘了在文件顶部要引入React,在底部设置
module.exports等于你的组建类。
In the terminal, type:
在终端,键入:
(译者注:若运行出现以下错误:
Failed to parse json
Unexpected token ‘b’ at 7:5
build: “webpack”,
^
File: E:\html\react\package.json
Failed to parse package.json data.
则可能是因为package.json中属性没有加双引号导致的)
Check for a newly created
buildfolder inside of your root directory. The build folder should contain new html and js files: build/index.html and build/transformed.js.
检查根目录下新建的
build的文件夹。该目录应该包含新的html和js文件:build/index.html和build/transformed.js。
In the terminal, type:
在终端,键入:
Open a new browser tab and navigate to
http://localhost:8080. See your React component shining gloriously in the sun.
打开一个新浏览器标签,导航到
http://localhost:8080。你会发现你的React组件在太阳下闪烁着耀眼的光。
In app/components/App.js, make a change to your component class’s render function. Refresh the browser tab and see your change appear on the screen!
在app/components/App.js中,改一下组建的render方法,刷新页面,你会看到更改在显示器上呈现出来。
The Condensed Version(简洁的版本)
That seems like an enormous amount of work to get a simple app up and running!看起来建立一个简单地app并让它运行起来要做大量的工作。
Perhaps it was, but that was in large part due to the fact that we slowly explained every step. Executing the steps by rote is much faster.
可能如此,但在很大程度上是因为我们对每一个步骤解释得很慢。通过死记硬背执行这些步骤就很快了。
Here’s a condensed version of how to get a React app up and running:
下面是对如何建立并运行React app的简洁版本:
In the terminal, create a new directory.
cdinto that directory.
在终端,创建一个新目录,并
cd到该目录。
Type the following command-line scripts:
npm init
npm i -S {react,react-dom}
npm i -D babel-{core,loader} babel-preset-react
npm i -D webpack webpack-dev-server html-webpack-plugin
键入以下命令行脚本:
npm init
npm i -S {react,react-dom}
npm i -D babel-{core,loader} babel-preset-react
npm i -D webpack webpack-dev-server html-webpack-plugin
In your root directory, create a file named .babelrc. Write this inside:
在根目录下,新建.babelrc文件,并编写:
In your root directory, create another file named webpack.config.js. Write (or copy) this inside:
在根目录下,创建另一个webpack.config.js文件,并编写:
In package.json, replace the
scriptsobject with this:
在package.json中,将
scripts对象替换成这样:
Create a directory inside of your root directory named
app, and another directory inside of app named app/components.
在根目录下新建目录
app,在app中新建目录app/components。
Create three new files: app/index.js, app/index.html, and app/components/App.js.
新建app/index.js文件、app/index.html文件和app/components/App.js文件。
Copy the code from “Run a Local React App” into app/index.js and app/index.html. Make app/components/App.js the outermost component class of your new app.
复制”Run a Local React App”部分中的代码到app/index.js和app/index.html。app/components/App.js作为app的最外层组件类。
In the terminal, type these two commands:
在终端,键入以下命令:
Enjoy programming in React!
祝您React编程愉快!
(完)
相关文章推荐
- 自己动手搭建React开发环境之三Webpack
- 自己动手搭建React开发环境之一React和ReactDOM
- 自己动手搭建React开发环境之四HTMLWebpackPlugin
- 自己动手搭建React开发环境之二Babel
- [置顶] 自己动手安装turbogears -- 搭建基本Python web开发服务器环境
- 自己动手写操作系统——开发环境搭建
- 自己动手写虚拟机(二)——搭建开发环境,基于Ubuntu+Bochs
- 自己动手搭建Android开发环境
- 自己动手在Red Hat Enterprise 6.0上搭建LAMP开发环境,你也可以的!
- React - Native 开发环境搭建
- 自己动手搭建 Linux 0.12 编译环境 — Linux主机
- 自己动手打造嵌入式Linux软硬件开发环境
- 自己动手写操作系统:0.环境搭建
- [React Native混合开发]React Native for iOS之环境搭建
- React-Native痛点解析之开发环境搭建及扩展
- 搭建express+reactjs前后端分离开发环境
- 自己动手写操作系统(二)——搭建bochs环境
- Sublime Text 3 搭建 React.js 开发环境
- 自己动手搭建 MongoDB 环境,并建立一个 .NET HelloWorld 程序测试
- 在Windows平台搭建ReactNative全套开发环境