From f2fc02577d8f136c9b87de470814864efe41420c Mon Sep 17 00:00:00 2001 From: Chunpeng Huo Date: Wed, 16 Sep 2020 12:13:32 +1000 Subject: [PATCH] fix(cli-bundler): correctly handle browser "." mapping --- lib/build/dependency-description.js | 2 ++ lib/build/utils.js | 3 +++ spec/lib/build/dependency-description.spec.js | 12 ++++++++++ .../lib/build/{util.spec.js => utils.spec.js} | 24 +++++++++++++++++++ 4 files changed, 41 insertions(+) rename spec/lib/build/{util.spec.js => utils.spec.js} (85%) diff --git a/lib/build/dependency-description.js b/lib/build/dependency-description.js index 5315c0fb5..76258cd13 100644 --- a/lib/build/dependency-description.js +++ b/lib/build/dependency-description.js @@ -51,6 +51,8 @@ exports.DependencyDescription = class { for (let i = 0, keys = Object.keys(browser); i < keys.length; i++) { let key = keys[i]; + // leave {".": "dist/index.js"} for main replacement + if (key === '.') continue; let target = browser[key]; let sourceModule = filePathToModuleId(key); diff --git a/lib/build/utils.js b/lib/build/utils.js index 97be1588c..b697e5500 100644 --- a/lib/build/utils.js +++ b/lib/build/utils.js @@ -191,6 +191,9 @@ function nodejsLoadAsDirectory(resourcePath) { if (typeof metadata.browser === 'string') { // use package.json browser field if possible. metaMain = metadata.browser; + } else if (typeof metadata.browser === 'object' && typeof metadata.browser['.'] === 'string') { + // use package.json browser mapping {".": "dist/index.js"} if possible. + metaMain = metadata.browser['.']; } else if (typeof metadata.module === 'string' && !(metadata.name && metadata.name.startsWith('aurelia-'))) { // prefer es module format over cjs, just like webpack. diff --git a/spec/lib/build/dependency-description.spec.js b/spec/lib/build/dependency-description.spec.js index b91049ebb..c71d6e620 100644 --- a/spec/lib/build/dependency-description.spec.js +++ b/spec/lib/build/dependency-description.spec.js @@ -38,4 +38,16 @@ describe('The DependencyDescription', () => { './server/only': './shims/server-only' }); }); + + it('gets browser replacement but leave . for main replacement', () => { + sut.metadata = { + browser: { + "readable-stream": "./lib/readable-stream-browser.js", + ".": "dist/jszip.min.js" + } + }; + expect(sut.browserReplacement()).toEqual({ + "readable-stream": "./lib/readable-stream-browser" + }); + }); }); diff --git a/spec/lib/build/util.spec.js b/spec/lib/build/utils.spec.js similarity index 85% rename from spec/lib/build/util.spec.js rename to spec/lib/build/utils.spec.js index 9a437e7fe..5b3908039 100644 --- a/spec/lib/build/util.spec.js +++ b/spec/lib/build/utils.spec.js @@ -152,6 +152,30 @@ describe('the Utils.nodejsLoad function', () => { expect(Utils.nodejsLoad(path.resolve('foo', 'bar'))).toBe(path.resolve('foo', 'bar', 'lo.js')); }); + it('load directory with package.json browser field', () => { + const fsConfig = {}; + fsConfig[path.join('foo', 'bar', 'package.json')] = '{"main": "lo2.js", "browser": "lo.js"}'; + fsConfig[path.join('foo', 'bar', 'lo.js')] = 'bar/lo.js'; + mockfs(fsConfig); + expect(Utils.nodejsLoad(path.resolve('foo', 'bar'))).toBe(path.resolve('foo', 'bar', 'lo.js')); + }); + + it('load directory with package.json browser "." mapping', () => { + const fsConfig = {}; + fsConfig[path.join('foo', 'bar', 'package.json')] = '{"main": "lo2.js", "browser": {".": "lo.js"}}'; + fsConfig[path.join('foo', 'bar', 'lo.js')] = 'bar/lo.js'; + mockfs(fsConfig); + expect(Utils.nodejsLoad(path.resolve('foo', 'bar'))).toBe(path.resolve('foo', 'bar', 'lo.js')); + }); + + it('load directory with package.json browser field', () => { + const fsConfig = {}; + fsConfig[path.join('foo', 'bar', 'package.json')] = '{"main": "lo2.js", "module": "lo.js"}'; + fsConfig[path.join('foo', 'bar', 'lo.js')] = 'bar/lo.js'; + mockfs(fsConfig); + expect(Utils.nodejsLoad(path.resolve('foo', 'bar'))).toBe(path.resolve('foo', 'bar', 'lo.js')); + }); + it('load directory with package.json, case2', () => { const fsConfig = {}; fsConfig[path.join('foo', 'bar', 'package.json')] = '{"main": "lo.js"}';