What is rollup-plugin-peer-deps-external?
The rollup-plugin-peer-deps-external package is a Rollup plugin that automatically marks peer dependencies as external. This helps to avoid bundling peer dependencies into your package, ensuring that they are resolved from the consuming project's dependencies.
What are rollup-plugin-peer-deps-external's main functionalities?
Automatically Externalize Peer Dependencies
This feature automatically marks all peer dependencies listed in your package.json as external, preventing them from being bundled into your output file. This is useful for library authors who want to ensure that peer dependencies are resolved from the consuming project's dependencies.
const peerDepsExternal = require('rollup-plugin-peer-deps-external');
module.exports = {
input: 'src/index.js',
output: {
file: 'dist/bundle.js',
format: 'cjs'
},
plugins: [
peerDepsExternal()
]
};
Other packages similar to rollup-plugin-peer-deps-external
rollup-plugin-auto-external
The rollup-plugin-auto-external package automatically marks dependencies and peer dependencies as external. It provides more flexibility by allowing you to configure which dependencies to externalize. Compared to rollup-plugin-peer-deps-external, it offers a broader scope by including regular dependencies as well.
rollup-plugin-node-externals
The rollup-plugin-node-externals package is another Rollup plugin that marks Node.js built-in modules and dependencies as external. It is more comprehensive in scope, as it can handle built-in modules, dependencies, and peer dependencies. This makes it a good choice for Node.js projects that need to externalize a wide range of modules.
Rollup Plugin Peer Deps External
Automatically externalize peerDependencies
in a rollup
bundle.
Motivation
When bundling a library using rollup
, we generally want to keep from including peerDependencies
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.
We can achieve this using the rollup 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.
Installation
npm install --save-dev rollup-plugin-peer-deps-external
Usage
import peerDepsExternal from 'rollup-plugin-peer-deps-external';
export default {
plugins: [
peerDepsExternal(),
],
}
Options
packageJsonPath
If your package.json
is not in the current working directory you can specify the path to the file
import peerDepsExternal from 'rollup-plugin-peer-deps-external';
export default {
plugins: [
peerDepsExternal({
packageJsonPath: 'my/folder/package.json'
}),
],
}
includeDependencies **deprecated**
Set includeDependencies
to true
to also externalize regular dependencies in addition to peer deps.
import peerDepsExternal from 'rollup-plugin-peer-deps-external';
export default {
plugins: [
peerDepsExternal({
includeDependencies: true,
}),
],
}
Module paths
This plugin is compatible with module path format applied by, for example, babel-plugin-lodash
. For any module name in peerDependencies
, all paths beginning with that module name will also be added to external
.
E.g.: If lodash
is in peerDependencies
, an import of lodash/map
would be added to externals.