From 9ce01d4c45606db04d1d6c7213f9dad7ba63551c Mon Sep 17 00:00:00 2001 From: Allex Date: Fri, 22 Feb 2019 14:42:18 +0800 Subject: [PATCH] Fix pkg.browser mappings issue by specifying a value of `false` (#183) * Fix pkg.browser mappings issue by specifying a value of `false` * Add tests for #183 --- src/index.js | 17 ++++++++++------ .../lib/client/http-tracker.js | 4 ++++ .../lib/client/udp-tracker.js | 4 ++++ .../lib/client/websocket-tracker.js | 4 ++++ .../isomorphic-object-with-false/lib/index.js | 20 +++++++++++++++++++ .../lib/subpath/foo/index.js | 2 ++ .../isomorphic-object-with-false/package.json | 8 ++++++++ .../samples/browser-object-with-false/main.js | 15 ++++++++++++++ test/test.js | 13 ++++++++++++ 9 files changed, 81 insertions(+), 6 deletions(-) create mode 100644 test/node_modules/isomorphic-object-with-false/lib/client/http-tracker.js create mode 100644 test/node_modules/isomorphic-object-with-false/lib/client/udp-tracker.js create mode 100644 test/node_modules/isomorphic-object-with-false/lib/client/websocket-tracker.js create mode 100644 test/node_modules/isomorphic-object-with-false/lib/index.js create mode 100644 test/node_modules/isomorphic-object-with-false/lib/subpath/foo/index.js create mode 100644 test/node_modules/isomorphic-object-with-false/package.json create mode 100644 test/samples/browser-object-with-false/main.js diff --git a/src/index.js b/src/index.js index 6b59116..47c83e3 100644 --- a/src/index.js +++ b/src/index.js @@ -82,6 +82,7 @@ export default function nodeResolve ( options = {} ) { const basedir = importer ? dirname( importer ) : process.cwd(); + // https://github.com/defunctzombie/package-browser-field-spec if (options.browser && browserMapCache[importer]) { const resolvedImportee = resolve( basedir, importee ); const browser = browserMapCache[importer]; @@ -157,16 +158,19 @@ export default function nodeResolve ( options = {} ) { importee, Object.assign( resolveOptions, customResolveOptions ) ) - .catch(() => false) .then(resolved => { - if (options.browser && packageBrowserField) { - if (packageBrowserField[ resolved ]) { + if ( resolved && options.browser && packageBrowserField ) { + if ( packageBrowserField.hasOwnProperty(resolved) ) { + if (!packageBrowserField[resolved]) { + browserMapCache[resolved] = packageBrowserField; + return ES6_BROWSER_EMPTY; + } resolved = packageBrowserField[ resolved ]; } browserMapCache[resolved] = packageBrowserField; } - if ( !disregardResult && resolved !== false ) { + if ( !disregardResult ) { if ( !preserveSymlinks && resolved && fs.existsSync( resolved ) ) { resolved = fs.realpathSync( resolved ); } @@ -190,9 +194,10 @@ export default function nodeResolve ( options = {} ) { if ( resolved && options.modulesOnly ) { return readFileAsync( resolved, 'utf-8').then(code => isModule( code ) ? resolved : null); } else { - return resolved === false ? null : resolved; + return resolved; } - }); + }) + .catch(() => null); } }; } diff --git a/test/node_modules/isomorphic-object-with-false/lib/client/http-tracker.js b/test/node_modules/isomorphic-object-with-false/lib/client/http-tracker.js new file mode 100644 index 0000000..75e80bd --- /dev/null +++ b/test/node_modules/isomorphic-object-with-false/lib/client/http-tracker.js @@ -0,0 +1,4 @@ + +module.exports = function(client, announceUrl) { + this.name = 'http-tracker' +} diff --git a/test/node_modules/isomorphic-object-with-false/lib/client/udp-tracker.js b/test/node_modules/isomorphic-object-with-false/lib/client/udp-tracker.js new file mode 100644 index 0000000..bb2c98b --- /dev/null +++ b/test/node_modules/isomorphic-object-with-false/lib/client/udp-tracker.js @@ -0,0 +1,4 @@ + +module.exports = function(client, announceUrl) { + this.name = 'udp-tracker' +} diff --git a/test/node_modules/isomorphic-object-with-false/lib/client/websocket-tracker.js b/test/node_modules/isomorphic-object-with-false/lib/client/websocket-tracker.js new file mode 100644 index 0000000..c7a38e4 --- /dev/null +++ b/test/node_modules/isomorphic-object-with-false/lib/client/websocket-tracker.js @@ -0,0 +1,4 @@ + +module.exports = function(client, announceUrl) { + this.name = 'websocket-tracker' +} diff --git a/test/node_modules/isomorphic-object-with-false/lib/index.js b/test/node_modules/isomorphic-object-with-false/lib/index.js new file mode 100644 index 0000000..073ab9f --- /dev/null +++ b/test/node_modules/isomorphic-object-with-false/lib/index.js @@ -0,0 +1,20 @@ +// sample code inspired by npm: bittorrent-tracker-client + +var HTTPTracker = require('./client/http-tracker') // empty object in browser +var UDPTracker = require('./client/udp-tracker') // empty object in browser +var WebSocketTracker = require('./client/websocket-tracker') + +function Client(protocol, announceUrl) { + var self = this; + if ((protocol === 'http:' || protocol === 'https:') && + typeof HTTPTracker === 'function') { + return new HTTPTracker(self, announceUrl) + } else if (protocol === 'udp:' && typeof UDPTracker === 'function') { + return new UDPTracker(self, announceUrl) + } else if ((protocol === 'ws:' || protocol === 'wss:')) { + return new WebSocketTracker(self, announceUrl) + } + this.name = 'NULL'; +} + +module.exports = Client diff --git a/test/node_modules/isomorphic-object-with-false/lib/subpath/foo/index.js b/test/node_modules/isomorphic-object-with-false/lib/subpath/foo/index.js new file mode 100644 index 0000000..082f03b --- /dev/null +++ b/test/node_modules/isomorphic-object-with-false/lib/subpath/foo/index.js @@ -0,0 +1,2 @@ +var HTTPTracker = require('../../client/http-tracker') // empty object in browser +module.exports = HTTPTracker diff --git a/test/node_modules/isomorphic-object-with-false/package.json b/test/node_modules/isomorphic-object-with-false/package.json new file mode 100644 index 0000000..6b9be50 --- /dev/null +++ b/test/node_modules/isomorphic-object-with-false/package.json @@ -0,0 +1,8 @@ +{ + "main": "./lib/index.js", + "browser": { + "./lib/common.js": "./lib/common-browser.js", + "./lib/client/http-tracker.js": false, + "./lib/client/udp-tracker.js": false + } +} diff --git a/test/samples/browser-object-with-false/main.js b/test/samples/browser-object-with-false/main.js new file mode 100644 index 0000000..4897457 --- /dev/null +++ b/test/samples/browser-object-with-false/main.js @@ -0,0 +1,15 @@ +import Client from 'isomorphic-object-with-false'; +import HTTPTracker from 'isomorphic-object-with-false/lib/client/http-tracker'; +import ES6_BROWSER_EMPTY from '../../../src/empty'; +import HTTPTrackerWithSubPath from 'isomorphic-object-with-false/lib/subpath/foo'; + +// do some assert + +assert.deepEqual(new Client('ws:'), { name: 'websocket-tracker' }) +assert.deepEqual(new Client('http:'), { name: 'NULL' }) +assert.equal(HTTPTracker, ES6_BROWSER_EMPTY); +assert.equal(HTTPTrackerWithSubPath, ES6_BROWSER_EMPTY); + + +// expose +export default "ok" diff --git a/test/test.js b/test/test.js index fbf6c2f..815d677 100644 --- a/test/test.js +++ b/test/test.js @@ -657,4 +657,17 @@ describe( 'rollup-plugin-node-resolve', function () { }).then(executeBundle) .then(({exports}) => exports.then(result => assert.equal(result.default, 42))); }); + + it( 'pkg.browser with mapping to prevent bundle by specifying a value of false', () => { + return rollup.rollup({ + input: 'samples/browser-object-with-false/main.js', + plugins: [ + nodeResolve({ browser: true }), + commonjs() + ] + }).then( executeBundle ).then( module => { + assert.equal( module.exports, 'ok' ); + }); + }); + });