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

Commit d041e6b

Browse files
author
Artem Butusov
committed
Added dedupe option to prevent bundling the same package multiple times
1 parent f8dfa57 commit d041e6b

File tree

6 files changed

+49
-1
lines changed

6 files changed

+49
-1
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ export default {
6565
// ES2015 modules
6666
modulesOnly: true, // Default: false
6767

68+
// Force resolving for these modules to root's node_modules that helps
69+
// to prevent bundling the same package multiple times if package is
70+
// imported from dependencies.
71+
dedupe: [ 'react', 'react-dom' ], // Default: []
72+
6873
// Any additional options that should be passed through
6974
// to node-resolve
7075
customResolveOptions: {

src/index.js

Lines changed: 6 additions & 1 deletion
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';
@@ -40,6 +40,7 @@ function cachedIsFile (file, cb) {
4040
const resolveIdAsync = (file, opts) => new Promise((fulfil, reject) => resolveId(file, opts, (err, contents) => err ? reject(err) : fulfil(contents)));
4141

4242
export default function nodeResolve ( options = {} ) {
43+
const dedupe = options.dedupe || [];
4344
const useModule = options.module !== false;
4445
const useMain = options.main !== false;
4546
const useJsnext = options.jsnext === true;
@@ -82,6 +83,10 @@ export default function nodeResolve ( options = {} ) {
8283

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

86+
if (dedupe.indexOf(importee) !== -1) {
87+
importee = join(process.cwd(), 'node_modules', importee);
88+
}
89+
8590
// https://github.com/defunctzombie/package-browser-field-spec
8691
if (options.browser && browserMapCache[importer]) {
8792
const resolvedImportee = resolve( basedir, importee );

test/node_modules/react-consumer/index.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/node_modules/react/index.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/samples/react-app/main.js

Lines changed: 4 additions & 0 deletions
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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,4 +761,34 @@ describe( 'rollup-plugin-node-resolve', function () {
761761
});
762762
});
763763

764+
it( 'single module version is bundle if dedupe is set', () => {
765+
return rollup.rollup({
766+
input: 'samples/react-app/main.js',
767+
plugins: [
768+
nodeResolve({
769+
dedupe: [ 'react' ]
770+
})
771+
]
772+
}).then( executeBundle ).then( module => {
773+
assert.deepEqual(module.exports, {
774+
React: 'react:root',
775+
ReactConsumer: 'react-consumer:react:root'
776+
});
777+
});
778+
});
779+
780+
it( 'multiple module versions are bundled if dedupe is not set', () => {
781+
return rollup.rollup({
782+
input: 'samples/react-app/main.js',
783+
plugins: [
784+
nodeResolve()
785+
]
786+
}).then( executeBundle ).then( module => {
787+
assert.deepEqual(module.exports, {
788+
React: 'react:root',
789+
ReactConsumer: 'react-consumer:react:child'
790+
});
791+
});
792+
});
793+
764794
});

0 commit comments

Comments
 (0)