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
@@ -0,0 +1,234 @@
1
+ const assert = require("node:assert/strict");
2
+ const fs = require("node:fs");
3
+ const os = require("node:os");
4
+ const path = require("node:path");
5
+ const test = require("node:test");
6
+
7
+ const { TtscCompiler } = require("../lib/index.js");
8
+ const { resolveTsgo } = require("../lib/compiler/internal/resolveTsgo.js");
9
+ const tsgo = resolveTsgo({ cwd: path.resolve(__dirname, "..") }).binary;
10
+
11
+ test("TtscCompiler.compile returns output without writing project files", () => {
12
+ const root = createProject();
13
+ const compiler = new TtscCompiler({ binary: tsgo, cwd: root, plugins: false });
14
+
15
+ const result = compiler.compile();
16
+
17
+ assert.equal(result.type, "success");
18
+ assert.match(result.output["dist/main.js"], /api-ok/);
19
+ assert.match(result.output["dist/main.js"], /console\.log\(\s*message\s*\)/);
20
+ assert.match(result.output["dist/main.d.ts"], /declare const message/);
21
+ assert.match(result.output["dist/main.js.map"], /"version":3/);
22
+ assert.match(result.output["dist/main.d.ts.map"], /"version":3/);
23
+ assert.equal(fs.existsSync(path.join(root, "dist")), false);
24
+ });
25
+
26
+ test("TtscCompiler can disable project plugin loading", () => {
27
+ const root = createProject({
28
+ plugins: [{ transform: "./missing-plugin.cjs" }],
29
+ });
30
+ const compiler = new TtscCompiler({ binary: tsgo, cwd: root, plugins: false });
31
+
32
+ const result = compiler.compile();
33
+
34
+ assert.equal(result.type, "success");
35
+ });
36
+
37
+ test("TtscCompiler.compile applies configured source plugins without project output", () => {
38
+ const root = createProject({
39
+ plugins: [{ transform: "./plugin.cjs" }],
40
+ source: 'export const value = goUpper("plugin");\nconsole.log(value);\n',
41
+ });
42
+ writeCompilerPlugin(root);
43
+ const compiler = new TtscCompiler({ binary: tsgo, cwd: root });
44
+
45
+ const result = compiler.compile();
46
+
47
+ assert.equal(result.type, "success");
48
+ assert.match(result.output["dist/main.js"], /PLUGIN/);
49
+ assert.equal(fs.existsSync(path.join(root, "dist")), false);
50
+ });
51
+
52
+ test("TtscCompiler.compile returns structured diagnostics", () => {
53
+ const root = createProject({
54
+ source: 'const value: number = "not-a-number";\nconsole.log(value);\n',
55
+ });
56
+ const compiler = new TtscCompiler({ binary: tsgo, cwd: root, plugins: false });
57
+
58
+ const result = compiler.compile();
59
+
60
+ assert.equal(result.type, "failure");
61
+ assert.equal(result.diagnostics.length, 1);
62
+ assert.equal(result.diagnostics[0].category, "error");
63
+ assert.equal(result.diagnostics[0].code, 2322);
64
+ assert.equal(typeof result.diagnostics[0].start, "number");
65
+ assert.equal(typeof result.diagnostics[0].length, "number");
66
+ assert.equal(result.diagnostics[0].line, 1);
67
+ assert.equal(result.diagnostics[0].character, 7);
68
+ assert.equal(result.diagnostics[0].file.endsWith("src/main.ts"), true);
69
+ assert.match(result.diagnostics[0].messageText, /not assignable/);
70
+ assert.equal(typeof result.output, "object");
71
+ assert.equal(fs.existsSync(path.join(root, "dist")), false);
72
+ });
73
+
74
+ test("TtscCompiler.compile does not accept per-call context overrides", () => {
75
+ const root = createProject();
76
+ const other = createProject({
77
+ plugins: [{ transform: "./missing-plugin.cjs" }],
78
+ });
79
+ const compiler = new TtscCompiler({ binary: tsgo, cwd: root, plugins: false });
80
+
81
+ const result = compiler.compile({
82
+ binary: path.join(other, "missing-tsgo"),
83
+ cwd: other,
84
+ plugins: [{ transform: "./missing-plugin.cjs" }],
85
+ });
86
+
87
+ assert.equal(result.type, "success");
88
+ assert.match(result.output["dist/main.js"], /api-ok/);
89
+ assert.equal(fs.existsSync(path.join(root, "dist")), false);
90
+ assert.equal(fs.existsSync(path.join(other, "dist")), false);
91
+ });
92
+
93
+ test("TtscCompiler.prepare builds source plugins and clean removes context cache", () => {
94
+ const root = createProject({
95
+ plugins: [{ transform: "./plugin.cjs" }],
96
+ });
97
+ writeSourcePlugin(root);
98
+ const cacheDir = path.join(root, ".cache", "ttsc");
99
+ const compiler = new TtscCompiler({ binary: tsgo, cacheDir, cwd: root });
100
+
101
+ const prepared = compiler.prepare();
102
+
103
+ assert.equal(prepared.length, 1);
104
+ assert.equal(fs.existsSync(prepared[0]), true);
105
+ assert.equal(prepared[0].startsWith(path.join(cacheDir, "plugins")), true);
106
+
107
+ const removed = compiler.clean();
108
+
109
+ assert.deepEqual(removed, [cacheDir]);
110
+ assert.equal(fs.existsSync(cacheDir), false);
111
+ });
112
+
113
+ function createProject(options = {}) {
114
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), "ttsc-compiler-api-"));
115
+ fs.mkdirSync(path.join(root, "src"), { recursive: true });
116
+ fs.writeFileSync(
117
+ path.join(root, "src", "main.ts"),
118
+ options.source ??
119
+ 'const message: string = "api-ok";\nconsole.log(message);\n',
120
+ "utf8",
121
+ );
122
+ fs.writeFileSync(
123
+ path.join(root, "tsconfig.json"),
124
+ JSON.stringify(
125
+ {
126
+ compilerOptions: {
127
+ target: "ES2022",
128
+ module: "commonjs",
129
+ strict: true,
130
+ outDir: "dist",
131
+ declaration: true,
132
+ declarationMap: true,
133
+ rootDir: "src",
134
+ sourceMap: true,
135
+ plugins: options.plugins,
136
+ },
137
+ include: ["src"],
138
+ },
139
+ null,
140
+ 2,
141
+ ),
142
+ "utf8",
143
+ );
144
+ return root;
145
+ }
146
+
147
+ function writeSourcePlugin(root) {
148
+ fs.writeFileSync(
149
+ path.join(root, "plugin.cjs"),
150
+ 'module.exports = { name: "prepare-fixture", source: "./plugin-go" };\n',
151
+ "utf8",
152
+ );
153
+ fs.mkdirSync(path.join(root, "plugin-go"), { recursive: true });
154
+ fs.writeFileSync(
155
+ path.join(root, "plugin-go", "go.mod"),
156
+ "module example.com/preparefixture\n\ngo 1.26\n",
157
+ "utf8",
158
+ );
159
+ fs.writeFileSync(
160
+ path.join(root, "plugin-go", "main.go"),
161
+ "package main\n\nfunc main() {}\n",
162
+ "utf8",
163
+ );
164
+ }
165
+
166
+ function writeCompilerPlugin(root) {
167
+ fs.writeFileSync(
168
+ path.join(root, "plugin.cjs"),
169
+ 'module.exports = { name: "compile-fixture", source: "./plugin-go" };\n',
170
+ "utf8",
171
+ );
172
+ fs.mkdirSync(path.join(root, "plugin-go"), { recursive: true });
173
+ fs.writeFileSync(
174
+ path.join(root, "plugin-go", "go.mod"),
175
+ "module example.com/compilefixture\n\ngo 1.26\n",
176
+ "utf8",
177
+ );
178
+ fs.writeFileSync(
179
+ path.join(root, "plugin-go", "main.go"),
180
+ [
181
+ "package main",
182
+ "",
183
+ "import (",
184
+ '\t"flag"',
185
+ '\t"fmt"',
186
+ '\t"os"',
187
+ '\t"path/filepath"',
188
+ '\t"strings"',
189
+ ")",
190
+ "",
191
+ "func main() { os.Exit(run(os.Args[1:])) }",
192
+ "",
193
+ "func run(args []string) int {",
194
+ "\tif len(args) == 0 { return 2 }",
195
+ "\tswitch args[0] {",
196
+ '\tcase "build":',
197
+ "\t\treturn build(args[1:])",
198
+ '\tcase "check", "version":',
199
+ "\t\treturn 0",
200
+ "\tdefault:",
201
+ "\t\treturn 2",
202
+ "\t}",
203
+ "}",
204
+ "",
205
+ "func build(args []string) int {",
206
+ '\tfs := flag.NewFlagSet("build", flag.ContinueOnError)',
207
+ '\tfs.SetOutput(os.Stderr)',
208
+ '\tcwd := fs.String("cwd", "", "")',
209
+ '\toutDir := fs.String("outDir", "dist", "")',
210
+ '\t_ = fs.String("tsconfig", "", "")',
211
+ '\t_ = fs.String("plugins-json", "", "")',
212
+ '\t_ = fs.Bool("emit", false, "")',
213
+ '\t_ = fs.Bool("quiet", false, "")',
214
+ '\t_ = fs.Bool("verbose", false, "")',
215
+ '\t_ = fs.Bool("noEmit", false, "")',
216
+ "\tif err := fs.Parse(args); err != nil { return 2 }",
217
+ "\troot := *cwd",
218
+ "\tif root == \"\" { root, _ = os.Getwd() }",
219
+ '\tinput, err := os.ReadFile(filepath.Join(root, "src", "main.ts"))',
220
+ '\tif err != nil { fmt.Fprintln(os.Stderr, err); return 2 }',
221
+ '\tvalue := "PLUGIN"',
222
+ '\tif !strings.Contains(string(input), `goUpper("plugin")`) { value = "UNKNOWN" }',
223
+ '\toutput := fmt.Sprintf("\\"use strict\\";\\nObject.defineProperty(exports, \\"__esModule\\", { value: true });\\nexports.value = void 0;\\nconst value = %q;\\nexports.value = value;\\nconsole.log(value);\\n", value)',
224
+ '\tfile := filepath.Join(*outDir, "main.js")',
225
+ "\tif !filepath.IsAbs(*outDir) { file = filepath.Join(root, *outDir, \"main.js\") }",
226
+ "\tif err := os.MkdirAll(filepath.Dir(file), 0o755); err != nil { fmt.Fprintln(os.Stderr, err); return 2 }",
227
+ "\tif err := os.WriteFile(file, []byte(output), 0o644); err != nil { fmt.Fprintln(os.Stderr, err); return 2 }",
228
+ "\treturn 0",
229
+ "}",
230
+ "",
231
+ ].join("\n"),
232
+ "utf8",
233
+ );
234
+ }
@@ -1,58 +1,14 @@
1
1
  const assert = require("node:assert/strict");
2
2
  const test = require("node:test");
3
3
  const {
4
- binaryName,
5
- installHint,
6
- platformPackageRequest,
7
4
  resolveBinary,
8
- } = require("../src/platform.ts");
5
+ } = require("../lib/compiler/internal/resolveBinary.js");
9
6
 
10
7
  test("resolveBinary prefers TTSC_BINARY absolute override", () => {
11
8
  const resolved = resolveBinary({
12
9
  env: {
13
10
  TTSC_BINARY: "/tmp/custom-ttsc",
14
11
  },
15
- localBinaryLookup: () => "/tmp/local-ttsc",
16
- resolver: () => {
17
- throw new Error("resolver should not be called when TTSC_BINARY is set");
18
- },
19
12
  });
20
13
  assert.equal(resolved, "/tmp/custom-ttsc");
21
14
  });
22
-
23
- test("resolveBinary falls back to local binary when optional package is missing", () => {
24
- const resolved = resolveBinary({
25
- env: {},
26
- localBinaryLookup: () => "/tmp/local-ttsc",
27
- resolver: () => {
28
- throw new Error("optional package missing");
29
- },
30
- });
31
- assert.equal(resolved, "/tmp/local-ttsc");
32
- });
33
-
34
- test("resolveBinary returns null when only published and local binaries are missing", () => {
35
- const resolved = resolveBinary({
36
- env: {},
37
- localBinaryLookup: () => null,
38
- moduleDir: `${process.cwd()}/src`,
39
- resolver: () => {
40
- throw new Error("optional package missing");
41
- },
42
- });
43
- assert.equal(resolved, null);
44
- });
45
-
46
- test("platform helpers describe the published package contract", () => {
47
- assert.equal(binaryName({ platform: "win32" }), "ttsc.exe");
48
- assert.equal(binaryName({ platform: "linux" }), "ttsc");
49
- assert.equal(
50
- platformPackageRequest({ platform: "linux", arch: "x64" }),
51
- "@ttsc/linux-x64/bin/ttsc",
52
- );
53
-
54
- const hint = installHint({ platform: "linux", arch: "x64" });
55
- assert.match(hint, /@ttsc\/linux-x64/);
56
- assert.match(hint, /TTSC_BINARY env var/);
57
- assert.doesNotMatch(hint, /ttsc-dev\.js/);
58
- });
@@ -6,8 +6,10 @@ const test = require("node:test");
6
6
 
7
7
  const {
8
8
  readProjectConfig,
9
+ } = require("../lib/compiler/internal/project/readProjectConfig.js");
10
+ const {
9
11
  resolveProjectConfig,
10
- } = require("../src/project.ts");
12
+ } = require("../lib/compiler/internal/project/resolveProjectConfig.js");
11
13
 
12
14
  test("resolveProjectConfig canonicalizes symlinked tsconfig paths", () => {
13
15
  const root = fs.mkdtempSync(path.join(os.tmpdir(), "ttsc-project-"));
@@ -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
  });
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>;