|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import fs from "node:fs"; |
| 3 | +import path from "node:path"; |
| 4 | +import { test } from "node:test"; |
| 5 | +import { fileURLToPath } from "node:url"; |
| 6 | + |
| 7 | +const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), ".."); |
| 8 | +const npmDir = path.join(root, "npm"); |
| 9 | + |
| 10 | +function collectStrings(value: unknown, out: string[]): void { |
| 11 | + if (typeof value === "string") { |
| 12 | + out.push(value); |
| 13 | + return; |
| 14 | + } |
| 15 | + |
| 16 | + if (Array.isArray(value)) { |
| 17 | + for (const item of value) collectStrings(item, out); |
| 18 | + return; |
| 19 | + } |
| 20 | + |
| 21 | + if (value != null && typeof value === "object") { |
| 22 | + for (const item of Object.values(value)) collectStrings(item, out); |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +function isEsmPackPackage(packageDir: string): boolean { |
| 27 | + const configPath = path.join(packageDir, "vite.config.ts"); |
| 28 | + if (!fs.existsSync(configPath)) return false; |
| 29 | + |
| 30 | + const config = fs.readFileSync(configPath, "utf-8"); |
| 31 | + return config.includes('format: "esm"') && config.includes("pack:"); |
| 32 | +} |
| 33 | + |
| 34 | +test("esm packed npm manifests point at mjs and d.mts outputs", () => { |
| 35 | + const failures: string[] = []; |
| 36 | + |
| 37 | + for (const entry of fs.readdirSync(npmDir, { withFileTypes: true })) { |
| 38 | + if (!entry.isDirectory()) continue; |
| 39 | + |
| 40 | + const packageDir = path.join(npmDir, entry.name); |
| 41 | + if (!isEsmPackPackage(packageDir)) continue; |
| 42 | + |
| 43 | + const packageJsonPath = path.join(packageDir, "package.json"); |
| 44 | + if (!fs.existsSync(packageJsonPath)) continue; |
| 45 | + |
| 46 | + const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8")) as { |
| 47 | + bin?: unknown; |
| 48 | + exports?: unknown; |
| 49 | + main?: unknown; |
| 50 | + name?: string; |
| 51 | + types?: unknown; |
| 52 | + }; |
| 53 | + |
| 54 | + const publishedPaths: string[] = []; |
| 55 | + collectStrings(packageJson.main, publishedPaths); |
| 56 | + collectStrings(packageJson.types, publishedPaths); |
| 57 | + collectStrings(packageJson.bin, publishedPaths); |
| 58 | + collectStrings(packageJson.exports, publishedPaths); |
| 59 | + |
| 60 | + for (const publishedPath of publishedPaths) { |
| 61 | + if (!publishedPath.includes("dist/")) continue; |
| 62 | + |
| 63 | + if (publishedPath.endsWith(".js") || publishedPath.endsWith(".d.ts")) { |
| 64 | + failures.push(`${packageJson.name ?? entry.name}: ${publishedPath}`); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + assert.deepEqual(failures, []); |
| 70 | +}); |
0 commit comments