ttsc 0.6.0 → 0.7.0

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 (261) hide show
  1. package/cmd/ttsc/api_compile.go +134 -0
  2. package/cmd/ttsc/build.go +1 -7
  3. package/cmd/ttsc/main.go +2 -2
  4. package/cmd/ttsc/transform.go +0 -6
  5. package/driver/program.go +11 -1
  6. package/lib/TtscCompiler.d.ts +76 -0
  7. package/lib/TtscCompiler.js +245 -0
  8. package/lib/TtscCompiler.js.map +1 -0
  9. package/lib/compiler/internal/buildNativeCompiler.d.ts +6 -0
  10. package/lib/compiler/internal/buildNativeCompiler.js +72 -0
  11. package/lib/compiler/internal/buildNativeCompiler.js.map +1 -0
  12. package/lib/compiler/internal/compileProjectInMemory.d.ts +7 -0
  13. package/lib/compiler/internal/compileProjectInMemory.js +176 -0
  14. package/lib/compiler/internal/compileProjectInMemory.js.map +1 -0
  15. package/lib/compiler/internal/project/readProjectConfig.d.ts +4 -0
  16. package/lib/{project.js → compiler/internal/project/readProjectConfig.js} +12 -82
  17. package/lib/compiler/internal/project/readProjectConfig.js.map +1 -0
  18. package/lib/compiler/internal/project/resolveProjectConfig.d.ts +3 -0
  19. package/lib/compiler/internal/project/resolveProjectConfig.js +94 -0
  20. package/lib/compiler/internal/project/resolveProjectConfig.js.map +1 -0
  21. package/lib/compiler/internal/resolveBinary.d.ts +4 -0
  22. package/lib/compiler/internal/resolveBinary.js +65 -0
  23. package/lib/compiler/internal/resolveBinary.js.map +1 -0
  24. package/lib/compiler/internal/resolveEmittedJavaScript.d.ts +7 -0
  25. package/lib/compiler/internal/resolveEmittedJavaScript.js +118 -0
  26. package/lib/compiler/internal/resolveEmittedJavaScript.js.map +1 -0
  27. package/lib/compiler/internal/resolveTsgo.d.ts +20 -0
  28. package/lib/{tsgo.js → compiler/internal/resolveTsgo.js} +5 -10
  29. package/lib/compiler/internal/resolveTsgo.js.map +1 -0
  30. package/lib/compiler/internal/runBuild.d.ts +10 -0
  31. package/lib/compiler/internal/runBuild.js +480 -0
  32. package/lib/compiler/internal/runBuild.js.map +1 -0
  33. package/lib/compiler/internal/runTransform.d.ts +3 -0
  34. package/lib/compiler/internal/runTransform.js +100 -0
  35. package/lib/compiler/internal/runTransform.js.map +1 -0
  36. package/lib/index.d.ts +6 -15
  37. package/lib/index.js +6 -15
  38. package/lib/index.js.map +1 -1
  39. package/lib/launcher/internal/getCompilerVersionText.d.ts +3 -0
  40. package/lib/launcher/internal/getCompilerVersionText.js +71 -0
  41. package/lib/launcher/internal/getCompilerVersionText.js.map +1 -0
  42. package/lib/launcher/internal/prepareExecution.d.ts +10 -0
  43. package/lib/launcher/internal/prepareExecution.js +120 -0
  44. package/lib/launcher/internal/prepareExecution.js.map +1 -0
  45. package/lib/launcher/internal/runTtsc.d.ts +1 -0
  46. package/lib/{cli.js → launcher/internal/runTtsc.js} +48 -47
  47. package/lib/launcher/internal/runTtsc.js.map +1 -0
  48. package/lib/launcher/internal/runTtsx.d.ts +1 -0
  49. package/lib/{runner/cli.js → launcher/internal/runTtsx.js} +13 -35
  50. package/lib/launcher/internal/runTtsx.js.map +1 -0
  51. package/lib/launcher/ttsc.js +2 -2
  52. package/lib/launcher/ttsc.js.map +1 -1
  53. package/lib/launcher/ttsx.js +2 -2
  54. package/lib/launcher/ttsx.js.map +1 -1
  55. package/lib/plugin/internal/buildSourcePlugin.d.ts +12 -0
  56. package/lib/{source-build.js → plugin/internal/buildSourcePlugin.js} +77 -84
  57. package/lib/plugin/internal/buildSourcePlugin.js.map +1 -0
  58. package/lib/plugin/internal/loadProjectPlugins.d.ts +14 -0
  59. package/lib/{plugin.js → plugin/internal/loadProjectPlugins.js} +38 -76
  60. package/lib/plugin/internal/loadProjectPlugins.js.map +1 -0
  61. package/lib/structures/ITtscCompilerContext.d.ts +78 -0
  62. package/lib/structures/ITtscCompilerContext.js +3 -0
  63. package/lib/structures/ITtscCompilerContext.js.map +1 -0
  64. package/lib/structures/ITtscCompilerDiagnostic.d.ts +83 -0
  65. package/lib/structures/ITtscCompilerDiagnostic.js +3 -0
  66. package/lib/structures/ITtscCompilerDiagnostic.js.map +1 -0
  67. package/lib/structures/ITtscCompilerResult.d.ts +83 -0
  68. package/lib/structures/{ITtscCacheKeyInput.js → ITtscCompilerResult.js} +1 -1
  69. package/lib/structures/ITtscCompilerResult.js.map +1 -0
  70. package/lib/structures/ITtscPlugin.d.ts +52 -7
  71. package/lib/structures/ITtscPluginFactoryContext.d.ts +47 -0
  72. package/lib/structures/ITtscProjectPluginConfig.d.ts +41 -0
  73. package/lib/structures/TtscPluginStage.d.ts +15 -0
  74. package/lib/structures/{ITtscBuildResult.js → TtscPluginStage.js} +1 -1
  75. package/lib/structures/TtscPluginStage.js.map +1 -0
  76. package/lib/structures/index.d.ts +7 -0
  77. package/lib/structures/index.js +24 -0
  78. package/lib/structures/index.js.map +1 -0
  79. package/lib/structures/internal/ITtscLoadedNativePlugin.d.ts +13 -0
  80. package/lib/structures/internal/ITtscLoadedNativePlugin.js.map +1 -0
  81. package/lib/structures/internal/ITtscParsedProjectConfig.d.ts +15 -0
  82. package/lib/structures/internal/ITtscParsedProjectConfig.js.map +1 -0
  83. package/lib/structures/internal/ITtscProjectLocatorOptions.d.ts +9 -0
  84. package/lib/structures/internal/ITtscProjectLocatorOptions.js.map +1 -0
  85. package/lib/structures/internal/TtscBuildOptions.d.ts +18 -0
  86. package/lib/structures/{ITtscBuildOptions.js → internal/TtscBuildOptions.js} +1 -1
  87. package/lib/structures/internal/TtscBuildOptions.js.map +1 -0
  88. package/lib/structures/internal/TtscBuildResult.d.ts +14 -0
  89. package/lib/structures/{ITtscCheckOptions.js → internal/TtscBuildResult.js} +1 -1
  90. package/lib/structures/internal/TtscBuildResult.js.map +1 -0
  91. package/lib/structures/internal/TtscCommonOptions.d.ts +27 -0
  92. package/lib/structures/{ITtscNativeSource.js → internal/TtscCommonOptions.js} +1 -1
  93. package/lib/structures/internal/TtscCommonOptions.js.map +1 -0
  94. package/lib/structures/internal/TtscTransformOptions.d.ts +14 -0
  95. package/lib/structures/internal/TtscTransformOptions.js +3 -0
  96. package/lib/structures/internal/TtscTransformOptions.js.map +1 -0
  97. package/package.json +9 -9
  98. package/src/TtscCompiler.ts +241 -0
  99. package/src/compiler/internal/buildNativeCompiler.ts +43 -0
  100. package/src/compiler/internal/compileProjectInMemory.ts +189 -0
  101. package/src/{project.ts → compiler/internal/project/readProjectConfig.ts} +36 -111
  102. package/src/compiler/internal/project/resolveProjectConfig.ts +67 -0
  103. package/src/compiler/internal/resolveBinary.ts +41 -0
  104. package/src/compiler/internal/resolveEmittedJavaScript.ts +102 -0
  105. package/src/{tsgo.ts → compiler/internal/resolveTsgo.ts} +20 -23
  106. package/src/compiler/internal/runBuild.ts +644 -0
  107. package/src/compiler/internal/runTransform.ts +69 -0
  108. package/src/index.ts +6 -15
  109. package/src/launcher/internal/getCompilerVersionText.ts +44 -0
  110. package/src/launcher/internal/prepareExecution.ts +115 -0
  111. package/src/{cli.ts → launcher/internal/runTtsc.ts} +61 -70
  112. package/src/{runner/cli.ts → launcher/internal/runTtsx.ts} +21 -49
  113. package/src/launcher/ttsc.ts +2 -2
  114. package/src/launcher/ttsx.ts +2 -2
  115. package/src/{source-build.ts → plugin/internal/buildSourcePlugin.ts} +121 -115
  116. package/src/plugin/internal/loadProjectPlugins.ts +214 -0
  117. package/src/structures/ITtscCompilerContext.ts +84 -0
  118. package/src/structures/ITtscCompilerDiagnostic.ts +91 -0
  119. package/src/structures/ITtscCompilerResult.ts +94 -0
  120. package/src/structures/ITtscPlugin.ts +54 -7
  121. package/src/structures/ITtscPluginFactoryContext.ts +51 -0
  122. package/src/structures/ITtscProjectPluginConfig.ts +43 -0
  123. package/src/structures/TtscPluginStage.ts +15 -0
  124. package/src/structures/index.ts +7 -0
  125. package/src/structures/internal/ITtscLoadedNativePlugin.ts +14 -0
  126. package/src/structures/internal/ITtscParsedProjectConfig.ts +16 -0
  127. package/src/structures/internal/ITtscProjectLocatorOptions.ts +9 -0
  128. package/src/structures/internal/TtscBuildOptions.ts +19 -0
  129. package/src/structures/internal/TtscBuildResult.ts +15 -0
  130. package/src/structures/internal/TtscCommonOptions.ts +28 -0
  131. package/src/structures/internal/TtscTransformOptions.ts +15 -0
  132. package/test/compiler.test.ts +234 -0
  133. package/test/platform.test.ts +1 -45
  134. package/test/project.test.ts +3 -1
  135. package/test/source-build.test.ts +33 -220
  136. package/test/tsgo.test.ts +1 -8
  137. package/lib/api.d.ts +0 -40
  138. package/lib/api.js +0 -680
  139. package/lib/api.js.map +0 -1
  140. package/lib/cli.d.ts +0 -1
  141. package/lib/cli.js.map +0 -1
  142. package/lib/native.d.ts +0 -8
  143. package/lib/native.js +0 -42
  144. package/lib/native.js.map +0 -1
  145. package/lib/platform.d.ts +0 -52
  146. package/lib/platform.js +0 -175
  147. package/lib/platform.js.map +0 -1
  148. package/lib/plugin.d.ts +0 -13
  149. package/lib/plugin.js.map +0 -1
  150. package/lib/project.d.ts +0 -14
  151. package/lib/project.js.map +0 -1
  152. package/lib/runner/cli.d.ts +0 -1
  153. package/lib/runner/cli.js.map +0 -1
  154. package/lib/runner/register.d.ts +0 -6
  155. package/lib/runner/register.js +0 -358
  156. package/lib/runner/register.js.map +0 -1
  157. package/lib/source-build.d.ts +0 -19
  158. package/lib/source-build.js.map +0 -1
  159. package/lib/structures/ITtscBuildOptions.d.ts +0 -14
  160. package/lib/structures/ITtscBuildOptions.js.map +0 -1
  161. package/lib/structures/ITtscBuildResult.d.ts +0 -6
  162. package/lib/structures/ITtscBuildResult.js.map +0 -1
  163. package/lib/structures/ITtscBuildSourcePluginOptions.d.ts +0 -8
  164. package/lib/structures/ITtscBuildSourcePluginOptions.js +0 -3
  165. package/lib/structures/ITtscBuildSourcePluginOptions.js.map +0 -1
  166. package/lib/structures/ITtscCacheKeyInput.d.ts +0 -7
  167. package/lib/structures/ITtscCacheKeyInput.js.map +0 -1
  168. package/lib/structures/ITtscCheckOptions.d.ts +0 -2
  169. package/lib/structures/ITtscCheckOptions.js.map +0 -1
  170. package/lib/structures/ITtscCommonOptions.d.ts +0 -22
  171. package/lib/structures/ITtscCommonOptions.js +0 -3
  172. package/lib/structures/ITtscCommonOptions.js.map +0 -1
  173. package/lib/structures/ITtscLoadPluginsOptions.d.ts +0 -8
  174. package/lib/structures/ITtscLoadPluginsOptions.js +0 -3
  175. package/lib/structures/ITtscLoadPluginsOptions.js.map +0 -1
  176. package/lib/structures/ITtscLoadedNativePlugin.d.ts +0 -7
  177. package/lib/structures/ITtscLoadedNativePlugin.js.map +0 -1
  178. package/lib/structures/ITtscLoadedPlugins.d.ts +0 -11
  179. package/lib/structures/ITtscLoadedPlugins.js +0 -3
  180. package/lib/structures/ITtscLoadedPlugins.js.map +0 -1
  181. package/lib/structures/ITtscNativeBackend.d.ts +0 -10
  182. package/lib/structures/ITtscNativeBackend.js +0 -3
  183. package/lib/structures/ITtscNativeBackend.js.map +0 -1
  184. package/lib/structures/ITtscNativePluginContractVersion.d.ts +0 -1
  185. package/lib/structures/ITtscNativePluginContractVersion.js +0 -3
  186. package/lib/structures/ITtscNativePluginContractVersion.js.map +0 -1
  187. package/lib/structures/ITtscNativePluginShape.d.ts +0 -8
  188. package/lib/structures/ITtscNativePluginShape.js +0 -3
  189. package/lib/structures/ITtscNativePluginShape.js.map +0 -1
  190. package/lib/structures/ITtscNativeRewriteMode.d.ts +0 -1
  191. package/lib/structures/ITtscNativeRewriteMode.js +0 -3
  192. package/lib/structures/ITtscNativeRewriteMode.js.map +0 -1
  193. package/lib/structures/ITtscNativeSource.d.ts +0 -4
  194. package/lib/structures/ITtscNativeSource.js.map +0 -1
  195. package/lib/structures/ITtscParsedProjectConfig.d.ts +0 -9
  196. package/lib/structures/ITtscParsedProjectConfig.js.map +0 -1
  197. package/lib/structures/ITtscPluginFactory.d.ts +0 -4
  198. package/lib/structures/ITtscPluginFactory.js +0 -3
  199. package/lib/structures/ITtscPluginFactory.js.map +0 -1
  200. package/lib/structures/ITtscPluginModule.d.ts +0 -3
  201. package/lib/structures/ITtscPluginModule.js +0 -3
  202. package/lib/structures/ITtscPluginModule.js.map +0 -1
  203. package/lib/structures/ITtscPreparedExecution.d.ts +0 -5
  204. package/lib/structures/ITtscPreparedExecution.js +0 -3
  205. package/lib/structures/ITtscPreparedExecution.js.map +0 -1
  206. package/lib/structures/ITtscProjectLocatorOptions.d.ts +0 -5
  207. package/lib/structures/ITtscProjectLocatorOptions.js.map +0 -1
  208. package/lib/structures/ITtscRegisterOptions.d.ts +0 -6
  209. package/lib/structures/ITtscRegisterOptions.js +0 -3
  210. package/lib/structures/ITtscRegisterOptions.js.map +0 -1
  211. package/lib/structures/ITtscResolveGoBinaryOptions.d.ts +0 -7
  212. package/lib/structures/ITtscResolveGoBinaryOptions.js +0 -3
  213. package/lib/structures/ITtscResolveGoBinaryOptions.js.map +0 -1
  214. package/lib/structures/ITtscResolveOptions.d.ts +0 -20
  215. package/lib/structures/ITtscResolveOptions.js +0 -3
  216. package/lib/structures/ITtscResolveOptions.js.map +0 -1
  217. package/lib/structures/ITtscResolvedTsgo.d.ts +0 -8
  218. package/lib/structures/ITtscResolvedTsgo.js +0 -3
  219. package/lib/structures/ITtscResolvedTsgo.js.map +0 -1
  220. package/lib/structures/ITtscTransformOptions.d.ts +0 -12
  221. package/lib/structures/ITtscTransformOptions.js +0 -3
  222. package/lib/structures/ITtscTransformOptions.js.map +0 -1
  223. package/lib/structures/ITtscTsgoResolveOptions.d.ts +0 -14
  224. package/lib/structures/ITtscTsgoResolveOptions.js +0 -3
  225. package/lib/structures/ITtscTsgoResolveOptions.js.map +0 -1
  226. package/lib/tsgo.d.ts +0 -6
  227. package/lib/tsgo.js.map +0 -1
  228. package/src/api.ts +0 -872
  229. package/src/native.ts +0 -64
  230. package/src/platform.ts +0 -151
  231. package/src/plugin.ts +0 -253
  232. package/src/runner/register.ts +0 -406
  233. package/src/structures/ITtscBuildOptions.ts +0 -19
  234. package/src/structures/ITtscBuildResult.ts +0 -6
  235. package/src/structures/ITtscBuildSourcePluginOptions.ts +0 -9
  236. package/src/structures/ITtscCacheKeyInput.ts +0 -7
  237. package/src/structures/ITtscCheckOptions.ts +0 -3
  238. package/src/structures/ITtscCommonOptions.ts +0 -23
  239. package/src/structures/ITtscLoadPluginsOptions.ts +0 -9
  240. package/src/structures/ITtscLoadedNativePlugin.ts +0 -8
  241. package/src/structures/ITtscLoadedPlugins.ts +0 -12
  242. package/src/structures/ITtscNativeBackend.ts +0 -11
  243. package/src/structures/ITtscNativePluginContractVersion.ts +0 -1
  244. package/src/structures/ITtscNativePluginShape.ts +0 -9
  245. package/src/structures/ITtscNativeRewriteMode.ts +0 -1
  246. package/src/structures/ITtscNativeSource.ts +0 -4
  247. package/src/structures/ITtscParsedProjectConfig.ts +0 -10
  248. package/src/structures/ITtscPluginFactory.ts +0 -7
  249. package/src/structures/ITtscPluginModule.ts +0 -4
  250. package/src/structures/ITtscPreparedExecution.ts +0 -5
  251. package/src/structures/ITtscProjectLocatorOptions.ts +0 -5
  252. package/src/structures/ITtscRegisterOptions.ts +0 -7
  253. package/src/structures/ITtscResolveGoBinaryOptions.ts +0 -7
  254. package/src/structures/ITtscResolveOptions.ts +0 -20
  255. package/src/structures/ITtscResolvedTsgo.ts +0 -8
  256. package/src/structures/ITtscTransformOptions.ts +0 -13
  257. package/src/structures/ITtscTsgoResolveOptions.ts +0 -14
  258. package/test/native.test.ts +0 -103
  259. /package/lib/structures/{ITtscLoadedNativePlugin.js → internal/ITtscLoadedNativePlugin.js} +0 -0
  260. /package/lib/structures/{ITtscParsedProjectConfig.js → internal/ITtscParsedProjectConfig.js} +0 -0
  261. /package/lib/structures/{ITtscProjectLocatorOptions.js → internal/ITtscProjectLocatorOptions.js} +0 -0
@@ -1,406 +0,0 @@
1
- import * as fs from "node:fs";
2
- import * as path from "node:path";
3
-
4
- import type { ITtscPreparedExecution } from "../structures/ITtscPreparedExecution";
5
- import type { ITtscRegisterOptions } from "../structures/ITtscRegisterOptions";
6
- import { build, check } from "../api";
7
- import {
8
- defaultCacheDirectory,
9
- resolveProjectConfig,
10
- resolveProjectRoot,
11
- } from "../project";
12
-
13
- export type { ITtscPreparedExecution } from "../structures/ITtscPreparedExecution";
14
- export type { ITtscRegisterOptions } from "../structures/ITtscRegisterOptions";
15
-
16
- type ITtscRequireExtension = (module: NodeJS.Module, filename: string) => void;
17
- type ITtscCompilableModule = NodeJS.Module & {
18
- _compile(code: string, filename: string): void;
19
- };
20
-
21
- interface ITtscRunnerProjectContext {
22
- cacheDir: string;
23
- emitDir: string;
24
- emittedFiles: string[] | null;
25
- diagnosticsChecked: boolean;
26
- entryMap: Map<string, string>;
27
- root: string;
28
- tsconfig: string;
29
- }
30
-
31
- const DEFAULT_EXTENSIONS: readonly string[] = Object.freeze([
32
- ".ts",
33
- ".tsx",
34
- ".cts",
35
- ".mts",
36
- ]);
37
-
38
- const PROCESS_CACHE_KEY = String(process.pid);
39
- const preparedContexts = new Map<string, ITtscRunnerProjectContext>();
40
-
41
- export function register(options: ITtscRegisterOptions = {}): () => void {
42
- const cwd = path.resolve(options.cwd ?? process.cwd());
43
- const extensions = [...(options.extensions ?? DEFAULT_EXTENSIONS)];
44
- const contextCache = new Map<string, ITtscRunnerProjectContext>();
45
- const previous = new Map<string, ITtscRequireExtension | undefined>();
46
-
47
- const getContext = (filename: string): ITtscRunnerProjectContext => {
48
- if (options.project) {
49
- const key = path.resolve(cwd, options.project);
50
- const cached = contextCache.get(key);
51
- if (cached) return cached;
52
- const tsconfig = resolveProjectConfig({ cwd, tsconfig: key });
53
- const root = resolveProjectRoot({ cwd, tsconfig });
54
- const cacheDir = options.cacheDir ?? defaultCacheDirectory(root, "ttsx");
55
- const prepared = takePreparedContext(tsconfig, cacheDir, options);
56
- if (prepared) {
57
- contextCache.set(key, prepared);
58
- return prepared;
59
- }
60
- const created = {
61
- tsconfig,
62
- root,
63
- cacheDir,
64
- diagnosticsChecked: false,
65
- emitDir: path.join(cacheDir, "project", PROCESS_CACHE_KEY),
66
- emittedFiles: null,
67
- entryMap: new Map<string, string>(),
68
- };
69
- contextCache.set(key, created);
70
- return created;
71
- }
72
-
73
- const tsconfig = resolveProjectConfig({ cwd, file: filename });
74
- const cached = contextCache.get(tsconfig);
75
- if (cached) return cached;
76
- const root = resolveProjectRoot({ cwd, tsconfig });
77
- const cacheDir = options.cacheDir ?? defaultCacheDirectory(root, "ttsx");
78
- const prepared = takePreparedContext(tsconfig, cacheDir, options);
79
- if (prepared) {
80
- contextCache.set(tsconfig, prepared);
81
- return prepared;
82
- }
83
- const created = {
84
- tsconfig,
85
- root,
86
- cacheDir,
87
- diagnosticsChecked: false,
88
- emitDir: path.join(cacheDir, "project", PROCESS_CACHE_KEY),
89
- emittedFiles: null,
90
- entryMap: new Map<string, string>(),
91
- };
92
- contextCache.set(tsconfig, created);
93
- return created;
94
- };
95
-
96
- const compile = (filename: string): string => {
97
- const context = getContext(filename);
98
- ensureProjectDiagnostics(context, options);
99
- const output = readCompiledOutput(context, filename, options);
100
- if (looksLikeESM(output)) {
101
- throw new Error(
102
- `ttsx: ESM output is not yet supported by the in-process CJS runner (${filename}).`,
103
- );
104
- }
105
- return output;
106
- };
107
-
108
- for (const extension of extensions) {
109
- previous.set(extension, require.extensions[extension]);
110
- require.extensions[extension] = (module: NodeJS.Module, filename: string) => {
111
- const compiled = compile(filename);
112
- (module as ITtscCompilableModule)._compile(compiled, filename);
113
- };
114
- }
115
-
116
- return () => {
117
- for (const extension of extensions) {
118
- const handler = previous.get(extension);
119
- if (handler) {
120
- require.extensions[extension] = handler;
121
- } else {
122
- delete require.extensions[extension];
123
- }
124
- }
125
- };
126
- }
127
-
128
- export function prepareExecution(
129
- entryFile: string,
130
- options: ITtscRegisterOptions = {},
131
- ): ITtscPreparedExecution {
132
- const cwd = path.resolve(options.cwd ?? process.cwd());
133
- const context = resolveProjectContext(cwd, entryFile, options);
134
- ensureProjectDiagnostics(context, options);
135
- const entryOutput = readCompiledOutput(context, entryFile, options);
136
- if (!looksLikeESM(entryOutput)) {
137
- storePreparedContext(context, options);
138
- return {
139
- emitDir: context.emitDir,
140
- entryFile,
141
- moduleKind: "cjs",
142
- };
143
- }
144
- ensureProjectBuild(context, options);
145
- const resolvedEntry = resolveEmittedFile(context, entryFile);
146
- if (resolvedEntry === null) {
147
- throw new Error(`ttsx: emitted entry not found for ${entryFile}`);
148
- }
149
- const output = fs.readFileSync(resolvedEntry, "utf8");
150
- return {
151
- emitDir: context.emitDir,
152
- entryFile: resolvedEntry,
153
- moduleKind: looksLikeESM(output) ? "esm" : "cjs",
154
- };
155
- }
156
-
157
- function storePreparedContext(
158
- context: ITtscRunnerProjectContext,
159
- options: ITtscRegisterOptions,
160
- ): void {
161
- preparedContexts.set(preparedContextKey(context.tsconfig, context.cacheDir, options), context);
162
- }
163
-
164
- function takePreparedContext(
165
- tsconfig: string,
166
- cacheDir: string,
167
- options: ITtscRegisterOptions,
168
- ): ITtscRunnerProjectContext | null {
169
- const key = preparedContextKey(tsconfig, cacheDir, options);
170
- const context = preparedContexts.get(key);
171
- if (!context) {
172
- return null;
173
- }
174
- preparedContexts.delete(key);
175
- return context;
176
- }
177
-
178
- function preparedContextKey(
179
- tsconfig: string,
180
- cacheDir: string,
181
- options: ITtscRegisterOptions,
182
- ): string {
183
- return JSON.stringify([
184
- PROCESS_CACHE_KEY,
185
- tsconfig,
186
- cacheDir,
187
- options.binary ?? "",
188
- options.rewriteMode ?? "",
189
- options.env?.TTSC_TSGO_BINARY ?? "",
190
- options.env?.TTSC_BINARY ?? "",
191
- options.plugins === false ? false : options.plugins ?? null,
192
- ]);
193
- }
194
-
195
- function ensureProjectBuild(context: ITtscRunnerProjectContext, options: ITtscRegisterOptions): void {
196
- if (context.emittedFiles !== null) return;
197
-
198
- fs.mkdirSync(context.cacheDir, { recursive: true });
199
- fs.rmSync(context.emitDir, { recursive: true, force: true });
200
- const result = build({
201
- binary: options.binary,
202
- cwd: context.root,
203
- env: options.env,
204
- emit: true,
205
- outDir: context.emitDir,
206
- plugins: options.plugins,
207
- quiet: true,
208
- rewriteMode: options.rewriteMode,
209
- skipDiagnosticsCheck: context.diagnosticsChecked,
210
- tsconfig: context.tsconfig,
211
- });
212
- if (result.status === 0) {
213
- context.emittedFiles = listEmittedFiles(context.emitDir);
214
- return;
215
- }
216
-
217
- fs.rmSync(context.emitDir, { recursive: true, force: true });
218
- const detail = [
219
- `ttsx: project build failed for ${context.tsconfig}`,
220
- result.stderr || result.stdout,
221
- ]
222
- .filter((line) => line.trim().length !== 0)
223
- .join("\n");
224
- throw new Error(detail);
225
- }
226
-
227
- function readCompiledOutput(
228
- context: ITtscRunnerProjectContext,
229
- filename: string,
230
- options: ITtscRegisterOptions,
231
- ): string {
232
- ensureProjectBuild(context, options);
233
- const emitted = resolveEmittedFile(context, filename);
234
- if (!emitted) {
235
- throw new Error(`ttsx: emitted file not found for ${filename}`);
236
- }
237
- return fs.readFileSync(emitted, "utf8");
238
- }
239
-
240
- function resolveProjectContext(
241
- cwd: string,
242
- filename: string,
243
- options: ITtscRegisterOptions,
244
- ): ITtscRunnerProjectContext {
245
- if (options.project) {
246
- const tsconfig = resolveProjectConfig({
247
- cwd,
248
- tsconfig: path.resolve(cwd, options.project),
249
- });
250
- return createProjectContext(cwd, tsconfig, options);
251
- }
252
- const tsconfig = resolveProjectConfig({ cwd, file: filename });
253
- return createProjectContext(cwd, tsconfig, options);
254
- }
255
-
256
- function createProjectContext(
257
- cwd: string,
258
- tsconfig: string,
259
- options: ITtscRegisterOptions,
260
- ): ITtscRunnerProjectContext {
261
- const root = resolveProjectRoot({ cwd, tsconfig });
262
- const cacheDir = options.cacheDir ?? defaultCacheDirectory(root, "ttsx");
263
- return {
264
- tsconfig,
265
- root,
266
- cacheDir,
267
- diagnosticsChecked: false,
268
- emitDir: path.join(cacheDir, "project", PROCESS_CACHE_KEY),
269
- emittedFiles: null,
270
- entryMap: new Map<string, string>(),
271
- };
272
- }
273
-
274
- function ensureProjectDiagnostics(
275
- context: ITtscRunnerProjectContext,
276
- options: ITtscRegisterOptions,
277
- ): void {
278
- if (context.diagnosticsChecked) {
279
- return;
280
- }
281
- const result = check({
282
- binary: options.binary,
283
- cwd: context.root,
284
- env: options.env,
285
- plugins: options.plugins,
286
- quiet: true,
287
- rewriteMode: options.rewriteMode,
288
- tsconfig: context.tsconfig,
289
- });
290
- if (result.status !== 0) {
291
- const detail = [result.stderr.trim(), result.stdout.trim()]
292
- .filter(Boolean)
293
- .join("\n");
294
- throw new Error(
295
- `ttsx: project check failed for ${context.tsconfig}` +
296
- (detail ? `\n${detail}` : ""),
297
- );
298
- }
299
- context.diagnosticsChecked = true;
300
- }
301
-
302
- function resolveEmittedFile(context: ITtscRunnerProjectContext, filename: string): string | null {
303
- const normalized = path.resolve(filename);
304
- const cached = context.entryMap.get(normalized);
305
- if (cached && fs.existsSync(cached)) return cached;
306
-
307
- const exact = resolveExactEmittedFile(context, normalized);
308
- if (exact && fs.existsSync(exact)) {
309
- context.entryMap.set(normalized, exact);
310
- return exact;
311
- }
312
-
313
- const files = context.emittedFiles ?? [];
314
- let best: string | null = null;
315
- let bestScore = 0;
316
- for (const file of files) {
317
- const score = sharedSourceStemSegments(file, normalized);
318
- if (score > bestScore) {
319
- best = file;
320
- bestScore = score;
321
- }
322
- }
323
- if (!best || !fs.existsSync(best)) {
324
- return null;
325
- }
326
- context.entryMap.set(normalized, best);
327
- return best;
328
- }
329
-
330
- function resolveExactEmittedFile(
331
- context: ITtscRunnerProjectContext,
332
- filename: string,
333
- ): string | null {
334
- const relative = path.relative(context.root, filename);
335
- if (relative === "" || relative.startsWith("..") || path.isAbsolute(relative)) {
336
- return null;
337
- }
338
- return path.resolve(
339
- context.emitDir,
340
- relative.slice(0, relative.length - path.extname(relative).length) +
341
- emittedJavaScriptExtension(filename),
342
- );
343
- }
344
-
345
- function listEmittedFiles(root: string): string[] {
346
- if (!fs.existsSync(root)) return [];
347
- const output: string[] = [];
348
- const stack = [root];
349
- while (stack.length !== 0) {
350
- const current = stack.pop()!;
351
- for (const entry of fs.readdirSync(current, { withFileTypes: true })) {
352
- const next = path.join(current, entry.name);
353
- if (entry.isDirectory()) {
354
- stack.push(next);
355
- } else if (entry.isFile() && isJavaScriptOutput(next)) {
356
- output.push(path.resolve(next));
357
- }
358
- }
359
- }
360
- return output;
361
- }
362
-
363
- function sharedSourceStemSegments(outPath: string, srcPath: string): number {
364
- const trim = (location: string): string[] => {
365
- const normalized = location.split(path.sep).join("/");
366
- return normalized
367
- .slice(0, normalized.length - path.extname(normalized).length)
368
- .split("/");
369
- };
370
- const a = trim(outPath);
371
- const b = trim(srcPath);
372
- const count = Math.min(a.length, b.length);
373
- let shared = 0;
374
- for (let i = 1; i <= count; i += 1) {
375
- if (a[a.length - i] !== b[b.length - i]) break;
376
- shared += 1;
377
- }
378
- return shared;
379
- }
380
-
381
- function looksLikeESM(output: string): boolean {
382
- if (
383
- /\bObject\.defineProperty\(exports\b/.test(output) ||
384
- /\bmodule\.exports\b/.test(output) ||
385
- /\brequire\(/.test(output) ||
386
- /\bexports\./.test(output)
387
- ) {
388
- return false;
389
- }
390
- return /^\s*(import|export)\s/m.test(output);
391
- }
392
-
393
- function emittedJavaScriptExtension(filename: string): string {
394
- switch (path.extname(filename).toLowerCase()) {
395
- case ".mts":
396
- return ".mjs";
397
- case ".cts":
398
- return ".cjs";
399
- default:
400
- return ".js";
401
- }
402
- }
403
-
404
- function isJavaScriptOutput(filename: string): boolean {
405
- return /\.(?:[cm]?js)$/i.test(filename);
406
- }
@@ -1,19 +0,0 @@
1
- import type { ITtscCommonOptions } from "./ITtscCommonOptions";
2
-
3
- export interface ITtscBuildOptions extends ITtscCommonOptions {
4
- /** Path to tsconfig.json. Default: `tsconfig.json`. */
5
- tsconfig?: string;
6
- /**
7
- * Emit override. `true` forces emit, `false` forces noEmit, `undefined`
8
- * follows tsconfig.
9
- */
10
- emit?: boolean;
11
- /** Override compilerOptions.outDir for this invocation. */
12
- outDir?: string;
13
- /** Suppress the per-call summary banner. Default: `true`. */
14
- quiet?: boolean;
15
- /** @internal Caller already ran diagnostics and accepts responsibility. */
16
- skipDiagnosticsCheck?: boolean;
17
- /** @internal Force `tsgo --listEmittedFiles` for caller-side output discovery. */
18
- forceListEmittedFiles?: boolean;
19
- }
@@ -1,6 +0,0 @@
1
- export interface ITtscBuildResult {
2
- emittedFiles?: string[];
3
- status: number;
4
- stdout: string;
5
- stderr: string;
6
- }
@@ -1,9 +0,0 @@
1
- import type { ITtscNativeSource } from "./ITtscNativeSource";
2
-
3
- export interface ITtscBuildSourcePluginOptions {
4
- source: ITtscNativeSource;
5
- pluginName: string;
6
- baseDir: string;
7
- ttscVersion: string;
8
- tsgoVersion: string;
9
- }
@@ -1,7 +0,0 @@
1
- export interface ITtscCacheKeyInput {
2
- file: string;
3
- tsconfig: string;
4
- version: string;
5
- mode?: string;
6
- extra?: readonly string[];
7
- }
@@ -1,3 +0,0 @@
1
- import type { ITtscBuildOptions } from "./ITtscBuildOptions";
2
-
3
- export type ITtscCheckOptions = Omit<ITtscBuildOptions, "emit">;
@@ -1,23 +0,0 @@
1
- import type { ITtscProjectPluginConfig } from "./ITtscProjectPluginConfig";
2
- import type { ITtscResolveOptions } from "./ITtscResolveOptions";
3
-
4
- export interface ITtscCommonOptions extends ITtscResolveOptions {
5
- /** Absolute path to an already-resolved tsgo binary. Skips package resolution. */
6
- binary?: string;
7
- /** Working directory passed to the child process. */
8
- cwd?: string;
9
- /** Extra environment variables; merged onto `process.env`. */
10
- env?: NodeJS.ProcessEnv;
11
- /**
12
- * Override project plugin loading. `false` disables tsconfig plugins; an
13
- * array replaces the tsconfig `compilerOptions.plugins` list.
14
- */
15
- plugins?: readonly ITtscProjectPluginConfig[] | false;
16
- /**
17
- * Override the native rewrite backend. Defaults to the loaded plugin mode.
18
- *
19
- * @deprecated Prefer plugin-declared `native.mode`; this override is for
20
- * low-level tests and migration probes.
21
- */
22
- rewriteMode?: string;
23
- }
@@ -1,9 +0,0 @@
1
- import type { ITtscProjectPluginConfig } from "./ITtscProjectPluginConfig";
2
-
3
- export interface ITtscLoadPluginsOptions {
4
- binary: string;
5
- cwd?: string;
6
- entries?: readonly ITtscProjectPluginConfig[] | false;
7
- file?: string;
8
- tsconfig?: string;
9
- }
@@ -1,8 +0,0 @@
1
- import type { ITtscNativeBackend } from "./ITtscNativeBackend";
2
- import type { ITtscProjectPluginConfig } from "./ITtscProjectPluginConfig";
3
-
4
- export interface ITtscLoadedNativePlugin {
5
- backend: ITtscNativeBackend;
6
- config: ITtscProjectPluginConfig;
7
- name: string;
8
- }
@@ -1,12 +0,0 @@
1
- import type { ITtscLoadedNativePlugin } from "./ITtscLoadedNativePlugin";
2
- import type { ITtscParsedProjectConfig } from "./ITtscParsedProjectConfig";
3
- import type { ITtscPlugin } from "./ITtscPlugin";
4
-
5
- export interface ITtscLoadedPlugins {
6
- compatibilityFallback: boolean;
7
- nativeBinary: string | null;
8
- nativeBinaries: string[];
9
- nativePlugins: ITtscLoadedNativePlugin[];
10
- plugins: ITtscPlugin[];
11
- project: ITtscParsedProjectConfig;
12
- }
@@ -1,11 +0,0 @@
1
- import type { ITtscNativePluginContractVersion } from "./ITtscNativePluginContractVersion";
2
- import type { ITtscNativeRewriteMode } from "./ITtscNativeRewriteMode";
3
- import type { ITtscNativeSource } from "./ITtscNativeSource";
4
-
5
- export interface ITtscNativeBackend {
6
- mode: ITtscNativeRewriteMode;
7
- binary?: string;
8
- source?: ITtscNativeSource;
9
- contractVersion?: ITtscNativePluginContractVersion;
10
- capabilities?: readonly string[];
11
- }
@@ -1 +0,0 @@
1
- export type ITtscNativePluginContractVersion = 1;
@@ -1,9 +0,0 @@
1
- import type { ITtscNativeBackend } from "./ITtscNativeBackend";
2
- import type { ITtscNativeRewriteMode } from "./ITtscNativeRewriteMode";
3
-
4
- export interface ITtscNativePluginShape {
5
- name: string;
6
- native?: ITtscNativeBackend;
7
- nativeMode?: ITtscNativeRewriteMode;
8
- nativeBinary?: string;
9
- }
@@ -1 +0,0 @@
1
- export type ITtscNativeRewriteMode = string;
@@ -1,4 +0,0 @@
1
- export interface ITtscNativeSource {
2
- dir: string;
3
- entry?: string;
4
- }
@@ -1,10 +0,0 @@
1
- import type { ITtscProjectPluginConfig } from "./ITtscProjectPluginConfig";
2
-
3
- export interface ITtscParsedProjectConfig {
4
- compilerOptions: {
5
- outDir?: string;
6
- plugins: ITtscProjectPluginConfig[];
7
- } & Record<string, unknown>;
8
- path: string;
9
- root: string;
10
- }
@@ -1,7 +0,0 @@
1
- import type { ITtscPlugin } from "./ITtscPlugin";
2
- import type { ITtscPluginFactoryContext } from "./ITtscPluginFactoryContext";
3
- import type { ITtscProjectPluginConfig } from "./ITtscProjectPluginConfig";
4
-
5
- export type ITtscPluginFactory<T = ITtscProjectPluginConfig> = (
6
- context: ITtscPluginFactoryContext<T>,
7
- ) => ITtscPlugin;
@@ -1,4 +0,0 @@
1
- import type { ITtscPlugin } from "./ITtscPlugin";
2
- import type { ITtscPluginFactory } from "./ITtscPluginFactory";
3
-
4
- export type ITtscPluginModule = ITtscPlugin | ITtscPluginFactory;
@@ -1,5 +0,0 @@
1
- export interface ITtscPreparedExecution {
2
- emitDir: string;
3
- entryFile: string;
4
- moduleKind: "cjs" | "esm";
5
- }
@@ -1,5 +0,0 @@
1
- export interface ITtscProjectLocatorOptions {
2
- cwd?: string;
3
- file?: string;
4
- tsconfig?: string;
5
- }
@@ -1,7 +0,0 @@
1
- import type { ITtscCommonOptions } from "./ITtscCommonOptions";
2
-
3
- export interface ITtscRegisterOptions extends ITtscCommonOptions {
4
- cacheDir?: string;
5
- project?: string;
6
- extensions?: readonly string[];
7
- }
@@ -1,7 +0,0 @@
1
- export interface ITtscResolveGoBinaryOptions {
2
- arch?: string;
3
- env?: NodeJS.ProcessEnv;
4
- localGoLookup?: () => string | null;
5
- platform?: NodeJS.Platform;
6
- resolver?: (request: string) => string;
7
- }
@@ -1,20 +0,0 @@
1
- export interface ITtscResolveOptions {
2
- /**
3
- * Override `require.resolve` for testing. Takes a module request string and
4
- * returns an absolute path (or throws).
5
- */
6
- resolver?: (request: string) => string;
7
- /** Override `process.platform` for testing. */
8
- platform?: NodeJS.Platform;
9
- /** Override `process.arch` for testing. */
10
- arch?: string;
11
- /** Override `process.env` for testing. */
12
- env?: NodeJS.ProcessEnv;
13
- /**
14
- * Override the package-local scan used for the local `native/ttsc-native`
15
- * fallback. Returns an absolute path if present, else null.
16
- */
17
- localBinaryLookup?: () => string | null;
18
- /** Override the module directory used to resolve the package-local binary. */
19
- moduleDir?: string;
20
- }
@@ -1,8 +0,0 @@
1
- export interface ITtscResolvedTsgo {
2
- binary: string;
3
- gitHead?: string;
4
- packageJson: string;
5
- packageRoot: string;
6
- platformPackageJson?: string;
7
- version: string;
8
- }
@@ -1,13 +0,0 @@
1
- import type { ITtscCommonOptions } from "./ITtscCommonOptions";
2
-
3
- export interface ITtscTransformOptions extends ITtscCommonOptions {
4
- /** Path to the .ts file to transform. Absolute or `cwd`-relative. */
5
- file: string;
6
- /** Path to the tsconfig owning `file`. Default: `tsconfig.json`. */
7
- tsconfig?: string;
8
- /**
9
- * When provided, the binary writes JS directly to this path instead of piping
10
- * stdout. Useful when the emitted text is large.
11
- */
12
- out?: string;
13
- }
@@ -1,14 +0,0 @@
1
- export interface ITtscTsgoResolveOptions {
2
- /** Absolute path to an explicit tsgo-compatible binary. */
3
- binary?: string;
4
- /** Project directory used for node_modules resolution. */
5
- cwd?: string;
6
- /** Environment variables used for explicit binary overrides. */
7
- env?: NodeJS.ProcessEnv;
8
- /** Override process.platform for tests. */
9
- platform?: NodeJS.Platform;
10
- /** Override process.arch for tests. */
11
- arch?: string;
12
- /** Override package resolution for tests. */
13
- resolver?: (request: string) => string;
14
- }