gulp + gulp-better-rollup + rollup 构建 ES6 开发环境
gulp + gulp-better-rollup + rollup 构建 ES6 开发环境
关于 Gulp 就不过多啰嗦了。常用的 js 模块打包工具主要有 webpack、rollup 和 browserify 三个,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 开发环境的更多相关文章
- Gulp安装及配合组件构建前端开发一体化(转)
Gulp安装及配合组件构建前端开发一体化 所有功能前提需要安装nodejs(本人安装版本v0.10.26)和ruby(本人安装版本1.9.3p484). Gulp 是一款基于任务的设计模式的自动化工具 ...
- 使用create-react-app 快速构建 React 开发环境以及react-router 4.x路由配置
create-react-app 是来自于 Facebook,通过该命令我们无需配置就能快速构建 React 开发环境. create-react-app 自动创建的项目是基于 Webpack + E ...
- react学习笔记(一)用create-react-app构建 React 开发环境
React 可以高效.灵活的用来构建用户界面框架,react利用高效的算法最小化重绘DOM. create-react-app 是来自于 Facebook,通过该命令不需配置就能快速构建 React ...
- 【React】使用 create-react-app 快速构建 React 开发环境
create-react-app 是来自于 Facebook,通过该命令我们无需配置就能快速构建 React 开发环境. create-react-app 自动创建的项目是基于 Webpack + E ...
- 快速构建 React 开发环境
使用 create-react-app 快速构建 React 开发环境 create-react-app 是来自于 Facebook,通过该命令我们无需配置就能快速构建 React 开发环境. cre ...
- [webpack] 配置react+es6开发环境
写在前面 每次开新项目都要重新安装需要的包,简单记录一下. 以下仅包含最简单的功能: 编译react 编译es6 打包src中入口文件index.js至dist webpack配置react+es6开 ...
- 从源代码构建 Go 开发环境
从源代码构建 Go 开发环境 Go 1.5 之前的版本 安装C 语言开发环境 在Go 1.5 之前的版本(比如 1.3.1.4),都会部分的依赖 C 语言的工具链,所以如果你有C 语言的开发环境,就可 ...
- Python黑帽编程1.2 基于VS Code构建Python开发环境
Python黑帽编程1.2 基于VS Code构建Python开发环境 0.1 本系列教程说明 本系列教程,采用的大纲母本为<Understanding Network Hacks Atta ...
- 使用Intellij IDEA构建spark开发环境
近期开始研究学习spark,开发环境有多种,由于习惯使用STS的maven项目,但是按照许多资料的方法尝试以后并没有成功,也可能是我环境问题:也可以是用scala中自带的eclipse,但是不太习惯, ...
随机推荐
- 第二篇 Html(13章节)-a标签,img标签,列表,表格
1. a标签 - 超链接,可以跳转 - 锚 href='#某个标签的ID' 标签的ID不允许重复 <!DOCTYPE html> <html lang="en&qu ...
- 网站流量统计PV&UV
统计网站pv和uv PV是网站分析的一个术语,用以衡量网站用户访问的网页的数量. 对于广告主,PV值可预期它可以带来多少广告收入.一般来说,PV与来访者的数量成正比,但是PV并不直接决定页面的真实来访 ...
- redis搭建主从(1主2从)
一,先附上配置文件 1,master(6379.conf)上面的配置文件 daemonize yes pidfile /usr/local/redis/logs/redis_6379.pid port ...
- linux hadoop2.x快速安装
........ http://blog.csdn.net/se7en_q/article/details/47258007
- fedora 29 桌面版 设置 cockpit 自动开机启动
systemctl enable cockpit 时,会出现如下错误: The unit files have no installation config (WantedBy, RequiredBy ...
- jQuery中 对标签元素操作(1)
一:创建元素节点(添加) 创建元素节点并且把节点作为元素的子节点添加到DOM树上 append(): 在元素下添加元素 用法:$("id").append(" ...
- Markdown语法大全
目录 前言: 1.Markdown基础用法 1.1 目录 1.2 标题 1.3 字体样式 1.4 引用 1.5 图片 1.6 超链接 1.7 列表 1.8 表格 1.9 代码 1.10 流程图 1.1 ...
- Java多线程(一) 什么是线程
声明:本系列大多是翻译自https://www.javatpoint.com,加上自己的增删改,尽力写的系统而通俗易懂,后文不再重复声明. 点我跳过黑哥的卑鄙广告行为,进入正文. Java多线程系列更 ...
- Announcing the Updated NGINX and NGINX Plus Plug‑In for New Relic (Version 2)
In March, 2013 we released the first version of the “nginx web server” plug‑in for New Relic monitor ...
- input reset 重置时间
经验规律,301毫秒. function autoFormatMoney() { if (!this.value.length) {return} var num = parseFloat(this. ...