|
3 | 3 | // Split from release.ts to allow building packages without publishing |
4 | 4 |
|
5 | 5 | import { copyFileSync, readFileSync, writeFileSync } from "node:fs"; |
6 | | -import { join } from "node:path"; |
| 6 | +import { join, resolve } from "node:path"; |
| 7 | +import { publint } from "publint"; |
| 8 | +import { formatMessage } from "publint/utils"; |
7 | 9 |
|
8 | 10 | console.log("✍️ Rewriting package.json..."); |
9 | 11 | const pkg = JSON.parse(readFileSync("package.json", "utf8")); |
10 | 12 |
|
11 | | -function rewritePath(p: string): string { |
12 | | - return p.replace(/^\.\/src/, ".").replace(/\.ts(x)?$/, ".js"); |
| 13 | +function rewritePath(p: string, ext: string): string { |
| 14 | + return p.replace(/^\.\/src/, ".").replace(/\.ts(x)?$/, `.${ext}`); |
13 | 15 | } |
14 | 16 |
|
15 | | -pkg.main &&= rewritePath(pkg.main); |
16 | | -pkg.types &&= rewritePath(pkg.types); |
| 17 | +pkg.main &&= rewritePath(pkg.main, "js"); |
| 18 | +pkg.types &&= rewritePath(pkg.types, "d.ts"); |
17 | 19 |
|
18 | 20 | if (pkg.exports) { |
19 | 21 | for (const key in pkg.exports) { |
20 | 22 | const val = pkg.exports[key]; |
21 | 23 | if (typeof val === "string") { |
22 | | - pkg.exports[key] = rewritePath(val); |
| 24 | + if (val.endsWith(".css")) { |
| 25 | + // CSS exports are only needed for dev-time resolution; |
| 26 | + // consumers inline them at build time via @import. |
| 27 | + // We purposely do not copy them to the dist to help catch bugs. |
| 28 | + delete pkg.exports[key]; |
| 29 | + } else { |
| 30 | + pkg.exports[key] = { |
| 31 | + types: rewritePath(val, "d.ts"), |
| 32 | + default: rewritePath(val, "js"), |
| 33 | + }; |
| 34 | + } |
23 | 35 | } else if (typeof val === "object") { |
24 | 36 | for (const sub in val) { |
25 | 37 | if (typeof val[sub] === "string") { |
26 | | - val[sub] = rewritePath(val[sub]); |
| 38 | + val[sub] = rewritePath(val[sub], sub === "types" ? "d.ts" : "js"); |
27 | 39 | } |
28 | 40 | } |
29 | 41 | } |
30 | 42 | } |
31 | 43 | } |
32 | 44 |
|
33 | 45 | if (pkg.sideEffects) { |
34 | | - pkg.sideEffects = pkg.sideEffects.map(rewritePath); |
| 46 | + pkg.sideEffects = pkg.sideEffects.map((p: string) => rewritePath(p, "js")); |
35 | 47 | } |
36 | 48 |
|
37 | 49 | if (pkg.files) { |
38 | | - pkg.files = pkg.files.map(rewritePath); |
| 50 | + pkg.files = pkg.files.map((p: string) => rewritePath(p, "js")); |
39 | 51 | } |
40 | 52 |
|
41 | 53 | if (pkg.bin) { |
42 | 54 | if (typeof pkg.bin === "string") { |
43 | | - pkg.bin = rewritePath(pkg.bin); |
| 55 | + pkg.bin = rewritePath(pkg.bin, "js"); |
44 | 56 | } else if (typeof pkg.bin === "object") { |
45 | 57 | for (const key in pkg.bin) { |
46 | | - pkg.bin[key] = rewritePath(pkg.bin[key]); |
| 58 | + pkg.bin[key] = rewritePath(pkg.bin[key], "js"); |
47 | 59 | } |
48 | 60 | } |
49 | 61 | } |
@@ -78,4 +90,19 @@ writeFileSync("dist/package.json", JSON.stringify(pkg, null, 2)); |
78 | 90 | console.log("📄 Copying README.md..."); |
79 | 91 | copyFileSync("README.md", join("dist", "README.md")); |
80 | 92 |
|
| 93 | +// Lint the package to catch publishing issues |
| 94 | +console.log("🔍 Running publint..."); |
| 95 | +const { messages, pkg: lintPkg } = await publint({ |
| 96 | + pkgDir: resolve("dist"), |
| 97 | + level: "warning", |
| 98 | + pack: false, |
| 99 | +}); |
| 100 | + |
| 101 | +if (messages.length > 0) { |
| 102 | + for (const message of messages) { |
| 103 | + console.error(formatMessage(message, lintPkg)); |
| 104 | + } |
| 105 | + process.exit(1); |
| 106 | +} |
| 107 | + |
81 | 108 | console.log("📦 Package built successfully in dist/"); |
0 commit comments