用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/

使用Webpack和Babel来搭建React应用程序的更多相关文章

  1. 使用Gulp和Browserify来搭建React应用程序

    对React有一定了解之后,我们知道,需要把JSX文件转换成JS文件,组件需要导入导出.本篇就体验使用Gulp把JSX文件转换成JS文件,使用Browserify来把组件捆绑到一个文件并理顺组件之间的 ...

  2. react+webpack+babel环境搭建

    [react+webpack+babel环境搭建] 1.react官方文档推荐使用 babel-preset-react.babel-preset-es2015 两个perset. Babel官方文档 ...

  3. react基于webpack和babel以及es6的项目搭建

    项目demo地址https://github.com/aushion/webpack_reac_config 1.打开命令提示窗口,输入 mkdir react_test cd react_test ...

  4. React+Webpack+Webstorm开发环境搭建

    需要安装的软件 node.js npm包管理 Webstorm 由于6.3.0版本之后会自带npm的包管理所以不需要单独的安装npm nodejs(包含npm)安装在默认路径C:\Program Fi ...

  5. 使用Yarn+Webpack+Babel6搭建React.js环境

    使用Yarn+Webpack+Babel6搭建React.js环境 Facebook开源的React.js已经改变了世人对前端UI的思考方式.这种基于组件方式的优势之一,就是使View更加的简单,因为 ...

  6. 使用webpack搭建react项目 webpack-react-project

    webpack-react-project 使用webpack搭建react项目 webpack搭建react项目 github源码 具体配置信息参照package.json和webpack.conf ...

  7. 使用webpack、babel、react、antdesign配置单页面应用开发环境

    这是Webpack+React系列配置过程记录的第一篇.其他内容请参考: 第一篇:使用webpack.babel.react.antdesign配置单页面应用开发环境 第二篇:使用react-rout ...

  8. 真刀实战地搭建React+Webpack+Express搭建一个简易聊天室

    一.前面bb两句 因为自惭(自残)webpack配置还不够熟悉,想折腾着做一个小实例熟悉.想着七夕快到了,做一个聊天室自己和自己聊天吧哈哈.好了,可以停止bb了,说一下干货. 二. 这个项目能学到啥? ...

  9. 手把手教你用webpack3搭建react项目(开发环境和生产环境)(一)

    开发环境和生产环境整个配置源码在github上,源码地址:github-webpack-react 如果觉得有帮助,点个Star谢谢!! (一)是开发环境,(二)是生产环境. 一.首先创建packag ...

随机推荐

  1. Android AppWidget

    AppWidget不知道大家使用这个多不多,这个在手机上也叫做挂件,挂件也就是放在桌面方便用户进行使用的,从android1.6开始挂件支持一些简单的lauout和view,到了android4.0之 ...

  2. apache.http.client.HttpClient

    前言 HTTP 协议可能是现在 Internet 上使用得最多.最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源.虽然在 JDK 的 java net包中已经提 ...

  3. 10 个强大的开源 Web 流量分析工具(转帖)

    Web 流量分析工具多不胜数,从 WebTrends 这样专业而昂贵的,到 Google Analytics 这样强大而免费的,从需要在服务器端单独部署的,到可以从前端集成的,不一而足.本文收集并介绍 ...

  4. servers中添加server时,看不到运行环境的选择。

    servers中添加server时,看不到运行环境的选择. 主要原因是tomcat目录中的配置文件格式不对.

  5. Hibernate使用count(*)取得表中记录总数

    /** * @TODO:查询某一年度的所有计划数量 */ public int findCountByYear(String currYear) { String hqlString = " ...

  6. 读取iOS通讯录

    首先导入头文件 #import <AddressBook/AddressBook.h> 获取权限 读取通讯录 - (void)loadPerson { ABAddressBookRef a ...

  7. tesseract api C++使用例子

    转自:https://code.google.com/p/tesseract-ocr/wiki/APIExample APIExample API examples Updated Aug 12, 2 ...

  8. ubuntu下phpmyadmin配置

    经常出现的问题就是明明安装了phpmyadmin但却在输入 http://localhost/phpmyadmin的时候,没有出现管理界面,反而出现没有找到的页面. 不急,我们先安装再了phpmyad ...

  9. 【kd-tree】bzoj4154 [Ipsc2015]Generating Synergy

    区间修改的kd-tree,打标记,下传. 每次询问的时候,从询问点向上找到根,然后依次下传下来,再回答询问. #include<cstdio> #include<algorithm& ...

  10. 关于eclipse在线下载安装android SDK没反应解决方法

    虽然天朝将google这个服务器给和谐掉了 虽然我们也可以选择其他各类FQ软件 虽然你可能有其他手段解决 不过我还是要提供一个参考方法: 具体操作方式: 点击桌面左下角开始菜单中的搜索,把以下路径,复 ...