|
| 1 | +// Compiles src/ to dist/ for publishing (run only at publish time; see AGENTS.md "Publishing"). |
| 2 | + |
| 3 | +import { cp } from "node:fs/promises"; |
| 4 | +import ts from "typescript"; |
| 5 | + |
| 6 | +const root = new URL("..", import.meta.url).pathname; |
| 7 | +const dist = `${root}dist`; |
| 8 | +const configPath = `${root}tsconfig.build.json`; |
| 9 | + |
| 10 | +// Rewrite a relative `.ts`/`.d.ts` specifier to its emitted `.js` sibling (TS resolves it against the `.d.ts`). |
| 11 | +function rewriteSpecifier(specifier: string): string { |
| 12 | + if (!specifier.startsWith(".")) return specifier; |
| 13 | + for (const ext of [".d.ts", ".ts"]) { |
| 14 | + if (specifier.endsWith(ext)) return `${specifier.slice(0, -ext.length)}.js`; |
| 15 | + } |
| 16 | + return specifier; |
| 17 | +} |
| 18 | + |
| 19 | +// afterDeclarations transformer: fix specifiers on import/export-from and inline import("...") type nodes. |
| 20 | +const rewriteDeclarationSpecifiers: ts.TransformerFactory< |
| 21 | + ts.SourceFile | ts.Bundle |
| 22 | +> = (context) => { |
| 23 | + const { factory } = context; |
| 24 | + const visit = (node: ts.Node): ts.Node => { |
| 25 | + if ( |
| 26 | + ts.isImportDeclaration(node) && |
| 27 | + ts.isStringLiteral(node.moduleSpecifier) |
| 28 | + ) { |
| 29 | + return factory.updateImportDeclaration( |
| 30 | + node, |
| 31 | + node.modifiers, |
| 32 | + node.importClause, |
| 33 | + factory.createStringLiteral( |
| 34 | + rewriteSpecifier(node.moduleSpecifier.text), |
| 35 | + ), |
| 36 | + node.attributes, |
| 37 | + ); |
| 38 | + } |
| 39 | + if ( |
| 40 | + ts.isExportDeclaration(node) && |
| 41 | + node.moduleSpecifier && |
| 42 | + ts.isStringLiteral(node.moduleSpecifier) |
| 43 | + ) { |
| 44 | + return factory.updateExportDeclaration( |
| 45 | + node, |
| 46 | + node.modifiers, |
| 47 | + node.isTypeOnly, |
| 48 | + node.exportClause, |
| 49 | + factory.createStringLiteral( |
| 50 | + rewriteSpecifier(node.moduleSpecifier.text), |
| 51 | + ), |
| 52 | + node.attributes, |
| 53 | + ); |
| 54 | + } |
| 55 | + if ( |
| 56 | + ts.isImportTypeNode(node) && |
| 57 | + ts.isLiteralTypeNode(node.argument) && |
| 58 | + ts.isStringLiteral(node.argument.literal) |
| 59 | + ) { |
| 60 | + return factory.updateImportTypeNode( |
| 61 | + node, |
| 62 | + factory.createLiteralTypeNode( |
| 63 | + factory.createStringLiteral( |
| 64 | + rewriteSpecifier(node.argument.literal.text), |
| 65 | + ), |
| 66 | + ), |
| 67 | + node.attributes, |
| 68 | + node.qualifier, |
| 69 | + node.typeArguments, |
| 70 | + node.isTypeOf, |
| 71 | + ); |
| 72 | + } |
| 73 | + return ts.visitEachChild(node, visit, context); |
| 74 | + }; |
| 75 | + return (sourceFile) => |
| 76 | + ts.visitNode(sourceFile, visit) as ts.SourceFile | ts.Bundle; |
| 77 | +}; |
| 78 | + |
| 79 | +function reportDiagnostics(diagnostics: readonly ts.Diagnostic[]): void { |
| 80 | + if (diagnostics.length === 0) return; |
| 81 | + const host: ts.FormatDiagnosticsHost = { |
| 82 | + getCanonicalFileName: (f) => f, |
| 83 | + getCurrentDirectory: ts.sys.getCurrentDirectory, |
| 84 | + getNewLine: () => ts.sys.newLine, |
| 85 | + }; |
| 86 | + console.error(ts.formatDiagnosticsWithColorAndContext(diagnostics, host)); |
| 87 | + process.exit(1); |
| 88 | +} |
| 89 | + |
| 90 | +// 1. Emit JS + declarations. |
| 91 | +const parsed = ts.getParsedCommandLineOfConfigFile( |
| 92 | + configPath, |
| 93 | + {}, |
| 94 | + { |
| 95 | + ...ts.sys, |
| 96 | + onUnRecoverableConfigFileDiagnostic: (d) => reportDiagnostics([d]), |
| 97 | + }, |
| 98 | +); |
| 99 | +if (!parsed) { |
| 100 | + console.error(`Could not read ${configPath}`); |
| 101 | + process.exit(1); |
| 102 | +} |
| 103 | + |
| 104 | +const program = ts.createProgram({ |
| 105 | + rootNames: parsed.fileNames, |
| 106 | + options: parsed.options, |
| 107 | +}); |
| 108 | +reportDiagnostics(ts.getPreEmitDiagnostics(program)); |
| 109 | + |
| 110 | +const emitResult = program.emit(undefined, undefined, undefined, false, { |
| 111 | + afterDeclarations: [rewriteDeclarationSpecifiers], |
| 112 | +}); |
| 113 | +reportDiagnostics(emitResult.diagnostics); |
| 114 | + |
| 115 | +// 2. Ship the generated declaration files (tsc does not emit its inputs). |
| 116 | +await cp(`${root}src/generated`, `${dist}/generated`, { recursive: true }); |
| 117 | + |
| 118 | +console.log("build: emitted dist/, copied generated declarations"); |
0 commit comments