Environment for building react projects

Source: Internet
Author: User
Tags what php


JaneRegisterLoginAdd concernauthor Xiao Huang man 2016.03.25 17:27* Wrote 3090 words, was 7 people attention, gained 5 likes to build amazeui+react+webpack+webstorm development environmentwords 2549 read 3292 comments 5 likes To a paragraph of advertising


Have you ever met a problem with vertical centering? Have you ever heard of flex flexible layouts? Never heard of it. Find out more about flex layouts and see how powerful flex layouts are!!! Still worried Flexbox layout is not compatible with the latest browser? Click the compatible ie9+, ucbroswer flexlayout for compatibility flexlayout react components.


From source


Recently in the development of learning amazeui-react, found that react is really a good thing, but the egg pain is webpack this thing is very difficult to configure, a little attention will lead to automatic compilation failure, there will be a bunch of problems, please follow me from the zero start learning react+ Webpack configuration, and how to use them well in webstorm.


Get ready


1, download Webstorm11 and Nodejs.
Download too slow? Direct Baidu Cloud Packaging share it ~~windows mac OSX


Begin


Because my computer's MacBook is not windows, there may be slightly different installation and operation procedures, but the difference is not big, the following operation is basically generic, not generic I will specify. Or you can leave a message below.


    1. First of all, make sure you already understand what react is doing, to react still do not understand, please move here: react Quick Start, I summarize the following: You will directly understand the react to be able to use JS to implement the Web front-end of the component of the development of a framework is good, It simplifies DOM operations and improves performance through virtual DOM, while enabling better modular encapsulation. There's no need to say anything deeper.
    2. Second, you have to know what Webpack is doing, webpack do not understand, please go here: Webpack official website. This site I did not find the official Chinese site, but Csdn should have a lot of relevant tutorials. Webpack is a build tool that can compile and package all the static resources, such as. js,. css,. png,. Less and so on, as long as it is static, not what PHP, JSP and other dynamic Web files can be packaged. Unified management of resources and modules through it.
    3. Again, you have to know a little nodejs knowledge, this site Nodejs Chinese course will tell you nodejs what to do, what API. Nodejs is a high-performance JavaScript framework on the server side that enables fast processing of high-concurrency requests. It mainly uses the callback principle, the API provided by it can implement some Web request processing. Among them, Nodejs contains a NPM, the full name is the node Package Manager, is plainly the node project's packet manager, through the NPM command, we can use thousands of JavaScript framework.
    4. In the end, you'll have to use Webstorm. This is known as the JavaScript development artifact IDE, which can greatly improve the efficiency of your web front-end development.
      In conclusion, I think the person who can enter this blog must have already known what the above tools are for, or else come in? Stopping by?
