簡介:html
webpack:是近期最火的一款模塊加載器兼打包工具,它能把各類資源,例如JS(含JSX)、coffee、樣式(含less/sass)、圖片等都做爲模塊來使用和處理。前端
react:如今最熱門的前端框架,毫無疑問是 React 。node
es2015:ECMAScript 6。react
以上是她們的基本定義,想一想能將她們結合在一塊兒開發咱們的項目是多麼的激動人心啊。固然在正式開發以前咱們也有不少準備工做須要作好,這也多是對新手最棘手的問題。下面咱們就直接進入正題吧。webpack
環境搭建:nginx
首選咱們需下載安裝Node.js,下載地址:https://nodejs.org/en/ (注意:爲了能支持es2015請下載4.0以上版本)git
安裝了node以後咱們還須要npm也就是包管理器,固然新的node已經集成了npm的。因此此步驟能夠跳過了es6
有了npm後,win+r而後輸入cmd打開,而後輸入命令:github
npm install webpack -g
執行命令後個人webpack就全局安裝好了,這裏提醒一下,在項目文件中咱們還須要將webpack寫入package.json(局部安裝)web
項目建立:
在D盤建立咱們的項目文件「reactPro」,目錄結構以下:
在cmd中定位項目地址,執行命令:
cd D:\reactPro
D:
定位完成後,咱們利用npm生成package.json文件,執行命令:
npm init
上面命令執行完成以後咱們就生成了一個node項目,接下來就能夠安裝咱們的node模塊了
首選安裝webpack,執行命令:
npm install webpack --save-dev
而後安裝webpack須要的加載器,執行命令:
npm install babel-loader babel-preset-es2015 babel-preset-react --save-dev
npm install react react-dom --save-dev
文件配置&運行:
var path = require('path'); var webpack = require('webpack'); module.exports = { entry: ['webpack/hot/dev-server', path.resolve(__dirname, './app/main.js')], output: { path: path.resolve(__dirname, './build'), filename: 'bundle.js' }, module: { loaders: [ { test: /\.js?$/, exclude: /(node_modules|bower_components)/, loader: 'babel', query: { presets: ['es2015', 'react'] } } ] }, plugins: [ new webpack.HotModuleReplacementPlugin() ] };
npm install webpack-dev-server --save-dev
"scripts": { "build": "webpack", "dev": "webpack-dev-server --devtool eval --progress --colors --content-base build" }
{ "name": "reactpro1", "version": "1.0.0", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "bundle": "babel-node tools/run bundle", "build": "webpack", "dev": "webpack-dev-server --devtool eval --progress --colors --content-base build" }, "author": "jx", "license": "ISC", "devDependencies": { "babel-core": "^6.8.0", "babel-loader": "^6.2.4", "babel-preset-es2015": "^6.6.0", "babel-preset-react": "^6.5.0", "react": "^15.0.2", "react-dom": "^15.1.0", "webpack": "^1.13.0", "webpack-dev-server": "^1.14.1" }, "dependencies": { "webpack": "^1.13.0", "webpack-dev-server": "^1.14.1" }, "description": "" }
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>React Project</title> </head> <body> <script src="http://localhost:8080/webpack-dev-server.js"></script> <div id="content"></div> <script src="./bundle.js"></script> </body> </html>
npm run dev
react&es2015代碼編寫:
let React = require('react'); let ReactDOM = require('react-dom'); let AppComponent = require('./components/productBox.js'); ReactDOM.render(<AppComponent />, document.getElementById('content'));
var React = require('react'); var ProductBox; ProductBox = React.createClass({ render: function () { return ( <div className="productBox"> hello react&es2015&webpack! </div> ); } }); module.exports = ProductBox;
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>React Project</title> </head> <body> <script src="http://localhost:8080/webpack-dev-server.js"></script> <div id="content"></div> <script src="./bundle.js"></script> </body> </html>
npm run dev
總結: