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

Commit 86905f4

Browse files
sormylukastaegert
authored andcommitted
Added dedupe option to prevent bundling the same package multiple times (#201)
1 parent 68f80df commit 86905f4

File tree

10 files changed

+57
-1
lines changed

10 files changed

+57
-1
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ node_modules
33
dist
44
.gobble*
55
!test/node_modules
6+
!test/node_modules/react-consumer/node_modules

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ export default {
7474
// ES2015 modules
7575
modulesOnly: true, // Default: false
7676

77+
// Force resolving for these modules to root's node_modules that helps
78+
// to prevent bundling the same package multiple times if package is
79+
// imported from dependencies.
80+
dedupe: [ 'react', 'react-dom' ], // Default: []
81+
7782
// Any additional options that should be passed through
7883
// to node-resolve
7984
customResolveOptions: {

index.d.ts

+6
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ interface RollupNodeResolveOptions {
7070
* @default false
7171
*/
7272
modulesOnly?: boolean;
73+
/**
74+
* Force resolving for these modules to root's node_modules that helps
75+
* to prevent bundling the same package multiple times if package is
76+
* imported from dependencies.
77+
*/
78+
dedupe?: string[];
7379
/**
7480
* Any additional options that should be passed through
7581
* to node-resolve

src/index.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {dirname, extname, normalize, resolve, sep} from 'path';
1+
import {dirname, extname, normalize, resolve, sep, join} from 'path';
22
import builtins from 'builtin-modules';
33
import resolveId from 'resolve';
44
import isModule from 'is-module';
@@ -72,6 +72,7 @@ const resolveIdAsync = (file, opts) => new Promise((fulfil, reject) => resolveId
7272
export default function nodeResolve ( options = {} ) {
7373
const mainFields = getMainFields(options);
7474
const useBrowserOverrides = mainFields.indexOf('browser') !== -1;
75+
const dedupe = options.dedupe || [];
7576
const isPreferBuiltinsSet = options.preferBuiltins === true || options.preferBuiltins === false;
7677
const preferBuiltins = isPreferBuiltinsSet ? options.preferBuiltins : true;
7778
const customResolveOptions = options.customResolveOptions || {};
@@ -107,6 +108,10 @@ export default function nodeResolve ( options = {} ) {
107108

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

111+
if (dedupe.indexOf(importee) !== -1) {
112+
importee = join(process.cwd(), 'node_modules', importee);
113+
}
114+
110115
// https://github.com/defunctzombie/package-browser-field-spec
111116
if (useBrowserOverrides && browserMapCache[importer]) {
112117
const resolvedImportee = resolve( basedir, importee );

test/node_modules/react-consumer/index.js

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/node_modules/react-consumer/node_modules/.gitkeep

Whitespace-only changes.

test/node_modules/react-consumer/node_modules/react/index.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/node_modules/react/index.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/samples/react-app/main.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import React from 'react'
2+
import ReactConsumer from 'react-consumer'
3+
4+
export { React, ReactConsumer }

test/test.js

+30
Original file line numberDiff line numberDiff line change
@@ -777,4 +777,34 @@ describe( 'rollup-plugin-node-resolve', function () {
777777
});
778778
});
779779

780+
it( 'single module version is bundle if dedupe is set', () => {
781+
return rollup.rollup({
782+
input: 'samples/react-app/main.js',
783+
plugins: [
784+
nodeResolve({
785+
dedupe: [ 'react' ]
786+
})
787+
]
788+
}).then( executeBundle ).then( module => {
789+
assert.deepEqual(module.exports, {
790+
React: 'react:root',
791+
ReactConsumer: 'react-consumer:react:root'
792+
});
793+
});
794+
});
795+
796+
it( 'multiple module versions are bundled if dedupe is not set', () => {
797+
return rollup.rollup({
798+
input: 'samples/react-app/main.js',
799+
plugins: [
800+
nodeResolve()
801+
]
802+
}).then( executeBundle ).then( module => {
803+
assert.deepEqual(module.exports, {
804+
React: 'react:root',
805+
ReactConsumer: 'react-consumer:react:child'
806+
});
807+
});
808+
});
809+
780810
});

0 commit comments

Comments
 (0)