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
@@ -5,228 +5,41 @@ const path = require("node:path");
5
5
  const test = require("node:test");
6
6
 
7
7
  const {
8
- bundledGoPackageRequest,
9
- computeCacheKey,
10
- goBinaryName,
11
- pluginCacheCleanupTargets,
12
- resolveDefaultPluginCacheRoot,
13
- resolveGoCompiler,
14
- resolvePluginCacheRoot,
15
- sourceBuildWorkspaceReplacements,
16
- } = require("../src/source-build.ts");
17
-
18
- function withTempSourceDir(files: Record<string, string>): string {
19
- const root = fs.mkdtempSync(path.join(os.tmpdir(), "ttsc-cache-key-"));
20
- for (const [name, contents] of Object.entries(files)) {
21
- const file = path.join(root, name);
22
- fs.mkdirSync(path.dirname(file), { recursive: true });
23
- fs.writeFileSync(file, contents, "utf8");
24
- }
25
- return root;
26
- }
27
-
28
- test("computeCacheKey is stable across calls for identical inputs", () => {
29
- const dir = withTempSourceDir({
30
- "go.mod": "module example\n\ngo 1.26\n",
31
- "main.go": "package main\nfunc main() {}\n",
32
- });
33
- const inputs = { dir, entry: ".", ttscVersion: "1.0.0", tsgoVersion: "7.0.0-dev" };
34
- assert.equal(computeCacheKey(inputs), computeCacheKey(inputs));
35
- });
36
-
37
- test("computeCacheKey changes when ttscVersion changes", () => {
38
- const dir = withTempSourceDir({
39
- "go.mod": "module example\n\ngo 1.26\n",
40
- "main.go": "package main\nfunc main() {}\n",
41
- });
42
- const a = computeCacheKey({ dir, entry: ".", ttscVersion: "1.0.0", tsgoVersion: "7.0.0-dev" });
43
- const b = computeCacheKey({ dir, entry: ".", ttscVersion: "1.0.1", tsgoVersion: "7.0.0-dev" });
44
- assert.notEqual(a, b);
45
- });
46
-
47
- test("computeCacheKey changes when tsgoVersion changes", () => {
48
- const dir = withTempSourceDir({
49
- "go.mod": "module example\n\ngo 1.26\n",
50
- "main.go": "package main\nfunc main() {}\n",
51
- });
52
- const a = computeCacheKey({ dir, entry: ".", ttscVersion: "1.0.0", tsgoVersion: "7.0.0-dev.A" });
53
- const b = computeCacheKey({ dir, entry: ".", ttscVersion: "1.0.0", tsgoVersion: "7.0.0-dev.B" });
54
- assert.notEqual(a, b);
55
- });
56
-
57
- test("computeCacheKey changes when entry changes", () => {
58
- const dir = withTempSourceDir({
59
- "go.mod": "module example\n\ngo 1.26\n",
60
- "main.go": "package main\nfunc main() {}\n",
61
- "cmd/foo/main.go": "package main\nfunc main() {}\n",
62
- });
63
- const a = computeCacheKey({ dir, entry: ".", ttscVersion: "1.0.0", tsgoVersion: "x" });
64
- const b = computeCacheKey({ dir, entry: "./cmd/foo", ttscVersion: "1.0.0", tsgoVersion: "x" });
65
- assert.notEqual(a, b);
66
- });
67
-
68
- test("computeCacheKey changes when source content changes", () => {
69
- const dir = withTempSourceDir({
70
- "go.mod": "module example\n\ngo 1.26\n",
71
- "main.go": "package main\nfunc main() {}\n",
72
- });
73
- const before = computeCacheKey({ dir, entry: ".", ttscVersion: "1.0.0", tsgoVersion: "x" });
74
- fs.writeFileSync(
75
- path.join(dir, "main.go"),
76
- "package main\nfunc main() { _ = 1 }\n",
77
- );
78
- const after = computeCacheKey({ dir, entry: ".", ttscVersion: "1.0.0", tsgoVersion: "x" });
79
- assert.notEqual(before, after);
80
- });
81
-
82
- test("computeCacheKey ignores non-Go files (e.g. README.md)", () => {
83
- const dir = withTempSourceDir({
84
- "go.mod": "module example\n\ngo 1.26\n",
85
- "main.go": "package main\nfunc main() {}\n",
86
- });
87
- const before = computeCacheKey({ dir, entry: ".", ttscVersion: "1.0.0", tsgoVersion: "x" });
88
- fs.writeFileSync(path.join(dir, "README.md"), "documentation\n");
89
- const after = computeCacheKey({ dir, entry: ".", ttscVersion: "1.0.0", tsgoVersion: "x" });
90
- assert.equal(before, after);
91
- });
92
-
93
- test("resolveGoCompiler prefers TTSC_GO_BINARY override", () => {
94
- const resolved = resolveGoCompiler({
95
- env: { TTSC_GO_BINARY: "/tmp/custom-go" },
96
- localGoLookup: () => "/tmp/local-go",
97
- resolver: () => {
98
- throw new Error("resolver should not be called when TTSC_GO_BINARY is set");
99
- },
100
- });
101
- assert.equal(resolved, "/tmp/custom-go");
102
- });
103
-
104
- test("resolveGoCompiler uses bundled platform package before PATH fallback", () => {
105
- const resolved = resolveGoCompiler({
106
- arch: "x64",
107
- env: {},
108
- localGoLookup: () => null,
109
- platform: "linux",
110
- resolver: (request) => {
111
- assert.equal(request, "@ttsc/linux-x64/bin/go/bin/go");
112
- return "/node_modules/@ttsc/linux-x64/bin/go/bin/go";
113
- },
114
- });
115
- assert.equal(resolved, "/node_modules/@ttsc/linux-x64/bin/go/bin/go");
116
- });
117
-
118
- test("resolveGoCompiler falls back to package-local bundled Go before PATH", () => {
119
- const resolved = resolveGoCompiler({
120
- env: {},
121
- localGoLookup: () => "/workspace/packages/ttsc/native/go/bin/go",
122
- resolver: () => {
123
- throw new Error("platform package missing");
124
- },
125
- });
126
- assert.equal(resolved, "/workspace/packages/ttsc/native/go/bin/go");
127
- });
128
-
129
- test("resolveGoCompiler keeps go on PATH as the last development fallback", () => {
130
- const resolved = resolveGoCompiler({
131
- env: {},
132
- localGoLookup: () => null,
133
- resolver: () => {
134
- throw new Error("platform package missing");
135
- },
136
- });
137
- assert.equal(resolved, "go");
138
- });
139
-
140
- test("bundled Go package request follows platform package layout", () => {
141
- assert.equal(goBinaryName({ platform: "win32" }), "go.exe");
142
- assert.equal(goBinaryName({ platform: "linux" }), "go");
143
- assert.equal(
144
- bundledGoPackageRequest({ platform: "darwin", arch: "arm64" }),
145
- "@ttsc/darwin-arm64/bin/go/bin/go",
146
- );
147
- assert.equal(
148
- bundledGoPackageRequest({ platform: "win32", arch: "x64" }),
149
- "@ttsc/win32-x64/bin/go/bin/go.exe",
8
+ buildSourcePlugin,
9
+ } = require("../lib/plugin/internal/buildSourcePlugin.js");
10
+
11
+ test("buildSourcePlugin rejects a source outside a nearby Go module", () => {
12
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), "ttsc-source-plugin-"));
13
+ const source = path.join(root, "a", "b", "c", "d", "cmd");
14
+ fs.mkdirSync(source, { recursive: true });
15
+
16
+ assert.throws(
17
+ () =>
18
+ buildSourcePlugin({
19
+ baseDir: root,
20
+ pluginName: "missing-go-mod",
21
+ source,
22
+ ttscVersion: "1.0.0",
23
+ tsgoVersion: "7.0.0-dev",
24
+ }),
25
+ /go\.mod within 3 parent directories/,
150
26
  );
151
27
  });
