Skip to content

Commit 29456b9

Browse files
authored
fix: add esm and cjs paths to browser map for overriden files (#12)
* fix: add esm and cjs paths to browser map for overriden files See evanw/esbuild#1443 for discussion. In brief if you have a project that is built by ipjs and you have file A that imports file B, esbuild (maybe others?) resolves file B using it's full path within the module (e.g. including `esm` or `cjs`), which it then uses as a key to look up overrides in the `browser` map in your `package.json`. If there is an override it'll fail to be found because ipjs doesn't add paths prefixed with `esm` or `cjs` to the browser map where overrides have been delcared in the `exports` map, so the change here is to add files with their full path prefix to the `browser` map. * fix: browser overrides in webpack
1 parent 729ffbf commit 29456b9

File tree

7 files changed

+35
-5
lines changed

7 files changed

+35
-5
lines changed

src/package/index.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ class Package {
190190
json.browser = {}
191191
json.exports = {}
192192
const _join = (...args) => './' + join(...args)
193+
const esmBrowser = {}
193194
for (const [key, ex] of Object.entries(this.exports)) {
194195
const _import = this.relative(await ex.import)
195196
const _browser = ex.browser ? this.relative(await ex.browser) : _import
@@ -199,15 +200,23 @@ class Package {
199200
import: _join('esm', _import)
200201
}
201202
json.browser[key] = _join('cjs', _browser)
203+
if (_import !== _browser) {
204+
json.browser[_join('esm', _import)] = _join('esm', _browser)
205+
json.browser[_join('cjs', _import)] = _join('cjs', _browser)
206+
esmBrowser[_import] = _browser
207+
}
202208
}
203209
if (json.exports.import) {
204210
json.exports = json.exports.import
205211
json.browser = json.browser.import
206212
}
207213
let files = Promise.all(pending)
208214
pending.push(writeFile(new URL(dist + '/package.json'), JSON.stringify(json, null, 2)))
209-
const typeModule = '{ "type" : "module" }'
210-
pending.push(writeFile(new URL(dist + '/esm/package.json'), typeModule))
215+
const typeModule = {
216+
type: 'module',
217+
browser: esmBrowser
218+
}
219+
pending.push(writeFile(new URL(dist + '/esm/package.json'), JSON.stringify(typeModule, null, 2)))
211220
await Promise.all(pending)
212221
files = await files
213222
return files
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
{ "type" : "module" }
1+
{
2+
"type": "module",
3+
"browser": {
4+
"./src/index.js": "./src/browser.js"
5+
}
6+
}

test/fixtures/pkg-kitchensink/output-main/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
},
2424
"browser": {
2525
".": "./cjs/src/browser.js",
26+
"./esm/src/index.js": "./esm/src/browser.js",
27+
"./cjs/src/index.js": "./cjs/src/browser.js",
2628
"./secondary": "./cjs/src/secondary.js"
2729
}
2830
}
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
{ "type" : "module" }
1+
{
2+
"type": "module",
3+
"browser": {
4+
"./src/index.js": "./src/browser.js"
5+
}
6+
}

test/fixtures/pkg-kitchensink/output-notests/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
},
2323
"browser": {
2424
".": "./cjs/src/browser.js",
25+
"./esm/src/index.js": "./esm/src/browser.js",
26+
"./cjs/src/index.js": "./cjs/src/browser.js",
2527
"./secondary": "./cjs/src/secondary.js"
2628
}
2729
}
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
{ "type" : "module" }
1+
{
2+
"type": "module",
3+
"browser": {
4+
"./src/index.js": "./src/browser.js"
5+
}
6+
}

test/fixtures/pkg-kitchensink/output-tests/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
},
2323
"browser": {
2424
".": "./cjs/src/browser.js",
25+
"./esm/src/index.js": "./esm/src/browser.js",
26+
"./cjs/src/index.js": "./cjs/src/browser.js",
2527
"./secondary": "./cjs/src/secondary.js"
2628
}
2729
}

0 commit comments

Comments
 (0)