Entry
  1. After installing the above tools, please open your command line tool: Windows press the Windows symbol key +r, enter CMD, will pop up the command line tool, Mac directly in the launchpad to find the terminal can, Linux and Mac no difference, to find the terminal, Let's call it a command line.
  2. First enter: npm-v on the command line to see if a version number appears. If it does, it proves that your Nodejs installation is successful and you do not need to configure environment variables. If it doesn't, then you have to configure the environment variable. However, the Nodejs default is to configure your environment variables.
  3. Configure the proxy address for NPM: (due to the Great Wall, it can cause the download package to be slow and slow ~ ~ ~)
    set registry https://registry.npm.taobao.org
  4. It's on the right track!!! Refreshing! Attention! Look at this, look at this ~ ~
  5. Starting Webstorm, the first time you use it, will let you import the previous configuration. If there is a previous configuration file to import, do not choose the first time to use that item. Then select, create New project, and create a project. The project name is called:react_webpack_test Bar.
  6. Click OK to enter the interface. Then open the Preferences setting for Webstorm (Windows is file-settings), select Jsx Harmony in Languages & Frameworks JavaScript column, click OK.
  7. reopen the command line, enter
    sudo npm install webpack webpack-dev-server babel  -g
    sudo npm install react react-dom babel-loader less-loader css-loader style-loader url-loader file-loader babel-preset-es2015 babel-preset-react --save-dev
    installation results will show some warn, ignore! But don't make any error.
  8. The first command is a global installation, the module will be installed in/usr/local/, note that the global installation of Mac/linux system requires sudo and then enter the password, and Windows does not require sudo oh ~ ~ The second will be installed under ~/node_modules/.

    Parameter explanation:
    -G: Allows the package to be used globally
    --save-dev: Writes dependency information to the Package.json devdependencies.
    Each component of the installation is interpreted as:
    Babel: Can parse the ES6 syntax into ES5 (what is es??). ecmascript*6* introduction himself, JS is a subset of it, White is the latest JavaScript syntax), used to use Jsx-loader to parse, and now directly with the Babel replacement on the line.
    Babel-preset-es2015 and Babel-preset-react: Let Babel support the two preset values for es2015 syntax and JSX syntax.
    Webpack: Does this still mean anything?
    React-dom: Depending on react, it is now rendered through the Reactdom.render () method instead of React.render (), as Facebook officials have said.
    Webpack-dev-server:webpack Development Server to help developers monitor Webpack project file changes and reflect them to the browser in real time. So you can preview it in real time ~
    Babel-loader css-loader babel-loader style-loader url-loader file-loader: Several types of webpack need to use the loader, convenient to parse CSS, ES6/JSX, and other static files.
    Tips:
    The global installation supports the command to enable the installation package directly on the command line, for example: If you install Webpack, then the command line input Webpack will appear the corresponding execution, but non-global does not support the use of this command OH.

  9. Next, turn on Webstorm (it's best if you don't close the two tools). At that moment.react_webpack_testProject, create a file with the file name Webpack.config.js, and then write the following code inside:
     
    
    var webpack=require(‘webpack‘); var commonsPlugin=new webpack.optimize.CommonsChunkPlugin(‘common.js‘); module.exports={
    entry:{index:‘./src/js/entry.js‘},
    output:{
        path:‘dist‘,
        filename:‘bundle.js‘
    }, module:{
        loaders:[
            {
                test:/\.css$/,
                loader:‘style-loader!css-loader‘
            },
            {
                test:/\.jsx?$/,
                loader:‘babel‘,
                exclude: /node_modules/,
                query:{
                    presets:[‘es2015‘,‘react‘]
                }
            },
            {
                test:/\.(png|jpg)$/,
                loader:‘url-loader?limit=8192‘
            },
            {
                test:/\.less$/,
                loader:‘style-loader!css-loader!less-loader‘
            },
            {
                test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
                loader: "url-loader?limit=10000&mimetype=application/font-woff"
            },
            {
                test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
                loader: "file-loader"
            }
        ],
        plugins:[
            commonsPlugin
        ]
    },
    resolve:{
        root:‘‘,
        extensions:[‘‘,‘.js‘,‘.json‘,‘.less‘],
        alias:{
            AppStore:‘js/stores/AppStores.js‘
        }
    }
    }
    Then create a src folder in the current project directory, create a JS folder in Src, a new entry.js file in the JS folder, the file content is as follows:
     
    
    var React=require(‘react‘); var ReactDOM=require(‘react-dom‘);
    ReactDOM.render(<div>Hello world!</div>, document.getElementById("test"));
    Next, create a new Dist folder in the current project directory, and create a new index.html file in the Dist folder. The contents are as follows:
     
    
     
    
    <html>
    <meta charset="utf-8">
    <title>React webpack test</title>
    </head>
    <body>
    <div id="test"></div>
    </div>
    <script type="text/javascript" src="./bundle.js" charset="utf-8"></script>
    </body>
    </html>
    One last step, one last step.~ ~ Join the Package.json file to make it a project that can be executed with the NPM command.
    {
    "name": "react_webpack_test",
    "version": "1.0.0",
    "description": "just webpack and react hello world project!",
    "scripts": {
    "build": "webpack --progress --profile --colors",
    "watch": "webpack-dev-server --port 63340 --content-base ./dist/",
    "test": "echo \"Error: no test specified\" && exit 1"
    }
    The most important of these is the script tag.
    Next we open the Webstorm terminal function (at the bottom of the IDE) and enter
    npm run build
    Then, in your terminal, you'll see the following.
    > [email protected]1.0.0 build /Users/你的工程所在目录/react_webpack_test
    > webpack \--progress \--profile \--color 2m0ms op1ms optimiz12ms emit
    Hash: 292766b53fb3969afd55
    Version: webpack 1.12.14
    Time: 5109ms
    Asset    Size  Chunks             Chunk Names
    bundle.js 676 kB 0  [emitted]  index
    \+ 159 hidden modules
    If there's a red error inside, then you have some problems with the project configuration, if OK, open the browser and run your dist/index.html file inside to see if there is: Hello world! Output
  10. Debug your code in real time ~
    Enter NPM run watch in the Webstorm terminal, and then output a bunch of logs in the log, no red error in the logs, and output the following two sentences:
    Http://localhost:63340/webpack-dev-server/
    Omit this part...
    Webpack: bundle is now VALID.
    
    Input: http://localhost:63340/webpack-dev-server/, you should be able to see the results you want, the results are as follows:
    Insert Picture OH
  11. This is used to sucks, happy Singles Day!!
Advanced


Drink a cup of tea ~
...

By the way, you can say that your project is done, and if you want to use the Amazeui-react component like I do, keep going with me.


  1. Installing Amazeui-react
    install amazeui amazeui-react --save-dev
  2. Modify the./src/js/entry.js file and replace it with the following code:
    Var React=require(‘react‘);
    Var ReactDOM=require(‘react-dom‘);
    Var AMUI=require(‘amazeui/dist/css/amazeui.min.css‘);
    Var AMUIReact = require(‘amazeui-react‘);
    Var button=(
      <AMUIReact.Button> This is a button </AMUIReact.Button>
    );
    ReactDOM.render(button,document.getElementById("test")); 
  3. Ctrl+s, save the current code, check your browser http://localhost:63340/webpack-dev-server/page has changed ~ ~


Here is my test project link, download it yourself and take it.
React_webpack_test



Full ligatures Belt Test toss me an afternoon, in order to ensure that every place write no omission, I now unload my computer on the Webstorm and nodejs~~


recommended Expand ReadingCopyright belongs to the author


If you feel that my article is useful to you, please feel free to make a reward. Your support will encourage me to continue to create!


CNY Rewards supportlike5share to WeiboShare toMore SharexLike the user
    • Autumn Night has cooled 2016.09.12 13:42
    • Remilia2016.09.12 02:20
    • Broken Jade 2016.07.26 09:48
    • Bugfucker2016.06.04 13:29
    • _yuchen earthquake 2016.04.18 08:59
25 Reviews Add a new comment by time sequence • Reverse chronological • Sort by preference


_yuchen earthquake


2/F ·2016.04.18 15:02


Excuse me, why webpack. Config. JS without require (' path ')
And please ask the package. What is the function of/.dist/in the watch in JSON??????


enjoyed (0)Reply


Small yellow man: @_yuchen, webpack.config.js inside used to require, the beginning of this sentence, Var webpack=require (' Webpack '); all Nodejs dependencies if you need to reference an external class, The Require is unavoidable. Watch in the./dist/refers to the Webpack-dev-server listening directory, has been listening to the current Location Dist directory, can be guaranteed when the Dist directory file changes in real-time from the browser can be refreshed, Specific Webpack-dev-server usage Please check http://webpack.github.io/docs/webpack-dev-server.html, he is not necessary for webpack, just to assist the development of real-time view of the preview effect.


Reply2016.04.19 10:24


_yuchen earthquake: @ Small yellow man warrior good thank Bo Master answer ~ ~ There are also wooden amazeui+react+webpack project, hope to be like Bo Master study


Reply2016.04.19 11:13


_yuchen: Can you tell me how to write the modal frame of the next amuireact?
>var modal = <amuireact.modal title= "Amaze UI modal" > This one modal window. </AMUIReact.Modal>;

Module.exports =react.createclass ({
Render:function () {
Return (
<amuireact.modaltrigger modal={modal}>
<amuireact.button amstyle= ' primary ' > Open Modal window </AMUIReact.Button>
</AMUIReact.ModalTrigger>
);
}

});

This can only show buttons, no modal frame 0 0


Reply2016.04.20 13:20Add a new reply


Hstonel


3/F ·2016.05.17 17:58


Learn, thank you


enjoyed (0)Reply


Neon Court


4/F ·2016.05.21 20:57


@ Small yellow people heroes bo, I am back-end to the front end, I used to be IntelliJ idea, and then I saw some of the front-end tutorials, a lot of automation things yeomen,grunt, but also webpack and so on, but I think IntelliJ Does idea or webstorm seem to replace them? I have been very puzzled, please bo master to help answer.
Also want Bo Master writes the article I very much like, convenient leave a contact way QQ or mailbox?


enjoyed (0)Reply


Small yellow man: @ Neon Pavilion can not be said to replace, just like Nodejs can not be expected to completely replace Java, PHP, we must know that these technologies are good at the field, only need to take its director. And I think you might be talking about these development tools instead of the development tools you used? Development tools are always towards more intelligent, powerful and convenient development, learning more new things will continue to improve their development efficiency ~


Reply2016.05.22 21:19Add a new reply


708d0f26b105


5/F ·2016.05.24 10:28


Webstorm is automatically saved but auto-save that react auto-refresh can't be used


enjoyed (0)Reply


Small Huang Man: @708d0f26b105 You can turn off the auto-save function, set the Ctrl+s shortcut to save manually.


Reply2016.05.24 15:07


Small yellow man: @708d0f26b105 I this seems to have been wrong, react automatic refresh is the use of webpack-dev-server to test ~


Reply2016.06.17 22:00


AAFFF584FEC2: Set to Manual save, then the command line input NPM Run watch can only be detected once, the interface will not work after the switch


Reply2016.07.19 10:09And also3 Reviews, expand View Add a new reply


Jyoung


6/F ·2016.07.18


Specifically registered an account to thank for a while, the morning did not understand the configuration, fortunately the group of people recommend to check this article ~ now can be happy with the code ~


enjoyed (0)Reply


Small yellow man: @JYoung hehe, do not hesitate, your sincerity has been received, enjoy webpack and react to bring you the fun ~


Reply2016.07.20 14:20Add a new reply


Albertbreeze


7/F ·2016.07.27 11:11


The generated bundle.js file has more than 700 KB size, is there a way to compress it?


enjoyed (0)Reply


Small Huang Man: @AlbertBreeze about compression please go to Webpack official website to see the Code spliting section.


Reply2016.07.27 11:18Add a new reply


Vargent


8/F ·2016.08.22 09:59


According to your this configuration, or not configured successfully, can not find webpack himself novice


enjoyed (0)Reply


The small Huang Man warrior: @vargent Webpack did not install the success


Reply2016.08.24 14:39


Small yellow Warrior: @vargent Use global installation NPM install-g Webpack


Reply2016.08.24 14:40Add a new reply


Chozo


9/F ·2016.09.11 23:42


Error:cannot Find module ' webpack '
What's the situation?
Failed at the [email protected] Build script ' Webpack--progress--profile--colors '. and this.
I'm a Mac, too.


enjoyed (0)Reply


Little Yellow Warrior: @Chozo You do not have Webpack installed, use global command: NPM install webpack webpack-dev-server-g--save-dev installation Once


Reply2016.09.12 11:22 Add a new reply


Autumn Night is cold


10/F ·2016.09.12 13:43


This is used to sucks, happy Singles Day!! Visible the author is a great God, write very good!??


enjoyed (0)Reply


The Little Yellow Man Warrior: @ Autumn Night has been cold??


Reply2016.09.12 19:30Add a new reply


Post a comment after logging in



Environment for building react projects


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.