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

Commit

Permalink
feat(): dedupe accepts a function (#225)
Browse files Browse the repository at this point in the history
* feat(): dedupe accepts a function

* Add test and shouldDedupe
  • Loading branch information
manucorporat authored and lukastaegert committed Jun 29, 2019
1 parent 3a8ebf1 commit df90657
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
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
Expand Up @@ -130,6 +130,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 @@ -216,7 +220,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
Expand Up @@ -880,6 +880,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

0 comments on commit df90657

Please sign in to comment.