UNPKG

6.01 kBMarkdownView Raw
1# Rollup
2
3<p align="center">
4 <a href="https://travis-ci.org/rollup/rollup">
5 <img src="https://api.travis-ci.org/rollup/rollup.svg?branch=master"
6 alt="build status">
7 </a>
8 <a href="https://npmjs.org/package/rollup">
9 <img src="https://img.shields.io/npm/v/rollup.svg"
10 alt="npm version">
11 </a>
12 <a href="https://github.com/rollup/rollup/blob/master/LICENSE.md">
13 <img src="https://img.shields.io/npm/l/rollup.svg"
14 alt="license">
15 </a>
16 <a href="https://david-dm.org/rollup/rollup">
17 <img src="https://david-dm.org/rollup/rollup.svg"
18 alt="dependency status">
19 </a>
20 <a href="http://codecov.io/github/rollup/rollup?branch=master">
21 <img src="http://codecov.io/github/rollup/rollup/coverage.svg?branch=master" alt="Coverage via Codecov" />
22 </a>
23</p>
24
25> *I roll up, I roll up, I roll up, Shawty I roll up*
26>
27> *I roll up, I roll up, I roll up*
28> &ndash;[Wiz Khalifa](https://www.youtube.com/watch?v=UhQz-0QVmQ0)
29
30
31## Quickstart
32
33Rollup can be used via a [JavaScript API](https://github.com/rollup/rollup/wiki/JavaScript-API) or a [Command Line Interface](https://github.com/rollup/rollup/wiki/Command-Line-Interface). Install with `npm install -g rollup` and run `rollup --help` to get started.
34
35[Dive into the wiki](https://github.com/rollup/rollup/wiki) when you're ready to learn more about Rollup and ES6 modules.
36
37
38## A next-generation ES6 module bundler
39
40When you're developing software, it's much easier to break your library or application apart into separate pieces that you can work on separately. It's also very likely that you'll have dependencies on third party libraries. The result is lots of small files – but that's bad news for browsers, which get slowed down by having to make many requests. (It's also [bad news for Node!](https://kev.inburke.com/kevin/node-require-is-dog-slow/))
41
42The solution is to write your code as **modules**, and use a **module bundler** to concatenate everything into a single file. [Browserify](http://browserify.org/) and [Webpack](http://webpack.github.io/) are examples of module bundlers.
43
44So far, so good, **but there's a problem**. When you include a library in your bundle...
45
46```js
47var utils = require( 'utils' );
48
49var query = 'Rollup';
50utils.ajax( 'https://api.example.com?search=' + query ).then( handleResponse );
51```
52
53...you include the *whole* library, including lots of code you're not actually using.
54
55**ES6 modules solve this problem.** Instead of importing the whole of `utils`, we can just import the `ajax` function we need:
56
57```js
58import { ajax } from 'utils';
59
60var query = 'Rollup';
61ajax( 'https://api.example.com?search=' + query ).then( handleResponse );
62```
63
64Rollup statically analyses your code, and your dependencies, and includes the bare minimum in your bundle.
65
66
67## Shouldn't we be writing those utilities as small modules anyway?
68
69[Not always, no.](https://medium.com/@Rich_Harris/small-modules-it-s-not-quite-that-simple-3ca532d65de4)
70
71
72## Don't minifiers already do this?
73
74If you minify code with something like [UglifyJS](https://github.com/mishoo/UglifyJS2) (and you should!) then some unused code will be removed:
75
76```js
77(function () {
78 function foo () {
79 console.log( 'this function was included!' );
80 }
81
82 function bar () {
83 console.log( 'this function was not' );
84 baz();
85 }
86
87 function baz () {
88 console.log( 'neither was this' );
89 }
90
91 foo();
92})();
93```
94
95A minifier can detect that `foo` gets called, but that `bar` doesn't. When we remove `bar`, it turns out that we can also remove `baz`.
96
97But because of the limitations of static analysis, and the dynamic nature of JavaScript, it can't do the same thing with code like this:
98
99```js
100(function () {
101 var obj = {
102 foo: function () {
103 console.log( 'this method was included!' );
104 },
105
106 bar: function () {
107 console.log( 'so was this :-(' );
108 this.baz();
109 },
110
111 baz: function () {
112 console.log( 'and this :-(' );
113 }
114 };
115
116 obj.foo();
117})();
118```
119
120Unfortunately, **traditional modules – CommonJS and AMD – result in code more like the second example than the first, making them next-to-impossible to optimise**. Rather than *excluding dead code*, we should be *including live code* (aka 'tree-shaking'). That's only possible with ES6 modules.
121
122
123## Can I use it with my non-ES6 dependencies?
124
125[Yes!](https://github.com/rollup/rollup/wiki/Bundling-CommonJS-modules) Rollup can't work its tree-shaking magic on CommonJS modules, but it can convert them to ES6 via [plugins](https://github.com/rollup/rollup/wiki/Plugins).
126
127
128## Can I distribute my package as an ES6 module?
129
130If your `package.json` has a `jsnext:main` field, ES6-aware tools like Rollup can import the ES6 version of the package instead of the legacy CommonJS or UMD version. You'll be writing your code in a more future-proof way, and helping to bring an end to the [dark days of JavaScript package management](https://medium.com/@trek/last-week-i-had-a-small-meltdown-on-twitter-about-npms-future-plans-around-front-end-packaging-b424dd8d367a). [Learn more here.](https://github.com/rollup/rollup/wiki/jsnext:main)
131
132See [rollup-starter-project](https://github.com/eventualbuddha/rollup-starter-project) for inspiration on how to get started.
133
134
135## How does this compare to JSPM/SystemJS?
136
137[JSPM](http://jspm.io/) is awesome! It's a little different to this project though – it combines a repository with a package manager and a client-side module loader, as opposed to simply bundling modules. JSPM allows you to use any module format and even develop without a build step, so it's a great choice for creating applications. Rollup generates smaller bundles that don't use the complex SystemJS format, and so is a better choice for creating libraries. A future version of JSPM [may use Rollup internally](https://github.com/systemjs/builder/pull/205), so you'll get the best of both worlds.
138
139
140## License
141
142Released under the [MIT license](https://github.com/rollup/rollup/blob/master/LICENSE.md).