npm搭建React項目

轉自:http://blog.csdn.net/u012859720/article/details/70597119javascript

要想使用npm,首先安裝Node.jshtml

一.安裝全局包

$ npm install babel -g
$ npm install webpack -g
$ npm install webpack-dev-server -g

二.建立根目錄

建立一個根目錄,目錄名爲:myApp,再使用npm init初始化,生成package.json文件:java

$ mkdir myApp
$ cd myApp/
$ npm init
name: (myApp) 
version: (1.0.0)
description: 
entry point: (index.js)
test command:
git repository:
keywords:
author:
license: (ISC)
About to write to /Users/tianqixin/www/reactApp/package.json:{
    "name": "react-runoob",
    "version": "1.0.0",
    "description": "cllgeek test",
    "main": "index.js",
    "scripts": {
    "start": "webpack-dev-server --hot"
    },
    "author": "",
    "license": "ISC",
    "dependencies": {
        "react": "^0.14.8",
        "react-dom": "^0.14.8"
    }
}
Is this ok? (yes)

三.添加包及插件

由於咱們須要使用React,因此咱們須要先安裝它,–save命令用於將包添加至package.json文件中node

$ npm install react --save
$ npm install react-dom --save

同時咱們須要安裝一些babel插件react

$ npm install babel-core
$ npm install babel-loader
$ npm install babel-preset-react
$ npm install babel-preset-es2015

四.建立文件

接下來咱們建立一些必要文件:webpack

$ touch index.html
$ touch App.jsx
$ touch main.js
$ touch webpack.config.js

五.設置編譯器,服務器,載入器

打開webpack.config.js文件添加如下代碼:git

var config = {
    entry:'./main.js',
    output:{
        path:'./build',
        filename:'index.js',
    },
    devServer:{
        inline:true,
        port:7777
    },
    module:{
        loaders:[{
            test:/.jsx?$/,
            exclude:/node_modules/,
            loader:'babel',
            query:{
                presets:['es2015','react']
            }
        }]
    }
}

module.exports = config;

注:該配置適用於webpack1,使用webpack2如此配置會報錯,默認安裝高版本的插件,咱們須要在前面安裝webpack的時候指定版本號 
安裝方式以下:web

npm install webpack@1.* -g

@1.*會安裝1.X的最高版本npm

entry: 指定打包的入口文件 main.js。 
output:配置打包結果,path定義了輸出的文件夾,filename則定義了打包結果文件的名稱。 
devServer:設置服務器端口號爲 7777,端口後你能夠本身設定 。 
module:定義了對模塊的處理邏輯,這裏能夠用loaders定義了一系列的加載器,以及一些正則。當須要加載的文件匹配test的正則時,就會調用後面的loader對文件進行處理,這正是webpack強大的緣由。 
如今打開 package.json 文件,找到 「scripts」 中的 「test」 「echo \」Error: no test specified\」 && exit 1″ 使用如下代碼替換: 
「start」: 「webpack-dev-server –hot」 
替換後的 package.json 文件 內容以下:json

$ cat package.json
{
  "name": "myApp",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "webpack-dev-server --hot"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "react": "^15.5.4",
    "react-dom": "^15.5.4",
    "webpack": "^1.15.0",
    "webpack-dev-server": "^1.16.4"
  }
}

如今便可使用npm start命令來啓動服務了,–hot命令會在文件變化後從新載入,這樣就不須要修改代碼後從新刷新瀏覽器進行更新操做

六.編寫index.html

設置div id=」app」 爲咱們應用的根元素,並引入上面ouput指定的文件index.js腳本文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <div id="app"></div>
    <script src="index.js"></script>
</body>
</html>

七.編寫App.jsx 和main.js文件

這是第一個react組件,這個組件將輸出Hello World!。 
App.jsx文件代碼:

import React from 'react';

class App extends React.Component {
    render(){
        return (
            <div>
                Hello World!
            </div>
        )
    }
}

export default App;

咱們須要引入組件並將其渲染到根元素App上,這樣咱們才能夠在瀏覽器上看到它。 
main.js文件代碼:

import React from 'react';
import ReactDOM from 'react-dom'

import App from './App.jsx'

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

注意: 
若是想要組件能夠在任何的應用中使用,須要在建立後使用export將其導出,在使用組件的文件使用 import將其導入。

文成上述配置,便可運行該服務

npm start

經過瀏覽器訪問:http://loaclhost:7777/

相關文章
相關標籤/搜索