ttsc 0.6.0 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (271) hide show
  1. package/cmd/platform/main.go +1 -1
  2. package/cmd/ttsc/api_compile.go +134 -0
  3. package/cmd/ttsc/api_transform.go +64 -0
  4. package/cmd/ttsc/build.go +1 -7
  5. package/cmd/ttsc/main.go +6 -20
  6. package/driver/program.go +13 -3
  7. package/lib/TtscCompiler.d.ts +96 -0
  8. package/lib/TtscCompiler.js +294 -0
  9. package/lib/TtscCompiler.js.map +1 -0
  10. package/lib/compiler/internal/buildNativeCompiler.d.ts +6 -0
  11. package/lib/compiler/internal/buildNativeCompiler.js +72 -0
  12. package/lib/compiler/internal/buildNativeCompiler.js.map +1 -0
  13. package/lib/compiler/internal/compileProjectInMemory.d.ts +7 -0
  14. package/lib/compiler/internal/compileProjectInMemory.js +176 -0
  15. package/lib/compiler/internal/compileProjectInMemory.js.map +1 -0
  16. package/lib/compiler/internal/project/readProjectConfig.d.ts +4 -0
  17. package/lib/{project.js → compiler/internal/project/readProjectConfig.js} +12 -82
  18. package/lib/compiler/internal/project/readProjectConfig.js.map +1 -0
  19. package/lib/compiler/internal/project/resolveProjectConfig.d.ts +3 -0
  20. package/lib/compiler/internal/project/resolveProjectConfig.js +94 -0
  21. package/lib/compiler/internal/project/resolveProjectConfig.js.map +1 -0
  22. package/lib/compiler/internal/resolveBinary.d.ts +4 -0
  23. package/lib/compiler/internal/resolveBinary.js +65 -0
  24. package/lib/compiler/internal/resolveBinary.js.map +1 -0
  25. package/lib/compiler/internal/resolveEmittedJavaScript.d.ts +7 -0
  26. package/lib/compiler/internal/resolveEmittedJavaScript.js +118 -0
  27. package/lib/compiler/internal/resolveEmittedJavaScript.js.map +1 -0
  28. package/lib/compiler/internal/resolveTsgo.d.ts +20 -0
  29. package/lib/{tsgo.js → compiler/internal/resolveTsgo.js} +5 -10
  30. package/lib/compiler/internal/resolveTsgo.js.map +1 -0
  31. package/lib/compiler/internal/runBuild.d.ts +10 -0
  32. package/lib/compiler/internal/runBuild.js +480 -0
  33. package/lib/compiler/internal/runBuild.js.map +1 -0
  34. package/lib/compiler/internal/runSingleFileEmit.d.ts +3 -0
  35. package/lib/compiler/internal/runSingleFileEmit.js +102 -0
  36. package/lib/compiler/internal/runSingleFileEmit.js.map +1 -0
  37. package/lib/compiler/internal/transformProjectInMemory.d.ts +7 -0
  38. package/lib/compiler/internal/transformProjectInMemory.js +275 -0
  39. package/lib/compiler/internal/transformProjectInMemory.js.map +1 -0
  40. package/lib/index.d.ts +6 -15
  41. package/lib/index.js +6 -15
  42. package/lib/index.js.map +1 -1
  43. package/lib/launcher/internal/getCompilerVersionText.d.ts +3 -0
  44. package/lib/launcher/internal/getCompilerVersionText.js +71 -0
  45. package/lib/launcher/internal/getCompilerVersionText.js.map +1 -0
  46. package/lib/launcher/internal/prepareExecution.d.ts +10 -0
  47. package/lib/launcher/internal/prepareExecution.js +120 -0
  48. package/lib/launcher/internal/prepareExecution.js.map +1 -0
  49. package/lib/launcher/internal/runTtsc.d.ts +1 -0
  50. package/lib/{cli.js → launcher/internal/runTtsc.js} +45 -118
  51. package/lib/launcher/internal/runTtsc.js.map +1 -0
  52. package/lib/launcher/internal/runTtsx.d.ts +1 -0
  53. package/lib/{runner/cli.js → launcher/internal/runTtsx.js} +13 -35
  54. package/lib/launcher/internal/runTtsx.js.map +1 -0
  55. package/lib/launcher/ttsc.js +2 -2
  56. package/lib/launcher/ttsc.js.map +1 -1
  57. package/lib/launcher/ttsx.js +2 -2
  58. package/lib/launcher/ttsx.js.map +1 -1
  59. package/lib/plugin/internal/buildSourcePlugin.d.ts +12 -0
  60. package/lib/{source-build.js → plugin/internal/buildSourcePlugin.js} +77 -84
  61. package/lib/plugin/internal/buildSourcePlugin.js.map +1 -0
  62. package/lib/plugin/internal/loadProjectPlugins.d.ts +14 -0
  63. package/lib/{plugin.js → plugin/internal/loadProjectPlugins.js} +38 -76
  64. package/lib/plugin/internal/loadProjectPlugins.js.map +1 -0
  65. package/lib/structures/ITtscCompilerContext.d.ts +78 -0
  66. package/lib/structures/ITtscCompilerContext.js +3 -0
  67. package/lib/structures/ITtscCompilerContext.js.map +1 -0
  68. package/lib/structures/ITtscCompilerDiagnostic.d.ts +83 -0
  69. package/lib/structures/ITtscCompilerDiagnostic.js +3 -0
  70. package/lib/structures/ITtscCompilerDiagnostic.js.map +1 -0
  71. package/lib/structures/ITtscCompilerResult.d.ts +83 -0
  72. package/lib/structures/{ITtscCacheKeyInput.js → ITtscCompilerResult.js} +1 -1
  73. package/lib/structures/ITtscCompilerResult.js.map +1 -0
  74. package/lib/structures/ITtscCompilerTransformation.d.ts +35 -0
  75. package/lib/structures/ITtscCompilerTransformation.js +3 -0
  76. package/lib/structures/ITtscCompilerTransformation.js.map +1 -0
  77. package/lib/structures/ITtscPlugin.d.ts +52 -7
  78. package/lib/structures/ITtscPluginFactoryContext.d.ts +47 -0
  79. package/lib/structures/ITtscProjectPluginConfig.d.ts +41 -0
  80. package/lib/structures/TtscPluginStage.d.ts +18 -0
  81. package/lib/structures/{ITtscBuildResult.js → TtscPluginStage.js} +1 -1
  82. package/lib/structures/TtscPluginStage.js.map +1 -0
  83. package/lib/structures/index.d.ts +8 -0
  84. package/lib/structures/index.js +25 -0
  85. package/lib/structures/index.js.map +1 -0
  86. package/lib/structures/internal/ITtscLoadedNativePlugin.d.ts +13 -0
  87. package/lib/structures/internal/ITtscLoadedNativePlugin.js.map +1 -0
  88. package/lib/structures/internal/ITtscParsedProjectConfig.d.ts +15 -0
  89. package/lib/structures/internal/ITtscParsedProjectConfig.js.map +1 -0
  90. package/lib/structures/internal/ITtscProjectLocatorOptions.d.ts +9 -0
  91. package/lib/structures/internal/ITtscProjectLocatorOptions.js.map +1 -0
  92. package/lib/structures/internal/TtscBuildOptions.d.ts +18 -0
  93. package/lib/structures/{ITtscBuildOptions.js → internal/TtscBuildOptions.js} +1 -1
  94. package/lib/structures/internal/TtscBuildOptions.js.map +1 -0
  95. package/lib/structures/internal/TtscBuildResult.d.ts +14 -0
  96. package/lib/structures/{ITtscCheckOptions.js → internal/TtscBuildResult.js} +1 -1
  97. package/lib/structures/internal/TtscBuildResult.js.map +1 -0
  98. package/lib/structures/internal/TtscCommonOptions.d.ts +27 -0
  99. package/lib/structures/{ITtscNativeSource.js → internal/TtscCommonOptions.js} +1 -1
  100. package/lib/structures/internal/TtscCommonOptions.js.map +1 -0
  101. package/lib/structures/internal/TtscSingleFileEmitOptions.d.ts +14 -0
  102. package/lib/structures/internal/TtscSingleFileEmitOptions.js +3 -0
  103. package/lib/structures/internal/TtscSingleFileEmitOptions.js.map +1 -0
  104. package/package.json +9 -9
  105. package/src/TtscCompiler.ts +305 -0
  106. package/src/compiler/internal/buildNativeCompiler.ts +43 -0
  107. package/src/compiler/internal/compileProjectInMemory.ts +189 -0
  108. package/src/{project.ts → compiler/internal/project/readProjectConfig.ts} +36 -111
  109. package/src/compiler/internal/project/resolveProjectConfig.ts +67 -0
  110. package/src/compiler/internal/resolveBinary.ts +41 -0
  111. package/src/compiler/internal/resolveEmittedJavaScript.ts +102 -0
  112. package/src/{tsgo.ts → compiler/internal/resolveTsgo.ts} +20 -23
  113. package/src/compiler/internal/runBuild.ts +644 -0
  114. package/src/compiler/internal/runSingleFileEmit.ts +73 -0
  115. package/src/compiler/internal/transformProjectInMemory.ts +368 -0
  116. package/src/index.ts +6 -15
  117. package/src/launcher/internal/getCompilerVersionText.ts +44 -0
  118. package/src/launcher/internal/prepareExecution.ts +115 -0
  119. package/src/{cli.ts → launcher/internal/runTtsc.ts} +58 -140
  120. package/src/{runner/cli.ts → launcher/internal/runTtsx.ts} +21 -49
  121. package/src/launcher/ttsc.ts +2 -2
  122. package/src/launcher/ttsx.ts +2 -2
  123. package/src/{source-build.ts → plugin/internal/buildSourcePlugin.ts} +121 -115
  124. package/src/plugin/internal/loadProjectPlugins.ts +214 -0
  125. package/src/structures/ITtscCompilerContext.ts +84 -0
  126. package/src/structures/ITtscCompilerDiagnostic.ts +91 -0
  127. package/src/structures/ITtscCompilerResult.ts +94 -0
  128. package/src/structures/ITtscCompilerTransformation.ts +46 -0
  129. package/src/structures/ITtscPlugin.ts +54 -7
  130. package/src/structures/ITtscPluginFactoryContext.ts +51 -0
  131. package/src/structures/ITtscProjectPluginConfig.ts +43 -0
  132. package/src/structures/TtscPluginStage.ts +18 -0
  133. package/src/structures/index.ts +8 -0
  134. package/src/structures/internal/ITtscLoadedNativePlugin.ts +14 -0
  135. package/src/structures/internal/ITtscParsedProjectConfig.ts +16 -0
  136. package/src/structures/internal/ITtscProjectLocatorOptions.ts +9 -0
  137. package/src/structures/internal/TtscBuildOptions.ts +19 -0
  138. package/src/structures/internal/TtscBuildResult.ts +15 -0
  139. package/src/structures/internal/TtscCommonOptions.ts +28 -0
  140. package/src/structures/internal/TtscSingleFileEmitOptions.ts +15 -0
  141. package/test/compiler.test.ts +422 -0
  142. package/test/platform.test.ts +1 -45
  143. package/test/project.test.ts +3 -1
  144. package/test/source-build.test.ts +33 -220
  145. package/test/tsgo.test.ts +1 -8
  146. package/cmd/ttsc/transform.go +0 -166
  147. package/lib/api.d.ts +0 -40
  148. package/lib/api.js +0 -680
  149. package/lib/api.js.map +0 -1
  150. package/lib/cli.d.ts +0 -1
  151. package/lib/cli.js.map +0 -1
  152. package/lib/native.d.ts +0 -8
  153. package/lib/native.js +0 -42
  154. package/lib/native.js.map +0 -1
  155. package/lib/platform.d.ts +0 -52
  156. package/lib/platform.js +0 -175
  157. package/lib/platform.js.map +0 -1
  158. package/lib/plugin.d.ts +0 -13
  159. package/lib/plugin.js.map +0 -1
  160. package/lib/project.d.ts +0 -14
  161. package/lib/project.js.map +0 -1
  162. package/lib/runner/cli.d.ts +0 -1
  163. package/lib/runner/cli.js.map +0 -1
  164. package/lib/runner/register.d.ts +0 -6
  165. package/lib/runner/register.js +0 -358
  166. package/lib/runner/register.js.map +0 -1
  167. package/lib/source-build.d.ts +0 -19
  168. package/lib/source-build.js.map +0 -1
  169. package/lib/structures/ITtscBuildOptions.d.ts +0 -14
  170. package/lib/structures/ITtscBuildOptions.js.map +0 -1
  171. package/lib/structures/ITtscBuildResult.d.ts +0 -6
  172. package/lib/structures/ITtscBuildResult.js.map +0 -1
  173. package/lib/structures/ITtscBuildSourcePluginOptions.d.ts +0 -8
  174. package/lib/structures/ITtscBuildSourcePluginOptions.js +0 -3
  175. package/lib/structures/ITtscBuildSourcePluginOptions.js.map +0 -1
  176. package/lib/structures/ITtscCacheKeyInput.d.ts +0 -7
  177. package/lib/structures/ITtscCacheKeyInput.js.map +0 -1
  178. package/lib/structures/ITtscCheckOptions.d.ts +0 -2
  179. package/lib/structures/ITtscCheckOptions.js.map +0 -1
  180. package/lib/structures/ITtscCommonOptions.d.ts +0 -22
  181. package/lib/structures/ITtscCommonOptions.js +0 -3
  182. package/lib/structures/ITtscCommonOptions.js.map +0 -1
  183. package/lib/structures/ITtscLoadPluginsOptions.d.ts +0 -8
  184. package/lib/structures/ITtscLoadPluginsOptions.js +0 -3
  185. package/lib/structures/ITtscLoadPluginsOptions.js.map +0 -1
  186. package/lib/structures/ITtscLoadedNativePlugin.d.ts +0 -7
  187. package/lib/structures/ITtscLoadedNativePlugin.js.map +0 -1
  188. package/lib/structures/ITtscLoadedPlugins.d.ts +0 -11
  189. package/lib/structures/ITtscLoadedPlugins.js +0 -3
  190. package/lib/structures/ITtscLoadedPlugins.js.map +0 -1
  191. package/lib/structures/ITtscNativeBackend.d.ts +0 -10
  192. package/lib/structures/ITtscNativeBackend.js +0 -3
  193. package/lib/structures/ITtscNativeBackend.js.map +0 -1
  194. package/lib/structures/ITtscNativePluginContractVersion.d.ts +0 -1
  195. package/lib/structures/ITtscNativePluginContractVersion.js +0 -3
  196. package/lib/structures/ITtscNativePluginContractVersion.js.map +0 -1
  197. package/lib/structures/ITtscNativePluginShape.d.ts +0 -8
  198. package/lib/structures/ITtscNativePluginShape.js +0 -3
  199. package/lib/structures/ITtscNativePluginShape.js.map +0 -1
  200. package/lib/structures/ITtscNativeRewriteMode.d.ts +0 -1
  201. package/lib/structures/ITtscNativeRewriteMode.js +0 -3
  202. package/lib/structures/ITtscNativeRewriteMode.js.map +0 -1
  203. package/lib/structures/ITtscNativeSource.d.ts +0 -4
  204. package/lib/structures/ITtscNativeSource.js.map +0 -1
  205. package/lib/structures/ITtscParsedProjectConfig.d.ts +0 -9
  206. package/lib/structures/ITtscParsedProjectConfig.js.map +0 -1
  207. package/lib/structures/ITtscPluginFactory.d.ts +0 -4
  208. package/lib/structures/ITtscPluginFactory.js +0 -3
  209. package/lib/structures/ITtscPluginFactory.js.map +0 -1
  210. package/lib/structures/ITtscPluginModule.d.ts +0 -3
  211. package/lib/structures/ITtscPluginModule.js +0 -3
  212. package/lib/structures/ITtscPluginModule.js.map +0 -1
  213. package/lib/structures/ITtscPreparedExecution.d.ts +0 -5
  214. package/lib/structures/ITtscPreparedExecution.js +0 -3
  215. package/lib/structures/ITtscPreparedExecution.js.map +0 -1
  216. package/lib/structures/ITtscProjectLocatorOptions.d.ts +0 -5
  217. package/lib/structures/ITtscProjectLocatorOptions.js.map +0 -1
  218. package/lib/structures/ITtscRegisterOptions.d.ts +0 -6
  219. package/lib/structures/ITtscRegisterOptions.js +0 -3
  220. package/lib/structures/ITtscRegisterOptions.js.map +0 -1
  221. package/lib/structures/ITtscResolveGoBinaryOptions.d.ts +0 -7
  222. package/lib/structures/ITtscResolveGoBinaryOptions.js +0 -3
  223. package/lib/structures/ITtscResolveGoBinaryOptions.js.map +0 -1
  224. package/lib/structures/ITtscResolveOptions.d.ts +0 -20
  225. package/lib/structures/ITtscResolveOptions.js +0 -3
  226. package/lib/structures/ITtscResolveOptions.js.map +0 -1
  227. package/lib/structures/ITtscResolvedTsgo.d.ts +0 -8
  228. package/lib/structures/ITtscResolvedTsgo.js +0 -3
  229. package/lib/structures/ITtscResolvedTsgo.js.map +0 -1
  230. package/lib/structures/ITtscTransformOptions.d.ts +0 -12
  231. package/lib/structures/ITtscTransformOptions.js +0 -3
  232. package/lib/structures/ITtscTransformOptions.js.map +0 -1
  233. package/lib/structures/ITtscTsgoResolveOptions.d.ts +0 -14
  234. package/lib/structures/ITtscTsgoResolveOptions.js +0 -3
  235. package/lib/structures/ITtscTsgoResolveOptions.js.map +0 -1
  236. package/lib/tsgo.d.ts +0 -6
  237. package/lib/tsgo.js.map +0 -1
  238. package/src/api.ts +0 -872
  239. package/src/native.ts +0 -64
  240. package/src/platform.ts +0 -151
  241. package/src/plugin.ts +0 -253
  242. package/src/runner/register.ts +0 -406
  243. package/src/structures/ITtscBuildOptions.ts +0 -19
  244. package/src/structures/ITtscBuildResult.ts +0 -6
  245. package/src/structures/ITtscBuildSourcePluginOptions.ts +0 -9
  246. package/src/structures/ITtscCacheKeyInput.ts +0 -7
  247. package/src/structures/ITtscCheckOptions.ts +0 -3
  248. package/src/structures/ITtscCommonOptions.ts +0 -23
  249. package/src/structures/ITtscLoadPluginsOptions.ts +0 -9
  250. package/src/structures/ITtscLoadedNativePlugin.ts +0 -8
  251. package/src/structures/ITtscLoadedPlugins.ts +0 -12
  252. package/src/structures/ITtscNativeBackend.ts +0 -11
  253. package/src/structures/ITtscNativePluginContractVersion.ts +0 -1
  254. package/src/structures/ITtscNativePluginShape.ts +0 -9
  255. package/src/structures/ITtscNativeRewriteMode.ts +0 -1
  256. package/src/structures/ITtscNativeSource.ts +0 -4
  257. package/src/structures/ITtscParsedProjectConfig.ts +0 -10
  258. package/src/structures/ITtscPluginFactory.ts +0 -7
  259. package/src/structures/ITtscPluginModule.ts +0 -4
  260. package/src/structures/ITtscPreparedExecution.ts +0 -5
  261. package/src/structures/ITtscProjectLocatorOptions.ts +0 -5
  262. package/src/structures/ITtscRegisterOptions.ts +0 -7
  263. package/src/structures/ITtscResolveGoBinaryOptions.ts +0 -7
  264. package/src/structures/ITtscResolveOptions.ts +0 -20
  265. package/src/structures/ITtscResolvedTsgo.ts +0 -8
  266. package/src/structures/ITtscTransformOptions.ts +0 -13
  267. package/src/structures/ITtscTsgoResolveOptions.ts +0 -14
  268. package/test/native.test.ts +0 -103
  269. /package/lib/structures/{ITtscLoadedNativePlugin.js → internal/ITtscLoadedNativePlugin.js} +0 -0
  270. /package/lib/structures/{ITtscParsedProjectConfig.js → internal/ITtscParsedProjectConfig.js} +0 -0
  271. /package/lib/structures/{ITtscProjectLocatorOptions.js → internal/ITtscProjectLocatorOptions.js} +0 -0
