见知乎文章
写在前面:使用的工具是iTerm命令行工具和Atom代码编辑器。
TBD
$ mkdir webpack-react-demo
$ cd webpack-react-demo
$ npm init
$ touch .gitignore
$ touch webpack.config.js
其中.gitignore用于说明代码提交时忽略的文件,可参考下面的常用配置
logs *.log .grunt node_modules bower_components build
$ npm install webpack webpack-dev-server html-webpack-plugin --save-dev
$ npm install css-loader style-loader sass-loader node-sass --save-dev
$ npm install file-loader url-loader --save-dev
$ npm install babel-core babel-loader babel-preset-es2015 babel-preset-react --save-dev
$ npm install jshint jshint-loader --save-dev
$ npm install react react-dom --save
$ npm install bootstrap@4.0.0-alpha.2 --save-dev
$ npm install bootstrap@4.0.0-alpha.2 --save-dev
$ npm install jquery moment --save-dev
向webpack.config.js文件写入一下内容:
var path = require('path');
var webpack = require('webpack');
var HtmlwebpackPlugin = require('html-webpack-plugin');
var ROOT_PATH = path.resolve(__dirname);
var APP_PATH = path.resolve(ROOT_PATH, 'app');
var BUILD_PATH = path.resolve(ROOT_PATH, 'build');
var TEM_PATH = path.resolve(ROOT_PATH, 'templates');
module.exports= {
entry: {
app: path.resolve(APP_PATH, 'index.jsx'),
mobile: path.resolve(APP_PATH, 'mobile.jsx'),
vendors: ['jquery', 'moment']
},
output: {
path: BUILD_PATH,
filename: '[name].js'
},
resolve: {
extensions: ['', '.js', '.jsx']
},
//启动dev source map,出错以后就会采用source-map的形式直接显示你出错代码的位置。
devtool: 'eval-source-map',
//enable dev server
devServer: {
historyApiFallback: true,
hot: true,
inline: true,
progress: true,
//只要配置dev server map这个参数就可以了
proxy:{
'/api/*':{
target: 'http://localhost:8080',
secure: false
}
}
},
//babel重要的loader在这里
module: {
//loader前执行处理,这样每次npm run start的时候就可以看到jshint的提示信息
preLoaders: [
{
test: /\.jsx?$/,
include: APP_PATH,
loader: "jshint-loader"
}
],
loaders: [
{
test: /\.jsx?$/,
loader: 'babel',
include: APP_PATH,
query: {
//添加两个presents 使用这两种presets处理js或者jsx文件
presets: ['es2015', 'react']
}
},
{
test: /\.scss$/,
loaders: ['style', 'css', 'sass']
}
]
},
//配置jshint的选项,支持es6的校验
// any jshint option http://www.jshint.com/docs/options/
jshint: {
"esnext": true
},
plugins: [
//这个使用uglifyJs压缩你的js代码
new webpack.optimize.UglifyJsPlugin({minimize: true}),
new HtmlwebpackPlugin({
title: 'My first react app',
template: path.resolve(TEM_PATH, 'index.html'),
filename: 'index.html',
chunks: ['app', 'vendors'],
inject: 'body'
}),
new HtmlwebpackPlugin({
title: 'Hello Mobile app',
template: path.resolve(TEM_PATH, 'mobile.html'),
filename: 'mobile.html'
}),
//把入口文件里面的数组打包成verdors.js
new webpack.optimize.CommonsChunkPlugin('vendors', 'vendors.js')
]
}
在package.json文件中添加:
...
"scripts": {
"dev": "webpack-dev-server --progress --profile --colors --hot --inline",
"build": "webpack --progress --profile --colors",
"test": "test",
},
...
分享下我通过npm安装的包版本信息
...
"devDependencies": {
"babel-core": "^6.4.5",
"babel-loader": "^6.2.1",
"babel-preset-es2015": "^6.3.13",
"babel-preset-react": "^6.3.13",
"bootstrap": "^4.0.0-alpha.2",
"css-loader": "^0.23.1",
"file-loader": "^0.8.5",
"html-webpack-plugin": "^2.7.2",
"jquery": "^2.2.0",
"jshint": "^2.9.1",
"jshint-loader": "^0.8.3",
"moment": "^2.11.1",
"node-sass": "^3.4.2",
"sass-loader": "^3.1.2",
"style-loader": "^0.13.0",
"url-loader": "^0.5.7",
"webpack": "^1.12.12",
"webpack-dev-server": "^1.14.1"
},
"dependencies": {
"react": "^0.14.6",
"react-dom": "^0.14.6"
}
...
import '../node_modules/bootstrap/scss/bootstrap.scss';
import React from 'react';
import ReactDOM from 'react-dom';
import $ from 'jquery';
class App extends React.Component{
constructor() {
super();
}
render() {
//JSX here!
return (
"container">
"jumbotron">
"jumbotron-heading">Search Github Users
)
}
};
const app = document.createElement('div');
$('body').append(app);
ReactDOM.render(<App />, app);
npm run dev
如下所示说明Babel和Bootstrap正确使用了。
隆重推出Babel-plugin-react-transform 名字挺长, 看起来挺吓人,其实你就可以想象用这个东西可以实时的对你的React Component做各种处理,它是基于Babel Plugin。
这是个基本的架子,可以通过它完成各种transform,如果想实现Hot Module Replacement (说白了就是页面不刷新,直接替换修改的Component),再安装react-transform-hmr。再来一个在页面上直接显示catch到的错误的transform。要让新建的两个transform生效,只需再安装一个present。
npm install babel-plugin-react-transform --save-dev`
npm install react-transform-hmr --save-dev
npm install --save-dev react-transform-catch-errors redbox-react
npm install babel-preset-react-hmre --save-dev
{
//添加两个presents 使用这两种presets处理js或者jsx文件
presets: ['es2015', 'react'],
//在开发的时候才启用HMR和Catch Error
"env": {
"development": {
"presets": ["react-hmre"]
}
}
}
npm run dev
,修改h3的颜色,可以看到效果如下