cheatsheets/rollup.md

191 lines
3.1 KiB
Markdown
Raw Permalink Normal View History

2017-11-02 14:58:09 +00:00
---
title: Rollup.js
category: JavaScript libraries
2020-07-04 13:33:09 +00:00
updated: 2020-01-29
2018-03-17 00:43:59 +00:00
authors:
- github: ryanSN
2017-11-02 14:58:09 +00:00
keywords:
- rollup.watch
- bundle
- rollup.config.js
intro: |
[Rollup](https://rollupjs.org/) 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.
---
### Basic config
#### rollup.config.js
```js
export default {
input: 'src/main.js',
output: {
file: 'bundle.js',
format: 'cjs'
}
}
```
#### Terminal
2018-03-17 00:43:59 +00:00
2017-11-02 14:58:09 +00:00
```bash
npm install -D rollup
```
2018-03-17 00:43:59 +00:00
| Command | Description |
| --- | --- |
| `rollup -c -o bundle.js` | bundle using config |
| `rollup main.js --o bundle.js --f cjs` | bundle |
| `rollup --watch` | bundle continuously |
2017-11-02 14:58:09 +00:00
2018-03-17 00:43:59 +00:00
You may need to use `./node_modules/.bin/rollup` as a command if you did not install rollup globally.
2017-11-02 14:58:09 +00:00
2018-06-14 15:48:42 +00:00
### Multiple outputs
2017-11-02 14:58:09 +00:00
#### rollup.config.js
```js
export default [
{
input: 'src/main.js',
output: {
file: __dirname + '/public/main.js',
format: 'cjs',
name: 'main'
}
},
{
input: 'src/vendor.js',
output: {
file: __dirname + 'public/vendor.js',
format: 'cjs',
name: 'vendor'
}
}
]
```
This creates `main.js` and `vendor.js`.
## Using plugins
### Plugins
#### Terminal
2018-03-17 00:43:59 +00:00
2017-11-02 14:58:09 +00:00
```bash
2020-01-29 13:43:22 +00:00
npm install -D @rollup/plugin-json
2017-11-02 14:58:09 +00:00
```
#### rollup.config.js
```js
2020-01-29 13:43:22 +00:00
import json from '@rollup/plugin-json'
2017-11-02 14:58:09 +00:00
export default {
input: 'src/main.js',
output: {
file: 'bundle.js',
format: 'cjs'
},
2019-08-22 11:45:19 +00:00
plugins: [ json() ]
2017-11-02 14:58:09 +00:00
}
```
### npm packages
#### Terminal
```bash
2020-01-29 13:43:22 +00:00
npm install -D @rollup/plugin-node-resolve
2017-11-02 14:58:09 +00:00
```
#### rollup.config.js
```js
2020-01-29 13:43:22 +00:00
import resolve from '@rollup/plugin-node-resolve'
2017-11-02 14:58:09 +00:00
export default {
input: 'src/main.js',
2018-06-14 15:48:42 +00:00
output: {
2017-11-02 14:58:09 +00:00
file: 'bundle.js',
format: 'cjs'
},
plugins: [ resolve() ]
}
```
2018-03-17 00:43:59 +00:00
When you run a npm run build, no warning is emitted and contains the imported modules.
2017-11-02 14:58:09 +00:00
### Peer dependencies
#### Terminal
2018-03-17 00:43:59 +00:00
2017-11-02 14:58:09 +00:00
```bash
2020-01-29 13:43:22 +00:00
npm install -D @rollup/plugin-node-resolve
2017-11-02 14:58:09 +00:00
```
#### rollup.config.js
2018-03-17 00:43:59 +00:00
2017-11-02 14:58:09 +00:00
```js
2020-01-29 13:43:22 +00:00
import resolve from '@rollup/plugin-node-resolve'
2017-11-02 14:58:09 +00:00
export default {
input: 'src/main.js',
output: {
file: 'bundle.js',
format: 'cjs'
},
plugins: [resolve({
// pass custom options to the resolve plugin
customResolveOptions: {
moduleDirectory: 'node_modules'
}
})],
// indicate which modules should be treated as external
external: ['lodash']
}
```
### Babel
#### Terminal
```bash
npm install -D @rollup/plugin-babel
2017-11-02 14:58:09 +00:00
```
#### rollup.config.js
```js
2020-01-29 13:43:22 +00:00
import resolve from '@rollup/plugin-node-resolve'
import babel from '@rollup/plugin-babel'
2017-11-02 14:58:09 +00:00
export default {
input: 'src/main.js',
output: {
file: 'bundle.js',
format: 'cjs'
},
plugins: [
resolve(),
babel({
exclude: 'node_modules/**' // only transpile our source code
})
]
}
```
2018-03-17 00:43:59 +00:00
2017-11-02 14:58:09 +00:00
#### src/.babelrc
```js
{
"presets": [
["latest", {
"es2015": {
"modules": false
}
}]
],
"plugins": ["external-helpers"]
}
```