Skip to content

Commit

Permalink
Handle the scenario where a package.json#browser field could be set t…
Browse files Browse the repository at this point in the history
…o {'./some-path': false}
  • Loading branch information
jeffsee55 committed Jun 21, 2024
1 parent bd68351 commit 593191b
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/resolve-dependency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ interface PkgCfg {
main: string | undefined;
exports: PackageTarget;
imports: { [key: string]: PackageTarget };
browser?: string | { [key: string]: string };
browser?: unknown;
}

async function getPkgCfg(
Expand Down Expand Up @@ -259,8 +259,21 @@ async function resolveRemappings(
): Promise<void> {
if (job.conditions?.includes('browser')) {
const { browser: pkgBrowser } = pkgCfg;
if (!pkgBrowser) {
return;
}
if (typeof pkgBrowser === 'string') {
return;
}
if (typeof pkgBrowser === 'object') {
for (const [key, value] of Object.entries(pkgBrowser)) {
if (typeof value !== 'string') {
/**
* `false` can be used to specify that a file is not meant to be included.
* Downstream processing is expected to handle this case, and it should remain in the mapping result
*/
continue;
}
if (!key.startsWith('./') || !value.startsWith('./')) {
continue;
}
Expand Down
2 changes: 2 additions & 0 deletions test/unit/browser-remappings-false/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# include node_modules for testing
!node_modules
2 changes: 2 additions & 0 deletions test/unit/browser-remappings-false/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require('pkg');

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions test/unit/browser-remappings-false/node_modules/pkg/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions test/unit/browser-remappings-false/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
"package.json",
"test/unit/browser-remappings-false/input.js",
"test/unit/browser-remappings-false/node_modules/pkg/browser.js",
"test/unit/browser-remappings-false/node_modules/pkg/index.js",
"test/unit/browser-remappings-false/node_modules/pkg/package.json"
]
3 changes: 3 additions & 0 deletions test/unit/browser-remappings-false/test-opts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"conditions": ["browser"]
}

0 comments on commit 593191b

Please sign in to comment.