152
28
 
153
- test("sourceBuildWorkspaceReplacements pins the ttsc module for source plugins", () => {
154
- const ttscRoot = withTempSourceDir({
155
- "go.mod": "module github.com/samchon/ttsc/packages/ttsc\n\ngo 1.26\n",
156
- });
157
- const otherRoot = withTempSourceDir({
158
- "go.mod": "module example.com/other\n\ngo 1.26\n",
159
- });
160
- assert.deepEqual(sourceBuildWorkspaceReplacements([otherRoot, ttscRoot]), [
161
- `replace github.com/samchon/ttsc/packages/ttsc v0.0.0 => ${ttscRoot.replace(/\\/g, "/")}`,
162
- ]);
163
- });
164
-
165
- test("resolvePluginCacheRoot prefers project node_modules cache", () => {
166
- const root = fs.mkdtempSync(path.join(os.tmpdir(), "ttsc-local-cache-"));
167
- fs.mkdirSync(path.join(root, "node_modules"));
168
- assert.equal(
169
- resolvePluginCacheRoot(root),
170
- path.join(root, "node_modules", ".ttsc", "plugins"),
29
+ test("buildSourcePlugin rejects non-directory and non-go.mod sources", () => {
30
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), "ttsc-source-plugin-"));
31
+ const source = path.join(root, "plugin.txt");
32
+ fs.writeFileSync(source, "not a Go package\n", "utf8");
33
+
34
+ assert.throws(
35
+ () =>
36
+ buildSourcePlugin({
37
+ baseDir: root,
38
+ pluginName: "bad-source",
39
+ source,
40
+ ttscVersion: "1.0.0",
41
+ tsgoVersion: "7.0.0-dev",
42
+ }),
43
+ /Go package directory or go\.mod file/,
171
44
  );
172
45
  });
173
-
174
- test("resolvePluginCacheRoot falls back to project .ttsc without node_modules", () => {
175
- const root = fs.mkdtempSync(path.join(os.tmpdir(), "ttsc-local-cache-"));
176
- assert.equal(resolvePluginCacheRoot(root), path.join(root, ".ttsc", "plugins"));
177
- });
178
-
179
- test("resolveDefaultPluginCacheRoot ignores TTSC_CACHE_DIR for local clean paths", () => {
180
- const previous = process.env.TTSC_CACHE_DIR;
181
- const root = fs.mkdtempSync(path.join(os.tmpdir(), "ttsc-local-cache-"));
182
- fs.mkdirSync(path.join(root, "node_modules"));
183
- try {
184
- process.env.TTSC_CACHE_DIR = path.join(root, "override");
185
- assert.equal(
186
- resolveDefaultPluginCacheRoot(root),
187
- path.join(root, "node_modules", ".ttsc", "plugins"),
188
- );
189
- } finally {
190
- if (previous === undefined) {
191
- delete process.env.TTSC_CACHE_DIR;
192
- } else {
193
- process.env.TTSC_CACHE_DIR = previous;
194
- }
195
- }
196
- });
197
-
198
- test("resolvePluginCacheRoot honors TTSC_CACHE_DIR as an explicit test override", () => {
199
- const previous = process.env.TTSC_CACHE_DIR;
200
- const root = fs.mkdtempSync(path.join(os.tmpdir(), "ttsc-local-cache-"));
201
- const override = path.join(root, "override");
202
- try {
203
- process.env.TTSC_CACHE_DIR = override;
204
- assert.equal(resolvePluginCacheRoot(root), path.join(override, "plugins"));
205
- } finally {
206
- if (previous === undefined) {
207
- delete process.env.TTSC_CACHE_DIR;
208
- } else {
209
- process.env.TTSC_CACHE_DIR = previous;
210
- }
211
- }
212
- });
213
-
214
- test("pluginCacheCleanupTargets deletes both local roots and explicit override", () => {
215
- const previous = process.env.TTSC_CACHE_DIR;
216
- const root = fs.mkdtempSync(path.join(os.tmpdir(), "ttsc-local-cache-"));
217
- const override = path.join(root, "override");
218
- try {
219
- process.env.TTSC_CACHE_DIR = override;
220
- assert.deepEqual(pluginCacheCleanupTargets(root), [
221
- path.join(root, "node_modules", ".ttsc"),
222
- path.join(root, ".ttsc"),
223
- path.join(override, "plugins"),
224
- ]);
225
- } finally {
226
- if (previous === undefined) {
227
- delete process.env.TTSC_CACHE_DIR;
228
- } else {
229
- process.env.TTSC_CACHE_DIR = previous;
230
- }
231
- }
232
- });
package/test/tsgo.test.ts CHANGED
@@ -3,10 +3,7 @@ const fs = require("node:fs");
3
3
  const os = require("node:os");
