使用Webpack和Babel來搭建React應用程序


用Webpack(npm install -g webpack)代碼打包,Webpack大致需要知道三件事:


1)讓Webpack知道應用程序或js文件的根目錄
2)讓Webpack知道做何種轉換
3)讓Webpack知道轉換后的文件保存在哪里


具體來說,大致要做以下幾件事情:


1)在項目根目錄下有一個webpack.config.js文件,這個是慣例
2)確保webpack.config.js能導出一個對象


module.exports = {};

3)告訴Webpack入口js文件在哪里


module.exports = {
	entry: ['./app/index.js']
}

4)告訴Webpack需要哪些轉換插件


module.exports = {
	entry: ['./app/index.js'],
	module: {
		loaders: []
	}
}

所有的轉換插件放在loaders數組中。


5)設置轉換插件的細節


module.exports = {
	entry: ['./app/index.js'],
	module: {
		loaders: [
			{
				test: /\.coffee$/,
				include: __dirname + 'app',
				loader: "coffee-loader"
			}
		]
	}
}
  • test:運行.coffee結尾的文件
  • include:哪些文件件
  • loader:具體的轉換插件

6)告訴Webpack導出到哪里


module.exports = {
	entry: ['./app/index.js'],
	module: {
		loaders: [
			{
				test: /\.coffee$/,
				include: __dirname + 'app',
				loader: "coffee-loader"
			}
		]
	},
	output: {
		filename: "index_bundle.js",
		path: __dirname + '/dist'
	}
}

【文件結構】


app/
.....components/
.....containers/
.....config/
.....utils/
.....index.js
.....index.html
dist/
.....index.html
.....index_bundle.js
package.json
webpack.config.js
.gitignore


我們不禁要問,如何保證app/index.html和dist/index.html同步呢?


如果,我們每次運行webpack命令后,把app/index.html拷貝到dist/index.html中就好了。


解決這個問題的人是:html-webpack-plugin(npm install --save-dev html-webpack-plugin)。


引入html-webpack-plugin之后,webpack.config.js看起來是這樣的:

var HtmlWebpackPlugin = require('html-webpack-plugin');
var HtmlWebpackPluginConfig = new HtmlWebpackPlugin({
	template: __dirname + '/app/index.html',
	filename: 'index.html',
	inject: 'body'
});

module.exports = {
	entry: ['./app/index.js'],
	module: {
		loaders: [
			{
				test: /\.coffee$/,
				include: __dirname + 'app',
				loader: "coffee-loader"
			}
		]
	},
	output: {
		filename: "index_bundle.js",
		path: __dirname + '/dist'
	},
	plugins: [HTMLWebpackPluginConfig]
}
  • template: 表示源文件來自
  • filename:目標文件的名稱
  • inject: 'body'表示把對dist/index_bundle.js文件的引用,放到目標文件dist/index.html的body部分

【Webpack的一些指令】


webpack
webpack -w //監測文件變化
webpack -p //不僅包括轉換,還包括最小化文件

【Babel】


Babel可以用來把JSX文件轉換成js文件。與Babel相關的安裝包括:


npm install --save-dev babel-core
npm install --save-dev babel-loader
npm install --save-dev babel-preset-react
  • babel-core: 就是babel本身
  • babel-loader:用來加載
  • babel-preset-react:用來完成JSX到JS的文件轉換

在項目根文件夾下創建.babelrc文件。

{
	"presets": ["react"]
}

把babel-loader放到webpack.config.js文件的設置中去。

var HtmlWebpackPlugin = require('html-webpack-plugin');
var HtmlWebpackPluginConfig = new HtmlWebpackPlugin({
	template: __dirname + '/app/index.html',
	filename: 'index.html',
	inject: 'body'
});

module.exports = {
	entry: ['./app/index.js'],
	module: {
		loaders: [
			{
				test: /\.js$/,
				include: __dirname + 'app',
				loader: "babel-loader"
			}
		]
	},
	output: {
		filename: "index_bundle.js",
		path: __dirname + '/dist'
	},
	plugins: [HTMLWebpackPluginConfig]
}

參考:http://tylermcginnis.com/react-js-tutorial-1-5-utilizing-webpack-and-babel-to-build-a-react-js-app/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM