下一代打包工具,这是rollup对本身的定位。现在的前端领域,构建工具并不缺乏,每一个前端工程师都用过或者听过webpack。能够看到的是像React、Vue等框架的构建工具使用的都是rollup。既然如此,这些框架为何会选择rollup?它的特性是什么?面对不一样场景,咱们要怎么选择构建工具?本文将一一为你呈现。javascript
tree shaking是rollup提出的,这也是rollup一个很是重要的feature,那什么是tree shaking,rollup的解释是在构建代码时,在使用ES6模块化的代码中,会对你的代码进行静态分析,只打包使用到的代码。这样的好处是减小代码的体积。css
能够看到它的实现依赖于静态分析,为何必须使用ES6 modules呢?咱们来复习一下ES6 modules的几个特性:前端
import
的模块名只能是字符串常量const
function
里面或是 if
里面等块级做用域中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
在构建代码的时候,能够根据代码运行环境选择不一样的输出格式,若是你的代码是运行在node中那么cjs
就能够,若是你的代码运行在浏览器环境中,那么iife
就很好,若是二者兼具,那么选择umd
。
在webpack的编译&构建中,提到webpack构建输出的代码其实有三种。segmentfault
若是咱们对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; } /***/ }) /******/ ]);
__webpack_require__
加载square
iife
格式大iife
输出格式,代码执行的速度更快,webpack构建出来的还有依赖查找,并且每一个模块经过一个函数包裹形式,执行的时候,就造成了一个个的闭包,占用了内存,固然能够在webpack3使用ConcatenationPlugin
插件优化这样的输出格式,打包到一个依赖中对于性能方面the-cost-of-small-modules作了很好的测评,能够了解一下。
webpack诞生的时候,为了解决css、图片等静态文件的构建和使得代码可以按需加载实现了code-splitting,在咱们平常线上业务代码开发中,或多或少有一些静态资源须要打包,此时rollup显得不太适用。因此咱们能够看到,在构建一些lib的时候能够选择rollup,而在构建一些应用的时候,选择webpack.
腾讯IVWEB团队的工程化解决方案feflow已经开源:Github主页:https://github.com/feflow/feflow
若是对您的团队或者项目有帮助,请给个Star支持一下哈~