为什么create react app失败-react-app卡在了installing react-scripts那里

react-scripts build和Webpack都是打包,为什么要使用Webpack? - 知乎4被浏览434分享邀请回答1添加评论分享收藏感谢收起Getting React Projects Ready Fast with Pre-configured Builds & SitePoint
Getting React Projects Ready Fast with Pre-configured Builds
Related Topics:
For a high-quality, in-depth introduction to React, you can’t go past Canadian full-stack developer Wes Bos. , and use the code SITEPOINT to get 25% off and to help support SitePoint.
Starting a new React project nowadays is not as simple as we’d like it to be. Instead of instantly diving into the code and bringing your application to life, you have to spend time configuring the right build tools to set up a local development environment, unit testing, and a production build. But there are projects where all you need is a simple setup to get things running quickly and with minimal effort.
provides just that. It’s a CLI tool from Facebook that allows you to generate a new React project and use a pre-configured Webpack build for development. Using it, you’ll never have to look at the Webpack config again.
How Does Create React App Work?
Create React App is a standalone tool that should be installed globally via , and called each time you need to create a new project:
npm install -g create-react-app
To create a new project, run:
create-react-app react-app
Create React App will set up the following project structure:
├── .gitignore
├── README.md
├── package.json
├── node_modules
├── public
├── favicon.ico
└── index.html
└── src
├── App.css
├── App.js
├── App.test.js
├── index.css
├── index.js
└── logo.svg
It will also add a react-scripts package to your project that will contain all of the configuration and build scripts. In other words, your project depends react-scripts, not on create-react-app itself. Once the installation is complete, you can start working on your project.
Starting a Local Development Server
The first thing you’ll need is a local development environment. Running npm start will fire up a Webpack development server with a watcher that will automatically reload the application once you change something. Hot reloading, however, is only supported for styles.
The application will be generated with a number of features built-in.
ES6 and ES7
The application comes with its own Babel preset, , to support a set of ES6 and ES7 features. It even supports some of the newer features like async/await, and import/export statements. However, certain features, like decorators, have been intentionally left out.
Asset import
You can also import CSS files from your JS modules that allow you to bundle styles that are only relevant for the modules that you ship. The same thing can be done for images and fonts.
During development, your code will also be run through , a static code analyzer that will help you spot errors during development.
Environment variables
You can use Node environment variables to inject values into your code at built-time. React-scripts will automatically look for any environment variables starting with REACT_APP_ and make them available under the global process.env. These variables can be in a .env file for convenience:
REACT_APP_BACKEND=
REACT_APP_BACKEND_USER=root
You can then reference them in your code:
fetch({process.env.REACT_APP_SECRET_CODE}/endpoint)
Proxying to a backend
If your application will be working with a remote backend, you might need to be able to proxy requests during local development to bypass CORS. This can be set up by adding a proxy field to your package.json file:
"proxy": "http://localhost:4000",
This way, the server will forward any request that doesn’t point to a static file the given address.
Running Unit Tests
Executing npm test will run tests using Jest and start a watcher to re-run them whenever you change something:
src/App.test.js
? renders without crashing (7ms)
Test Suites: 1 passed, 1 total
1 passed, 1 total
Snapshots:
0.123s, estimated 1s
Ran all test suites.
Watch Usage
> Press p to filter by a filename regex pattern.
> Press q to quit watch mode.
> Press Enter to trigger a test run.
is a test runner also developed by Facebook as an alternative to Mocha or Karma. It runs the tests on a Node environment instead of a real browser, but provides some of the browser-specific globals using .
Jest also comes integrated with your VCS and by default will only run tests on files changed since your last commit. For more on this, refer to “”.
Creating a Production Bundle
When you finally have something you deploy, you can create a production bundle using npm run build. This will generate an optimized build of your application, ready to be deployed to your environment. The generated artifacts will be placed in the build folder:
├── asset-manifest.json
├── favicon.ico
├── index.html
└── static
├── css
├── main.9a0fe4f1.css
└── main.9a0fe4f1.css.map
├── main.3b7bfee7.js
└── main.3b7bfee7.js.map
└── media
└── logo.5d5d9eef.svg
The JavaScript and CSS code will be minified, and CSS will additionally be run through
to enable better cross-browser compatibility.
Deployment
React-scripts provides a way to deploy your application to GitHub pages by simply adding a homepage property to package.json. There’s also a separate .
Opting Out
If at some point you feel that the features provided are no longer enough for your project, you can always opt out of using react-scripts by running npm run eject. This will copy the Webpack configuration and build scripts from react-scripts into your project and remove the dependency. After that, you’re free to modify the configuration however you see fit.
Recommended Courses
A step-by-step training course to get you building real world React.js + Firebase apps and website components in a couple of afternoons. Use coupon code 'SITEPOINT' at checkout to get 25% off.
In Conclusion
If you’re looking to start a new React project look no further. Create React App will allow you to quickly start working on your application instead of writing yet another Webpack config.
Have you given it a try yet? What did you think? Let me know in the comments!
This post was peer reviewed by . Thanks to all of SitePoint’s peer reviewers for making SitePoint content the best it can be!
Meet the author
Pavels is a software developer from Riga, Latvia, with a keen interest for everything web-related. His interests range from back-end to front-end development, as well as analysis and automation. If you have something to discuss, you can always reach him via Facebook or LinkedIn.
& 2000 & 2017 SitePoint Pty. Ltd.
Recommended Hosting Partner:更好的工作机会
在100offer,提交一份个人资料,一周内即会有5-10家Top互联网公司主动向你发出邀请。100offer确保你的隐私万无一失,同时Consultant将为你全程提供专业服务。
最具价值web全栈课程
只做前端开发培训的好学校,专注于多方向全栈工程师培养。前端名师邵山欢亲自授课,课程涵盖HTML5、CSS3、Node.js、Angular、React诸多课程,免费视频广受学生好评。
跟牛人学前端
跟牛人学前端
妙味大前端自学宝典
妙味课堂2016年JavaScript课程大纲震撼升级、全栈来袭!
前端最新干货
前端最新干货
web在线直播课
潭州教育是中国较早的在线教育平台,教学内容涵盖网络营销,java,javascript,jquery,android,ios,mysql,围棋,刺绣,养殖,农业,手艺,网页设计,平面设计,影视后期,CAD建筑机械,网络营销,商战智慧,办公软件,三维设计,工业设计,淘宝摄影,英语,音乐,大学代理,Photoshop教程,
Max教程,Maya教程,CAD教程,会声会影教程,AI教程,淘宝开店,摄影教程,免费教程,素材下载等众多在线学习精品课程。经过10年的发展,潭州教育已经发展为中国规模较大的在线教育平台。
React 中文文档 (v15.6.1)
React-用于构建用户界面的 JAVASCRIPT 库
CSS3参考手册
CSS3参考手册 - 最新最全的CSS参考手册,CSS3属性集合,CSS3 Properties, CSS3, CSS3手册, CSS3参考手册
您的位置: » 分类:
» 文章: React入门:关于JSX语法
您可能感兴趣的文章
近期最热文章
- 7,757 - 7,473 - 5,055 - 2,914 - 2,464 - 1,426
关注WEB前端开发公众号

我要回帖

更多关于 create react app 慢 的文章

 

随机推荐