From 5424b8a64d191599860b863b8b0cae253c1efc01 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Wed, 24 Mar 2021 07:54:42 +0000 Subject: [PATCH] fix: allow overriding entry point in browser field Some modules are [published](https://unpkg.com/cborg@1.1.2/package.json) with no `main` field in `package.json` and no `/index.js`, instead using `exports` to define the entry point for the module. They sometimes specify an entry point for the browser using a `"."` or `"./"` key in the `browser` field, but currently these are broken with browserify as it tries to resolve `/index.js` when no `main` field is present. The change here is to allow define the entry point for the module using the `browser` field: ```json { "browser": { ".": "path/to/index.js" } } ``` --- index.js | 4 +++- .../entry-point-overridden-in-browser-field/foo/bar.js | 0 .../entry-point-overridden-in-browser-field/package.json | 5 +++++ test/modules-sync.js | 5 +++++ test/modules.js | 8 ++++++++ 5 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/node_modules/entry-point-overridden-in-browser-field/foo/bar.js create mode 100644 test/fixtures/node_modules/entry-point-overridden-in-browser-field/package.json diff --git a/index.js b/index.js index 8a1908a..b0de81f 100644 --- a/index.js +++ b/index.js @@ -169,7 +169,9 @@ function build_resolve_opts(opts, base) { } var replace_main = replacements[info.main || './index.js'] || - replacements['./' + info.main || './index.js']; + replacements['./' + info.main || './index.js'] || + replacements['.'] || + replacements['./']; info.main = replace_main || info.main; return info; diff --git a/test/fixtures/node_modules/entry-point-overridden-in-browser-field/foo/bar.js b/test/fixtures/node_modules/entry-point-overridden-in-browser-field/foo/bar.js new file mode 100644 index 0000000..e69de29 diff --git a/test/fixtures/node_modules/entry-point-overridden-in-browser-field/package.json b/test/fixtures/node_modules/entry-point-overridden-in-browser-field/package.json new file mode 100644 index 0000000..798dbaa --- /dev/null +++ b/test/fixtures/node_modules/entry-point-overridden-in-browser-field/package.json @@ -0,0 +1,5 @@ +{ + "browser": { + ".": "./foo/bar.js" + } +} diff --git a/test/modules-sync.js b/test/modules-sync.js index 4ae3b64..a2aea1d 100644 --- a/test/modules-sync.js +++ b/test/modules-sync.js @@ -158,3 +158,8 @@ test('alt-browser field', function() { var path = resolve.sync('url', parent); assert.equal(path, require.resolve('./fixtures/node_modules/alt-browser-field/url-chromeapp')); }); + +test('allow overriding entry point in browser field', function() { + var path = resolve.sync('entry-point-overridden-in-browser-field', { paths: [ fixtures_dir ], package: { main: 'fixtures' } }); + assert.equal(path, require.resolve('./fixtures/node_modules/entry-point-overridden-in-browser-field/foo/bar.js')); +}); diff --git a/test/modules.js b/test/modules.js index ddf26d2..fc6687d 100644 --- a/test/modules.js +++ b/test/modules.js @@ -338,3 +338,11 @@ test('respect symlinks', function (done) { done(); }); }); + +test('allow overriding entry point in browser field', function (done) { + resolve('entry-point-overridden-in-browser-field', { paths: [ fixtures_dir ] }, function(err, path, pkg) { + assert.ifError(err); + assert.equal(path, require.resolve('./fixtures/node_modules/entry-point-overridden-in-browser-field/foo/bar.js')); + done(); + }); +});