Skip to content

Commit

Permalink
fix: Support browser overrides in package.json (#424)
Browse files Browse the repository at this point in the history
`browser` section `package.json` is used to expand a list of dependent
files. The downstream build tool would normally do the right thing and
use the `browser` section for bundling correctly. I tested the bundling
itself on esbuild and WebPack, and it works correctly. All that's needed
is to make sure the files are there.
  • Loading branch information
dvoytenko authored Jun 5, 2024
1 parent 27d47e3 commit ff4341b
Show file tree
Hide file tree
Showing 46 changed files with 238 additions and 0 deletions.
10 changes: 10 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"@ffmpeg-installer/ffmpeg": "^1.1.0",
"@google-cloud/bigquery": "^4.1.4",
"@google-cloud/firestore": "^7.6.0",
"@opentelemetry/api": "^1.7.0",
"@sentry/node": "^5.5.0",
"@tpluscode/sparql-builder": "^0.3.12",
"@types/bindings": "^1.3.0",
Expand Down
19 changes: 19 additions & 0 deletions src/node-file-trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export class Job {
public warnings: Set<Error>;
public reasons: NodeFileTraceReasons = new Map();
private cachedFileSystem: CachedFileSystem;
private remappings: Map<string, Set<string>> = new Map();

constructor({
base = process.cwd(),
Expand Down Expand Up @@ -154,6 +155,16 @@ export class Job {
this.warnings = new Set();
}

addRemapping(path: string, dep: string) {
if (path === dep) return;
let deps = this.remappings.get(path);
if (!deps) {
deps = new Set();
this.remappings.set(path, deps);
}
deps.add(dep);
}

async readlink(path: string) {
return this.cachedFileSystem.readlink(path);
}
Expand Down Expand Up @@ -316,6 +327,14 @@ export class Job {
}
this.processed.add(path);

// Additional dependencies.
const additionalDeps = this.remappings.get(path);
if (additionalDeps) {
await Promise.all(
[...additionalDeps].map(async (dep) => this.emitDependency(dep, path)),
);
}

const emitted = await this.emitFile(path, 'dependency', parent);
if (!emitted) return;
if (path.endsWith('.json')) return;
Expand Down
38 changes: 38 additions & 0 deletions src/resolve-dependency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ interface PkgCfg {
main: string | undefined;
exports: PackageTarget;
imports: { [key: string]: PackageTarget };
browser?: string | { [key: string]: string };
}

async function getPkgCfg(
Expand Down Expand Up @@ -250,6 +251,33 @@ function resolveExportsImports(
return undefined;
}

async function resolveRemappings(
pkgPath: string,
pkgCfg: PkgCfg,
parent: string,
job: Job,
): Promise<void> {
if (job.conditions?.includes('browser')) {
const { browser: pkgBrowser } = pkgCfg;
if (typeof pkgBrowser === 'object') {
for (const [key, value] of Object.entries(pkgBrowser)) {
if (!key.startsWith('./') || !value.startsWith('./')) {
continue;
}
const keyResolved = await resolveFile(pkgPath + sep + key, parent, job);
const valueResolved = await resolveFile(
pkgPath + sep + value,
parent,
job,
);
if (keyResolved && valueResolved) {
job.addRemapping(keyResolved, valueResolved);
}
}
}
}
}

async function packageImportsResolve(
name: string,
parent: string,
Expand Down Expand Up @@ -355,6 +383,16 @@ async function resolvePackage(
if (!stat || !stat.isDirectory()) continue;
const pkgCfg = await getPkgCfg(nodeModulesDir + sep + pkgName, job);
const { exports: pkgExports } = pkgCfg || {};

if (pkgCfg) {
await resolveRemappings(
nodeModulesDir + sep + pkgName,
pkgCfg,
parent,
job,
);
}

if (
job.conditions &&
pkgExports !== undefined &&
Expand Down
3 changes: 3 additions & 0 deletions test/integration/otel-api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const { trace } = require('@opentelemetry/api');

void trace.getTracer('test').startSpan('test');
11 changes: 11 additions & 0 deletions test/unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,20 @@ for (const { testName, isRoot } of unitTests) {
inputFileNames.push('input-2.js', 'input-3.js', 'input-4.js');
}

// Type: { conditions?: string[] }
let testOpts = {};
try {
testOpts = JSON.parse(
fs.readFileSync(join(unitPath, 'test-opts.json')).toString(),
);
} catch {
// Ignore.
}

const { fileList, reasons } = await nodeFileTrace(
inputFileNames.map((file) => join(unitPath, file)),
{
conditions: testOpts.conditions,
base: isRoot ? '/' : `${__dirname}/../`,
processCwd: unitPath,
paths: {
Expand Down
2 changes: 2 additions & 0 deletions test/unit/browser-remappings-disabled/.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-disabled/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.

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.

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.

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

9 changes: 9 additions & 0 deletions test/unit/browser-remappings-disabled/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
"package.json",
"test/unit/browser-remappings-disabled/input.js",
"test/unit/browser-remappings-disabled/node_modules/pkg/index.js",
"test/unit/browser-remappings-disabled/node_modules/pkg/package.json",
"test/unit/browser-remappings-disabled/node_modules/pkg/require-main.cjs",
"test/unit/browser-remappings-disabled/node_modules/pkg/subdir/import-main.js",
"test/unit/browser-remappings-disabled/node_modules/pkg/subdir/package.json"
]
3 changes: 3 additions & 0 deletions test/unit/browser-remappings-disabled/test-opts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"conditions": ["node"]
}
2 changes: 2 additions & 0 deletions test/unit/browser-remappings-malformed/.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-malformed/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.

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.

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.

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

9 changes: 9 additions & 0 deletions test/unit/browser-remappings-malformed/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
"package.json",
"test/unit/browser-remappings-malformed/input.js",
"test/unit/browser-remappings-malformed/node_modules/pkg/index.js",
"test/unit/browser-remappings-malformed/node_modules/pkg/package.json",
"test/unit/browser-remappings-malformed/node_modules/pkg/require-main.cjs",
"test/unit/browser-remappings-malformed/node_modules/pkg/subdir/import-main.js",
"test/unit/browser-remappings-malformed/node_modules/pkg/subdir/package.json"
]
3 changes: 3 additions & 0 deletions test/unit/browser-remappings-malformed/test-opts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"conditions": ["browser"]
}
2 changes: 2 additions & 0 deletions test/unit/browser-remappings-malformed2/.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-malformed2/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.

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.

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.

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

9 changes: 9 additions & 0 deletions test/unit/browser-remappings-malformed2/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
"package.json",
"test/unit/browser-remappings-malformed2/input.js",
"test/unit/browser-remappings-malformed2/node_modules/pkg/index.js",
"test/unit/browser-remappings-malformed2/node_modules/pkg/package.json",
"test/unit/browser-remappings-malformed2/node_modules/pkg/require-main.cjs",
"test/unit/browser-remappings-malformed2/node_modules/pkg/subdir/import-main.js",
"test/unit/browser-remappings-malformed2/node_modules/pkg/subdir/package.json"
]
3 changes: 3 additions & 0 deletions test/unit/browser-remappings-malformed2/test-opts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"conditions": ["browser"]
}
2 changes: 2 additions & 0 deletions test/unit/browser-remappings/.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/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require('pkg');

1 change: 1 addition & 0 deletions test/unit/browser-remappings/node_modules/pkg/index.js

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

17 changes: 17 additions & 0 deletions test/unit/browser-remappings/node_modules/pkg/package.json

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.

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.

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

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

0 comments on commit ff4341b

Please sign in to comment.