You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/******/(function(modules){// webpackBootstrap/******/// The module cache/******/varinstalledModules={};/******//******/// The require function/******/function__webpack_require__(moduleId){/******//******/// Check if module is in cache/******/if(installedModules[moduleId]){/******/returninstalledModules[moduleId].exports;/******/}/******/// Create a new module (and put it into the cache)/******/varmodule=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/******/returnmodule.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){/******/vargetter=module&&module.__esModule ?
/******/functiongetDefault(){returnmodule['default'];} :
/******/functiongetModuleExports(){returnmodule;};/******/__webpack_require__.d(getter,'a',getter);/******/returngetter;/******/};/******//******/// Object.prototype.hasOwnProperty.call/******/__webpack_require__.o=function(object,property){returnObject.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.jsfunctionsquare(x){returnx*x;}functioncube(x){returnx*x*x;}/***/})/******/]);
Rollup
下一代打包工具,这是rollup对自己的定位。如今的前端领域,构建工具并不缺少,每个前端工程师都用过或者听过webpack。可以看到的是像React、Vue等框架的构建工具使用的都是rollup。既然如此,这些框架为什么会选择rollup?它的特性是什么?面对不同场景,我们要怎么选择构建工具?本文将一一为你呈现。
Tree Shaking
tree shaking是rollup提出的,这也是rollup一个非常重要的feature,那什么是tree shaking,rollup的解释是在构建代码时,在使用ES6模块化的代码中,会对你的代码进行静态分析,只打包使用到的代码。这样的好处是减少代码的体积。
可以看到它的实现依赖于静态分析,为什么必须使用ES6 modules呢?我们来复习一下ES6 modules的几个特性:
import
的模块名只能是字符串常量const
function
里面或是if
里面等块级作用域中import
的语句出现的位置在哪里,在模块初始化的时候所有的import
都必须已经导入完成。以上特性使得ES6 Modules缺少了一定的灵活性,但使得所有的依赖都是确定的,能够对代码进行静态分析。不需要依靠运行时去确定依赖关系。
举个栗子:
maths.js
main.js
执行下面的命令后
输出bundle.js
可以看到,maths.js中
square
方法没有使用到,没有打包到构建结果中。在构建的时候,加了个参数f
,值为iife
的选项,构建的后代码的组织形式被一个立即执行函数包裹。代码构建后输出格式
上面在构建的时候指定了参数
f
,值为iife
的选项,输出了立即执行风格的构建代码,rollup还支持下面几种输出格式:在构建代码的时候,可以根据代码运行环境选择不同的输出格式,如果你的代码是运行在node中那么
cjs
就可以,如果你的代码运行在浏览器环境中,那么iife
就很好,如果两者兼具,那么选择umd
。在webpack的编译&构建中,提到webpack构建输出的代码其实有三种。
如果我们对main.js执行下面的命令构建后
输出dist.js
__webpack_require__
加载square
iife
格式大iife
输出格式,代码执行的速度更快,webpack构建出来的还有依赖查找,而且每个模块通过一个函数包裹形式,执行的时候,就形成了一个个的闭包,占用了内存,当然可以在webpack3使用ConcatenationPlugin
插件优化这样的输出格式,打包到一个依赖中对于性能方面the-cost-of-small-modules做了很好的测评,可以了解一下。
没有银弹
webpack诞生的时候,为了解决css、图片等静态文件的构建和使得代码能够按需加载实现了code-splitting,在我们日常线上业务代码开发中,或多或少有一些静态资源需要打包,此时rollup显得不太适用。所以我们可以看到,在构建一些lib的时候可以选择rollup,而在构建一些应用的时候,选择webpack.
The text was updated successfully, but these errors were encountered: