关于Rollup那些事

avatar
@腾讯科技(深圳)有限公司

Rollup

下一代打包工具,这是rollup对自己的定位。如今的前端领域,构建工具并不缺少,每个前端工程师都用过或者听过webpack。可以看到的是像React、Vue等框架的构建工具使用的都是rollup。既然如此,这些框架为什么会选择rollup?它的特性是什么?面对不同场景,我们要怎么选择构建工具?本文将一一为你呈现。

Tree Shaking

tree shaking是rollup提出的,这也是rollup一个非常重要的feature,那什么是tree shaking,rollup的解释是在构建代码时,在使用ES6模块化的代码中,会对你的代码进行静态分析,只打包使用到的代码。这样的好处是减少代码的体积。

可以看到它的实现依赖于静态分析,为什么必须使用ES6 modules呢?我们来复习一下ES6 modules的几个特性:

  • import 的模块名只能是字符串常量
  • import binding 是 immutable 的,类似 const
  • 只能作为模块顶层的语句出现,不能出现在 function 里面或是 if里面等块级作用域中
  • import hoisted,不管 import的语句出现的位置在哪里,在模块初始化的时候所有的import 都必须已经导入完成。

以上特性使得ES6 Modules缺少了一定的灵活性,但使得所有的依赖都是确定的,能够对代码进行静态分析。不需要依靠运行时去确定依赖关系。 举个栗子: maths.js

// maths.js
export function square ( x ) {
	return x * x;
}
export function cube ( x ) {
	return x * x * x;
}

main.js

import { cube } from './maths.js';
console.log( cube( 5 ) ); 

执行下面的命令后

$ rollup main.js --o bundle.js --f iife

输出bundle.js

(function () {
'use strict';

// maths.js

function cube(x) {
    return x * x * x;
}

console.log(cube(5));

}());

可以看到,maths.js中square方法没有使用到,没有打包到构建结果中。在构建的时候,加了个参数f,值为iife的选项,构建的后代码的组织形式被一个立即执行函数包裹。

代码构建后输出格式

上面在构建的时候指定了参数f,值为iife的选项,输出了立即执行风格的构建代码,rollup还支持下面几种输出格式:

  • amd - AMD
  • cjs -CommonJS
  • es - ES6 modules
  • umd - UMD
  • system - SystemJS loader

在构建代码的时候,可以根据代码运行环境选择不同的输出格式,如果你的代码是运行在node中那么cjs就可以,如果你的代码运行在浏览器环境中,那么iife就很好,如果两者兼具,那么选择umd。 在webpack的编译&构建中,提到webpack构建输出的代码其实有三种。

  • 你的业务逻辑代码
  • Runtime - 代码执行的引导
  • Manifest - 模块依赖关系的记录

如果我们对main.js执行下面的命令构建后

webpack main.js dist.js

输出dist.js

/******/ (function(modules) { // webpackBootstrap
/******/ 	// The module cache
/******/ 	var installedModules = {};
/******/
/******/ 	// The require function
/******/ 	function __webpack_require__(moduleId) {
/******/
/******/ 		// Check if module is in cache
/******/ 		if(installedModules[moduleId]) {
/******/ 			return installedModules[moduleId].exports;
/******/ 		}
/******/ 		// Create a new module (and put it into the cache)
/******/ 		var module = installedModules[moduleId] = {
/******/ 			i: moduleId,
/******/ 			l: false,
/******/ 			exports: {}
/******/ 		};
/******/
/******/ 		// Execute the module function
/******/ 		modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ 		// Flag the module as loaded
/******/ 		module.l = true;
/******/
/******/ 		// Return the exports of the module
/******/ 		return module.exports;
/******/ 	}
/******/
/******/
/******/ 	// expose the modules object (__webpack_modules__)
/******/ 	__webpack_require__.m = modules;
/******/
/******/ 	// expose the module cache
/******/ 	__webpack_require__.c = installedModules;
/******/
/******/ 	// define getter function for harmony exports
/******/ 	__webpack_require__.d = function(exports, name, getter) {
/******/ 		if(!__webpack_require__.o(exports, name)) {
/******/ 			Object.defineProperty(exports, name, {
/******/ 				configurable: false,
/******/ 				enumerable: true,
/******/ 				get: getter
/******/ 			});
/******/ 		}
/******/ 	};
/******/
/******/ 	// getDefaultExport function for compatibility with non-harmony modules
/******/ 	__webpack_require__.n = function(module) {
/******/ 		var getter = module && module.__esModule ?
/******/ 			function getDefault() { return module['default']; } :
/******/ 			function getModuleExports() { return module; };
/******/ 		__webpack_require__.d(getter, 'a', getter);
/******/ 		return getter;
/******/ 	};
/******/
/******/ 	// Object.prototype.hasOwnProperty.call
/******/ 	__webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ 	// __webpack_public_path__
/******/ 	__webpack_require__.p = "";
/******/
/******/ 	// Load entry module and return exports
/******/ 	return __webpack_require__(__webpack_require__.s = 0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, __webpack_exports__, __webpack_require__) {

"use strict";
Object.defineProperty(__webpack_exports__, "__esModule", { value: true });
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__maths_js__ = __webpack_require__(1);

console.log(Object(__WEBPACK_IMPORTED_MODULE_0__maths_js__["a" /* cube */])(5));

/***/ }),
/* 1 */
/***/ (function(module, __webpack_exports__, __webpack_require__) {

"use strict";
/* unused harmony export square */
/* harmony export (immutable) */ __webpack_exports__["a"] = cube;
// maths.js
function square(x) {
    return x * x;
}
function cube(x) {
    return x * x * x;
}

/***/ })
/******/ ]);
  • 可以看到构建结果中的业务逻辑代码,Runtime和Manifest
  • 在Manifest中记录中依赖关系,通过__webpack_require__加载
  • 构建结果中包含了没有使用到的square
  • 构建体积明显比rollup中iife格式大
  • 代码执行的时候,rollup中iife输出格式,代码执行的速度更快,webpack构建出来的还有依赖查找,而且每个模块通过一个函数包裹形式,执行的时候,就形成了一个个的闭包,占用了内存,当然可以在webpack3使用ConcatenationPlugin插件优化这样的输出格式,打包到一个依赖中

对于性能方面the-cost-of-small-modules做了很好的测评,可以了解一下。

没有银弹

webpack诞生的时候,为了解决css、图片等静态文件的构建和使得代码能够按需加载实现了code-splitting,在我们日常线上业务代码开发中,或多或少有一些静态资源需要打包,此时rollup显得不太适用。所以我们可以看到,在构建一些lib的时候可以选择rollup,而在构建一些应用的时候,选择webpack.


《IVWEB 技术周刊》 震撼上线了,关注公众号:IVWEB社区,每周定时推送优质文章。