rollup.js 试玩

简单、使用、强大,处理模块依赖,打包模块成一个文件。目前浏览器对ES2015的模块支持并不好。

Rollup is a JavaScript module bundler. It takes a file - let's call it main.js, and recursively imports its dependencies until your whole library or application is contained in a single file.

 Tree-shaking:unlike other bundlers (such as Browserify or Webpack), Rollup only includes the code you actually need. In many cases, the resulting bundle is a fraction of the size. Even in the worst case scenario it will still be smaller than bundles generated by other tools

modules advantage:

  • reusable
  • maintainable
  • testable
  • easier to collaborate on, because Alice and Bob can work on the same app simultaneously (Alice can work on dictionary while Bob fixes extractWords) without stomping each other's changes
  • more bug-resistant, because we don't need to worry about things like naming conflicts between modules

http://rollupjs.org/guide/ 指南:

支持:AMD CommonJS ES6 IIFE UMD 五种打包方式

创建 rollup.config.js:

export default {  
    entry: 'toc.es6.js',
    format: 'iife',
 //amd – 使用像requirejs一样的银木块定义
 // cjs – CommonJS,适用于node和browserify / Webpack
 // es6 (default) – 保持ES6的格式
 // iife – 使用于<script> 标签引用的方式
 // umd – 适用于CommonJs和AMD风格通用模式
    moduleName: 'tocx',  // 使用iife 模式时 加入模块名
    dest: 'bundle.js'
};
rollup -c  //To use the config file, we use the --config or -c flag: