ttsc 0.6.0 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (271) hide show
  1. package/cmd/platform/main.go +1 -1
  2. package/cmd/ttsc/api_compile.go +134 -0
  3. package/cmd/ttsc/api_transform.go +64 -0
  4. package/cmd/ttsc/build.go +1 -7
  5. package/cmd/ttsc/main.go +6 -20
  6. package/driver/program.go +13 -3
  7. package/lib/TtscCompiler.d.ts +96 -0
  8. package/lib/TtscCompiler.js +294 -0
  9. package/lib/TtscCompiler.js.map +1 -0
  10. package/lib/compiler/internal/buildNativeCompiler.d.ts +6 -0
  11. package/lib/compiler/internal/buildNativeCompiler.js +72 -0
  12. package/lib/compiler/internal/buildNativeCompiler.js.map +1 -0
  13. package/lib/compiler/internal/compileProjectInMemory.d.ts +7 -0
  14. package/lib/compiler/internal/compileProjectInMemory.js +176 -0
  15. package/lib/compiler/internal/compileProjectInMemory.js.map +1 -0
  16. package/lib/compiler/internal/project/readProjectConfig.d.ts +4 -0
  17. package/lib/{project.js → compiler/internal/project/readProjectConfig.js} +12 -82
  18. package/lib/compiler/internal/project/readProjectConfig.js.map +1 -0
  19. package/lib/compiler/internal/project/resolveProjectConfig.d.ts +3 -0
  20. package/lib/compiler/internal/project/resolveProjectConfig.js +94 -0
  21. package/lib/compiler/internal/project/resolveProjectConfig.js.map +1 -0
  22. package/lib/compiler/internal/resolveBinary.d.ts +4 -0
  23. package/lib/compiler/internal/resolveBinary.js +65 -0
  24. package/lib/compiler/internal/resolveBinary.js.map +1 -0
  25. package/lib/compiler/internal/resolveEmittedJavaScript.d.ts +7 -0
  26. package/lib/compiler/internal/resolveEmittedJavaScript.js +118 -0
  27. package/lib/compiler/internal/resolveEmittedJavaScript.js.map +1 -0
  28. package/lib/compiler/internal/resolveTsgo.d.ts +20 -0
  29. package/lib/{tsgo.js → compiler/internal/resolveTsgo.js} +5 -10
  30. package/lib/compiler/internal/resolveTsgo.js.map +1 -0
  31. package/lib/compiler/internal/runBuild.d.ts +10 -0
  32. package/lib/compiler/internal/runBuild.js +480 -0
  33. package/lib/compiler/internal/runBuild.js.map +1 -0
  34. package/lib/compiler/internal/runSingleFileEmit.d.ts +3 -0
  35. package/lib/compiler/internal/runSingleFileEmit.js +102 -0
  36. package/lib/compiler/internal/runSingleFileEmit.js.map +1 -0
  37. package/lib/compiler/internal/transformProjectInMemory.d.ts +7 -0
  38. package/lib/compiler/internal/transformProjectInMemory.js +275 -0
  39. package/lib/compiler/internal/transformProjectInMemory.js.map +1 -0
  40. package/lib/index.d.ts +6 -15
  41. package/lib/index.js +6 -15
  42. package/lib/index.js.map +1 -1
  43. package/lib/launcher/internal/getCompilerVersionText.d.ts +3 -0
  44. package/lib/launcher/internal/getCompilerVersionText.js +71 -0
  45. package/lib/launcher/internal/getCompilerVersionText.js.map +1 -0
  46. package/lib/launcher/internal/prepareExecution.d.ts +10 -0
  47. package/lib/launcher/internal/prepareExecution.js +120 -0
  48. package/lib/launcher/internal/prepareExecution.js.map +1 -0
  49. package/lib/launcher/internal/runTtsc.d.ts +1 -0
  50. package/lib/{cli.js → launcher/internal/runTtsc.js} +45 -118
  51. package/lib/launcher/internal/runTtsc.js.map +1 -0
  52. package/lib/launcher/internal/runTtsx.d.ts +1 -0
  53. package/lib/{runner/cli.js → launcher/internal/runTtsx.js} +13 -35
  54. package/lib/launcher/internal/runTtsx.js.map +1 -0
  55. package/lib/launcher/ttsc.js +2 -2
  56. package/lib/launcher/ttsc.js.map +1 -1
  57. package/lib/launcher/ttsx.js +2 -2
  58. package/lib/launcher/ttsx.js.map +1 -1
  59. package/lib/plugin/internal/buildSourcePlugin.d.ts +12 -0
  60. package/lib/{source-build.js → plugin/internal/buildSourcePlugin.js} +77 -84
  61. package/lib/plugin/internal/buildSourcePlugin.js.map +1 -0
  62. package/lib/plugin/internal/loadProjectPlugins.d.ts +14 -0
  63. package/lib/{plugin.js → plugin/internal/loadProjectPlugins.js} +38 -76
  64. package/lib/plugin/internal/loadProjectPlugins.js.map +1 -0
  65. package/lib/structures/ITtscCompilerContext.d.ts +78 -0
  66. package/lib/structures/ITtscCompilerContext.js +3 -0
  67. package/lib/structures/ITtscCompilerContext.js.map +1 -0
  68. package/lib/structures/ITtscCompilerDiagnostic.d.ts +83 -0
  69. package/lib/structures/ITtscCompilerDiagnostic.js +3 -0
  70. package/lib/structures/ITtscCompilerDiagnostic.js.map +1 -0
  71. package/lib/structures/ITtscCompilerResult.d.ts +83 -0
  72. package/lib/structures/{ITtscCacheKeyInput.js → ITtscCompilerResult.js} +1 -1
  73. package/lib/structures/ITtscCompilerResult.js.map +1 -0
  74. package/lib/structures/ITtscCompilerTransformation.d.ts +35 -0
  75. package/lib/structures/ITtscCompilerTransformation.js +3 -0
  76. package/lib/structures/ITtscCompilerTransformation.js.map +1 -0
  77. package/lib/structures/ITtscPlugin.d.ts +52 -7
  78. package/lib/structures/ITtscPluginFactoryContext.d.ts +47 -0
  79. package/lib/structures/ITtscProjectPluginConfig.d.ts +41 -0
  80. package/lib/structures/TtscPluginStage.d.ts +18 -0
  81. package/lib/structures/{ITtscBuildResult.js → TtscPluginStage.js} +1 -1
  82. package/lib/structures/TtscPluginStage.js.map +1 -0
  83. package/lib/structures/index.d.ts +8 -0
  84. package/lib/structures/index.js +25 -0
  85. package/lib/structures/index.js.map +1 -0
  86. package/lib/structures/internal/ITtscLoadedNativePlugin.d.ts +13 -0
  87. package/lib/structures/internal/ITtscLoadedNativePlugin.js.map +1 -0
  88. package/lib/structures/internal/ITtscParsedProjectConfig.d.ts +15 -0
  89. package/lib/structures/internal/ITtscParsedProjectConfig.js.map +1 -0
  90. package/lib/structures/internal/ITtscProjectLocatorOptions.d.ts +9 -0
  91. package/lib/structures/internal/ITtscProjectLocatorOptions.js.map +1 -0
  92. package/lib/structures/internal/TtscBuildOptions.d.ts +18 -0
  93. package/lib/structures/{ITtscBuildOptions.js → internal/TtscBuildOptions.js} +1 -1
  94. package/lib/structures/internal/TtscBuildOptions.js.map +1 -0
  95. package/lib/structures/internal/TtscBuildResult.d.ts +14 -0
  96. package/lib/structures/{ITtscCheckOptions.js → internal/TtscBuildResult.js} +1 -1
  97. package/lib/structures/internal/TtscBuildResult.js.map +1 -0
  98. package/lib/structures/internal/TtscCommonOptions.d.ts +27 -0
  99. package/lib/structures/{ITtscNativeSource.js → internal/TtscCommonOptions.js} +1 -1
  100. package/lib/structures/internal/TtscCommonOptions.js.map +1 -0
  101. package/lib/structures/internal/TtscSingleFileEmitOptions.d.ts +14 -0
  102. package/lib/structures/internal/TtscSingleFileEmitOptions.js +3 -0
  103. package/lib/structures/internal/TtscSingleFileEmitOptions.js.map +1 -0
  104. package/package.json +9 -9
  105. package/src/TtscCompiler.ts +305 -0
  106. package/src/compiler/internal/buildNativeCompiler.ts +43 -0
  107. package/src/compiler/internal/compileProjectInMemory.ts +189 -0
  108. package/src/{project.ts → compiler/internal/project/readProjectConfig.ts} +36 -111
  109. package/src/compiler/internal/project/resolveProjectConfig.ts +67 -0
  110. package/src/compiler/internal/resolveBinary.ts +41 -0
  111. package/src/compiler/internal/resolveEmittedJavaScript.ts +102 -0
  112. package/src/{tsgo.ts → compiler/internal/resolveTsgo.ts} +20 -23
  113. package/src/compiler/internal/runBuild.ts +644 -0
  114. package/src/compiler/internal/runSingleFileEmit.ts +73 -0
  115. package/src/compiler/internal/transformProjectInMemory.ts +368 -0
  116. package/src/index.ts +6 -15
  117. package/src/launcher/internal/getCompilerVersionText.ts +44 -0
  118. package/src/launcher/internal/prepareExecution.ts +115 -0
  119. package/src/{cli.ts → launcher/internal/runTtsc.ts} +58 -140
  120. package/src/{runner/cli.ts → launcher/internal/runTtsx.ts} +21 -49
  121. package/src/launcher/ttsc.ts +2 -2
  122. package/src/launcher/ttsx.ts +2 -2
  123. package/src/{source-build.ts → plugin/internal/buildSourcePlugin.ts} +121 -115
  124. package/src/plugin/internal/loadProjectPlugins.ts +214 -0
  125. package/src/structures/ITtscCompilerContext.ts +84 -0
  126. package/src/structures/ITtscCompilerDiagnostic.ts +91 -0
  127. package/src/structures/ITtscCompilerResult.ts +94 -0
  128. package/src/structures/ITtscCompilerTransformation.ts +46 -0
  129. package/src/structures/ITtscPlugin.ts +54 -7
  130. package/src/structures/ITtscPluginFactoryContext.ts +51 -0
  131. package/src/structures/ITtscProjectPluginConfig.ts +43 -0
  132. package/src/structures/TtscPluginStage.ts +18 -0
  133. package/src/structures/index.ts +8 -0
  134. package/src/structures/internal/ITtscLoadedNativePlugin.ts +14 -0
  135. package/src/structures/internal/ITtscParsedProjectConfig.ts +16 -0
  136. package/src/structures/internal/ITtscProjectLocatorOptions.ts +9 -0
  137. package/src/structures/internal/TtscBuildOptions.ts +19 -0
  138. package/src/structures/internal/TtscBuildResult.ts +15 -0
  139. package/src/structures/internal/TtscCommonOptions.ts +28 -0
  140. package/src/structures/internal/TtscSingleFileEmitOptions.ts +15 -0
  141. package/test/compiler.test.ts +422 -0
  142. package/test/platform.test.ts +1 -45
  143. package/test/project.test.ts +3 -1
  144. package/test/source-build.test.ts +33 -220
  145. package/test/tsgo.test.ts +1 -8
  146. package/cmd/ttsc/transform.go +0 -166
  147. package/lib/api.d.ts +0 -40
  148. package/lib/api.js +0 -680
  149. package/lib/api.js.map +0 -1
  150. package/lib/cli.d.ts +0 -1
  151. package/lib/cli.js.map +0 -1
  152. package/lib/native.d.ts +0 -8
  153. package/lib/native.js +0 -42
  154. package/lib/native.js.map +0 -1
  155. package/lib/platform.d.ts +0 -52
  156. package/lib/platform.js +0 -175
  157. package/lib/platform.js.map +0 -1
  158. package/lib/plugin.d.ts +0 -13
  159. package/lib/plugin.js.map +0 -1
  160. package/lib/project.d.ts +0 -14
  161. package/lib/project.js.map +0 -1
  162. package/lib/runner/cli.d.ts +0 -1
  163. package/lib/runner/cli.js.map +0 -1
  164. package/lib/runner/register.d.ts +0 -6
  165. package/lib/runner/register.js +0 -358
  166. package/lib/runner/register.js.map +0 -1
  167. package/lib/source-build.d.ts +0 -19
  168. package/lib/source-build.js.map +0 -1
  169. package/lib/structures/ITtscBuildOptions.d.ts +0 -14
  170. package/lib/structures/ITtscBuildOptions.js.map +0 -1
  171. package/lib/structures/ITtscBuildResult.d.ts +0 -6
  172. package/lib/structures/ITtscBuildResult.js.map +0 -1
  173. package/lib/structures/ITtscBuildSourcePluginOptions.d.ts +0 -8
  174. package/lib/structures/ITtscBuildSourcePluginOptions.js +0 -3
  175. package/lib/structures/ITtscBuildSourcePluginOptions.js.map +0 -1
  176. package/lib/structures/ITtscCacheKeyInput.d.ts +0 -7
  177. package/lib/structures/ITtscCacheKeyInput.js.map +0 -1
  178. package/lib/structures/ITtscCheckOptions.d.ts +0 -2
  179. package/lib/structures/ITtscCheckOptions.js.map +0 -1
  180. package/lib/structures/ITtscCommonOptions.d.ts +0 -22
  181. package/lib/structures/ITtscCommonOptions.js +0 -3
  182. package/lib/structures/ITtscCommonOptions.js.map +0 -1
  183. package/lib/structures/ITtscLoadPluginsOptions.d.ts +0 -8
  184. package/lib/structures/ITtscLoadPluginsOptions.js +0 -3
  185. package/lib/structures/ITtscLoadPluginsOptions.js.map +0 -1
  186. package/lib/structures/ITtscLoadedNativePlugin.d.ts +0 -7
  187. package/lib/structures/ITtscLoadedNativePlugin.js.map +0 -1
  188. package/lib/structures/ITtscLoadedPlugins.d.ts +0 -11
  189. package/lib/structures/ITtscLoadedPlugins.js +0 -3
  190. package/lib/structures/ITtscLoadedPlugins.js.map +0 -1
  191. package/lib/structures/ITtscNativeBackend.d.ts +0 -10
  192. package/lib/structures/ITtscNativeBackend.js +0 -3
  193. package/lib/structures/ITtscNativeBackend.js.map +0 -1
  194. package/lib/structures/ITtscNativePluginContractVersion.d.ts +0 -1
  195. package/lib/structures/ITtscNativePluginContractVersion.js +0 -3
  196. package/lib/structures/ITtscNativePluginContractVersion.js.map +0 -1
  197. package/lib/structures/ITtscNativePluginShape.d.ts +0 -8
  198. package/lib/structures/ITtscNativePluginShape.js +0 -3
  199. package/lib/structures/ITtscNativePluginShape.js.map +0 -1
  200. package/lib/structures/ITtscNativeRewriteMode.d.ts +0 -1
  201. package/lib/structures/ITtscNativeRewriteMode.js +0 -3
  202. package/lib/structures/ITtscNativeRewriteMode.js.map +0 -1
  203. package/lib/structures/ITtscNativeSource.d.ts +0 -4
  204. package/lib/structures/ITtscNativeSource.js.map +0 -1
  205. package/lib/structures/ITtscParsedProjectConfig.d.ts +0 -9
  206. package/lib/structures/ITtscParsedProjectConfig.js.map +0 -1
  207. package/lib/structures/ITtscPluginFactory.d.ts +0 -4
  208. package/lib/structures/ITtscPluginFactory.js +0 -3
  209. package/lib/structures/ITtscPluginFactory.js.map +0 -1
  210. package/lib/structures/ITtscPluginModule.d.ts +0 -3
  211. package/lib/structures/ITtscPluginModule.js +0 -3
  212. package/lib/structures/ITtscPluginModule.js.map +0 -1
  213. package/lib/structures/ITtscPreparedExecution.d.ts +0 -5
  214. package/lib/structures/ITtscPreparedExecution.js +0 -3
  215. package/lib/structures/ITtscPreparedExecution.js.map +0 -1
  216. package/lib/structures/ITtscProjectLocatorOptions.d.ts +0 -5
  217. package/lib/structures/ITtscProjectLocatorOptions.js.map +0 -1
  218. package/lib/structures/ITtscRegisterOptions.d.ts +0 -6
  219. package/lib/structures/ITtscRegisterOptions.js +0 -3
  220. package/lib/structures/ITtscRegisterOptions.js.map +0 -1
  221. package/lib/structures/ITtscResolveGoBinaryOptions.d.ts +0 -7
  222. package/lib/structures/ITtscResolveGoBinaryOptions.js +0 -3
  223. package/lib/structures/ITtscResolveGoBinaryOptions.js.map +0 -1
  224. package/lib/structures/ITtscResolveOptions.d.ts +0 -20
  225. package/lib/structures/ITtscResolveOptions.js +0 -3
  226. package/lib/structures/ITtscResolveOptions.js.map +0 -1
  227. package/lib/structures/ITtscResolvedTsgo.d.ts +0 -8
  228. package/lib/structures/ITtscResolvedTsgo.js +0 -3
  229. package/lib/structures/ITtscResolvedTsgo.js.map +0 -1
  230. package/lib/structures/ITtscTransformOptions.d.ts +0 -12
  231. package/lib/structures/ITtscTransformOptions.js +0 -3
  232. package/lib/structures/ITtscTransformOptions.js.map +0 -1
  233. package/lib/structures/ITtscTsgoResolveOptions.d.ts +0 -14
  234. package/lib/structures/ITtscTsgoResolveOptions.js +0 -3
  235. package/lib/structures/ITtscTsgoResolveOptions.js.map +0 -1
  236. package/lib/tsgo.d.ts +0 -6
  237. package/lib/tsgo.js.map +0 -1
  238. package/src/api.ts +0 -872
  239. package/src/native.ts +0 -64
  240. package/src/platform.ts +0 -151
  241. package/src/plugin.ts +0 -253
  242. package/src/runner/register.ts +0 -406
  243. package/src/structures/ITtscBuildOptions.ts +0 -19
  244. package/src/structures/ITtscBuildResult.ts +0 -6
  245. package/src/structures/ITtscBuildSourcePluginOptions.ts +0 -9
  246. package/src/structures/ITtscCacheKeyInput.ts +0 -7
  247. package/src/structures/ITtscCheckOptions.ts +0 -3
  248. package/src/structures/ITtscCommonOptions.ts +0 -23
  249. package/src/structures/ITtscLoadPluginsOptions.ts +0 -9
  250. package/src/structures/ITtscLoadedNativePlugin.ts +0 -8
  251. package/src/structures/ITtscLoadedPlugins.ts +0 -12
  252. package/src/structures/ITtscNativeBackend.ts +0 -11
  253. package/src/structures/ITtscNativePluginContractVersion.ts +0 -1
  254. package/src/structures/ITtscNativePluginShape.ts +0 -9
  255. package/src/structures/ITtscNativeRewriteMode.ts +0 -1
  256. package/src/structures/ITtscNativeSource.ts +0 -4
  257. package/src/structures/ITtscParsedProjectConfig.ts +0 -10
  258. package/src/structures/ITtscPluginFactory.ts +0 -7
  259. package/src/structures/ITtscPluginModule.ts +0 -4
  260. package/src/structures/ITtscPreparedExecution.ts +0 -5
  261. package/src/structures/ITtscProjectLocatorOptions.ts +0 -5
  262. package/src/structures/ITtscRegisterOptions.ts +0 -7
  263. package/src/structures/ITtscResolveGoBinaryOptions.ts +0 -7
  264. package/src/structures/ITtscResolveOptions.ts +0 -20
  265. package/src/structures/ITtscResolvedTsgo.ts +0 -8
  266. package/src/structures/ITtscTransformOptions.ts +0 -13
  267. package/src/structures/ITtscTsgoResolveOptions.ts +0 -14
  268. package/test/native.test.ts +0 -103
  269. /package/lib/structures/{ITtscLoadedNativePlugin.js → internal/ITtscLoadedNativePlugin.js} +0 -0
  270. /package/lib/structures/{ITtscParsedProjectConfig.js → internal/ITtscParsedProjectConfig.js} +0 -0
  271. /package/lib/structures/{ITtscProjectLocatorOptions.js → internal/ITtscProjectLocatorOptions.js} +0 -0
package/lib/api.js DELETED
@@ -1,680 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
- Object.defineProperty(exports, "__esModule", { value: true });
36
- exports.transform = transform;
37
- exports.build = build;
38
- exports.check = check;
39
- exports.version = version;
40
- exports.transformAsync = transformAsync;
41
- /**
42
- * Programmatic API for ttsc.
43
- *
44
- * This module is the TypeScript surface bundler adapters (unplugin, vite,
45
- * webpack, rollup, esbuild, rspack, farm, next/swc, bun) consume. Project
46
- * builds delegate to the consuming project's `@typescript/native-preview`
47
- * `tsgo` binary, while plugin-selected native sidecars remain opt-in.
48
- *
49
- * Contract:
50
- *
51
- * - `transform()` emits one file's rewritten JS, returning the text.
52
- * - `build()` runs the whole project (tsgo + ttsc rewrite + --emit).
53
- * - `check()` runs the analysis pass without emitting (CI gate use).
54
- * - `version()` returns the wrapper and resolved `tsgo` version banner.
55
- *
56
- * All helpers accept a `binary` override so tests can point at a specific
57
- * `tsgo` executable without touching PATH or node_modules.
58
- */
59
- const node_child_process_1 = require("node:child_process");
60
- const fs = __importStar(require("node:fs"));
61
- const os = __importStar(require("node:os"));
62
- const path = __importStar(require("node:path"));
63
- const platform_1 = require("./platform");
64
- const plugin_1 = require("./plugin");
65
- const project_1 = require("./project");
66
- const tsgo_1 = require("./tsgo");
67
- /** Merge spawn env without clobbering unrelated vars. */
68
- function mergeEnv(extra) {
69
- const base = {
70
- ...process.env,
71
- TTSC_NODE_BINARY: process.env.TTSC_NODE_BINARY ?? process.execPath,
72
- };
73
- if (!extra)
74
- return base;
75
- return { ...base, ...extra };
76
- }
77
- function nativePluginEnv(extra, execution) {
78
- return mergeEnv({
79
- TTSC_TSGO_BINARY: process.env.TTSC_TSGO_BINARY ?? execution.tsgo.binary,
80
- TTSC_TTSX_BINARY: process.env.TTSC_TTSX_BINARY ?? path.join(__dirname, "launcher", "ttsx.js"),
81
- ...extra,
82
- });
83
- }
84
- function spawnBinary(binary, args, options) {
85
- const viaNode = shouldSpawnViaNode(binary);
86
- if (!viaNode) {
87
- ensureExecutable(binary);
88
- }
89
- return (0, node_child_process_1.spawnSync)(viaNode ? process.execPath : binary, viaNode ? [binary, ...args] : [...args], {
90
- cwd: options.cwd,
91
- env: options.env,
92
- encoding: options.encoding,
93
- maxBuffer: 1024 * 1024 * 256,
94
- windowsHide: true,
95
- });
96
- }
97
- function shouldSpawnViaNode(binary) {
98
- return /\.(?:[cm]?js|ts)$/i.test(binary);
99
- }
100
- function ensureExecutable(binary) {
101
- if (process.platform === "win32") {
102
- return;
103
- }
104
- try {
105
- const mode = fs.statSync(binary).mode & 0o777;
106
- if ((mode & 0o111) !== 0) {
107
- return;
108
- }
109
- fs.chmodSync(binary, mode | 0o755);
110
- }
111
- catch {
112
- /* keep the original spawn error path */
113
- }
114
- }
115
- function outputText(value) {
116
- if (value == null) {
117
- return "";
118
- }
119
- return typeof value === "string" ? value : value.toString("utf8");
120
- }
121
- function hasCapability(plugin, capability) {
122
- return plugin.backend.capabilities?.includes(capability) === true;
123
- }
124
- function isOutputPlugin(plugin) {
125
- return hasCapability(plugin, "output");
126
- }
127
- function isCheckOnlyPlugin(plugin) {
128
- const capabilities = plugin.backend.capabilities ?? [];
129
- return (capabilities.length > 0 &&
130
- capabilities.every((capability) => capability === "check"));
131
- }
132
- function isCompilerPlugin(plugin) {
133
- return !isOutputPlugin(plugin) && !isCheckOnlyPlugin(plugin);
134
- }
135
- function outputPlugins(plugins) {
136
- return plugins.filter(isOutputPlugin);
137
- }
138
- function checkOnlyPlugins(plugins) {
139
- return plugins.filter(isCheckOnlyPlugin);
140
- }
141
- function compilerPlugins(plugins) {
142
- return plugins.filter(isCompilerPlugin);
143
- }
144
- /**
145
- * Transform a single .ts file and return the rewritten JS as a string.
146
- *
147
- * Intended for bundler per-file transforms (unplugin `transform()`). The caller
148
- * passes the absolute path; ttsc loads the enclosing tsconfig, compiles with
149
- * tsgo, and returns the rewritten JS.
150
- *
151
- * Throws when the binary exits non-zero — the error includes stderr so bundler
152
- * error overlays surface the real cause.
153
- */
154
- function transform(options) {
155
- const execution = resolveExecutionContext(options);
156
- const sourceFile = realpathIfExists(path.isAbsolute(options.file)
157
- ? options.file
158
- : path.resolve(options.cwd ?? process.cwd(), options.file));
159
- if (execution.nativePlugins.length > 0) {
160
- return transformWithNativePlugins(options, execution, sourceFile);
161
- }
162
- const tempOutDir = fs.mkdtempSync(path.join(os.tmpdir(), "ttsc-transform-"));
163
- try {
164
- const result = build({
165
- ...options,
166
- emit: true,
167
- outDir: tempOutDir,
168
- plugins: false,
169
- tsconfig: execution.tsconfig,
170
- });
171
- if (result.status !== 0) {
172
- throw new Error("ttsc.transform exited " +
173
- result.status +
174
- "\n" +
175
- (result.stderr || result.stdout));
176
- }
177
- const emitted = findEmittedFile(tempOutDir, execution.projectRoot, sourceFile);
178
- if (!emitted) {
179
- throw new Error(`ttsc.transform: no output produced for ${sourceFile}`);
180
- }
181
- const transformed = fs.readFileSync(emitted, "utf8");
182
- if (options.out) {
183
- fs.mkdirSync(path.dirname(options.out), { recursive: true });
184
- fs.writeFileSync(options.out, transformed, "utf8");
185
- }
186
- return transformed;
187
- }
188
- finally {
189
- fs.rmSync(tempOutDir, { recursive: true, force: true });
190
- }
191
- }
192
- function transformWithNativePlugins(options, execution, sourceFile) {
193
- const checked = runNativeCheckPlugins(options, execution);
194
- if (checked.status !== 0) {
195
- throw new Error("ttsc.transform exited " + checked.status + "\n" + checked.stderr);
196
- }
197
- const compilers = compilerPlugins(execution.nativePlugins);
198
- const outputs = outputPlugins(execution.nativePlugins);
199
- const tempOutDir = fs.mkdtempSync(path.join(os.tmpdir(), "ttsc-transform-"));
200
- try {
201
- let emitted = null;
202
- if (compilers.length !== 0) {
203
- assertSingleCompilerHost(compilers);
204
- emitted = path.join(tempOutDir, "transform.js");
205
- const transformed = runNativeCompilerTransform(options, execution, sourceFile, emitted, compilers);
206
- if (transformed.status !== 0) {
207
- throw new Error("ttsc.transform exited " +
208
- transformed.status +
209
- "\n" +
210
- transformed.stderr);
211
- }
212
- }
213
- else {
214
- const result = build({
215
- ...options,
216
- emit: true,
217
- outDir: tempOutDir,
218
- plugins: false,
219
- skipDiagnosticsCheck: checkOnlyPlugins(execution.nativePlugins).length !== 0,
220
- tsconfig: execution.tsconfig,
221
- });
222
- if (result.status !== 0) {
223
- throw new Error("ttsc.transform exited " +
224
- result.status +
225
- "\n" +
226
- (result.stderr || result.stdout));
227
- }
228
- emitted = findEmittedFile(tempOutDir, execution.projectRoot, sourceFile);
229
- if (!emitted) {
230
- throw new Error(`ttsc.transform: no output produced for ${sourceFile}`);
231
- }
232
- }
233
- for (const plugin of outputs) {
234
- const result = runNativeOutputPlugin(options, execution, plugin, emitted);
235
- if (result.status !== 0) {
236
- throw new Error("ttsc.transform exited " +
237
- result.status +
238
- "\n" +
239
- (result.stderr || result.stdout));
240
- }
241
- }
242
- const transformed = fs.readFileSync(emitted, "utf8");
243
- if (options.out) {
244
- fs.mkdirSync(path.dirname(options.out), { recursive: true });
245
- fs.writeFileSync(options.out, transformed, "utf8");
246
- }
247
- return transformed;
248
- }
249
- finally {
250
- fs.rmSync(tempOutDir, { recursive: true, force: true });
251
- }
252
- }
253
- function realpathIfExists(file) {
254
- try {
255
- return fs.realpathSync(file);
256
- }
257
- catch {
258
- return file;
259
- }
260
- }
261
- /**
262
- * Run `ttsc` against a tsconfig. Returns once the binary exits so the caller
263
- * can decide how to surface diagnostics. Does not throw on non-zero exit —
264
- * bundler pipelines often want to continue and collect errors.
265
- */
266
- function build(options = {}) {
267
- const execution = resolveExecutionContext(options);
268
- if (execution.nativePlugins.length > 0) {
269
- const compilers = compilerPlugins(execution.nativePlugins);
270
- const outputs = outputPlugins(execution.nativePlugins);
271
- const checked = runNativeCheckPlugins(options, execution);
272
- if (checked.status !== 0) {
273
- return checked;
274
- }
275
- if (options.emit === false) {
276
- if (compilers.length !== 0) {
277
- assertSingleCompilerHost(compilers);
278
- return appendBuildOutput(checked, buildWithNativeCompilerPlugins(options, execution, compilers));
279
- }
280
- if (checked.stdout !== "" || checked.stderr !== "") {
281
- return checked;
282
- }
283
- return runTsgo(execution, ["--noEmit"], options);
284
- }
285
- let result;
286
- if (compilers.length !== 0) {
287
- assertSingleCompilerHost(compilers);
288
- result = appendBuildOutput(checked, buildWithNativeCompilerPlugins(options, execution, compilers));
289
- }
290
- else {
291
- if (checked.stdout === "" &&
292
- checked.stderr === "" &&
293
- options.skipDiagnosticsCheck !== true) {
294
- const tsgoChecked = runTsgo(execution, ["--noEmit"], options);
295
- if (tsgoChecked.status !== 0) {
296
- return tsgoChecked;
297
- }
298
- }
299
- const args = createTsgoBuildArgs(execution, options, {
300
- listEmittedFiles: outputs.length !== 0 || options.forceListEmittedFiles === true,
301
- });
302
- const emitted = runTsgoBuild(execution, options, args);
303
- result = appendBuildOutput(checked, emitted);
304
- }
305
- if (result.status !== 0 || outputs.length === 0) {
306
- return result;
307
- }
308
- return appendBuildOutput(result, applyOutputPlugins(options, execution, result.emittedFiles ?? [], outputs));
309
- }
310
- if (options.emit !== false && options.skipDiagnosticsCheck !== true) {
311
- const checked = runTsgo(execution, ["--noEmit"], options);
312
- if (checked.status !== 0) {
313
- return checked;
314
- }
315
- }
316
- const args = createTsgoBuildArgs(execution, options, {
317
- listEmittedFiles: options.emit !== false && options.forceListEmittedFiles === true,
318
- });
319
- return runTsgoBuild(execution, options, args);
320
- }
321
- function buildWithNativeCompilerPlugins(options, execution, plugins) {
322
- return runNativePluginCommand(plugins[0], createNativeBuildArgs(execution, options, plugins), options, execution, "ttsc.build");
323
- }
324
- /**
325
- * Run `ttsc check` (build without emit) — CI gate / pre-commit hook use.
326
- * Resolves with an exit-code record; does not throw.
327
- */
328
- function check(options = {}) {
329
- return build({ ...options, emit: false });
330
- }
331
- function runTsgo(execution, extraArgs, options) {
332
- const res = spawnBinary(execution.tsgo.binary, ["-p", execution.tsconfig, ...extraArgs], {
333
- cwd: execution.projectRoot,
334
- env: mergeEnv(options.env),
335
- encoding: "utf8",
336
- });
337
- if (res.error) {
338
- throw new Error("ttsc: failed to spawn " + execution.tsgo.binary + ": " + res.error.message);
339
- }
340
- return normalizeFailedDiagnostics({
341
- status: res.status ?? 1,
342
- stdout: outputText(res.stdout),
343
- stderr: outputText(res.stderr),
344
- });
345
- }
346
- function runTsgoBuild(execution, options, args) {
347
- const res = spawnBinary(execution.tsgo.binary, args, {
348
- cwd: execution.projectRoot,
349
- env: mergeEnv(options.env),
350
- encoding: "utf8",
351
- });
352
- if (res.error) {
353
- throw new Error("ttsc.build: failed to spawn " +
354
- execution.tsgo.binary +
355
- ": " +
356
- res.error.message);
357
- }
358
- const result = {
359
- status: res.status ?? 1,
360
- stdout: outputText(res.stdout),
361
- stderr: outputText(res.stderr),
362
- };
363
- const emittedFiles = parseEmittedFiles(result.stdout);
364
- if (emittedFiles.length !== 0) {
365
- result.stdout = stripEmittedFileLines(result.stdout);
366
- }
367
- return normalizeFailedDiagnostics({ ...result, emittedFiles });
368
- }
369
- function createTsgoBuildArgs(execution, options, flags) {
370
- const args = ["-p", execution.tsconfig];
371
- if (options.emit === true) {
372
- args.push("--noEmit", "false", "--emitDeclarationOnly", "false");
373
- }
374
- else if (options.emit === false) {
375
- args.push("--noEmit");
376
- }
377
- if (options.outDir) {
378
- args.push("--outDir", path.resolve(execution.cwd, options.outDir));
379
- }
380
- if (flags.listEmittedFiles) {
381
- args.push("--listEmittedFiles");
382
- }
383
- return args;
384
- }
385
- function createNativeBuildArgs(execution, options, plugins) {
386
- const args = [
387
- options.emit === false ? "check" : "build",
388
- "--tsconfig=" + execution.tsconfig,
389
- "--rewrite-mode=" + (options.rewriteMode ?? plugins[0]?.backend.mode ?? "none"),
390
- "--plugins-json=" + serializeNativePlugins(plugins),
391
- "--cwd=" + execution.projectRoot,
392
- ];
393
- if (options.emit === true) {
394
- args.push("--emit");
395
- }
396
- else if (options.emit === false) {
397
- args.push("--noEmit");
398
- }
399
- if (options.outDir) {
400
- args.push("--outDir=" + path.resolve(execution.cwd, options.outDir));
401
- }
402
- if (options.quiet === false) {
403
- args.push("--verbose");
404
- }
405
- else if (options.quiet === true) {
406
- args.push("--quiet");
407
- }
408
- return args;
409
- }
410
- function createNativeCheckArgs(execution, options, plugin) {
411
- const args = [
412
- "check",
413
- "--tsconfig=" + execution.tsconfig,
414
- "--rewrite-mode=" + (options.rewriteMode ?? plugin.backend.mode),
415
- "--plugins-json=" + serializeNativePlugins(execution.nativePlugins),
416
- "--cwd=" + execution.projectRoot,
417
- ];
418
- if ("outDir" in options && options.outDir) {
419
- args.push("--outDir=" + path.resolve(execution.cwd, options.outDir));
420
- }
421
- if ("quiet" in options && options.quiet === false) {
422
- args.push("--verbose");
423
- }
424
- else if ("quiet" in options && options.quiet === true) {
425
- args.push("--quiet");
426
- }
427
- return args;
428
- }
429
- function createNativeOutputArgs(execution, options, plugin, file) {
430
- const args = [
431
- "output",
432
- "--file=" + file,
433
- "--tsconfig=" + execution.tsconfig,
434
- "--rewrite-mode=" + (options.rewriteMode ?? plugin.backend.mode),
435
- "--plugins-json=" + serializeNativePlugins(execution.nativePlugins),
436
- "--cwd=" + execution.projectRoot,
437
- ];
438
- if ("outDir" in options && options.outDir) {
439
- args.push("--outDir=" + path.resolve(execution.cwd, options.outDir));
440
- }
441
- return args;
442
- }
443
- function createNativeTransformArgs(execution, options, sourceFile, out, plugins) {
444
- return [
445
- "transform",
446
- "--file=" + sourceFile,
447
- "--out=" + out,
448
- "--tsconfig=" + execution.tsconfig,
449
- "--rewrite-mode=" + (options.rewriteMode ?? plugins[0]?.backend.mode ?? "none"),
450
- "--plugins-json=" + serializeNativePlugins(plugins),
451
- ];
452
- }
453
- function serializeNativePlugins(plugins) {
454
- return JSON.stringify(plugins.map((plugin) => ({
455
- config: plugin.config,
456
- contractVersion: plugin.backend.contractVersion,
457
- mode: plugin.backend.mode,
458
- name: plugin.name,
459
- })));
460
- }
461
- function runNativeCheckPlugins(options, execution) {
462
- let out = { status: 0, stdout: "", stderr: "" };
463
- for (const plugin of checkOnlyPlugins(execution.nativePlugins)) {
464
- const result = runNativePluginCommand(plugin, createNativeCheckArgs(execution, options, plugin), options, execution, "ttsc.check");
465
- out = appendBuildOutput(out, result);
466
- if (result.status !== 0) {
467
- return out;
468
- }
469
- }
470
- return out;
471
- }
472
- function runNativeCompilerTransform(options, execution, sourceFile, out, plugins) {
473
- return runNativePluginCommand(plugins[0], createNativeTransformArgs(execution, options, sourceFile, out, plugins), options, execution, "ttsc.transform");
474
- }
475
- function applyOutputPlugins(options, execution, emittedFiles, plugins) {
476
- let out = { status: 0, stdout: "", stderr: "" };
477
- for (const plugin of plugins) {
478
- for (const file of emittedFiles) {
479
- if (!fs.existsSync(file)) {
480
- continue;
481
- }
482
- const result = runNativeOutputPlugin(options, execution, plugin, file);
483
- out = appendBuildOutput(out, result);
484
- if (result.status !== 0) {
485
- return out;
486
- }
487
- }
488
- }
489
- return out;
490
- }
491
- function runNativeOutputPlugin(options, execution, plugin, file) {
492
- return runNativePluginCommand(plugin, createNativeOutputArgs(execution, options, plugin, file), options, execution, "ttsc.output");
493
- }
494
- function runNativePluginCommand(plugin, args, options, execution, label) {
495
- const binary = plugin.backend.binary;
496
- if (!binary) {
497
- return {
498
- status: 2,
499
- stdout: "",
500
- stderr: `${label}: plugin "${plugin.name}" requires a version-matched binary\n`,
501
- };
502
- }
503
- const res = spawnBinary(binary, args, {
504
- cwd: execution.projectRoot,
505
- env: nativePluginEnv(options.env, execution),
506
- encoding: "utf8",
507
- });
508
- if (res.error) {
509
- throw new Error(`${label}: failed to spawn ${binary}: ${res.error.message}`);
510
- }
511
- return normalizeFailedDiagnostics({
512
- status: res.status ?? 1,
513
- stdout: outputText(res.stdout),
514
- stderr: outputText(res.stderr),
515
- });
516
- }
517
- function appendBuildOutput(left, right) {
518
- return normalizeFailedDiagnostics({
519
- emittedFiles: right.emittedFiles !== undefined ? right.emittedFiles : left.emittedFiles,
520
- status: right.status !== 0 ? right.status : left.status,
521
- stdout: left.stdout + right.stdout,
522
- stderr: left.stderr + right.stderr,
523
- });
524
- }
525
- function assertSingleCompilerHost(plugins) {
526
- const binaries = [
527
- ...new Set(plugins
528
- .map((plugin) => plugin.backend.binary)
529
- .filter((binary) => typeof binary === "string")),
530
- ];
531
- if (binaries.length > 1) {
532
- throw new Error("ttsc: multiple compiler native backends cannot share one emit pass; " +
533
- "use output-capability plugins for post-emit transforms");
534
- }
535
- }
536
- /** Ask the binary for its version banner. Handy for user-agent strings. */
537
- function version(options = {}) {
538
- const tsgo = (0, tsgo_1.resolveTsgo)(options);
539
- const res = spawnBinary(tsgo.binary, ["--version"], {
540
- encoding: "utf8",
541
- });
542
- if (res.error || res.status !== 0) {
543
- throw new Error("ttsc.version: failed: " + (outputText(res.stderr) || res.error?.message));
544
- }
545
- return `ttsc ${readOwnPackageVersion()} (${outputText(res.stdout).trim()})`;
546
- }
547
- /**
548
- * Promise-facing variant of `transform()`. The host path stays synchronous so
549
- * plugin descriptors can stay dependency-free, but many adapter surfaces still
550
- * prefer a Promise-returning function.
551
- */
552
- function transformAsync(options) {
553
- return Promise.resolve().then(() => transform(options));
554
- }
555
- function resolveExecutionContext(options) {
556
- const cwd = path.resolve(options.cwd ?? process.cwd());
557
- const tsconfig = (0, project_1.resolveProjectConfig)({
558
- cwd,
559
- tsconfig: options.tsconfig,
560
- });
561
- const projectRoot = (0, project_1.resolveProjectRoot)({ cwd, tsconfig });
562
- const tsgo = (0, tsgo_1.resolveTsgo)({ ...options, cwd: projectRoot });
563
- const fallbackBinary = (0, platform_1.resolveBinary)(options);
564
- const loaded = (0, plugin_1.loadProjectPlugins)({
565
- binary: fallbackBinary ?? "",
566
- cwd,
567
- entries: options.plugins,
568
- tsconfig,
569
- });
570
- return {
571
- compilerOptions: loaded.project.compilerOptions,
572
- cwd,
573
- nativePlugins: loaded.nativePlugins,
574
- projectRoot,
575
- tsgo,
576
- tsconfig,
577
- };
578
- }
579
- function isJavaScriptOutput(file) {
580
- return /\.(?:[cm]?js)$/i.test(file);
581
- }
582
- function parseEmittedFiles(stdout) {
583
- const out = [];
584
- for (const line of stdout.split(/\r?\n/)) {
585
- const match = line.match(/^TSFILE:\s*(.+)$/);
586
- if (match?.[1]) {
587
- out.push(path.resolve(match[1].trim()));
588
- }
589
- }
590
- return out;
591
- }
592
- function stripEmittedFileLines(stdout) {
593
- return stdout
594
- .split(/\r?\n/)
595
- .filter((line) => !/^TSFILE:\s*/.test(line))
596
- .join("\n")
597
- .replace(/\n+$/, "");
598
- }
599
- function normalizeFailedDiagnostics(result) {
600
- if (result.status === 0 || result.stderr.trim().length !== 0) {
601
- return result;
602
- }
603
- if (result.stdout.trim().length === 0) {
604
- return result;
605
- }
606
- return {
607
- emittedFiles: result.emittedFiles,
608
- status: result.status,
609
- stdout: "",
610
- stderr: result.stdout,
611
- };
612
- }
613
- function findEmittedFile(outDir, projectRoot, sourceFile) {
614
- let best = null;
615
- let bestScore = 0;
616
- for (const file of listJavaScriptFiles(outDir)) {
617
- const score = sharedSourceStemSegments(file, sourceFile);
618
- if (score > bestScore) {
619
- best = file;
620
- bestScore = score;
621
- }
622
- }
623
- if (best) {
624
- return best;
625
- }
626
- const relative = path.relative(projectRoot, sourceFile);
627
- if (relative && !relative.startsWith("..") && !path.isAbsolute(relative)) {
628
- const exact = path.resolve(outDir, relative).replace(/\.[cm]?tsx?$/i, ".js");
629
- if (fs.existsSync(exact)) {
630
- return exact;
631
- }
632
- }
633
- return null;
634
- }
635
- function listJavaScriptFiles(root) {
636
- const out = [];
637
- const stack = [root];
638
- while (stack.length !== 0) {
639
- const current = stack.pop();
640
- if (!fs.existsSync(current))
641
- continue;
642
- for (const entry of fs.readdirSync(current, { withFileTypes: true })) {
643
- const next = path.join(current, entry.name);
644
- if (entry.isDirectory()) {
645
- stack.push(next);
646
- }
647
- else if (entry.isFile() && isJavaScriptOutput(next)) {
648
- out.push(next);
649
- }
650
- }
651
- }
652
- return out;
653
- }
654
- function sharedSourceStemSegments(outPath, srcPath) {
655
- const trim = (value) => {
656
- const normalized = value.replace(/\\/g, "/");
657
- return normalized.replace(/\.[^.]+$/, "").split("/");
658
- };
659
- const a = trim(outPath);
660
- const b = trim(srcPath);
661
- const n = Math.min(a.length, b.length);
662
- let shared = 0;
663
- for (let i = 1; i <= n; i += 1) {
664
- if (a[a.length - i] !== b[b.length - i])
665
- break;
666
- shared += 1;
667
- }
668
- return shared;
669
- }
670
- function readOwnPackageVersion() {
671
- try {
672
- const file = path.resolve(__dirname, "..", "package.json");
673
- const pkg = JSON.parse(fs.readFileSync(file, "utf8"));
674
- return pkg.version ?? "0.0.0";
675
- }
676
- catch {
677
- return "0.0.0";
678
- }
679
- }
680
- //# sourceMappingURL=api.js.map