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

Commit df90657

Browse files
manucorporatlukastaegert
authored andcommittedJun 29, 2019
feat(): dedupe accepts a function (#225)
* feat(): dedupe accepts a function * Add test and shouldDedupe
1 parent 3a8ebf1 commit df90657

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed
 

‎index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export interface Options {
8585
* to prevent bundling the same package multiple times if package is
8686
* imported from dependencies.
8787
*/
88-
dedupe?: string[];
88+
dedupe?: string[] | ((importee: string) => boolean);
8989

9090
/**
9191
* Any additional options that should be passed through

‎src/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ export default function nodeResolve ( options = {} ) {
130130
const extensions = options.extensions || DEFAULT_EXTS;
131131
const packageInfoCache = new Map();
132132

133+
const shouldDedupe = typeof dedupe === 'function'
134+
? dedupe
135+
: importee => dedupe.includes(importee);
136+
133137
function getCachedPackageInfo (pkg, pkgPath) {
134138
if (packageInfoCache.has(pkgPath)) {
135139
return packageInfoCache.get(pkgPath);
@@ -216,7 +220,7 @@ export default function nodeResolve ( options = {} ) {
216220

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

219-
if (dedupe.indexOf(importee) !== -1) {
223+
if (shouldDedupe(importee)) {
220224
importee = join(process.cwd(), 'node_modules', importee);
221225
}
222226

‎test/test.js

+16
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,22 @@ describe( 'rollup-plugin-node-resolve', function () {
880880
});
881881
});
882882

883+
it( 'single module version is bundle if dedupe is set as a function', () => {
884+
return rollup.rollup({
885+
input: 'samples/react-app/main.js',
886+
plugins: [
887+
nodeResolve({
888+
dedupe: (dep) => dep === 'react'
889+
})
890+
]
891+
}).then( executeBundle ).then( module => {
892+
assert.deepEqual(module.exports, {
893+
React: 'react:root',
894+
ReactConsumer: 'react-consumer:react:root'
895+
});
896+
});
897+
});
898+
883899
it( 'multiple module versions are bundled if dedupe is not set', () => {
884900
return rollup.rollup({
885901
input: 'samples/react-app/main.js',

0 commit comments

Comments
 (0)
This repository has been archived.