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

Commit 26e5a74

Browse files
committed
Merge pull request #28 from rollup/prefer-builtin
Prefer built-in modules but allow opting out.
2 parents bc31e65 + 5c672c4 commit 26e5a74

File tree

5 files changed

+76
-5
lines changed

5 files changed

+76
-5
lines changed

src/index.js

+20-4
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@ import browserResolve from 'browser-resolve';
55

66
const COMMONJS_BROWSER_EMPTY = _nodeResolve.sync( 'browser-resolve/empty.js', __dirname );
77
const ES6_BROWSER_EMPTY = resolve( __dirname, '../src/empty.js' );
8+
const CONSOLE_WARN = ( ...args ) => console.warn( ...args );
89

910
export default function nodeResolve ( options ) {
1011
options = options || {};
1112

1213
const skip = options.skip || [];
1314
const useMain = options.main !== false;
15+
const isPreferBuiltinsSet = options.preferBuiltins === true || options.preferBuiltins === false;
16+
const preferBuiltins = isPreferBuiltinsSet ? options.preferBuiltins : true;
1417

18+
const onwarn = options.onwarn || CONSOLE_WARN;
1519
const resolveId = options.browser ? browserResolve : _nodeResolve;
1620

1721
return {
@@ -56,10 +60,22 @@ export default function nodeResolve ( options ) {
5660
if ( skip === true ) accept( false );
5761
else reject( err );
5862
} else {
59-
if ( resolved === COMMONJS_BROWSER_EMPTY ) resolved = ES6_BROWSER_EMPTY;
60-
if ( ~builtins.indexOf( resolved ) ) resolved = null;
61-
62-
accept( resolved );
63+
if ( resolved === COMMONJS_BROWSER_EMPTY ) {
64+
accept( ES6_BROWSER_EMPTY );
65+
} else if ( ~builtins.indexOf( resolved ) ) {
66+
accept( null );
67+
} else if ( ~builtins.indexOf( importee ) && preferBuiltins ) {
68+
if ( !isPreferBuiltinsSet ) {
69+
onwarn(
70+
`preferring built-in module '${importee}' over local alternative ` +
71+
`at '${resolved}', pass 'preferBuiltins: false' to disable this ` +
72+
`behavior or 'preferBuiltins: true' to disable this warning`
73+
);
74+
}
75+
accept( null );
76+
} else {
77+
accept( resolved );
78+
}
6379
}
6480
}
6581
);

test/node_modules/events/index.js

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

test/node_modules/events/package.json

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

test/samples/prefer-builtin/main.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import 'events';

test/test.js

+51-1
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,57 @@ describe( 'rollup-plugin-node-resolve', function () {
237237
})
238238
]
239239
}).then( bundle => {
240-
assert.deepEqual( bundle.imports.sort(), [ 'jsnext', 'legacy', 'missing' ]);
240+
assert.deepEqual( bundle.imports.sort(), [ 'jsnext', 'legacy', 'missing' ] );
241+
});
242+
});
243+
244+
it( 'preferBuiltins: true allows preferring a builtin to a local module of the same name', () => {
245+
return rollup.rollup({
246+
entry: 'samples/prefer-builtin/main.js',
247+
plugins: [
248+
nodeResolve({
249+
preferBuiltins: true
250+
})
251+
]
252+
}).then( bundle => {
253+
assert.deepEqual( bundle.imports.sort(), [ 'events' ] );
254+
});
255+
});
256+
257+
it( 'preferBuiltins: false allows resolving a local module with the same name as a builtin module', () => {
258+
return rollup.rollup({
259+
entry: 'samples/prefer-builtin/main.js',
260+
plugins: [
261+
nodeResolve({
262+
preferBuiltins: false
263+
})
264+
]
265+
}).then( bundle => {
266+
assert.deepEqual( bundle.imports.sort(), [] );
267+
});
268+
});
269+
270+
it( 'issues a warning when preferring a builtin module without having explicit configuration', () => {
271+
let warning = null;
272+
return rollup.rollup({
273+
entry: 'samples/prefer-builtin/main.js',
274+
plugins: [
275+
nodeResolve({
276+
onwarn( message ) {
277+
if ( ~message.indexOf( 'prefer' ) ) {
278+
warning = message;
279+
}
280+
}
281+
})
282+
]
283+
}).then( () => {
284+
let localPath = path.join(__dirname, 'node_modules/events/index.js');
285+
assert.strictEqual(
286+
warning,
287+
`preferring built-in module 'events' over local alternative ` +
288+
`at '${localPath}', pass 'preferBuiltins: false' to disable this behavior ` +
289+
`or 'preferBuiltins: true' to disable this warning`
290+
);
241291
});
242292
});
243293

0 commit comments

Comments
 (0)