使用Webpack和Babel来搭建React应用程序

用Webpack(npm install -g webpack)代码打包,Webpack大体须要知道三件事:html


1)让Webpack知道应用程序或js文件的根目录
2)让Webpack知道作何种转换
3)让Webpack知道转换后的文件保存在哪里react


具体来讲,大体要作如下几件事情:webpack


1)在项目根目录下有一个webpack.config.js文件,这个是惯例
2)确保webpack.config.js能导出一个对象git


module.exports = {};


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


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


4)告诉Webpack须要哪些转换插件npm


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

全部的转换插件放在loaders数组中。json


5)设置转换插件的细节数组


module.exports = {
    entry: ['./app/index.js'],
    module: {
        loaders: [
            {
                test: /\.coffee$/,
                include: __dirname + 'app',
                loader: "coffee-loader"
            }
        ]
    }
}
  • test:运行.coffee结尾的文件
  • include:哪些文件件
  • loader:具体的转换插件


6)告诉Webpack导出到哪里babel


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


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/

相关文章
相关标签/搜索