package/src/native.ts DELETED
@@ -1,64 +0,0 @@
1
- import type { ITtscNativeBackend } from "./structures/ITtscNativeBackend";
2
- import type { ITtscNativePluginShape } from "./structures/ITtscNativePluginShape";
3
-
4
- export type { ITtscNativeBackend } from "./structures/ITtscNativeBackend";
5
- export type { ITtscNativePluginContractVersion } from "./structures/ITtscNativePluginContractVersion";
6
- export type { ITtscNativePluginShape } from "./structures/ITtscNativePluginShape";
7
- export type { ITtscNativeRewriteMode } from "./structures/ITtscNativeRewriteMode";
8
- export type { ITtscNativeSource } from "./structures/ITtscNativeSource";
9
-
10
- export function resolveNativeBackend(
11
- plugin: ITtscNativePluginShape,
12
- ): ITtscNativeBackend | null {
13
- if (plugin.native && (plugin.nativeMode || plugin.nativeBinary)) {
14
- throw new Error(
15
- `ttsc: plugin "${plugin.name}" must use either native or nativeMode/nativeBinary, not both`,
16
- );
17
- }
18
- if (!plugin.native) {
19
- if (
20
- plugin.nativeBinary &&
21
- (!plugin.nativeMode || plugin.nativeMode === "none")
22
- ) {
23
- throw new Error(
24
- `ttsc: plugin "${plugin.name}" declared nativeBinary without a non-empty nativeMode`,
25
- );
26
- }
27
- if (!plugin.nativeMode || plugin.nativeMode === "none") {
28
- return null;
29
- }
30
- return {
31
- binary: plugin.nativeBinary,
32
- contractVersion: 1,
33
- mode: plugin.nativeMode,
34
- };
35
- }
36
-
37
- const backend = plugin.native;
38
- if (!backend.mode || backend.mode === "none") {
39
- throw new Error(
40
- `ttsc: plugin "${plugin.name}" declared native without a non-empty mode`,
41
- );
42
- }
43
- if (backend.contractVersion !== undefined && backend.contractVersion !== 1) {
44
- throw new Error(
45
- `ttsc: plugin "${plugin.name}" requested unsupported native contract version ${backend.contractVersion}`,
46
- );
47
- }
48
- if (backend.source) {
49
- if (backend.binary) {
50
- throw new Error(
51
- `ttsc: plugin "${plugin.name}" must use either native.binary or native.source, not both`,
52
- );
53
- }
54
- if (typeof backend.source.dir !== "string" || backend.source.dir.length === 0) {
55
- throw new Error(
56
- `ttsc: plugin "${plugin.name}" native.source.dir must be a non-empty string`,
57
- );
58
- }
59
- }
60
- return {
61
- ...backend,
62
- contractVersion: backend.contractVersion ?? 1,
63
- };
64
- }
package/src/platform.ts DELETED
@@ -1,151 +0,0 @@
1
- /**
2
- * Platform detection and helper-binary resolution for ttsc.
3
- *
4
- * This is the tested implementation used by the ttsc launcher after build.
5
- * Keep it pure and dependency-free so it can be unit-tested without spawning
6
- * any child process.
7
- *
8
- * The platform packages now ship a small compatibility helper, not the
9
- * TypeScript-Go compiler. Compiler calls resolve the consuming project's
10
- * `@typescript/native-preview` through `tsgo.ts`.
11
- *
12
- * Helper resolution order:
13
- *
14
- * 1. `TTSC_BINARY` environment variable (absolute path). Highest priority —
15
- * lets local devs or CI point at an ad-hoc build without touching
16
- * node_modules.
17
- * 2. `@ttsc/{platform}-{arch}/bin/ttsc{.exe}` optional dependency.
18
- * Standard distribution path for compatibility helper commands.
19
- * 3. A package-local `native/ttsc-native` helper. Used by this
20
- * repository's local `pnpm run build` output before the
21
- * platform packages exist on npm.
22
- *
23
- * If every strategy fails, `resolveBinary` returns `null` so the caller can print a
24
- * detailed install hint.
25
- */
26
-
27
- import * as fs from "node:fs";
28
- import * as path from "node:path";
29
-
30
- import type { ITtscResolveOptions } from "./structures/ITtscResolveOptions";
31
-
32
- export type { ITtscResolveOptions } from "./structures/ITtscResolveOptions";
33
-
34
- /** Canonical key `{platform}-{arch}` used in the binary package name. */
35
- export function platformKey(opts: ITtscResolveOptions = {}): string {
36
- const platform = opts.platform ?? process.platform;
37
- const arch = opts.arch ?? process.arch;
38
- return `${platform}-${arch}`;
39
- }
40
-
41
- /** Helper binary filename for the current platform (`.exe` on Windows, else none). */
42
- export function binaryName(opts: ITtscResolveOptions = {}): string {
43
- const platform = opts.platform ?? process.platform;
44
- return platform === "win32" ? "ttsc.exe" : "ttsc";
45
- }
46
-
47
- /**
48
- * Resolve the optional-dependency module request string for the current
49
- * platform. Separated from actual resolution so we can log the request when
50
- * the dependency is missing.
51
- */
52
- export function platformPackageRequest(opts: ITtscResolveOptions = {}): string {
53
- const key = platformKey(opts);
54
- const bin = binaryName(opts);
55
- return `@ttsc/${key}/bin/${bin}`;
56
- }
57
-
58
- /** The list of platform keys the package currently ships. */
59
- export const SUPPORTED_PLATFORMS: readonly string[] = Object.freeze([
60
- "linux-x64",
61
- "linux-arm",
62
- "linux-arm64",
63
- "darwin-x64",
64
- "darwin-arm64",
65
- "win32-x64",
66
- "win32-arm64",
67
- ]);
68
-
69
- /** True if the given platform key is in the supported list. */
70
- export function isSupported(key: string): boolean {
71
- return SUPPORTED_PLATFORMS.includes(key);
72
- }
73
-
74
- /**
75
- * Resolve an absolute path to the ttsc helper using the documented priority
76
- * order. Returns `null` when every strategy fails — the launcher then prints
77
- * the install hint and exits non-zero.
78
- */
79
- export function resolveBinary(opts: ITtscResolveOptions = {}): string | null {
80
- const env = opts.env ?? process.env;
81
-
82
- // 1. Env override. Absolute path is required; a relative value is ignored so
83
- // tests can't accidentally resolve something outside the intended tree.
84
- if (env.TTSC_BINARY && path.isAbsolute(env.TTSC_BINARY)) {
85
- return env.TTSC_BINARY;
86
- }
87
-
88
- // 2. Platform-specific optional dependency (the published distribution path).
89
- const resolver = opts.resolver ?? ((req: string) => require.resolve(req));
90
- try {
91
- return resolver(platformPackageRequest(opts));
92
- } catch {
93
- /* fall through */
94
- }
95
-
96
- // 3. Package-local fallback: <package>/native/ttsc-native.
97
- if (opts.localBinaryLookup) {
98
- const local = opts.localBinaryLookup();
99
- if (local) return local;
100
- } else {
101
- const local = defaultLocalBinaryPath(opts);
102
- if (local) return local;
103
- }
104
-
105
- return null;
106
- }
107
-
108
- /**
109
- * Human-readable message shown when no binary can be located. Pure string —
110
- * the launcher writes it to stderr.
111
- */
112
- export function installHint(opts: ITtscResolveOptions = {}): string {
113
- const key = platformKey(opts);
114
- const pkg = `@ttsc/${key}`;
115
- const supported = SUPPORTED_PLATFORMS.join(", ");
116
- return [
117
- `ttsc: platform-specific helper binary not found (${pkg}).`,
118
- `Platform: ${opts.platform ?? process.platform}/${opts.arch ?? process.arch}.`,
119
- ``,
120
- `Resolution order:`,
121
- ` 1. TTSC_BINARY env var (absolute path)`,
122
- ` 2. ${pkg} optional dependency`,
123
- ` 3. ./native/ttsc-native (local workspace build)`,
124
- ``,
125
- `Try:`,
126
- ` npm install --include=optional ${pkg}`,
127
- ` pnpm install --shamefully-hoist`,
128
- ``,
129
- `Supported platforms: ${supported}.`,
130
- `If your platform is not in that list, open an issue at`,
131
- `https://github.com/samchon/ttsc/issues.`,
132
- ].join("\n");
133
- }
134
-
135
- function defaultLocalBinaryPath(opts: ITtscResolveOptions): string | null {
136
- const root = packageRootDir(opts);
137
- const candidate = path.resolve(
138
- root,
139
- "..",
140
- "native",
141
- (opts.platform ?? process.platform) === "win32" ? "ttsc-native.exe" : "ttsc-native",
142
- );
143
- return fs.existsSync(candidate) ? candidate : null;
144
- }
145
-
146
- function packageRootDir(opts: ITtscResolveOptions = {}): string {
147
- if (opts.moduleDir) {
148
- return fs.realpathSync.native?.(opts.moduleDir) ?? fs.realpathSync(opts.moduleDir);
149
- }
150
- return fs.realpathSync.native?.(__dirname) ?? fs.realpathSync(__dirname);
151
- }
package/src/plugin.ts DELETED
@@ -1,253 +0,0 @@
1
- import { createRequire } from "node:module";
2
- import fs from "node:fs";
3
- import path from "node:path";
4
-
5
- import type { ITtscLoadPluginsOptions } from "./structures/ITtscLoadPluginsOptions";
6
- import type { ITtscLoadedNativePlugin } from "./structures/ITtscLoadedNativePlugin";
7
- import type { ITtscLoadedPlugins } from "./structures/ITtscLoadedPlugins";
8
- import type { ITtscPlugin } from "./structures/ITtscPlugin";
9
- import type { ITtscPluginFactory } from "./structures/ITtscPluginFactory";
10
- import type { ITtscPluginFactoryContext } from "./structures/ITtscPluginFactoryContext";
11
- import type { ITtscPluginModule } from "./structures/ITtscPluginModule";
12
- import type { ITtscProjectPluginConfig } from "./structures/ITtscProjectPluginConfig";
13
- import { resolveNativeBackend } from "./native";
14
- import { readProjectConfig } from "./project";
15
- import { buildSourcePlugin } from "./source-build";
16
-
17
- export type { ITtscLoadPluginsOptions } from "./structures/ITtscLoadPluginsOptions";
18
- export type { ITtscLoadedNativePlugin } from "./structures/ITtscLoadedNativePlugin";
19
- export type { ITtscLoadedPlugins } from "./structures/ITtscLoadedPlugins";
20
- export type {
21
- ITtscNativePluginContractVersion,
22
- ITtscNativeRewriteMode,
23
- ITtscNativeBackend,
24
- ITtscNativeSource,
25
- } from "./native";
26
- export type { ITtscPlugin } from "./structures/ITtscPlugin";
27
- export type { ITtscPluginFactory } from "./structures/ITtscPluginFactory";
28
- export type { ITtscPluginFactoryContext } from "./structures/ITtscPluginFactoryContext";
29
- export type { ITtscPluginModule } from "./structures/ITtscPluginModule";
30
-
31
- export function definePlugin<T extends ITtscPluginModule>(plugin: T): T {
32
- return plugin;
33
- }
34
-
35
- export function loadProjectPlugins(options: ITtscLoadPluginsOptions): ITtscLoadedPlugins {
36
- const project = readProjectConfig({
37
- cwd: options.cwd,
38
- file: options.file,
39
- tsconfig: options.tsconfig,
40
- });
41
- const entries =
42
- options.entries === false
43
- ? []
44
- : [...(options.entries ?? project.compilerOptions.plugins)].filter(
45
- (entry) => entry.enabled !== false,
46
- );
47
- if (entries.length === 0) {
48
- return {
49
- compatibilityFallback: false,
50
- nativeBinary: null,
51
- nativeBinaries: [],
52
- nativePlugins: [],
53
- plugins: [],
54
- project,
55
- };
56
- }
57
-
58
- const context = {
59
- binary: options.binary,
60
- cwd: path.resolve(options.cwd ?? process.cwd()),
61
- projectRoot: project.root,
62
- tsconfig: project.path,
63
- };
64
- const plugins = entries.map((entry) =>
65
- loadPluginEntry(entry, { ...context, plugin: entry }),
66
- );
67
-
68
- const nativePlugins: ITtscLoadedNativePlugin[] = [];
69
- const ttscVersion = readTtscVersion();
70
- const tsgoVersion = readTsgoVersion(context.projectRoot);
71
- plugins.forEach((plugin, index) => {
72
- let backend = resolveNativeBackend(plugin);
73
- if (!backend) {
74
- return;
75
- }
76
- if (backend.source && !backend.binary) {
77
- const built = buildSourcePlugin({
78
- baseDir: context.projectRoot,
79
- pluginName: plugin.name,
80
- source: backend.source,
81
- ttscVersion,
82
- tsgoVersion,
83
- });
84
- backend = { ...backend, binary: built };
85
- }
86
- nativePlugins.push({ backend, config: entries[index]!, name: plugin.name });
87
- });
88
- if (nativePlugins.length !== plugins.length) {
89
- const missing = plugins
90
- .filter((plugin) => !resolveNativeBackend(plugin))
91
- .map((plugin) => plugin.name)
92
- .join(", ");
93
- throw new Error(
94
- `ttsc: every plugin must declare a native backend; missing native for ${missing}`,
95
- );
96
- }
97
- return {
98
- compatibilityFallback: false,
99
- nativeBinary: nativePlugins[0]?.backend.binary ?? null,
100
- nativeBinaries: [
101
- ...new Set(
102
- nativePlugins
103
- .map((plugin) => plugin.backend.binary)
104
- .filter((binary): binary is string => typeof binary === "string"),
105
- ),
106
- ],
107
- nativePlugins,
108
- plugins,
109
- project,
110
- };
111
- }
112
-
113
- function loadPluginEntry(
114
- entry: ITtscProjectPluginConfig,
115
- context: ITtscPluginFactoryContext,
116
- ): ITtscPlugin {
117
- const specifier = entry.transform;
118
- if (typeof specifier !== "string" || specifier.length === 0) {
119
- throw new Error(`ttsc: plugin entry is missing a string "transform" field`);
120
- }
121
-
122
- const request = resolvePluginRequest(specifier, context.projectRoot);
123
- const mod = require(request) as {
124
- createTtscPlugin?: ITtscPluginFactory;
125
- default?: ITtscPluginModule;
126
- } & Partial<Record<"plugin", ITtscPluginModule>>;
127
- const candidate =
128
- mod.createTtscPlugin ??
129
- mod.default ??
130
- mod.plugin ??
131
- (mod as unknown as ITtscPluginModule);
132
- if (typeof candidate === "function") {
133
- const plugin = candidate(context);
134
- if (!isTtscPlugin(plugin)) {
135
- throw new Error(
136
- `ttsc: plugin "${specifier}" does not export a valid ttsc plugin`,
137
- );
138
- }
139
- rejectJsTransformHooks(specifier, plugin);
140
- return plugin;
141
- }
142
- if (isTtscPlugin(candidate)) {
143
- rejectJsTransformHooks(specifier, candidate);
144
- return candidate;
145
- }
146
- throw new Error(
147
- `ttsc: plugin "${specifier}" does not export a valid ttsc plugin`,
148
- );
149
- }
150
-
151
- function isTtscPlugin(value: unknown): value is ITtscPlugin {
152
- return (
153
- typeof value === "object" &&
154
- value !== null &&
155
- typeof (value as { name?: unknown }).name === "string"
156
- );
157
- }
158
-
159
- function rejectJsTransformHooks(specifier: string, candidate: object): void {
160
- if ("transformSource" in candidate || "transformOutput" in candidate) {
161
- throw new Error(
162
- `ttsc: plugin "${specifier}" declares unsupported JS transform hooks; ` +
163
- "declare a native backend instead",
164
- );
165
- }
166
- }
167
-
168
- function resolvePluginRequest(specifier: string, projectRoot: string): string {
169
- if (path.isAbsolute(specifier)) {
170
- return specifier;
171
- }
172
- if (isRelativePluginSpecifier(specifier)) {
173
- return path.resolve(projectRoot, specifier);
174
- }
175
- try {
176
- return require.resolve(specifier, { paths: [projectRoot] });
177
- } catch (error) {
178
- const sourceFallback = resolveSourceCheckoutPlugin(specifier, projectRoot);
179
- if (sourceFallback) {
180
- return sourceFallback;
181
- }
182
- throw error;
183
- }
184
- }
185
-
186
- function isRelativePluginSpecifier(specifier: string): boolean {
187
- return (
188
- specifier === "." ||
189
- specifier === ".." ||
190
- specifier.startsWith("./") ||
191
- specifier.startsWith("../") ||
192
- specifier.startsWith(".\\") ||
193
- specifier.startsWith("..\\")
194
- );
195
- }
196
-
197
- function resolveSourceCheckoutPlugin(
198
- specifier: string,
199
- projectRoot: string,
200
- ): string | null {
201
- const normalized = specifier.replace(/\\/g, "/");
202
- const match = normalized.match(/^(.*)\/lib\/transform$/);
203
- if (!match) {
204
- return null;
205
- }
206
- try {
207
- const packageJson = require.resolve(`${match[1]}/package.json`, {
208
- paths: [projectRoot],
209
- });
210
- const packageRoot = path.dirname(packageJson);
211
- const candidates = [
212
- path.join(packageRoot, "lib", "transform.js"),
213
- path.join(packageRoot, "src", "transform.ts"),
214
- path.join(packageRoot, "bin", "ttsc-plugin.cjs"),
215
- ];
216
- return candidates.find((candidate) => fs.existsSync(candidate)) ?? null;
217
- } catch {
218
- return null;
219
- }
220
- }
221
-
222
- let cachedTtscVersion: string | null = null;
223
-
224
- function readTtscVersion(): string {
225
- if (cachedTtscVersion !== null) {
226
- return cachedTtscVersion;
227
- }
228
- try {
229
- const file = path.resolve(__dirname, "..", "package.json");
230
- const pkg = JSON.parse(fs.readFileSync(file, "utf8")) as {
231
- version?: string;
232
- };
233
- cachedTtscVersion = pkg.version ?? "0.0.0";
234
- } catch {
235
- cachedTtscVersion = "0.0.0";
236
- }
237
- return cachedTtscVersion;
238
- }
239
-
240
- function readTsgoVersion(projectRoot: string): string {
241
- try {
242
- const projectRequire = createRequire(path.join(projectRoot, "package.json"));
243
- const pkgPath = projectRequire.resolve(
244
- "@typescript/native-preview/package.json",
245
- );
246
- const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf8")) as {
247
- version?: string;
248
- };
249
- return pkg.version ?? "unknown";
250
- } catch {
251
- return "unknown";
252
- }
253
- }