Skip to content
This repository was archived by the owner on Aug 4, 2021. It is now read-only.

feat(): dedupe accepts a function #225

Merged
merged 2 commits into from
Jun 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export interface Options {
* to prevent bundling the same package multiple times if package is
* imported from dependencies.
*/
dedupe?: string[];
dedupe?: string[] | ((importee: string) => boolean);

/**
* Any additional options that should be passed through
Expand Down
6 changes: 5 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ export default function nodeResolve ( options = {} ) {
const extensions = options.extensions || DEFAULT_EXTS;
const packageInfoCache = new Map();

const shouldDedupe = typeof dedupe === 'function'
? dedupe
: importee => dedupe.includes(importee);

function getCachedPackageInfo (pkg, pkgPath) {
if (packageInfoCache.has(pkgPath)) {
return packageInfoCache.get(pkgPath);
Expand Down Expand Up @@ -187,7 +191,7 @@ export default function nodeResolve ( options = {} ) {

const basedir = importer ? dirname( importer ) : process.cwd();

if (dedupe.indexOf(importee) !== -1) {
if (shouldDedupe(importee)) {
importee = join(process.cwd(), 'node_modules', importee);
}

Expand Down
16 changes: 16 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,22 @@ describe( 'rollup-plugin-node-resolve', function () {
});
});

it( 'single module version is bundle if dedupe is set as a function', () => {
return rollup.rollup({
input: 'samples/react-app/main.js',
plugins: [
nodeResolve({
dedupe: (dep) => dep === 'react'
})
]
}).then( executeBundle ).then( module => {
assert.deepEqual(module.exports, {
React: 'react:root',
ReactConsumer: 'react-consumer:react:root'
});
});
});

it( 'multiple module versions are bundled if dedupe is not set', () => {
return rollup.rollup({
input: 'samples/react-app/main.js',
Expand Down