Skip to content
This repository was archived by the owner on Jan 9, 2023. It is now read-only.

Commit

Permalink
docs(readme): improve documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
wmzy committed Aug 20, 2019
1 parent f974bda commit c426640
Showing 1 changed file with 78 additions and 8 deletions.
86 changes: 78 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ import plugins, {file} from 'rollup-plugin-by-output';
export default {
// ...
plugins: plugins(babel(), [file(pkg.browser), terser()]),
output: {
globals: {
lodash: '_'
},
name: pkg.name,
file: pkg.browser,
format: 'umd'
},
output: [
{
globals: {
lodash: '_'
},
name: pkg.name,
file: pkg.browser,
format: 'umd'
},
{
file: pkg.main,
format: 'cjs'
Expand All @@ -44,6 +44,73 @@ export default {

```
## What
Rollup support multiple outputs with the same input. But sometime we want to apply different plugins for these outputs.
The most common scenario is apply the [terser](https://github.com/TrySound/rollup-plugin-terser) plugin for a minify bundle.
Before we can write a config array, but there are a lot of duplicate code and operations.
This plugin (maybe not a plugin) gives you a slightly elegant and efficient solution for this scene.
## APIs
```js
import plugins, {when, whenAll, prop, format, file} from 'rollup-plugin-by-output';
```
### plugins
```js
{
//...
plugins: [pluginA, when(filter, pluginB), pluginsC, ..., ...whenAll(anotherFilter, pluginD, pluginE)]
}
```
same as:
```js
{
// ...
plugins: plugins(pluginA,
[filter, pluginB],// same as when
pluginsC, ...,
[anotherFilter, pluginD, pluginE] // same as whenAll and flat
)
}
```
The filter is a predicate function, the parameter is an output config object. If the filter return a truthy value, the rest plugins will be apply for the output.
`when` and `whenAll` is convenient for few filters and `plugins` is convenient for multiple filters.
### filter helpers
There are three [simple](https://github.com/wmzy/rollup-plugin-by-output/blob/master/src/index.js#L53) but useful filter helpers: `prop`, `format`, `file`.
With them you can write like:
```js
// output
[
{
name: pkg.name,
file: pkg.browser,
format: 'umd'
}
]

when(format('umd'), pluginA)
// same as
when(format(/^umd$/), pluginA)
// same as
when(format(f => f === 'umd'), pluginA)

when(file(pkg.browser), pluginA)

format = filter => prop('format', filter)
file = filter => prop('file', filter)
```
## Workflow
```bash
Expand All @@ -62,3 +129,6 @@ npm run commit
# publish
npm publish
```
## License
MIT

0 comments on commit c426640

Please sign in to comment.