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

Commit 05b272e

Browse files
bterlsonlukastaegert
authored andcommittedJun 13, 2019
Support resolve 1.11.1, add built-in test (#223)
1 parent 9a47c45 commit 05b272e

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed
 

‎src/index.js

+22-3
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,29 @@ export default function nodeResolve ( options = {} ) {
234234
resolveOptions.preserveSymlinks = preserveSymlinks;
235235
}
236236

237+
const importeeIsBuiltin = builtins.has(importee);
238+
const forceLocalLookup = importeeIsBuiltin && (!preferBuiltins || !isPreferBuiltinsSet);
239+
let importSpecifier = importee;
240+
241+
if (forceLocalLookup) {
242+
// need to attempt to look up a local module
243+
importSpecifier += '/';
244+
}
245+
237246
return resolveIdAsync(
238-
importee,
247+
importSpecifier,
239248
Object.assign( resolveOptions, customResolveOptions )
240249
)
250+
.catch(err => {
251+
if (forceLocalLookup && err.code === 'MODULE_NOT_FOUND') {
252+
// didn't find a local module, so fall back to the importee
253+
// (i.e. the builtin's name)
254+
return importee;
255+
}
256+
257+
// some other error, just forward it
258+
throw err;
259+
})
241260
.then(resolved => {
242261
if ( resolved && packageBrowserField ) {
243262
if ( packageBrowserField.hasOwnProperty(resolved) ) {
@@ -255,9 +274,9 @@ export default function nodeResolve ( options = {} ) {
255274
resolved = fs.realpathSync( resolved );
256275
}
257276

258-
if ( builtins.has( resolved ) ) {
277+
if (builtins.has(resolved) && preferBuiltins && isPreferBuiltinsSet) {
259278
return null;
260-
} else if ( builtins.has( importee ) && preferBuiltins ) {
279+
} else if (importeeIsBuiltin && preferBuiltins) {
261280
if ( !isPreferBuiltinsSet ) {
262281
this.warn(
263282
`preferring built-in module '${importee}' over local alternative ` +

‎test/test.js

+20-2
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ function executeBundle ( bundle ) {
2929
return bundle.generate({
3030
format: 'cjs'
3131
}).then( generated => {
32-
const fn = new Function ( 'module', 'exports', 'assert', generated.output[0].code );
32+
const fn = new Function ( 'module', 'exports', 'assert', 'require', generated.output[0].code );
3333
const module = { exports: {} };
3434

3535
try {
36-
fn(module, module.exports, assert);
36+
fn(module, module.exports, assert, require);
3737
} catch (error) {
3838
// eslint-disable-next-line no-console
3939
console.log(generated.output[0].code);
@@ -337,6 +337,24 @@ describe( 'rollup-plugin-node-resolve', function () {
337337
});
338338
});
339339

340+
it( 'warns when importing builtins', function () {
341+
return rollup.rollup({
342+
input: 'samples/builtins/main.js',
343+
onwarn: expectWarnings([{
344+
code: 'UNRESOLVED_IMPORT',
345+
source: 'path'
346+
}]),
347+
plugins: [
348+
nodeResolve({
349+
mainFields: ['browser', 'main'],
350+
preferBuiltins: true
351+
})
352+
]
353+
}).then(executeBundle).then(module => {
354+
assert.equal(module.exports, require('path').sep);
355+
});
356+
});
357+
340358
it( 'allows use of object browser field, resolving nested directories', function () {
341359
return rollup.rollup({
342360
input: 'samples/browser-object-nested/main.js',

0 commit comments

Comments
 (0)
This repository has been archived.