ixlei / jsnotes

1 stars 1 forks source link

Rollup #5

Open ixlei opened 6 years ago

ixlei commented 6 years ago

Rollup

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

Tree Shaking

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

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

以上特性使得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还支持下面几种输出格式:

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

如果我们对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;
}

/***/ })
/******/ ]);

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

没有银弹

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