4
4
  const path = require("node:path");
5
5
  const test = require("node:test");
6
- const {
7
- nativePreviewPlatformPackage,
8
- resolveTsgo,
9
- } = require("../src/tsgo.ts");
6
+ const { resolveTsgo } = require("../lib/compiler/internal/resolveTsgo.js");
10
7
 
11
8
  test("resolveTsgo accepts TTSC_TSGO_BINARY as an explicit compiler", () => {
12
9
  const root = fs.mkdtempSync(path.join(os.tmpdir(), "ttsc-tsgo-test-"));
@@ -61,8 +58,4 @@ test("resolveTsgo resolves the consumer native-preview platform package", () =>
61
58
  assert.equal(resolved.version, "7.0.0-dev.consumer");
62
59
  assert.equal(resolved.gitHead, "abc123");
63
60
  assert.equal(resolved.binary, path.join(platformRoot, "lib", "tsgo"));
64
- assert.equal(
65
- nativePreviewPlatformPackage({ platform: "linux", arch: "x64" }),
66
- "@typescript/native-preview-linux-x64",
67
- );
68
61
  });
@@ -1,166 +0,0 @@
1
- package main
2
-
3
- import (
4
- "bytes"
5
- "flag"
6
- "fmt"
7
- "os"
8
- "path/filepath"
9
- "strings"
10
-
11
- shimcompiler "github.com/microsoft/typescript-go/shim/compiler"
12
-
13
- "github.com/samchon/ttsc/packages/ttsc/driver"
14
- )
15
-
16
- // runTransform implements `ttsc transform` — a single-file transform used by
17
- // bundler plugin adapters (unplugin, vite, webpack, rollup, esbuild, rspack,
18
- // farm). Behavior:
19
- //
20
- // - Loads the enclosing tsconfig so tsgo resolves imports and types.
21
- // - Runs tsgo Emit, filtering WriteFile so only the requested source
22
- // file's JS leaves the binary.
23
- // - Applies the ttsc rewrite + import cleanup just like `build --emit`.
24
- // - Writes the result to stdout (or `--out` when provided).
25
- //
26
- // The contract matches a per-file bundler transform hook: bundlers
27
- // hand the plugin a source file path, the plugin shells out here, and the
28
- // returned text replaces the file in the bundle graph.
29
- func runTransform(args []string) int {
30
- fs := flag.NewFlagSet("transform", flag.ContinueOnError)
31
- fs.SetOutput(stderr)
32
- file := fs.String("file", "", "absolute or cwd-relative path of the .ts file to transform")
33
- tsconfigPath := fs.String("tsconfig", "tsconfig.json", "tsconfig.json owning --file")
34
- cwdOverride := fs.String("cwd", "", "override the working directory")
35
- out := fs.String("out", "", "write output JS to PATH (default: stdout)")
36
- rewriteMode := fs.String("rewrite-mode", "none", "native rewrite backend id")
37
- if err := fs.Parse(args); err != nil {
38
- return 2
39
- }
40
- if *file == "" {
41
- fmt.Fprintln(stderr, "ttsc transform: --file is required")
42
- return 2
43
- }
44
-
45
- cwd := *cwdOverride
46
- if cwd == "" {
47
- var err error
48
- cwd, err = os.Getwd()
49
- if err != nil {
50
- fmt.Fprintf(stderr, "ttsc transform: cwd: %v\n", err)
51
- return 2
52
- }
53
- }
54
-
55
- prog, diags, err := driver.LoadProgram(cwd, *tsconfigPath, driver.LoadProgramOptions{
56
- ForceEmit: true,
57
- })
58
- if err != nil {
59
- fmt.Fprintf(stderr, "ttsc transform: %v\n", err)
60
- return 2
61
- }
62
- if len(diags) > 0 {
63
- driver.WritePrettyDiagnostics(stderr, diags, cwd)
64
- return 2
65
- }
66
- defer prog.Close()
67
- if diags := prog.Diagnostics(); len(diags) > 0 {
68
- driver.WritePrettyDiagnostics(stderr, diags, cwd)
69
- return 2
70
- }
71
-
72
- absFile := *file
73
- if !filepath.IsAbs(absFile) {
74
- absFile = filepath.Join(cwd, absFile)
75
- }
76
- absFile = filepath.ToSlash(absFile)
77
-
78
- rewrites := driver.NewRewriteSet()
79
- if *rewriteMode != "none" {
80
- fmt.Fprintf(stderr, "ttsc transform: rewrite backend %q is not built into the standalone ttsc binary\n", *rewriteMode)
81
- fmt.Fprintln(stderr, "ttsc transform: run through the JS host with the matching compiler plugin so it can select the consumer-provided native binary")
82
- return 2
83
- }
84
-
85
- target := prog.SourceFile(absFile)
86
- if target == nil {
87
- fmt.Fprintf(stderr, "ttsc transform: source file is not in program: %s\n", absFile)
88
- return 2
89
- }
90
-
91
- // Capture the requested source file's JS output.
92
- var captured []byte
93
- capture := func(name, text string, _ *shimcompiler.WriteFileData) error {
94
- rel := filepath.ToSlash(name)
95
- if !isJavaScriptOutput(rel) {
96
- return nil
97
- }
98
- captured = []byte(text)
99
- return nil
100
- }
101
- _, eDiags, err := prog.EmitFile(rewrites, target, capture)
102
- if err != nil {
103
- fmt.Fprintf(stderr, "ttsc transform: emit: %v\n", err)
104
- return 3
105
- }
106
- for _, d := range eDiags {
107
- fmt.Fprintln(stderr, " -", d.String())
108
- }
109
- if captured == nil {
110
- fmt.Fprintf(stderr, "ttsc transform: no output produced for %s\n", absFile)
111
- return 3
112
- }
113
- if *out == "" {
114
- if _, werr := bytes.NewReader(captured).WriteTo(stdout); werr != nil {
115
- fmt.Fprintf(stderr, "ttsc transform: write stdout: %v\n", werr)
116
- return 3
117
- }
118
- return 0
119
- }
120
- if dir := filepath.Dir(*out); dir != "" {
121
- if err := os.MkdirAll(dir, 0o755); err != nil {
122
- fmt.Fprintf(stderr, "ttsc transform: mkdir: %v\n", err)
123
- return 3
124
- }
125
- }
126
- if err := os.WriteFile(*out, captured, 0o644); err != nil {
127
- fmt.Fprintf(stderr, "ttsc transform: write %s: %v\n", *out, err)
128
- return 3
129
- }
130
- return 0
131
- }
132
-
133
- // sharedSourceStemSegments counts the shared trailing path segments between the
134
- // emitted output path and the original source path after stripping extensions.
135
- // `src/foo/bar.ts` ↔ `dist/foo/bar.js` returns 2, while unrelated paths return
136
- // 0. `ttsc transform` uses the highest score to pick the emitted JS that
137
- // belongs to the requested source file even when a project contains multiple
138
- // `index.ts` files.
139
- func sharedSourceStemSegments(outPath, srcPath string) int {
140
- trim := func(p string) []string {
141
- p = strings.TrimSuffix(p, filepath.Ext(p))
142
- return strings.Split(p, "/")
143
- }
144
- a, b := trim(outPath), trim(srcPath)
145
- n := len(a)
146
- if len(b) < n {
147
- n = len(b)
148
- }
149
- shared := 0
150
- for i := 1; i <= n; i++ {
151
- if a[len(a)-i] != b[len(b)-i] {
152
- break
153
- }
154
- shared++
155
- }
156
- return shared
157
- }
158
-
159
- func isJavaScriptOutput(name string) bool {
160
- switch strings.ToLower(filepath.Ext(name)) {
161
- case ".js", ".mjs", ".cjs":
162
- return true
163
- default:
164
- return false
165
- }
166
- }
package/lib/api.d.ts DELETED
@@ -1,40 +0,0 @@
1
- import type { ITtscBuildOptions } from "./structures/ITtscBuildOptions";
2
- import type { ITtscBuildResult } from "./structures/ITtscBuildResult";
3
- import type { ITtscCheckOptions } from "./structures/ITtscCheckOptions";
4
- import type { ITtscCommonOptions } from "./structures/ITtscCommonOptions";
5
- import type { ITtscTransformOptions } from "./structures/ITtscTransformOptions";
6
- export type { ITtscBuildOptions } from "./structures/ITtscBuildOptions";
7
- export type { ITtscBuildResult } from "./structures/ITtscBuildResult";
8
- export type { ITtscCheckOptions } from "./structures/ITtscCheckOptions";
9
- export type { ITtscCommonOptions } from "./structures/ITtscCommonOptions";
10
- export type { ITtscTransformOptions } from "./structures/ITtscTransformOptions";
11
- /**
12
- * Transform a single .ts file and return the rewritten JS as a string.
13
- *
14
- * Intended for bundler per-file transforms (unplugin `transform()`). The caller
15
- * passes the absolute path; ttsc loads the enclosing tsconfig, compiles with
16
- * tsgo, and returns the rewritten JS.
17
- *
18
- * Throws when the binary exits non-zero — the error includes stderr so bundler
19
- * error overlays surface the real cause.
20
- */
21
- export declare function transform(options: ITtscTransformOptions): string;
22
- /**
23
- * Run `ttsc` against a tsconfig. Returns once the binary exits so the caller
24
- * can decide how to surface diagnostics. Does not throw on non-zero exit —
25
- * bundler pipelines often want to continue and collect errors.
26
- */
27
- export declare function build(options?: ITtscBuildOptions): ITtscBuildResult;
28
- /**
29
- * Run `ttsc check` (build without emit) — CI gate / pre-commit hook use.
30
- * Resolves with an exit-code record; does not throw.
31
- */
32
- export declare function check(options?: ITtscCheckOptions): ITtscBuildResult;
33
- /** Ask the binary for its version banner. Handy for user-agent strings. */
34
- export declare function version(options?: ITtscCommonOptions): string;
35
- /**
36
- * Promise-facing variant of `transform()`. The host path stays synchronous so
37
- * plugin descriptors can stay dependency-free, but many adapter surfaces still
38
- * prefer a Promise-returning function.
39
- */
40
- export declare function transformAsync(options: ITtscTransformOptions): Promise<string>;