Skip to content

Commit

Permalink
Merge branch 'main' into fix-issue-314
Browse files Browse the repository at this point in the history
  • Loading branch information
styfle authored Jun 7, 2024
2 parents d198252 + ff4341b commit b31ca3e
Show file tree
Hide file tree
Showing 47 changed files with 240 additions and 3 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
22 changes: 20 additions & 2 deletions src/node-file-trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import {
NodeFileTraceReasons,
NodeFileTraceReasonType,
} from './types';
import { basename, dirname, extname, relative, resolve, sep } from 'path';
import { basename, dirname, extname, join, relative, resolve, sep } from 'path';
import analyze, { AnalyzeResult } from './analyze';
import resolveDependency, { NotFoundError } from './resolve-dependency';
import { isMatch } from 'micromatch';
import { sharedLibEmit } from './utils/sharedlib-emit';
import { join } from 'path';
import { CachedFileSystem } from './fs';

function inPath(path: string, parent: string) {
Expand Down Expand Up @@ -69,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 @@ -155,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 @@ -317,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
2 changes: 1 addition & 1 deletion src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ export type EvaluatedValue = StaticValue | ConditionalValue | undefined;
export interface Node {
start: number;
end: number;
type: string;
type: ESTreeNode['type'];
[propName: string]: any;
}
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 @@ -131,10 +131,21 @@ 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 startTime = Date.now();
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 b31ca3e

Please sign in to comment.