標籤:url mon module 技術分享 arm ima sas pac get
react的安裝依賴nodejs,nodejs安裝下載可從http://nodejs.org/en/下載
1. 環境安裝
1.1 建立lib目錄
cd E:\ReactLib
1.2 安裝全域webpack包
npm install webpack -g
1.3 建立一個package.json依賴管理檔案
npm init
1.4 安裝webpack依賴包
npm install webpack --save-dev
2. webpack 基本入門
webpack主要結構分三個:入口檔案配置(entry)、入口檔案輸出配置(output)、載入器配置(module)、其他配置(resolve)
2.1 入口檔案配置
entry: {
index : ‘./src/main.js‘
},
2.2 入口檔案輸出配置
//配置編譯成功後檔案存放的位置
output: {
path: ‘dist/js‘, //編譯後檔案所在的檔案夾
filename: ‘[name].js‘ //根據入口檔案名稱命名編譯後的檔案名稱,編譯後檔案還是叫main.js
},
2.3 載入器配置
module: {
//最關鍵的一塊配置,它告知 webpack 每一種檔案都需要使用什麼載入器來處理
loaders: [
{ test: /\.css$/, loader: ‘style-loader!css-loader‘ },
{ test: /\.js$/, loader: ‘jsx-loader?harmony‘ },
{ test: /\.scss$/, loader: ‘style!css!sass?sourceMap‘},
{ test: /\.(png|jpg)$/, loader: ‘url-loader?limit=8192‘}
]
},
2.4 其他配置
resolve: {
//自動擴充檔案尾碼名,意味著我們require模組可以省略不寫尾碼名
extensions: [‘‘, ‘.js‘, ‘.json‘, ‘.scss‘]
},
2.5 先參考例子中的目錄結構
build存放編譯後的檔案,src存放react代碼的檔案夾,components存放react組件的檔案夾,node_modules存放安裝的依賴
3. react安裝:
npm install --save-dev react react-dom
npm install --save-dev babel-core babel-loader babel-preset-es2015 babel-preset-react
npm install --save-dev style-loader css-loader sass-loader url-loader
npm install --save-dev webpack-dev-server
//自動重新整理修改後的結果,在webpack.config.js中配置webpack-dev-server,
//在這裡需要修改下entry的路徑,給它加一個webpack/hot/dev-server,後文會提到
4. 檔案內容
4.1 package.json配置
{
"name": "redux",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"react": "^15.6.1",
"react-dom": "^15.6.1"
},
"scripts": {
"build": "webpack",
"start": "webpack-dev-server --devtool eval --progress --colors --content-base build"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"css-loader": "^0.28.5",
"sass-loader": "^6.0.6",
"style-loader": "^0.18.2",
"url-loader": "^0.5.9",
"webpack": "^3.5.5",
"webpack-dev-server": "^2.7.1"
}
}
4.2 webpack.config.js檔案的配置如下:
var path = require(‘path‘);
var webpack = require(‘webpack‘);
module.exports = {
entry: [‘webpack/hot/dev-server‘, path.resolve(__dirname, ‘./src/main.js‘)],
output: {
path: path.resolve(__dirname, ‘./build‘),
filename: ‘bundle.js‘,
},
module: {
loaders: [{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: ‘babel-loader‘,
query:
{
presets: [‘react‘]
}
}]
},
plugins: [new webpack.HotModuleReplacementPlugin()]
};
4.3 main.js內容如下:
let React = require(‘react‘);
let ReactDOM = require(‘react-dom‘);
let AppComponent = require(‘./components/index.js‘);
ReactDOM.render(<AppComponent />, document.getElementById(‘content‘));
4.4 index.js內容如下:
var React = require(‘react‘);
var ProductBox = React.createClass({
render: function () {
return (
<div className="productBox">
Hello World!
</div>
);
}
});
module.exports = ProductBox;
4.5 index.html內容如下:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>React Test</title>
</head>
<body>
<div id="content"></div>
<script src="http://localhost:8080/webpack-dev-server.js"></script>
<script src="./bundle.js"></script>
</body>
</html>
5. 編譯與啟動
將上面的檔案寫好後並放到對應檔案夾內運行命令npm run build,執行npm run start運行服務,成功後瀏覽器訪問地址location:8080
註:node_modules可以從E:\ReactLib擷取,代碼工程可以隨便放入到任何目錄,react安裝依賴nodejs,需先保證nodejs安裝成功。
React&Webpack 環境安裝