UNPKG

4.22 kBMarkdownView Raw
1# rollup-babel-lib-bundler
2Utility to bundle JavaScript libraries with Rollup
3
4[![Build Status](https://travis-ci.org/frostney/rollup-babel-lib-bundler.svg?branch=master)](https://travis-ci.org/frostney/rollup-babel-lib-bundler) [![Dependency Status](https://david-dm.org/frostney/rollup-babel-lib-bundler.svg)](https://david-dm.org/frostney/rollup-babel-lib-bundler) [![devDependency Status](https://david-dm.org/frostney/rollup-babel-lib-bundler/dev-status.svg)](https://david-dm.org/frostney/rollup-babel-lib-bundler#info=devDependencies) [![Coverage Status](https://coveralls.io/repos/github/frostney/rollup-babel-lib-bundler/badge.svg?branch=master)](https://coveralls.io/github/frostney/rollup-babel-lib-bundler?branch=master)
5
6As a library author, I always wanted to have a utility where I put in a file and I get a library for all the formats I want to distribute.
7(Ideally, it should pick up my Babel config and the library name from the `package.json`.)
8
9Originally, this started as a build script in one of open source projects, but then I needed it in other projects as well. That's when it became a NPM module.
10
11I really like Rollup's idea of not scoping each module individually which is closer to my perception of what the library what would look like if I were to write the library by hand without modules.
12
13## Installation
14```
15$ npm install rollup-babel-lib-bundler
16```
17
18## Usage
19
20Rollup requires a rollup-compatible Babel config. Simply change the `es2015` preset to `es2015-rollup` or `es2015-loose` to `es2015-loose-rollup`.
21
22![usage](https://github.com/frostney/rollup-babel-lib-bundler/blob/master/docs/usage.gif)
23
24### Command-line
25```
26rollup-babel-lib-bundler ./myFancyLibrary.js
27```
28
29The command-line app also supports passing in options through the `package.json`. Simply create a property called `rollupBabelLibBundler`. An example configuration would look like this:
30```json
31"rollupBabelLibBundler": {
32 "moduleName": "myFancyLibrary",
33 "dest": "dist",
34 "babel": {
35 "presets": ["es2015-rollup", "stage-1"]
36 }
37}
38```
39
40It also allows passing in Babel options passed in here. This will take precedence over the `.babelrc` file. The options are passed into the `rollup-babel-plugin` where the options are slightly different from the official Babel configuration. See https://github.com/rollup/rollup-plugin-babel#configuring-babel for more details.
41
42```
43Command-line options > Package configuration options
44Package babel configuration > .babelrc
45```
46
47### JavaScript API
48The API is very similar to a Rollup config file. In fact, additional options will be passed into Rollup or plugins `rollup-babel-lib-bundler` uses.
49
50```javascript
51var rollupBabelLibBundler = require('rollup-babel-lib-bundler');
52
53rollupBabelLibBundler({
54 name: 'myfancylibrary',
55 moduleName: 'myFancyLibrary',
56 dest: 'dist',
57 entry: './myFancyLibrary.js',
58 format: ['cjs', 'umd', 'es6'],
59});
60```
61
62#### options.name
63###### Type: `String`
64###### Default: `mylibrary`
65The name of the library. Will be used for the generated filenames.
66
67#### options.moduleName
68###### Type: `String`
69###### Default `myLibrary`
70This is needed for the UMD build. This is the property the library will bind itself to the global object. If omitted, it will automatically use `options.name` as camel case.
71
72#### options.dest
73###### Type: `String`
74###### Default `dist`
75The directory where the files will be generated to.
76
77#### options.entry
78###### Type: `String`
79###### Default `index.js`
80The path to the library itself.
81
82#### options.format
83###### Type: `Array`
84###### Default `['umd', 'es6', 'cjs']`
85Can be `umd`, `es6`, `cjs`, `iife` or a combination of these.
86
87#### options.babel
88###### Type: `String` or `Object`
89Allow to overwrite the babel configuration. By default its value is `inherit` and it will take the closest `.babelrc` file.
90
91#### Return value
92###### Type: `Promise`
93This returns a promise with an array of objects which have the following format:
94
95```javascript
96{
97 name: String, // The module name
98 format: String, // The module format (es6, umd, cjs)
99 endTime: Number, // The time where this module finished generating
100 startTime: Number, // The time before the generating process started
101}
102```
103
104## License
105MIT