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

Commit

Permalink
Support resolve 1.11.1, add built-in test (#223)
Browse files Browse the repository at this point in the history
  • Loading branch information
bterlson authored and lukastaegert committed Jun 13, 2019
1 parent 9a47c45 commit 05b272e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
25 changes: 22 additions & 3 deletions src/index.js
Expand Up @@ -234,10 +234,29 @@ export default function nodeResolve ( options = {} ) {
resolveOptions.preserveSymlinks = preserveSymlinks;
}

const importeeIsBuiltin = builtins.has(importee);
const forceLocalLookup = importeeIsBuiltin && (!preferBuiltins || !isPreferBuiltinsSet);
let importSpecifier = importee;

if (forceLocalLookup) {
// need to attempt to look up a local module
importSpecifier += '/';
}

return resolveIdAsync(
importee,
importSpecifier,
Object.assign( resolveOptions, customResolveOptions )
)
.catch(err => {
if (forceLocalLookup && err.code === 'MODULE_NOT_FOUND') {
// didn't find a local module, so fall back to the importee
// (i.e. the builtin's name)
return importee;
}

// some other error, just forward it
throw err;
})
.then(resolved => {
if ( resolved && packageBrowserField ) {
if ( packageBrowserField.hasOwnProperty(resolved) ) {
Expand All @@ -255,9 +274,9 @@ export default function nodeResolve ( options = {} ) {
resolved = fs.realpathSync( resolved );
}

if ( builtins.has( resolved ) ) {
if (builtins.has(resolved) && preferBuiltins && isPreferBuiltinsSet) {
return null;
} else if ( builtins.has( importee ) && preferBuiltins ) {
} else if (importeeIsBuiltin && preferBuiltins) {
if ( !isPreferBuiltinsSet ) {
this.warn(
`preferring built-in module '${importee}' over local alternative ` +
Expand Down
22 changes: 20 additions & 2 deletions test/test.js
Expand Up @@ -29,11 +29,11 @@ function executeBundle ( bundle ) {
return bundle.generate({
format: 'cjs'
}).then( generated => {
const fn = new Function ( 'module', 'exports', 'assert', generated.output[0].code );
const fn = new Function ( 'module', 'exports', 'assert', 'require', generated.output[0].code );
const module = { exports: {} };

try {
fn(module, module.exports, assert);
fn(module, module.exports, assert, require);
} catch (error) {
// eslint-disable-next-line no-console
console.log(generated.output[0].code);
Expand Down Expand Up @@ -337,6 +337,24 @@ describe( 'rollup-plugin-node-resolve', function () {
});
});

it( 'warns when importing builtins', function () {
return rollup.rollup({
input: 'samples/builtins/main.js',
onwarn: expectWarnings([{
code: 'UNRESOLVED_IMPORT',
source: 'path'
}]),
plugins: [
nodeResolve({
mainFields: ['browser', 'main'],
preferBuiltins: true
})
]
}).then(executeBundle).then(module => {
assert.equal(module.exports, require('path').sep);
});
});

it( 'allows use of object browser field, resolving nested directories', function () {
return rollup.rollup({
input: 'samples/browser-object-nested/main.js',
Expand Down

0 comments on commit 05b272e

Please sign in to comment.