Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"rollup": "^4.52.4",
"rollup-plugin-esbuild": "^6.2.1",
"semver": "^7.7.2",
"tinyglobby": "^0.2.15",
"typescript": "^5.9.2",
"vitest": "^3.2.4"
},
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

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

18 changes: 15 additions & 3 deletions src/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { parseNodeModulePath } from "mlly";
import { dirname, join, relative, resolve } from "pathe";
import { readPackageJSON, writePackageJSON } from "pkg-types";
import semver from "semver";
import { glob } from "tinyglobby";

type TracedFile = {
path: string;
Expand Down Expand Up @@ -116,6 +117,17 @@ export async function traceNodeModules(
pkgJSON,
};
tracedPackage.versions[pkgJSON.version || "0.0.0"] = tracedPackageVersion;

if (opts.fullTraceInclude) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This option could be more granular by giving an array of problematic packages to full trace.

const allFiles = await glob("**/*", {
cwd: tracedFile.pkgPath,
ignore: ["node_modules/**"],
});

for (const file of allFiles) {
tracedPackageVersion.files.push(file);
}
}
Comment on lines +120 to +130
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently this only adds all the files from a package detected by NFT. This does not account for new files discovered by nft. Not sure how to approach that

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With option bein array, we can pre-resolve them here

}
tracedPackageVersion.files.push(tracedFile.path);
tracedFile.pkgName = pkgName;
Expand Down Expand Up @@ -190,9 +202,9 @@ export async function traceNodeModules(
// Utility to find package parents
const findPackageParents = (pkg: TracedPackage, version: string) => {
// Try to find parent packages
const versionFiles: TracedFile[] = pkg.versions[version]!.files.map(
(path) => tracedFiles[path]!,
);
const versionFiles = pkg.versions[version]!.files.map(
(path) => tracedFiles[path],
).filter((x): x is TracedFile => x !== undefined);
const parentPkgs = [
...new Set(
versionFiles.flatMap((file) =>
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ export interface ExternalsTraceOptions {
* If `true`, writes a `package.json` file to the output directory (parent) with the traced files as dependencies.
*/
writePackageJson?: boolean;

/**
* If `true`, includes all files in the package in the trace.
*/
fullTraceInclude?: boolean;
}

export interface ExternalsPluginOptions extends ExternalsTraceOptions {
Expand Down
1 change: 1 addition & 0 deletions test/fixture/node_modules/@fixture/nitro-utils/README.md

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

23 changes: 23 additions & 0 deletions test/trace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { describe, expect, it } from "vitest";
import { traceNodeModules } from "../src/index.ts";
import { fileURLToPath } from "node:url";
import { cp } from "node:fs/promises";
import path from "node:path";
import { readFile } from "node:fs/promises";

describe("traceNodeModules", () => {
it("traceNodeModules", async () => {
Expand All @@ -21,4 +23,25 @@ describe("traceNodeModules", () => {
subpathLib: "@fixture/nitro-lib@2.0.0",
});
});

it.only("traceNodeModules with fullTraceInclude", async () => {
const input = fileURLToPath(new URL("fixture/index.mjs", import.meta.url));
const outDir = fileURLToPath(new URL("dist/trace", import.meta.url));

await cp(input, `${outDir}/index.mjs`);
await traceNodeModules([input], { outDir, fullTraceInclude: true });

expect(
await readFile(
path.join(
outDir,
"node_modules",
"@fixture",
"nitro-utils",
"README.md",
),
{ encoding: "utf8" },
),
).toMatch("# TEST");
});
});
Loading