gulp + gulp-better-rollup + rollup 构建 ES6 开发环境

关于 Gulp 就不过多啰嗦了。常用的 js 模块打包工具主要有 webpackrollupbrowserify 三个,Gulp 构建 ES6 开发环境通常需要借助这三者之一来合并打包 ES6 模块代码。因此,Gulp 构建 ES6 开发环境的方案有很多,例如:webpack-stream、rollup-stream 、browserify等,本文讲述使用 gulp-better-rollup 的构建过程。gulp-better-rollup 可以将 rollup 更深入地集成到Gulps管道链中。

GitHub地址:https://github.com/JofunLiang/gulp-translation-es6-demo

构建基础的 ES6 语法转译环境

首先,安装 gulp 工具,命令如下:

$ npm install --save-dev gulp

安装 gulp-better-rollup 插件,由于 gulp-better-rollup 需要 rollup 作为依赖,因此,还要安装 rollup 模块和 rollup-plugin-babel(rollup 和 babel 之间的无缝集成插件):

$ npm install --save-dev gulp-better-rollup rollup rollup-plugin-babel

安装 babel 核心插件:

$ npm install --save-dev @babel/core @babel/preset-env

安装完成后,配置 .babelrc 文件和 gulpfile.js文件,将这两个文件放在项目根目录下。

新建 .babelrc 配置文件如下:

{
"presets": [
[
"@babel/env",
{
"targets":{
"browsers": "last 2 versions, > 1%, ie >= 9"
},
"modules": false
}
]
]
}

新建 gulpfile.js 文件如下:

const gulp = require("gulp");
const rollup = require("gulp-better-rollup");
const babel = require("rollup-plugin-babel"); gulp.task("babel", () => {
return gulp.src("src/**/*.js")
.pipe(rollup({
plugins: [babel()]
},{
format: "iife"
}))
.pipe(gulp.dest("dist"))
}) gulp.task("watch", () => {
gulp.watch("src/**/*.js", gulp.series("babel"))
}) gulp.task("default", gulp.series(["babel", "watch"]))

在 src 目录下使用 ES6 语法新建 js 文件,然后运行 gulp 默认任务,检查 dist 下的文件是否编译成功。

使用 ployfill 兼容

经过上面的构建过程,成功将 ES6 语法转译为 ES5 语法,但也仅仅是转换的语法,新的 api(如:Set、Map、Promise等) 并没有被转译。关于 ployfill 兼容可以直接在页面中引入 ployfill.js 或 ployfill.min.js 文件实现,这种方式比较简单,本文不再赘述,下面讲下在构建中的实现方式。

安装 @babel/plugin-transform-runtime 、@babel/runtime-corejs2 和 core-js@2(注意:core-js的版本要和@babel/runtime的版本对应,如:@babel/runtime-corejs2对应core-js@2)。@babel/plugin-transform-runtime 的作用主要是避免污染全局变量和编译输出中的重复。@babel/runtime(此处指@babel/runtime-corejs2)实现运行时编译到您的构建中。

$ npm install --save-dev @babel/plugin-transform-runtime @babel/runtime-corejs2 core-js@2

修改 .babelrc 文件:

{
"presets": [
[
"@babel/env",
{
"targets":{
"browsers": "last 2 versions, > 1%, ie >= 9"
},
"modules": false
}
]
],
"plugins": [
[
"@babel/plugin-transform-runtime", {
"corejs": 2
}
]
]
}

同时修改 gulpfile.js 文件,给 rollup-plugin-babel 配置 runtimeHelpers 属性如下:

const gulp = require("gulp");
const rollup = require("gulp-better-rollup");
const babel = require("rollup-plugin-babel"); gulp.task("babel", () => {
return gulp.src("src/**/*.js")
.pipe(rollup({
plugins: [
babel({
runtimeHelpers: true
})
]
},{
format: "iife"
}))
.pipe(gulp.dest("dist"))
}) gulp.task("watch", () => {
gulp.watch("src/**/*.js", gulp.series("babel"))
}) gulp.task("default", gulp.series(["babel", "watch"]))

再安装 rollup-plugin-node-resolve 和 rollup-plugin-commonjs,这两个插件主要作用是注入 node_modules 下的基于 commonjs 模块标准的模块代码。在这里的作用主要是加载 ployfill 模块。

$ npm install --save-dev rollup-plugin-node-resolve rollup-plugin-commonjs

在修改 gulpfile.js 文件如下:

const gulp = require("gulp");
const rollup = require("gulp-better-rollup");
const babel = require("rollup-plugin-babel");
const resolve = require("rollup-plugin-node-resolve");
const commonjs = require("rollup-plugin-commonjs"); gulp.task("babel", () => {
return gulp.src("src/**/*.js")
.pipe(rollup({
plugins: [
commonjs(),
resolve(),
babel({
runtimeHelpers: true
})
]
},{
format: "iife"
}))
.pipe(gulp.dest("dist"))
}) gulp.task("watch", () => {
gulp.watch("src/**/*.js", gulp.series("babel"))
}) gulp.task("default", gulp.series(["babel", "watch"]))

使用 sourcemaps 和压缩

注意压缩使用 rollup-plugin-uglify 插件,为了提升打包速度,我们把模块文件放到 src/js/modules 文件夹下,将 gulp.src("src/js/.js") 改为 gulp.src("src/js/.js") 只打包主文件不打包依赖模块。

安装 gulp-sourcemaps 和 rollup-plugin-uglify 插件:

npm install --save-dev gulp-sourcemaps rollup-plugin-uglify 

