-
-
Notifications
You must be signed in to change notification settings - Fork 594
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: migrate rollup-plugin-multi-entry (#88)
* chore: migrate rollup-plugin-multi-entry * chore: address reviews
- Loading branch information
1 parent
13eadab
commit 825ef02
Showing
12 changed files
with
1,178 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
[npm]: https://img.shields.io/npm/v/@rollup/plugin-multi-entry | ||
[npm-url]: https://www.npmjs.com/package/@rollup/plugin-multi-entry | ||
[size]: https://packagephobia.now.sh/badge?p=@rollup/plugin-multi-entry | ||
[size-url]: https://packagephobia.now.sh/result?p=@rollup/plugin-multi-entry | ||
|
||
[![npm][npm]][npm-url] | ||
[![size][size]][size-url] | ||
[![libera manifesto](https://img.shields.io/badge/libera-manifesto-lightgrey.svg)](https://liberamanifesto.com) | ||
|
||
# @rollup/plugin-multi-entry | ||
|
||
🍣 A Rollup plugin which allows use of multiple entry points for a bundle. | ||
|
||
As an added bonus, the _named exports_ from all entry points will be combined. This is particularly useful for tests, but can also be used to package a library. | ||
|
||
_Note: `default` exports cannot be combined and exported by this plugin. Only named exports will be exported._ | ||
|
||
## Requirements | ||
|
||
This plugin requires an [LTS](https://github.com/nodejs/Release) Node version (v8.0.0+) and Rollup v1.20.0+. | ||
|
||
## Install | ||
|
||
Using npm: | ||
|
||
```console | ||
npm install @rollup/plugin-multi-entry --save-dev | ||
``` | ||
|
||
## Usage | ||
|
||
Suppose that we have three separate source files, each with their own export(s): | ||
|
||
```js | ||
// batman.js | ||
export const belt = 'utility'; | ||
``` | ||
|
||
```js | ||
// robin.js | ||
export const tights = 'tight'; | ||
``` | ||
|
||
```js | ||
// joker.js | ||
export const color = 'purple'; | ||
``` | ||
|
||
Then, create a `rollup.config.js` [configuration file](https://www.rollupjs.org/guide/en/#configuration-files) and import the plugin: | ||
|
||
```js | ||
import multi from '@rollup/plugin-multi-entry'; | ||
|
||
export default { | ||
input: ['batman.js', 'robin.js', 'joker.js'], | ||
output: { | ||
dir: 'output' | ||
}, | ||
plugins: [multi()] | ||
}; | ||
``` | ||
|
||
Then call `rollup` either via the [CLI](https://www.rollupjs.org/guide/en/#command-line-reference) or the [API](https://www.rollupjs.org/guide/en/#javascript-api). | ||
|
||
Using all three files above as entry points will yield a bundle with exports for `belt`, `tights`, and `color`. | ||
|
||
## Options | ||
|
||
### `exports` | ||
|
||
Type: `Boolean`<br> | ||
Default: `true` | ||
|
||
If `true`, instructs the plugin to export named exports to the bundle from all entries. If `false`, the plugin will not export any entry exports to the bundle. This can be useful when wanting to combine code from multiple entry files, but not necessarily to export each entry file's exports. | ||
|
||
## Supported Input Types | ||
|
||
This plugin extends Rollup's `input` option to support multiple new value types, in addition to a `String` specifying a path to a file. | ||
|
||
### Glob | ||
|
||
When using `plugin-multi-entry`, input values passed as a normal `String` are [glob aware](<https://en.wikipedia.org/wiki/Glob_(programming)>). Meaning you can utilize glob wildcards and other glob patterns to specify files as being input files. | ||
|
||
```js | ||
export default { | ||
input: 'batcave/friends/**/*.js', | ||
plugins: [multi()] | ||
// ... | ||
}; | ||
``` | ||
|
||
### Array | ||
|
||
An `Array` of `String` can be passed as the input. Values are glob-aware and can specify paths or globbed paths. | ||
|
||
```js | ||
export default { | ||
input: ['party/supplies.js', 'batcave/friends/**/*.js'], | ||
plugins: [multi()] | ||
// ... | ||
}; | ||
``` | ||
|
||
### `include` and `exclude` | ||
|
||
For fine-grain control, an `Object` may be passed containing `include` and `exclude` properties. These properties specify and `Array` of `String` representing paths (which are also glob-aware) which should be included as entry files, as well as files that should be excluded from any entries that may have been found with `include`, respectively. | ||
|
||
```js | ||
export default { | ||
input: { | ||
// invite everyone! | ||
include: ['food.js', 'drinks.js', 'batcave/friends/**/*.js'], | ||
// except for the joker | ||
exclude: ['**/joker.js'] | ||
}, | ||
plugins: [multi()] | ||
// ... | ||
}; | ||
``` | ||
|
||
## Meta | ||
|
||
[CONTRIBUTING](/.github/CONTRIBUTING.md) | ||
|
||
[LICENSE (MIT)](/LICENSE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
{ | ||
"name": "@rollup/plugin-multi-entry", | ||
"version": "3.0.0", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"description": "Use multiple entry points for a bundle", | ||
"license": "MIT", | ||
"repository": "rollup/plugins", | ||
"author": "rollup", | ||
"homepage": "https://github.com/rollup/plugins/packages/multi-entry/#readme", | ||
"bugs": "https://github.com/rollup/plugins/issues", | ||
"main": "dist/index.js", | ||
"scripts": { | ||
"build": "rollup -c", | ||
"ci:coverage": "nyc pnpm run test && nyc report --reporter=text-lcov > coverage.lcov", | ||
"ci:lint": "pnpm run build && pnpm run lint", | ||
"ci:lint:commits": "commitlint --from=${CIRCLE_BRANCH} --to=${CIRCLE_SHA1}", | ||
"ci:test": "pnpm run test -- --verbose", | ||
"lint": "pnpm run lint:js && pnpm run lint:docs && pnpm run lint:package", | ||
"lint:docs": "prettier --single-quote --write README.md", | ||
"lint:js": "eslint --fix --cache src test", | ||
"lint:package": "prettier --write package.json --plugin=prettier-plugin-package", | ||
"prebuild": "del-cli dist", | ||
"prepare": "pnpm run build", | ||
"prepublishOnly": "pnpm run lint", | ||
"pretest": "pnpm run build", | ||
"test": "ava" | ||
}, | ||
"files": [ | ||
"dist", | ||
"README.md", | ||
"LICENSE" | ||
], | ||
"keywords": [ | ||
"rollup", | ||
"plugin", | ||
"multi", | ||
"multiple", | ||
"entry", | ||
"entries" | ||
], | ||
"peerDependencies": { | ||
"rollup": "^1.20.0" | ||
}, | ||
"dependencies": { | ||
"matched": "^1.0.2" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "^7.2.0", | ||
"@babel/preset-env": "^7.2.0", | ||
"rollup": "^1.20.1", | ||
"rollup-plugin-babel": "^4.0.3" | ||
}, | ||
"ava": { | ||
"files": [ | ||
"!**/fixtures/**", | ||
"!**/helpers/**", | ||
"!**/recipes/**", | ||
"!**/types.ts" | ||
] | ||
}, | ||
"module": "dist/index.es.js" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import babel from 'rollup-plugin-babel'; | ||
|
||
const pkg = require('./package.json'); | ||
|
||
export default { | ||
input: 'src/index.js', | ||
plugins: [ | ||
babel({ | ||
presets: [['@babel/env', { targets: { node: '8' }, modules: false }]], | ||
babelrc: false | ||
}) | ||
], | ||
external: Object.keys(pkg.dependencies), | ||
output: [ | ||
{ format: 'cjs', file: pkg.main }, | ||
{ format: 'es', file: pkg.module } | ||
] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* eslint-disable consistent-return, no-param-reassign */ | ||
|
||
import { promise as matched } from 'matched'; | ||
|
||
const entry = '\0rollup:plugin-multi-entry:entry-point'; | ||
|
||
export default function multiEntry(conf) { | ||
let include = []; | ||
let exclude = []; | ||
let exporter = (path) => `export * from ${JSON.stringify(path)};`; | ||
|
||
function configure(config) { | ||
if (typeof config === 'string') { | ||
include = [config]; | ||
} else if (Array.isArray(config)) { | ||
include = config; | ||
} else { | ||
include = config.include || []; | ||
exclude = config.exclude || []; | ||
if (config.exports === false) { | ||
exporter = (path) => `import ${JSON.stringify(path)};`; | ||
} | ||
} | ||
} | ||
|
||
if (conf) { | ||
configure(conf); | ||
} | ||
|
||
return { | ||
options(options) { | ||
if (options.input && options.input !== entry) { | ||
configure(options.input); | ||
} | ||
options.input = entry; | ||
}, | ||
|
||
resolveId(id) { | ||
if (id === entry) { | ||
return entry; | ||
} | ||
}, | ||
|
||
load(id) { | ||
if (id === entry) { | ||
if (!include.length) { | ||
return Promise.resolve(''); | ||
} | ||
const patterns = include.concat(exclude.map((pattern) => `!${pattern}`)); | ||
return matched(patterns, { realpath: true }).then((paths) => | ||
paths.map(exporter).join('\n') | ||
); | ||
} | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"rules": { | ||
"import/prefer-default-export": "off", | ||
"no-console": "off" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const zero = 0; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const one = 1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
console.log('Hello, 2'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* eslint-disable no-bitwise */ | ||
|
||
import test from 'ava'; | ||
import { rollup } from 'rollup'; | ||
|
||
import { getCode } from '../../../util/test'; | ||
|
||
import multiEntry from '../'; | ||
|
||
test('takes a single file as input', async (t) => { | ||
const bundle = await rollup({ input: 'test/fixtures/0.js', plugins: [multiEntry()] }); | ||
const code = await getCode(bundle); | ||
t.truthy(code.includes('exports.zero = zero;')); | ||
}); | ||
|
||
test('takes an array of files as input', async (t) => { | ||
const bundle = await rollup({ | ||
input: ['test/fixtures/0.js', 'test/fixtures/1.js'], | ||
plugins: [multiEntry()] | ||
}); | ||
const code = await getCode(bundle); | ||
t.truthy(code.includes('exports.zero = zero;')); | ||
t.truthy(code.includes('exports.one = one;')); | ||
}); | ||
|
||
test('allows an empty array as input', async (t) => { | ||
const bundle = await rollup({ input: [], plugins: [multiEntry()] }); | ||
const code = await getCode(bundle); | ||
t.falsy(code.includes('exports')); | ||
}); | ||
|
||
test('takes a glob as input', async (t) => { | ||
const bundle = await rollup({ input: 'test/fixtures/{0,1}.js', plugins: [multiEntry()] }); | ||
const code = await getCode(bundle); | ||
t.truthy(code.includes('exports.zero = zero;')); | ||
t.truthy(code.includes('exports.one = one;')); | ||
}); | ||
|
||
test('takes an array of globs as input', async (t) => { | ||
const bundle = await rollup({ | ||
input: ['test/fixtures/{0,}.js', 'test/fixtures/{1,}.js'], | ||
plugins: [multiEntry()] | ||
}); | ||
const code = await getCode(bundle); | ||
t.truthy(code.includes('exports.zero = zero;')); | ||
t.truthy(code.includes('exports.one = one;')); | ||
}); | ||
|
||
test('takes an {include,exclude} object as input', async (t) => { | ||
const bundle = await rollup({ | ||
input: { | ||
include: ['test/fixtures/*.js'], | ||
exclude: ['test/fixtures/1.js'] | ||
}, | ||
plugins: [multiEntry()] | ||
}); | ||
const code = await getCode(bundle); | ||
t.truthy(code.includes('exports.zero = zero;')); | ||
t.falsy(code.includes('exports.one = one;')); | ||
}); | ||
|
||
test('allows to prevent exporting', async (t) => { | ||
const bundle = await rollup({ | ||
input: { | ||
include: ['test/fixtures/*.js'], | ||
exports: false | ||
}, | ||
plugins: [multiEntry()] | ||
}); | ||
const code = await getCode(bundle); | ||
t.truthy(code.includes(`console.log('Hello, 2');`)); | ||
t.falsy(code.includes('zero')); | ||
t.falsy(code.includes('one')); | ||
}); |
Oops, something went wrong.