因爲6.3.0版本以後會自帶npm的包管理因此不須要單獨的安裝npmjavascript
nodejs(包含npm)安裝在默認路徑C:\Program Files\nodejs比較好,也不是很大php
Webstrom安裝完成後,在打開的 License Activation 窗口中選擇 License server。css
在輸入框輸入網址:http://idea.iteblog.com/key.phphtml
最後點擊 Activatejava
npm init
命令來初始化一個package.json文件,這個文件是一個相似於pom.xml的文件,用來描述須要的依賴庫。npm install webpack webpack-dev-server babel --save-dev
npm install react react-dom babel-loader less-loader css-loader style-loader url-loader file-loader babel-preset-es2015 babel-preset-react react-hot-loader jquery eslint eslint-plugin-react --save-dev
--save-dev 會把下載包的相關信息寫到package.json的devDependencies裏面方便之後發佈,其餘人使用的時候只須要npm install
就能夠把相關的依賴下載到當前的項目裏面。node
在package.json裏面包的版本以前的^
表示能夠安裝相似2.x.x版本的組件可是不能安裝3.x.x版本的軟件
在package.json裏面包的版本以前的~
表示能夠安裝2.1.x的軟件不能安裝2.3.x的軟件react
/** * Created by Diablo on 17/11/11. */ 'use strict'; var webpack = require('webpack'); var commonsPlugin = new webpack.optimize.CommonsChunkPlugin('common'); var path = require('path'); module.exports = { devtool: 'eval', entry: [ 'webpack-dev-server/client?http://localhost:3000', 'webpack/hot/only-dev-server', './src/js/entry.js' ], output: { path: path.join(__dirname, 'dist'), filename: '[name].js', publicPath: '/static/' }, plugins: [ new webpack.HotModuleReplacementPlugin(), commonsPlugin ], module: { loaders: [ { test: /\.css$/, loader: 'style-loader!css-loader' }, { test: /\.jsx?$/, loaders: ['babel-loader'], exclude: /node_modules/, }, { 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" } ] }, resolve: { extensions: ['.js', '.json', '.less'] } };
這裏我取消了loders裏面的react-hot-loader包的加載,這個就是在修改代碼的時候會網站內容會自動修改,沒什麼做用,每次修改完後手動刷新網站就行了,由於npm run start一直在這裏報錯致使編譯失敗,就把這個包的加載取消了jquery
原來是這樣的loaders: ['react-hot-loader', 'babel-loader'],webpack
.babelrc
的文件裏面的內容以下:{ "presets": ["es2015", "react"] }
上面的是babel插件的相關信息web
.eslintrc
的文件裏面的內容以下:{ "ecmaFeatures": { "jsx": true, "modules": true }, "env": { "browser": true, "node": true }, "parser": "babel-eslint", "rules": { "quotes": [2, "single"], "strict": [2, "never"], "react/jsx-uses-react": 2, "react/jsx-uses-vars": 2, "react/react-in-jsx-scope": 2 }, "plugins": [ "react" ] }
這個配置是設置js的代碼相關規範的
/** * Created by Diablo on 17/11/11. */ var webpack = require('webpack'); var WebpackDevServer = require('webpack-dev-server'); var config = require('./webpack.config'); new WebpackDevServer(webpack(config), { publicPath: config.output.publicPath, hot: true, inline:true, historyApiFallback: true }).listen(3000, 'localhost', function (err, result) { if (err) { return console.log(err); } console.log('Listening at http://localhost:3000/'); });
"scripts": { "test": "echo \"Error: no test specified\" && exit 1", "start": "node server.js", "lint": "eslint src" },
src/js
文件夾
src/js/helloworld.js
import React, { Component } from 'react'; export default class Hello extends Component { render() { return ( <div> <h1>Hello, world.</h1> <p>this is a simple section</p> <p>搭建</p> </div> ); } }
src/js/entry.js
import React from 'react'; import ReactDOM from 'react-dom'; import Hello from './helloworld'; ReactDOM.render(<Hello />, document.getElementById('root'));
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>test</title> </head> <body> <div id="root"></div> <script type="text/javascript" src="/static/common.js" charset="utf-8"></script> <script type="text/javascript" src="/static/main.js" charset="utf-8"></script> </body> </html>
其實main.js沒有東西要顯示
npm run start
,在瀏覽器中輸入http://localhost:3000/
查看到以下內容: