關於Rollup那些事

Rollup

下一代打包工具,這是rollup對本身的定位。現在的前端領域,構建工具並不缺乏,每一個前端工程師都用過或者聽過webpack。能夠看到的是像React、Vue等框架的構建工具使用的都是rollup。既然如此,這些框架爲何會選擇rollup?它的特性是什麼?面對不一樣場景,咱們要怎麼選擇構建工具?本文將一一爲你呈現。javascript

Tree Shaking

tree shaking是rollup提出的,這也是rollup一個很是重要的feature,那什麼是tree shaking,rollup的解釋是在構建代碼時,在使用ES6模塊化的代碼中,會對你的代碼進行靜態分析,只打包使用到的代碼。這樣的好處是減小代碼的體積。css

能夠看到它的實現依賴於靜態分析,爲何必須使用ES6 modules呢?咱們來複習一下ES6 modules的幾個特性:前端

  • import 的模塊名只能是字符串常量
  • import binding 是 immutable 的,相似 const
  • 只能做爲模塊頂層的語句出現,不能出如今 function 裏面或是 if裏面等塊級做用域中
  • import hoisted,無論 import的語句出現的位置在哪裏,在模塊初始化的時候全部的import 都必須已經導入完成。

以上特性使得ES6 Modules缺乏了必定的靈活性,但使得全部的依賴都是肯定的,可以對代碼進行靜態分析。不須要依靠運行時去肯定依賴關係。 舉個栗子: maths.jsjava

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

main.jsnode

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

執行下面的命令後webpack

$ rollup main.js --o bundle.js --f iife
複製代碼

輸出bundle.jsgit

(function () {
'use strict';

// maths.js

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

console.log(cube(5));

}());
複製代碼

能夠看到,maths.js中square方法沒有使用到,沒有打包到構建結果中。在構建的時候,加了個參數f,值爲iife的選項,構建的後代碼的組織形式被一個當即執行函數包裹。github

代碼構建後輸出格式

上面在構建的時候指定了參數f,值爲iife的選項,輸出了當即執行風格的構建代碼,rollup還支持下面幾種輸出格式:web

  • 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社區,每週定時推送優質文章。

相關文章
相關標籤/搜索