Skip to content

Commit 325d65f

Browse files
authored
Inline docs (#2602)
* Add English docs to repo * Update PR template to mention documentation requirement * Add guide markdown linting
1 parent 5516591 commit 325d65f

14 files changed

+2392
-2
lines changed

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
Pull Request Requirements:
99
* Please include tests to illustrate the problem this PR resolves.
1010
* Please lint your changes by running `npm run lint` before creating a PR.
11+
* Please update the documentation in `/docs` where necessary
1112
1213
Please place an x (no spaces - [x]) in all [ ] that apply.
1314
-->

docs/00-introduction.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
---
2+
title: Introduction
3+
---
4+
5+
### Overview
6+
7+
Rollup is a module bundler for JavaScript which compiles small pieces of code into something larger and more complex, such as a library or application. It uses the new standardized format for code modules included in the ES6 revision of JavaScript, instead of previous idiosyncratic solutions such as CommonJS and AMD. ES6 modules let you freely and seamlessly combine the most useful individual functions from your favorite libraries. This will eventually be possible natively, but Rollup lets you do it today.
8+
9+
### Quick start
10+
11+
Install with `npm install --global rollup`. Rollup can be used either through a [command line interface](guide/en#command-line-reference) with an optional configuration file, or else through its [JavaScript API](guide/en#javascript-api). Run `rollup --help` to see the available options and parameters.
12+
13+
> See [rollup-starter-lib](https://github.com/rollup/rollup-starter-lib) and
14+
[rollup-starter-app](https://github.com/rollup/rollup-starter-app) to see
15+
example library and application projects using Rollup
16+
17+
These commands assume the entry point to your application is named `main.js`, and that you'd like all imports compiled into a single file named `bundle.js`.
18+
19+
For browsers:
20+
21+
```console
22+
# compile to a <script> containing a self-executing function ('iife')
23+
$ rollup main.js --file bundle.js --format iife
24+
```
25+
26+
For Node.js:
27+
28+
```console
29+
# compile to a CommonJS module ('cjs')
30+
$ rollup main.js --file bundle.js --format cjs
31+
```
32+
33+
For both browsers and Node.js:
34+
35+
```console
36+
# UMD format requires a bundle name
37+
$ rollup main.js --file bundle.js --format umd --name "myBundle"
38+
```
39+
40+
### The Why
41+
42+
Developing software is usually easier if you break your project into smaller separate pieces, since that often removes unexpected interactions and dramatically reduces the complexity of the problems you'll need to solve, and simply writing smaller projects in the first place [isn't necessarily the answer](https://medium.com/@Rich_Harris/small-modules-it-s-not-quite-that-simple-3ca532d65de4). Unfortunately, JavaScript has not historically included this capability as a core feature in the language.
43+
44+
This finally changed with the ES6 revision of JavaScript, which includes a syntax for importing and exporting functions and data so they can be shared between separate scripts. The specification is now fixed, but it is not yet implemented in browsers or Node.js. Rollup allows you to write your code using the new module system, and will then compile it back down to existing supported formats such as CommonJS modules, AMD modules, and IIFE-style scripts. This means that you get to *write future-proof code*, and you also get the tremendous benefits of...
45+
46+
### Tree-Shaking
47+
48+
In addition to enabling the use of ES6 modules, Rollup also statically analyzes the code you are importing, and will exclude anything that isn't actually used. This allows you to build on top of existing tools and modules without adding extra dependencies or bloating the size of your project.
49+
50+
For example, with CommonJS, the *entire tool or library must be imported*.
51+
52+
```js
53+
// import the entire utils object with CommonJS
54+
const utils = require( './utils' );
55+
const query = 'Rollup';
56+
// use the ajax method of the utils object
57+
utils.ajax(`https://api.example.com?search=${query}`).then(handleResponse);
58+
```
59+
60+
But with ES6 modules, instead of importing the whole `utils` object, we can just import the one `ajax` function we need:
61+
62+
```js
63+
// import the ajax function with an ES6 import statement
64+
import { ajax } from './utils';
65+
const query = 'Rollup';
66+
// call the ajax function
67+
ajax(`https://api.example.com?search=${query}`).then(handleResponse);
68+
```
69+
70+
Because Rollup includes the bare minimum, it results in lighter, faster, and less complicated libraries and applications. Since this approach is based on explicit `import` and `export` statements, it is more effective than simply running an automated minifier to detect unused variables in the compiled output code.
71+
72+
73+
### Compatibility
74+
75+
#### Importing CommonJS
76+
77+
Rollup can import existing CommonJS modules [through a plugin](https://github.com/rollup/rollup-plugin-commonjs).
78+
79+
#### Publishing ES6 Modules
80+
81+
To make sure your ES6 modules are immediately usable by tools that work with CommonJS such as Node.js and webpack, you can use Rollup to compile to UMD or CommonJS format, and then point to that compiled version with the `main` property in your `package.json` file. If your `package.json` file also has a `module` field, ES6-aware tools like Rollup and [webpack 2+](https://webpack.js.org/) will [import the ES6 module version](https://github.com/rollup/rollup/wiki/pkg.module) directly.
82+
83+
### Links
84+
85+
- step-by-step [tutorial video series](https://code.lengstorf.com/learn-rollup-js/), with accompanying written walkthrough
86+
- miscellaneous issues in the [wiki](https://github.com/rollup/rollup/wiki)
87+

docs/01-command-line-reference.md

Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
---
2+
title: Command Line Interface
3+
---
4+
5+
Rollup should typically be used from the command line. You can provide an optional Rollup configuration file to simplify command line usage and enable advanced Rollup functionality.
6+
7+
### Configuration Files
8+
9+
Rollup configuration files are optional, but they are powerful and convenient and thus **recommended**.
10+
11+
A config file is an ES6 module that exports a default object with the desired options. Typically, it is called `rollup.config.js` and sits in the root directory of your project.
12+
13+
Also you can use CJS modules syntax for the config file.
14+
15+
```javascript
16+
module.exports = {
17+
input: 'src/main.js',
18+
output: {
19+
file: 'bundle.js',
20+
format: 'cjs'
21+
}
22+
};
23+
```
24+
25+
It may be pertinent if you want to use the config file not only from the command line, but also from your custom scripts programmatically.
26+
27+
Consult the [big list of options](guide/en#big-list-of-options) for details on each option you can include in your config file.
28+
29+
```javascript
30+
// rollup.config.js
31+
32+
export default { // can be an array (for multiple inputs)
33+
// core input options
34+
input, // required
35+
external,
36+
plugins,
37+
38+
// advanced input options
39+
onwarn,
40+
perf,
41+
42+
// danger zone
43+
acorn,
44+
acornInjectPlugins,
45+
treeshake,
46+
context,
47+
moduleContext,
48+
49+
// experimental
50+
experimentalCodeSplitting,
51+
manualChunks,
52+
optimizeChunks,
53+
chunkGroupingSize,
54+
55+
output: { // required (can be an array, for multiple outputs)
56+
// core output options
57+
format, // required
58+
file,
59+
dir,
60+
name,
61+
globals,
62+
63+
// advanced output options
64+
paths,
65+
banner,
66+
footer,
67+
intro,
68+
outro,
69+
sourcemap,
70+
sourcemapFile,
71+
sourcemapPathTransform,
72+
interop,
73+
extend,
74+
75+
// danger zone
76+
exports,
77+
amd,
78+
indent,
79+
strict,
80+
freeze,
81+
namespaceToStringTag,
82+
83+
// experimental
84+
entryFileNames,
85+
chunkFileNames,
86+
assetFileNames
87+
},
88+
89+
watch: {
90+
chokidar,
91+
include,
92+
exclude,
93+
clearScreen
94+
}
95+
};
96+
```
97+
98+
You can export an **array** from your config file to build bundles from several different unrelated inputs at once, even in watch mode. To build different bundles with the same input, you supply an array of output options for each input:
99+
100+
```javascript
101+
// rollup.config.js (building more than one bundle)
102+
103+
export default [{
104+
input: 'main-a.js',
105+
output: {
106+
file: 'dist/bundle-a.js',
107+
format: 'cjs'
108+
}
109+
}, {
110+
input: 'main-b.js',
111+
output: [
112+
{
113+
file: 'dist/bundle-b1.js',
114+
format: 'cjs'
115+
},
116+
{
117+
file: 'dist/bundle-b2.js',
118+
format: 'esm'
119+
}
120+
]
121+
}];
122+
```
123+
124+
If you want to create your config asynchronously, Rollup can also handle a `Promise` which resolves to an object or an array.
125+
126+
```javascript
127+
// rollup.config.js
128+
import fetch from 'node-fetch';
129+
export default fetch('/some-remote-service-or-file-which-returns-actual-config');
130+
```
131+
132+
Similarly, you can do this as well:
133+
134+
```javascript
135+
// rollup.config.js (Promise resolving an array)
136+
export default Promise.all([
137+
fetch('get-config-1'),
138+
fetch('get-config-2')
139+
])
140+
```
141+
142+
You *must* use a configuration file in order to do any of the following:
143+
144+
- bundle one project into multiple output files
145+
- use Rollup plugins, such as [rollup-plugin-node-resolve](https://github.com/rollup/rollup-plugin-node-resolve) and [rollup-plugin-commonjs](https://github.com/rollup/rollup-plugin-commonjs) which let you load CommonJS modules from the Node.js ecosystem
146+
147+
To use Rollup with a configuration file, pass the `--config` or `-c` flags.
148+
149+
```console
150+
# use Rollup with a rollup.config.js file
151+
$ rollup --config
152+
153+
# alternatively, specify a custom config file location
154+
$ rollup --config my.config.js
155+
```
156+
157+
You can also export a function that returns any of the above configuration formats. This function will be passed the current command line arguments so that you can dynamically adapt your configuration to respect e.g. `--silent`. You can even define your own command line options if you prefix them with `config`:
158+
159+
```javascript
160+
// rollup.config.js
161+
import defaultConfig from './rollup.default.config.js';
162+
import debugConfig from './rollup.debug.config.js';
163+
164+
export default commandLineArgs => {
165+
if (commandLineArgs.configDebug === true) {
166+
return debugConfig;
167+
}
168+
return defaultConfig;
169+
}
170+
```
171+
172+
If you now run `rollup --config --configDebug`, the debug configuration will be used.
173+
174+
175+
### Command line flags
176+
177+
Many options have command line equivalents. Any arguments passed here will override the config file, if you're using one. See the [big list of options](guide/en#big-list-of-options) for details.
178+
179+
```text
180+
-c, --config Use this config file (if argument is used but value
181+
is unspecified, defaults to rollup.config.js)
182+
-i, --input Input (alternative to <entry file>)
183+
-o, --file <output> Output (if absent, prints to stdout)
184+
-f, --format [esm] Type of output (amd, cjs, esm, iife, umd)
185+
-e, --external Comma-separate list of module IDs to exclude
186+
-g, --globals Comma-separate list of `module ID:Global` pairs
187+
Any module IDs defined here are added to external
188+
-n, --name Name for UMD export
189+
-m, --sourcemap Generate sourcemap (`-m inline` for inline map)
190+
--amd.id ID for AMD module (default is anonymous)
191+
--amd.define Function to use in place of `define`
192+
--no-strict Don't emit a `"use strict";` in the generated modules.
193+
--no-indent Don't indent result
194+
--environment <values> Environment variables passed to config file
195+
--noConflict Generate a noConflict method for UMD globals
196+
--no-treeshake Disable tree-shaking
197+
--intro Content to insert at top of bundle (inside wrapper)
198+
--outro Content to insert at end of bundle (inside wrapper)
199+
--banner Content to insert at top of bundle (outside wrapper)
200+
--footer Content to insert at end of bundle (outside wrapper)
201+
--no-interop Do not include interop block
202+
```
203+
204+
In addition, the following arguments can be used:
205+
206+
#### `-h`/`--help`
207+
208+
Print the help document.
209+
210+
#### `-v`/`--version`
211+
212+
Print the installed version number.
213+
214+
#### `-w`/`--watch`
215+
216+
Rebuild the bundle when its source files change on disk.
217+
218+
#### `--silent`
219+
220+
Don't print warnings to the console.
221+
222+
#### `--environment <values>`
223+
224+
Pass additional settings to the config file via `process.ENV`.
225+
226+
```sh
227+
rollup -c --environment INCLUDE_DEPS,BUILD:production
228+
```
229+
230+
will set `process.env.INCLUDE_DEPS === 'true'` and `process.env.BUILD === 'production'`. You can use this option several times. In that case, subsequently set variables will overwrite previous definitions. This enables you for instance to overwrite environment variables in `package.json` scripts:
231+
232+
```json
233+
// in package.json
234+
{
235+
"scripts": {
236+
"build": "rollup -c --environment INCLUDE_DEPS,BUILD:production"
237+
}
238+
}
239+
```
240+
241+
If you call this script via:
242+
243+
```console
244+
npm run build -- --environment BUILD:development
245+
```
246+
247+
then the config file will receive `process.env.INCLUDE_DEPS === 'true'` and `process.env.BUILD === 'development'`.

0 commit comments

Comments
 (0)