1 | [![Build Status](https://travis-ci.org/Updater/rollup-plugin-peer-deps-external.svg?branch=master)](https://travis-ci.org/Updater/rollup-plugin-peer-deps-external) [![npm](https://img.shields.io/npm/v/rollup-plugin-peer-deps-external.svg)](https://www.npmjs.com/package/rollup-plugin-peer-deps-external) [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
|
2 |
|
3 | # Rollup Plugin Peer Deps External
|
4 | Automatically externalize `peerDependencies` in a `rollup` bundle.
|
5 | Also optionally externalize `dependencies` in a `rollup` bundle.
|
6 |
|
7 | ## Motivation
|
8 | When bundling a library using [`rollup`](https://github.com/rollup/rollup), we generally want to keep from including [`peerDependencies`](https://nodejs.org/en/blog/npm/peer-dependencies/) since they are expected to be provided by the consumer of the library. By excluding these dependencies, we keep bundle size down and avoid bundling duplicate dependencies.
|
9 |
|
10 | We can achieve this using the rollup [`external`](https://github.com/rollup/rollup/wiki/JavaScript-API#external) configuration option, providing it a list of the peer dependencies to exclude from the bundle. This plugin automates the process, automatically adding a library's `peerDependencies` to the `external` configuration.
|
11 |
|
12 | ## Module paths
|
13 | This plugin is compatible with module path format applied by, for example, [`babel-plugin-lodash`](https://github.com/lodash/babel-plugin-lodash). For any module name in `peerDependencies`, all paths beginning with that module name will also be added to `external`.
|
14 |
|
15 | E.g.: If `lodash` is in `peerDependencies`, an import of `lodash/map` would be added to externals.
|
16 |
|
17 | ## Installation
|
18 | ```bash
|
19 | npm install --save-dev rollup-plugin-peer-deps-external
|
20 | ```
|
21 |
|
22 | ## Usage
|
23 | ```javascript
|
24 | // Add to plugins array in rollup.config.js
|
25 | import peerDepsExternal from 'rollup-plugin-peer-deps-external';
|
26 |
|
27 | export default {
|
28 | plugins: [
|
29 | // Preferably set as first plugin.
|
30 | peerDepsExternal(),
|
31 | ],
|
32 | }
|
33 | ```
|
34 |
|
35 | ## Options
|
36 | ### packageJsonPath
|
37 | If your `package.json` is not in the current working directory you can specify the path to the file
|
38 | ```javascript
|
39 | // Add to plugins array in rollup.config.js
|
40 | import peerDepsExternal from 'rollup-plugin-peer-deps-external';
|
41 |
|
42 | export default {
|
43 | plugins: [
|
44 | // Preferably set as first plugin.
|
45 | peerDepsExternal({
|
46 | packageJsonPath: 'my/folder/package.json'
|
47 | }),
|
48 | ],
|
49 | }
|
50 | ```
|
51 |
|
52 | ### includeDependencies
|
53 | Sometimes it's necessary to include a package as a direct dependency instead of a peer dependency. This can happen if your package has a very specific requirement on the dependency and cannot accecpt another version. In this case set `includeDependencies` to `true` and all of your packages dependencies will also be included as externals
|
54 |
|
55 | ```javascript
|
56 | // Add to plugins array in rollup.config.js
|
57 | import peerDepsExternal from 'rollup-plugin-peer-deps-external';
|
58 |
|
59 | export default {
|
60 | plugins: [
|
61 | // Preferably set as first plugin.
|
62 | peerDepsExternal({
|
63 | includeDependencies: true,
|
64 | }),
|
65 | ],
|
66 | }
|
67 | ```
|