Skip to content

Commit 68e48e6

Browse files
authored
Merge pull request #7 from Updater/tcrescenzi/allow-deps-too
feat(includeDependencies): add option to also externalize all dependencies
2 parents af33927 + af1b2bb commit 68e48e6

File tree

4 files changed

+33
-13
lines changed

4 files changed

+33
-13
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
# Rollup Plugin Peer Deps External
44
Automatically externalize `peerDependencies` in a `rollup` bundle.
5+
Also optionally externalize `dependencies` in a `rollup` bundle.
56

67
## Motivation
78
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.
@@ -47,3 +48,20 @@ export default {
4748
],
4849
}
4950
```
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+
```

src/get-deps.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const { resolve } = require('path');
2+
3+
export default function getDeps(path = resolve(process.cwd(), 'package.json'), type = 'peerDependencies') {
4+
try {
5+
const pkg = require(path);
6+
return Object.keys(pkg[type]);
7+
} catch (err) {
8+
return [];
9+
}
10+
}

src/get-peer-deps.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
import { either, pipe } from 'ramda';
22
import externalToFn from './external-to-fn';
33
import getModulesMatcher from './get-modules-matcher';
4-
import getPeerDeps from './get-peer-deps';
4+
import getDeps from './get-deps';
55

6-
export default function PeerDepsExternalPlugin({packageJsonPath} = {}) {
6+
export default function PeerDepsExternalPlugin({packageJsonPath, includeDependencies} = {}) {
77
return {
88
name: 'peer-deps-external',
99
options: opts => {
1010
opts.external = either(
1111
// Retain existing `external` config
1212
externalToFn(opts.external),
1313
// Add `peerDependencies` to `external` config
14-
getModulesMatcher(getPeerDeps(packageJsonPath))
14+
getModulesMatcher(
15+
getDeps(packageJsonPath, 'peerDependencies')
16+
.concat(includeDependencies ? getDeps(packageJsonPath, 'dependencies') : []))
1517
);
1618

1719
return opts;

0 commit comments

Comments
 (0)