browserify / browser-resolve

resolve function which support the browser field in package.json
MIT License
102 stars 70 forks source link

Resolve fails for requires without file extension #69

Closed src-code closed 8 years ago

src-code commented 8 years ago

Ran into an issue recently running module-deps, which depends on browser-resolve, where a package which has a dependency with a browser field defined failed to resolve browser assets because the require() call omits the filename extension.

For example, package asap v2.0.3 defines the following browser field in package.json:

"browser": {
    "./asap.js": "./browser-asap.js",
    "./raw.js": "./browser-raw.js",
    "./test/domain.js": "./test/browser-domain.js"
  },

And in a module such as promise, you'll find a require() such as:

var asap = require('asap/raw')

This fails to resolve because browser-resolve doesn't seem to handle the missing extension when looking up the file in the browser field. Adding the .js extension to the require() call fixes the issue.

Here's a simple test case calling resolve() directly:

var resolve = require('browser-resolve');

resolve('asap/raw', {}, function (err, path) {
    console.log('asap/raw -> ', path);
});

resolve('asap/raw.js', {}, function (err, path) {
    console.log('asap/raw.js -> ', path);
});

Output:

asap/raw.js ->  /Users/steve/git/browser-resolve-bug/node_modules/asap/browser-raw.js
asap/raw ->  /Users/steve/git/browser-resolve-bug/node_modules/asap/raw.js

It'd be great if browser-resolve could resolve the missing extension automatically, otherwise tools like module-deps won't work with packages using shortened filenames.

defunctzombie commented 8 years ago

Great write up! Would you be willing to make a PR for this?

src-code commented 8 years ago

@defunctzombie Sure, I can take a stab at it... not sure how quickly though!