Skip to content
This repository has been 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 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
9 changes: 8 additions & 1 deletion src/index.js
Expand Up @@ -100,6 +100,13 @@ export default function nodeResolve ( options = {} ) {
const extensions = options.extensions || DEFAULT_EXTS;
const packageInfoCache = new Map();

function shouldDedupe (importee) {
manucorporat marked this conversation as resolved.
Show resolved Hide resolved
if (typeof dedupe === 'function') {
return !!dedupe(importee);
}
return dedupe.indexOf(importee) !== -1;
}

function getCachedPackageInfo (pkg, pkgPath) {
if (packageInfoCache.has(pkgPath)) {
return packageInfoCache.get(pkgPath);
Expand Down Expand Up @@ -187,7 +194,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