Skip to content

Commit 6709821

Browse files
committed
fix(scripts): leave type-only packages out of third-party notices
1 parent 5cc99e1 commit 6709821

2 files changed

Lines changed: 151 additions & 1 deletion

File tree

‎scripts/lib/third-party-licenses.test.ts‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,76 @@ describe("third-party license generation", () => {
202202
expect(manifest.entries.some((entry) => entry.name === "demo-dependency")).toBe(true);
203203
});
204204

205+
it("leaves type-only packages out of the notices but still walks their dependencies", async () => {
206+
const fixture = await createFixture();
207+
const modules = NodePath.join(fixture.root, "node_modules");
208+
const write = async (name: string, packageJson: Record<string, unknown>) => {
209+
await writeJson(NodePath.join(modules, name, "package.json"), {
210+
name,
211+
version: "1.0.0",
212+
license: "MIT",
213+
...packageJson,
214+
});
215+
await NodeFSP.writeFile(NodePath.join(modules, name, "LICENSE"), `${name} license\n`, "utf8");
216+
};
217+
await writeJson(fixture.appManifest, {
218+
name: "fixture-app",
219+
dependencies: { "demo-dependency": "1.2.3", "@types/demo": "1.0.0" },
220+
});
221+
// The DefinitelyTyped shape, and the csstype / undici-types one it pulls in.
222+
await write("@types/demo", {
223+
main: "",
224+
types: "index.d.ts",
225+
exports: {
226+
".": { types: { import: "./index.d.mts", default: "./index.d.ts" } },
227+
"./package.json": "./package.json",
228+
},
229+
dependencies: { "demo-css-types": "1.0.0", "demo-runtime": "1.0.0" },
230+
});
231+
await write("demo-css-types", { main: "", types: "index.d.ts" });
232+
await write("demo-runtime", { main: "index.js" });
233+
234+
const manifest = await generateThirdPartyLicenseManifest({
235+
configFile: fixture.configFile,
236+
packageManifests: [{ bundle: "web", path: fixture.appManifest }],
237+
});
238+
239+
const names = manifest.entries.map((entry) => entry.name);
240+
expect(names).toContain("demo-dependency");
241+
expect(names).toContain("demo-runtime");
242+
expect(names).not.toContain("@types/demo");
243+
expect(names).not.toContain("demo-css-types");
244+
});
245+
246+
it.each([
247+
{
248+
shape: "a JS entry next to its declarations",
249+
// The @shikijs/types shape: a types condition next to a real JS entry.
250+
entry: {
251+
main: "./dist/index.mjs",
252+
exports: { ".": { types: "./dist/index.d.mts", import: "./dist/index.mjs" } },
253+
},
254+
},
255+
{ shape: "only a stylesheet", entry: { main: "", style: "dist/theme.css" } },
256+
])("keeps a package that declares types but ships $shape", async ({ entry }) => {
257+
const fixture = await createFixture();
258+
await writeJson(NodePath.join(fixture.dependencyRoot, "package.json"), {
259+
name: "demo-dependency",
260+
version: "1.2.3",
261+
license: "MIT",
262+
types: "./dist/index.d.mts",
263+
...entry,
264+
repository: "example/demo-dependency",
265+
});
266+
267+
const manifest = await generateThirdPartyLicenseManifest({
268+
configFile: fixture.configFile,
269+
packageManifests: [{ bundle: "web", path: fixture.appManifest }],
270+
});
271+
272+
expect(manifest.entries.some((entry) => entry.name === "demo-dependency")).toBe(true);
273+
});
274+
205275
it("includes custom notices selected by the dev server bundle", async () => {
206276
const fixture = await createFixture();
207277
await writeJson(fixture.configFile, {

‎scripts/lib/third-party-licenses.ts‎

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,22 @@ interface GeneratedNoticeConfigEntry {
4545
}
4646

4747
interface PackageJson {
48+
readonly bin?: unknown;
49+
readonly browser?: unknown;
4850
readonly dependencies?: Readonly<Record<string, string>>;
51+
readonly exports?: unknown;
4952
readonly homepage?: unknown;
5053
readonly license?: unknown;
5154
readonly licenses?: unknown;
55+
readonly main?: unknown;
56+
readonly module?: unknown;
5257
readonly name?: unknown;
5358
readonly optionalDependencies?: Readonly<Record<string, string>>;
5459
readonly repository?: unknown;
60+
readonly sass?: unknown;
61+
readonly style?: unknown;
62+
readonly types?: unknown;
63+
readonly typings?: unknown;
5564
readonly version?: unknown;
5665
}
5766

@@ -510,6 +519,73 @@ function dependencyNames(packageJson: PackageJson): ReadonlyArray<string> {
510519
].sort((left, right) => left.localeCompare(right));
511520
}
512521

522+
interface ExportTarget {
523+
readonly path: string;
524+
readonly underTypesCondition: boolean;
525+
}
526+
527+
function exportTargets(value: unknown, underTypesCondition = false): ReadonlyArray<ExportTarget> {
528+
if (typeof value === "string") return [{ path: value, underTypesCondition }];
529+
if (Array.isArray(value))
530+
return value.flatMap((item) => exportTargets(item, underTypesCondition));
531+
if (value === null || typeof value !== "object") return [];
532+
return Object.entries(value).flatMap(([key, item]) =>
533+
exportTargets(item, underTypesCondition || key.startsWith("types") || key === "typings"),
534+
);
535+
}
536+
537+
/** Declarations, or the package's own manifest that many packages also export. Neither is code. */
538+
function isTypeDeclaration(target: ExportTarget): boolean {
539+
return (
540+
target.underTypesCondition ||
541+
/\.d\.[cm]?ts$/u.test(target.path) ||
542+
target.path === "./package.json"
543+
);
544+
}
545+
546+
function isDeclared(value: unknown): boolean {
547+
if (typeof value === "string") return value.trim() !== "";
548+
return value !== null && typeof value === "object" && Object.keys(value).length > 0;
549+
}
550+
551+
async function fileExists(path: string): Promise<boolean> {
552+
try {
553+
await NodeFSP.access(path);
554+
return true;
555+
} catch {
556+
return false;
557+
}
558+
}
559+
560+
/**
561+
* A package that only describes types (DefinitelyTyped, csstype, undici-types): it declares types
562+
* and has no JS entry, so nothing of it ships. A types condition or a name with "types" in it is not
563+
* enough on its own: `@shikijs/types` exports real code next to its declarations.
564+
*/
565+
async function isTypeOnlyPackage(packageJson: PackageJson, packageRoot: string): Promise<boolean> {
566+
const targets = exportTargets(packageJson.exports);
567+
const declaresTypes =
568+
isDeclared(packageJson.types) ||
569+
isDeclared(packageJson.typings) ||
570+
targets.some(isTypeDeclaration);
571+
if (!declaresTypes) return false;
572+
const entries = [
573+
packageJson.main,
574+
packageJson.module,
575+
packageJson.browser,
576+
packageJson.bin,
577+
packageJson.style,
578+
packageJson.sass,
579+
];
580+
if (entries.some(isDeclared) || targets.some((target) => !isTypeDeclaration(target))) {
581+
return false;
582+
}
583+
// Without `main` or `exports`, Node still loads the package's index.js.
584+
return packageJson.main !== undefined || packageJson.exports !== undefined
585+
? true
586+
: !(await fileExists(NodePath.join(packageRoot, "index.js")));
587+
}
588+
513589
async function collectProductionDependencyPackages(
514590
packageManifests: ReadonlyArray<ThirdPartyLicensePackageManifest>,
515591
): Promise<PackageCollection> {
@@ -530,7 +606,11 @@ async function collectProductionDependencyPackages(
530606
const dependencyPackageJsonPath = NodePath.join(resolved.packageRoot, "package.json");
531607
const name =
532608
typeof resolved.packageJson.name === "string" ? resolved.packageJson.name : dependencyName;
533-
if (!name.startsWith(FIRST_PARTY_PACKAGE_PREFIX)) {
609+
// Type-only packages ship nothing, but what they depend on may, so the walk goes on below.
610+
if (
611+
!name.startsWith(FIRST_PARTY_PACKAGE_PREFIX) &&
612+
!(await isTypeOnlyPackage(resolved.packageJson, resolved.packageRoot))
613+
) {
534614
const identity = packageIdentity(resolved.packageJson, resolved.packageRoot);
535615
const existing = collection.byIdentity.get(identity);
536616
if (existing) {

0 commit comments

Comments
 (0)