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

Commit

Permalink
Added dedupe option to prevent bundling the same package multiple tim…
Browse files Browse the repository at this point in the history
…es (#201)
  • Loading branch information
sormy authored and lukastaegert committed Apr 6, 2019
1 parent 68f80df commit 86905f4
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -3,3 +3,4 @@ node_modules
dist
.gobble*
!test/node_modules
!test/node_modules/react-consumer/node_modules
5 changes: 5 additions & 0 deletions README.md
Expand Up @@ -74,6 +74,11 @@ export default {
// ES2015 modules
modulesOnly: true, // Default: false

// Force resolving for these modules to root's node_modules that helps
// to prevent bundling the same package multiple times if package is
// imported from dependencies.
dedupe: [ 'react', 'react-dom' ], // Default: []

// Any additional options that should be passed through
// to node-resolve
customResolveOptions: {
Expand Down
6 changes: 6 additions & 0 deletions index.d.ts
Expand Up @@ -70,6 +70,12 @@ interface RollupNodeResolveOptions {
* @default false
*/
modulesOnly?: boolean;
/**
* Force resolving for these modules to root's node_modules that helps
* to prevent bundling the same package multiple times if package is
* imported from dependencies.
*/
dedupe?: string[];
/**
* Any additional options that should be passed through
* to node-resolve
Expand Down
7 changes: 6 additions & 1 deletion src/index.js
@@ -1,4 +1,4 @@
import {dirname, extname, normalize, resolve, sep} from 'path';
import {dirname, extname, normalize, resolve, sep, join} from 'path';
import builtins from 'builtin-modules';
import resolveId from 'resolve';
import isModule from 'is-module';
Expand Down Expand Up @@ -72,6 +72,7 @@ const resolveIdAsync = (file, opts) => new Promise((fulfil, reject) => resolveId
export default function nodeResolve ( options = {} ) {
const mainFields = getMainFields(options);
const useBrowserOverrides = mainFields.indexOf('browser') !== -1;
const dedupe = options.dedupe || [];
const isPreferBuiltinsSet = options.preferBuiltins === true || options.preferBuiltins === false;
const preferBuiltins = isPreferBuiltinsSet ? options.preferBuiltins : true;
const customResolveOptions = options.customResolveOptions || {};
Expand Down Expand Up @@ -107,6 +108,10 @@ export default function nodeResolve ( options = {} ) {

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

if (dedupe.indexOf(importee) !== -1) {
importee = join(process.cwd(), 'node_modules', importee);
}

// https://github.com/defunctzombie/package-browser-field-spec
if (useBrowserOverrides && browserMapCache[importer]) {
const resolvedImportee = resolve( basedir, importee );
Expand Down
3 changes: 3 additions & 0 deletions test/node_modules/react-consumer/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/node_modules/react/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions test/samples/react-app/main.js
@@ -0,0 +1,4 @@
import React from 'react'
import ReactConsumer from 'react-consumer'

export { React, ReactConsumer }
30 changes: 30 additions & 0 deletions test/test.js
Expand Up @@ -777,4 +777,34 @@ describe( 'rollup-plugin-node-resolve', function () {
});
});

it( 'single module version is bundle if dedupe is set', () => {
return rollup.rollup({
input: 'samples/react-app/main.js',
plugins: [
nodeResolve({
dedupe: [ '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',
plugins: [
nodeResolve()
]
}).then( executeBundle ).then( module => {
assert.deepEqual(module.exports, {
React: 'react:root',
ReactConsumer: 'react-consumer:react:child'
});
});
});

});

0 comments on commit 86905f4

Please sign in to comment.