修改 gulpfile.js 文件如下:

const gulp = require("gulp");
const rollup = require("gulp-better-rollup");
const babel = require("rollup-plugin-babel");
const resolve = require("rollup-plugin-node-resolve");
const commonjs = require("rollup-plugin-commonjs");
const uglify = require("rollup-plugin-uglify");
const sourcemaps = require("gulp-sourcemaps"); gulp.task("babel", () => {
return gulp.src("src/js/*.js")
.pipe(sourcemaps.init())
.pipe(rollup({
plugins: [
commonjs(),
resolve(),
babel({
runtimeHelpers: true
}),
uglify.uglify()
]
},{
format: "iife"
}))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest("dist/js"))
}) gulp.task("watch", () => {
gulp.watch("src/**/*.js", gulp.series("babel"))
}) gulp.task("default", gulp.series(["babel", "watch"]))

gulp + gulp-better-rollup + rollup 构建 ES6 开发环境的更多相关文章

  1. Gulp安装及配合组件构建前端开发一体化(转)

    Gulp安装及配合组件构建前端开发一体化 所有功能前提需要安装nodejs(本人安装版本v0.10.26)和ruby(本人安装版本1.9.3p484). Gulp 是一款基于任务的设计模式的自动化工具 ...

  2. 使用create-react-app 快速构建 React 开发环境以及react-router 4.x路由配置

    create-react-app 是来自于 Facebook,通过该命令我们无需配置就能快速构建 React 开发环境. create-react-app 自动创建的项目是基于 Webpack + E ...

  3. react学习笔记(一)用create-react-app构建 React 开发环境

    React 可以高效.灵活的用来构建用户界面框架,react利用高效的算法最小化重绘DOM. create-react-app 是来自于 Facebook,通过该命令不需配置就能快速构建 React ...

  4. 【React】使用 create-react-app 快速构建 React 开发环境

    create-react-app 是来自于 Facebook,通过该命令我们无需配置就能快速构建 React 开发环境. create-react-app 自动创建的项目是基于 Webpack + E ...

  5. 快速构建 React 开发环境

    使用 create-react-app 快速构建 React 开发环境 create-react-app 是来自于 Facebook,通过该命令我们无需配置就能快速构建 React 开发环境. cre ...

  6. [webpack] 配置react+es6开发环境

    写在前面 每次开新项目都要重新安装需要的包,简单记录一下. 以下仅包含最简单的功能: 编译react 编译es6 打包src中入口文件index.js至dist webpack配置react+es6开 ...

  7. 从源代码构建 Go 开发环境

    从源代码构建 Go 开发环境 Go 1.5 之前的版本 安装C 语言开发环境 在Go 1.5 之前的版本(比如 1.3.1.4),都会部分的依赖 C 语言的工具链,所以如果你有C 语言的开发环境,就可 ...

  8. Python黑帽编程1.2 基于VS Code构建Python开发环境

    Python黑帽编程1.2  基于VS Code构建Python开发环境 0.1  本系列教程说明 本系列教程,采用的大纲母本为<Understanding Network Hacks Atta ...

  9. 使用Intellij IDEA构建spark开发环境

    近期开始研究学习spark,开发环境有多种,由于习惯使用STS的maven项目,但是按照许多资料的方法尝试以后并没有成功,也可能是我环境问题:也可以是用scala中自带的eclipse,但是不太习惯, ...

随机推荐

  1. (后端)Spring Boot自定义错误页面,Whitelabel Error Page处理方式(转)

    我已经是Spring Framework框架的忠实粉丝.对于企业软件开发者来说它提供了对常见问题的通用解决方案,包括那些你在未来开发中没有意识到的问题.但是,它构建的J2EE项目变得比较臃肿,需要被一 ...

  2. ERP口碑后付关于如何设置后厨小票打印时间的问题解决方法

    1. 2.

  3. Spark数据倾斜及解决方案

    一.场景 1.绝大多数task执行得都非常快,但个别task执行极慢.比如,总共有100个task,97个task都在1s之内执行完了,但是剩余的task却要一两分钟.这种情况很常见. 2.原本能够正 ...

  4. Solr5.5高级应用(基于tomcat9)

    一.配置solr 1.配置 注意:要是想放到其它路径下,可以修改此路径下的web.xml配置文件 修改内容如下: <!-- 将solrhome的绝对路径写入env-entry-value --& ...

  5. 两层c:forEach循环嵌套

    jsp中两级菜单如何用c:forEach输出 items 要被循环的信息 否 无 begin 开始的元素(0=第一个元素,1=第二个元素) 否 0 end 最后一个元素(0=第一个元素,1=第二个元素 ...

  6. 基于PHP的颜色生成器

    <?php  function randomColor()  {      $str = '#';      for($i = 0 ; $i < 6 ; $i++)     {      ...

  7. mysql 中的内置函数

    一.字符串函数 select concat(name,"age is",age) from users;  insert(str,x,y,insert)//将字符串x位置开始y个位 ...

  8. Python虚拟环境笔记

    虚拟环境 为什么需要虚拟环境: 到目前位置,我们所有的第三方包安装都是直接通过pip install xx的方式进行安装的,这样安装会将那个包安装到你的系统级的Python环境中.但是这样有一个问题, ...

  9. B. Yet Another Array Partitioning Task ——cf

    B. Yet Another Array Partitioning Task time limit per test 2 seconds memory limit per test 256 megab ...

  10. C#中Request.ServerVariables详细说明及代理

    Request.ServerVariables("Url") 返回服务器地址 Request.ServerVariables("Path_Info") 客户端提 ...