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

refactor: remove browser-resolve dependency, inline logic #127

Merged
merged 1 commit into from
Jan 10, 2018

Conversation

keithamus
Copy link
Contributor

This PR removes browser-resolve as a dependency, inlining the logic steps it makes. It does this for a few reasons:

  1. browser-resolve is very ineficient on disk, it does lots of lookups using its own internal mechanisms where it could rely on resolve more.
  2. Similar to point 1 - browser-resolve duplicates a lot of code from resolve. Dropping this dependency means shipping less code.
  3. While I could have made a PR to browser-resolve to fix the above issues - doing so would reduce browser-resolve down to a few LOC, and I'm not sure it is under active maintainence. There are open PRs from a few years ago. I figured it would be best to move the few LOC it needs into this module.

/cc @mislav who has been working with me on this today.

if (browser[importee] === false) {
return ES6_BROWSER_EMPTY;
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the crux of the replacement logic with browser-resolve. Given a map from an importer - resolve to the values of that key. Here we can also take advantage of returning ES6_BROWSER_EMPTY as early as possible - as we don't need to resolbe that beyond what it is doing.

if ( !disregardResult && !err ) {
if ( resolved && fs.existsSync( resolved ) ) {
resolved = fs.realpathSync( resolved );
}

if ( resolved === COMMONJS_BROWSER_EMPTY ) {
fulfil( ES6_BROWSER_EMPTY );
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We no longer need this line as we're not relying on browser-resolves empty file.

if (key[0] === '.' && !extname(key)) browser[ key + '.js'] = browser[ key + '.json' ] = browser[ key ];
return browser;
}, {});
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a convenient side-effect-ful mechanism of the packageFilter function. We know that if we land inside a package.json file, that the resolved importee has this mapping inside of its own package.json. Therefore, if we collect these browser maps inside of a cache (browserMapCache) then by the time we get to the importee that owns this package.json being the importer, all of it's own dependencies can rely on this cache.

In other words - it is this operation that collects for us, the mapping to which files are replaced in a browser based environment.

if (options.browser && typeof pkg[ 'browser' ] === 'string') {
pkg[ 'main' ] = pkg[ 'browser' ];
} else if (options.browser && pkg[ 'browser' ][ pkg[ 'main' ] ]) {
pkg[ 'main' ] = pkg[ 'browser' ][ pkg[ 'main' ] ];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the second key part of the browser key logic - which is that if we hit a module, it's effective main field is either browser (if it is a string) or browser[pkg.main] (if it is an object). Luckily we can use packageFilter exactly how we already are to acheive this.

@keithamus
Copy link
Contributor Author

Tests are failing because Node 6 timed out. https://travis-ci.org/rollup/rollup-plugin-node-resolve/jobs/326432217. Nodes 4 and 9 pass, so I'm sure a rebuild of 6 will pass too.

@mislav
Copy link
Contributor

mislav commented Jan 9, 2018

Really happy with how this turned out. 👍 Thanks for the hard work @keithamus!

@Rich-Harris Rich-Harris merged commit f9ca240 into rollup:master Jan 10, 2018
@Rich-Harris
Copy link
Contributor

Thank you! Released 3.0.1

@keithamus keithamus deleted the remove-browser-resolve branch January 10, 2018 17:56
@Haroenv
Copy link

Haroenv commented Jan 10, 2018

Hey there, just letting you know that this broke our build, so it might have been different to the original implementation for the browser field resolver:

https://travis-ci.org/algolia/react-instantsearch/builds/327346207#L685-L691

/home/travis/build/algolia/react-instantsearch/node_modules/rollup-plugin-node-resolve/dist/rollup-plugin-node-resolve.cjs.js:91
							} else if (options.browser && pkg[ 'browser' ][ pkg[ 'main' ] ]) {
                                                          ^
TypeError: Cannot read property 'index.js' of undefined
    at Object.packageFilter (/home/travis/build/algolia/react-instantsearch/node_modules/rollup-plugin-node-resolve/dist/rollup-plugin-node-resolve.cjs.js:91:59)
    at /home/travis/build/algolia/react-instantsearch/node_modules/resolve/lib/async.js:127:32
    at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:505:3)

@keithamus
Copy link
Contributor Author

I'll get a fix for this.

@keithamus
Copy link
Contributor Author

@Haroenv this is now fixed in 3.0.2, thanks to @Rich-Harris 😄

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants