ttsc 0.3.1 → 0.4.0-dev.20260424

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 (112) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +216 -302
  3. package/cmd/ttsc/build.go +128 -0
  4. package/cmd/ttsc/main.go +222 -0
  5. package/cmd/ttsc/main_test.go +288 -0
  6. package/cmd/ttsc/transform.go +166 -0
  7. package/driver/host.go +31 -0
  8. package/driver/program.go +298 -0
  9. package/driver/rewrite.go +315 -0
  10. package/go.mod +48 -0
  11. package/go.sum +22 -0
  12. package/lib/api.d.ts +91 -0
  13. package/lib/api.js +325 -0
  14. package/lib/api.js.map +1 -0
  15. package/lib/cli.d.ts +1 -0
  16. package/lib/cli.js +481 -0
  17. package/lib/cli.js.map +1 -0
  18. package/lib/index.d.ts +17 -0
  19. package/lib/index.js +34 -0
  20. package/lib/index.js.map +1 -0
  21. package/lib/native.d.ts +15 -0
  22. package/lib/native.js +34 -0
  23. package/lib/native.js.map +1 -0
  24. package/lib/platform.d.ts +66 -0
  25. package/lib/platform.js +170 -0
  26. package/lib/platform.js.map +1 -0
  27. package/lib/plugin.d.ts +47 -0
  28. package/lib/plugin.js +150 -0
  29. package/lib/plugin.js.map +1 -0
  30. package/lib/project.d.ts +32 -0
  31. package/lib/project.js +303 -0
  32. package/lib/project.js.map +1 -0
  33. package/package.json +70 -63
  34. package/shim/ast/extra-shim.json +4 -0
  35. package/shim/ast/go.mod +5 -0
  36. package/shim/ast/shim.go +117 -0
  37. package/shim/bundled/extra-shim.json +1 -0
  38. package/shim/bundled/go.mod +5 -0
  39. package/shim/bundled/shim.go +19 -0
  40. package/shim/checker/extra-shim.json +31 -0
  41. package/shim/checker/go.mod +5 -0
  42. package/shim/checker/shim.go +146 -0
  43. package/shim/compiler/extra-shim.json +4 -0
  44. package/shim/compiler/go.mod +5 -0
  45. package/shim/compiler/shim.go +50 -0
  46. package/shim/core/extra-shim.json +4 -0
  47. package/shim/core/go.mod +5 -0
  48. package/shim/core/shim.go +11 -0
  49. package/shim/diagnosticwriter/go.mod +5 -0
  50. package/shim/diagnosticwriter/shim.go +29 -0
  51. package/shim/parser/extra-shim.json +4 -0
  52. package/shim/parser/go.mod +5 -0
  53. package/shim/parser/shim.go +55 -0
  54. package/shim/scanner/extra-shim.json +4 -0
  55. package/shim/scanner/go.mod +5 -0
  56. package/shim/scanner/shim.go +96 -0
  57. package/shim/tsoptions/extra-shim.json +4 -0
  58. package/shim/tsoptions/go.mod +5 -0
  59. package/shim/tsoptions/shim.go +123 -0
  60. package/shim/tspath/extra-shim.json +4 -0
  61. package/shim/tspath/go.mod +5 -0
  62. package/shim/tspath/shim.go +165 -0
  63. package/shim/vfs/cachedvfs/extra-shim.json +4 -0
  64. package/shim/vfs/cachedvfs/go.mod +5 -0
  65. package/shim/vfs/cachedvfs/shim.go +12 -0
  66. package/shim/vfs/extra-shim.json +4 -0
  67. package/shim/vfs/go.mod +5 -0
  68. package/shim/vfs/osvfs/extra-shim.json +4 -0
  69. package/shim/vfs/osvfs/go.mod +5 -0
  70. package/shim/vfs/osvfs/shim.go +13 -0
  71. package/shim/vfs/shim.go +19 -0
  72. package/src/api.ts +416 -0
  73. package/src/cli.ts +492 -0
  74. package/src/index.ts +18 -0
  75. package/src/launcher/ttsc-dev.js +42 -0
  76. package/src/launcher/ttsc.js +19 -0
  77. package/src/native.ts +60 -0
  78. package/src/platform.ts +163 -0
  79. package/src/plugin.ts +241 -0
  80. package/src/project.ts +330 -0
  81. package/test/native.test.ts +60 -0
  82. package/test/package.test.ts +123 -0
  83. package/test/platform.test.ts +58 -0
  84. package/test/project.test.ts +195 -0
  85. package/tools/gen_shims/go.mod +14 -0
  86. package/tools/gen_shims/go.sum +12 -0
  87. package/tools/gen_shims/main.go +446 -0
  88. package/CHANGELOG.md +0 -2
  89. package/bin/tsc +0 -2
  90. package/bin/tsserver +0 -2
  91. package/lib/PluginCreator.d.ts +0 -67
  92. package/lib/PluginCreator.js +0 -190
  93. package/lib/cancelationToken.d.ts +0 -0
  94. package/lib/cancelationToken.js +0 -2
  95. package/lib/loadTypescript.d.ts +0 -7
  96. package/lib/loadTypescript.js +0 -56
  97. package/lib/patchCreateProgram.d.ts +0 -21
  98. package/lib/patchCreateProgram.js +0 -190
  99. package/lib/tsc.d.ts +0 -1
  100. package/lib/tsc.js +0 -24
  101. package/lib/tsserver.d.ts +0 -0
  102. package/lib/tsserver.js +0 -2
  103. package/lib/tsserverlibrary.d.ts +0 -3
  104. package/lib/tsserverlibrary.js +0 -4
  105. package/lib/typescript.d.ts +0 -3
  106. package/lib/typescript.js +0 -4
  107. package/lib/typescriptServices.d.ts +0 -3
  108. package/lib/typescriptServices.js +0 -4
  109. package/lib/typingsInstaller.d.ts +0 -0
  110. package/lib/typingsInstaller.js +0 -2
  111. package/lib/watchGuard.d.ts +0 -0
  112. package/lib/watchGuard.js +0 -2
@@ -0,0 +1,66 @@
1
+ /**
2
+ * Platform detection and 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
+ * Resolution order:
9
+ *
10
+ * 1. `TTSC_BINARY` environment variable (absolute path). Highest priority —
11
+ * lets local devs or CI point at an ad-hoc build without touching
12
+ * node_modules.
13
+ * 2. `@ttsc/{platform}-{arch}/bin/ttsc{.exe}` optional dependency.
14
+ * Standard distribution path.
15
+ * 3. A package-local `native/ttsc-native` binary. Used by this
16
+ * repository's local `pnpm run build` output before the
17
+ * platform packages exist on npm.
18
+ *
19
+ * If every strategy fails, `resolveBinary` returns `null` so the caller can print a
20
+ * detailed install hint.
21
+ */
22
+ export interface ResolveOptions {
23
+ /**
24
+ * Override `require.resolve` for testing. Takes a module request string and
25
+ * returns an absolute path (or throws).
26
+ */
27
+ resolver?: (request: string) => string;
28
+ /** Override `process.platform` for testing. */
29
+ platform?: NodeJS.Platform;
30
+ /** Override `process.arch` for testing. */
31
+ arch?: string;
32
+ /** Override `process.env` for testing. */
33
+ env?: NodeJS.ProcessEnv;
34
+ /**
35
+ * Override the package-local scan used for the local `native/ttsc-native`
36
+ * fallback. Returns an absolute path if present, else null.
37
+ */
38
+ localBinaryLookup?: () => string | null;
39
+ /** Override the module directory used to resolve the package-local binary. */
40
+ moduleDir?: string;
41
+ }
42
+ /** Canonical key `{platform}-{arch}` used in the binary package name. */
43
+ export declare function platformKey(opts?: ResolveOptions): string;
44
+ /** Binary filename for the current platform (`.exe` on Windows, else none). */
45
+ export declare function binaryName(opts?: ResolveOptions): string;
46
+ /**
47
+ * Resolve the optional-dependency module request string for the current
48
+ * platform. Separated from actual resolution so we can log the request when
49
+ * the dependency is missing.
50
+ */
51
+ export declare function platformPackageRequest(opts?: ResolveOptions): string;
52
+ /** The list of platform keys the package currently ships. */
53
+ export declare const SUPPORTED_PLATFORMS: readonly string[];
54
+ /** True if the given platform key is in the supported list. */
55
+ export declare function isSupported(key: string): boolean;
56
+ /**
57
+ * Resolve an absolute path to the ttsc binary using the documented priority
58
+ * order. Returns `null` when every strategy fails — the launcher then prints
59
+ * the install hint and exits non-zero.
60
+ */
61
+ export declare function resolveBinary(opts?: ResolveOptions): string | null;
62
+ /**
63
+ * Human-readable message shown when no binary can be located. Pure string —
64
+ * the launcher writes it to stderr.
65
+ */
66
+ export declare function installHint(opts?: ResolveOptions): string;
@@ -0,0 +1,170 @@
1
+ "use strict";
2
+ /**
3
+ * Platform detection and binary resolution for ttsc.
4
+ *
5
+ * This is the tested implementation used by the ttsc launcher after build.
6
+ * Keep it pure and dependency-free so it can be unit-tested without spawning
7
+ * any child process.
8
+ *
9
+ * Resolution order:
10
+ *
11
+ * 1. `TTSC_BINARY` environment variable (absolute path). Highest priority —
12
+ * lets local devs or CI point at an ad-hoc build without touching
13
+ * node_modules.
14
+ * 2. `@ttsc/{platform}-{arch}/bin/ttsc{.exe}` optional dependency.
15
+ * Standard distribution path.
16
+ * 3. A package-local `native/ttsc-native` binary. Used by this
17
+ * repository's local `pnpm run build` output before the
18
+ * platform packages exist on npm.
19
+ *
20
+ * If every strategy fails, `resolveBinary` returns `null` so the caller can print a
21
+ * detailed install hint.
22
+ */
23
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
24
+ if (k2 === undefined) k2 = k;
25
+ var desc = Object.getOwnPropertyDescriptor(m, k);
26
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
27
+ desc = { enumerable: true, get: function() { return m[k]; } };
28
+ }
29
+ Object.defineProperty(o, k2, desc);
30
+ }) : (function(o, m, k, k2) {
31
+ if (k2 === undefined) k2 = k;
32
+ o[k2] = m[k];
33
+ }));
34
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
35
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
36
+ }) : function(o, v) {
37
+ o["default"] = v;
38
+ });
39
+ var __importStar = (this && this.__importStar) || (function () {
40
+ var ownKeys = function(o) {
41
+ ownKeys = Object.getOwnPropertyNames || function (o) {
42
+ var ar = [];
43
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
44
+ return ar;
45
+ };
46
+ return ownKeys(o);
47
+ };
48
+ return function (mod) {
49
+ if (mod && mod.__esModule) return mod;
50
+ var result = {};
51
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
52
+ __setModuleDefault(result, mod);
53
+ return result;
54
+ };
55
+ })();
56
+ Object.defineProperty(exports, "__esModule", { value: true });
57
+ exports.SUPPORTED_PLATFORMS = void 0;
58
+ exports.platformKey = platformKey;
59
+ exports.binaryName = binaryName;
60
+ exports.platformPackageRequest = platformPackageRequest;
61
+ exports.isSupported = isSupported;
62
+ exports.resolveBinary = resolveBinary;
63
+ exports.installHint = installHint;
64
+ const fs = __importStar(require("node:fs"));
65
+ const path = __importStar(require("node:path"));
66
+ /** Canonical key `{platform}-{arch}` used in the binary package name. */
67
+ function platformKey(opts = {}) {
68
+ const platform = opts.platform ?? process.platform;
69
+ const arch = opts.arch ?? process.arch;
70
+ return `${platform}-${arch}`;
71
+ }
72
+ /** Binary filename for the current platform (`.exe` on Windows, else none). */
73
+ function binaryName(opts = {}) {
74
+ const platform = opts.platform ?? process.platform;
75
+ return platform === "win32" ? "ttsc.exe" : "ttsc";
76
+ }
77
+ /**
78
+ * Resolve the optional-dependency module request string for the current
79
+ * platform. Separated from actual resolution so we can log the request when
80
+ * the dependency is missing.
81
+ */
82
+ function platformPackageRequest(opts = {}) {
83
+ const key = platformKey(opts);
84
+ const bin = binaryName(opts);
85
+ return `@ttsc/${key}/bin/${bin}`;
86
+ }
87
+ /** The list of platform keys the package currently ships. */
88
+ exports.SUPPORTED_PLATFORMS = Object.freeze([
89
+ "linux-x64",
90
+ "linux-arm64",
91
+ "darwin-x64",
92
+ "darwin-arm64",
93
+ "win32-x64",
94
+ "win32-arm64",
95
+ ]);
96
+ /** True if the given platform key is in the supported list. */
97
+ function isSupported(key) {
98
+ return exports.SUPPORTED_PLATFORMS.includes(key);
99
+ }
100
+ /**
101
+ * Resolve an absolute path to the ttsc binary using the documented priority
102
+ * order. Returns `null` when every strategy fails — the launcher then prints
103
+ * the install hint and exits non-zero.
104
+ */
105
+ function resolveBinary(opts = {}) {
106
+ const env = opts.env ?? process.env;
107
+ // 1. Env override. Absolute path is required; a relative value is ignored so
108
+ // tests can't accidentally resolve something outside the intended tree.
109
+ if (env.TTSC_BINARY && path.isAbsolute(env.TTSC_BINARY)) {
110
+ return env.TTSC_BINARY;
111
+ }
112
+ // 2. Platform-specific optional dependency (the published distribution path).
113
+ const resolver = opts.resolver ?? ((req) => require.resolve(req));
114
+ try {
115
+ return resolver(platformPackageRequest(opts));
116
+ }
117
+ catch {
118
+ /* fall through */
119
+ }
120
+ // 3. Package-local fallback: <package>/native/ttsc-native.
121
+ if (opts.localBinaryLookup) {
122
+ const local = opts.localBinaryLookup();
123
+ if (local)
124
+ return local;
125
+ }
126
+ else {
127
+ const local = defaultLocalBinaryPath(opts);
128
+ if (local)
129
+ return local;
130
+ }
131
+ return null;
132
+ }
133
+ /**
134
+ * Human-readable message shown when no binary can be located. Pure string —
135
+ * the launcher writes it to stderr.
136
+ */
137
+ function installHint(opts = {}) {
138
+ const key = platformKey(opts);
139
+ const pkg = `@ttsc/${key}`;
140
+ const supported = exports.SUPPORTED_PLATFORMS.join(", ");
141
+ return [
142
+ `ttsc: platform-specific binary not found (${pkg}).`,
143
+ `Platform: ${opts.platform ?? process.platform}/${opts.arch ?? process.arch}.`,
144
+ ``,
145
+ `Resolution order:`,
146
+ ` 1. TTSC_BINARY env var (absolute path)`,
147
+ ` 2. ${pkg} optional dependency`,
148
+ ` 3. ./native/ttsc-native (local workspace build)`,
149
+ ``,
150
+ `Try:`,
151
+ ` npm install --include=optional ${pkg}`,
152
+ ` pnpm install --shamefully-hoist`,
153
+ ``,
154
+ `Supported platforms: ${supported}.`,
155
+ `If your platform is not in that list, open an issue at`,
156
+ `https://github.com/samchon/ttsc/issues.`,
157
+ ].join("\n");
158
+ }
159
+ function defaultLocalBinaryPath(opts) {
160
+ const root = packageRootDir(opts);
161
+ const candidate = path.resolve(root, "..", "native", (opts.platform ?? process.platform) === "win32" ? "ttsc-native.exe" : "ttsc-native");
162
+ return fs.existsSync(candidate) ? candidate : null;
163
+ }
164
+ function packageRootDir(opts = {}) {
165
+ if (opts.moduleDir) {
166
+ return fs.realpathSync.native?.(opts.moduleDir) ?? fs.realpathSync(opts.moduleDir);
167
+ }
168
+ return fs.realpathSync.native?.(__dirname) ?? fs.realpathSync(__dirname);
169
+ }
170
+ //# sourceMappingURL=platform.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"platform.js","sourceRoot":"","sources":["../src/platform.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA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,wBAAwB,SAAS,GAAG;QACpC,wDAAwD;QACxD,yCAAyC;KAC1C,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AACf,CAAC;AAED,SAAS,sBAAsB,CAAC,IAAoB;IAClD,MAAM,IAAI,GAAG,cAAc,CAAC,IAAI,CAAC,CAAC;IAClC,MAAM,SAAS,GAAG,IAAI,CAAC,OAAO,CAC5B,IAAI,EACJ,IAAI,EACJ,QAAQ,EACR,CAAC,IAAI,CAAC,QAAQ,IAAI,OAAO,CAAC,QAAQ,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,iBAAiB,CAAC,CAAC,CAAC,aAAa,CACpF,CAAC;IACF,OAAO,EAAE,CAAC,UAAU,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC;AACrD,CAAC;AAED,SAAS,cAAc,CAAC,IAAI,GAAmB,EAAE;IAC/C,IAAI,IAAI,CAAC,SAAS,EAAE,CAAC;QACnB,OAAO,EAAE,CAAC,YAAY,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,CAAC,YAAY,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;IACrF,CAAC;IACD,OAAO,EAAE,CAAC,YAAY,CAAC,MAAM,EAAE,CAAC,SAAS,CAAC,IAAI,EAAE,CAAC,YAAY,CAAC,SAAS,CAAC,CAAC;AAC3E,CAAC"}
@@ -0,0 +1,47 @@
1
+ import { type NativeRewriteMode, type TtscNativeBackend } from "./native";
2
+ import { type ParsedProjectConfig, type ProjectPluginConfig } from "./project";
3
+ export type { NativePluginContractVersion, NativeRewriteMode, TtscNativeBackend, } from "./native";
4
+ export interface TtscPluginFactoryContext {
5
+ binary: string;
6
+ cwd: string;
7
+ projectRoot: string;
8
+ tsconfig: string;
9
+ }
10
+ export interface TtscTransformContext {
11
+ code: string;
12
+ command: "build" | "transform";
13
+ cwd: string;
14
+ outputFile?: string;
15
+ projectRoot: string;
16
+ sourceFile?: string;
17
+ tsconfig: string;
18
+ }
19
+ export interface TtscPlugin {
20
+ name: string;
21
+ native?: TtscNativeBackend;
22
+ /** @deprecated Use `native.mode` instead. */
23
+ nativeMode?: NativeRewriteMode;
24
+ /** @deprecated Use `native.binary` instead. */
25
+ nativeBinary?: string;
26
+ transformOutput?(context: TtscTransformContext): string;
27
+ }
28
+ export type TtscPluginFactory = (config: ProjectPluginConfig, context: TtscPluginFactoryContext) => TtscPlugin;
29
+ export type TtscPluginModule = TtscPlugin | TtscPluginFactory;
30
+ export interface LoadedPlugins {
31
+ compatibilityFallback: boolean;
32
+ nativeBackend: TtscNativeBackend | null;
33
+ nativeBinary: string | null;
34
+ nativeMode: NativeRewriteMode;
35
+ plugins: TtscPlugin[];
36
+ project: ParsedProjectConfig;
37
+ }
38
+ export interface LoadPluginsOptions {
39
+ binary: string;
40
+ cwd?: string;
41
+ entries?: readonly ProjectPluginConfig[] | false;
42
+ file?: string;
43
+ tsconfig?: string;
44
+ }
45
+ export declare function definePlugin<T extends TtscPluginModule>(plugin: T): T;
46
+ export declare function loadProjectPlugins(options: LoadPluginsOptions): LoadedPlugins;
47
+ export declare function applyPluginTransforms(plugins: readonly TtscPlugin[], context: TtscTransformContext): string;
package/lib/plugin.js ADDED
@@ -0,0 +1,150 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.definePlugin = definePlugin;
7
+ exports.loadProjectPlugins = loadProjectPlugins;
8
+ exports.applyPluginTransforms = applyPluginTransforms;
9
+ const node_fs_1 = __importDefault(require("node:fs"));
10
+ const node_path_1 = __importDefault(require("node:path"));
11
+ const native_1 = require("./native");
12
+ const project_1 = require("./project");
13
+ function definePlugin(plugin) {
14
+ return plugin;
15
+ }
16
+ function loadProjectPlugins(options) {
17
+ const project = (0, project_1.readProjectConfig)({
18
+ cwd: options.cwd,
19
+ file: options.file,
20
+ tsconfig: options.tsconfig,
21
+ });
22
+ const entries = options.entries === false
23
+ ? []
24
+ : [...(options.entries ?? project.compilerOptions.plugins)].filter((entry) => entry.enabled !== false);
25
+ if (entries.length === 0) {
26
+ return {
27
+ compatibilityFallback: false,
28
+ nativeBackend: null,
29
+ nativeBinary: null,
30
+ nativeMode: "none",
31
+ plugins: [],
32
+ project,
33
+ };
34
+ }
35
+ const context = {
36
+ binary: options.binary,
37
+ cwd: node_path_1.default.resolve(options.cwd ?? process.cwd()),
38
+ projectRoot: project.root,
39
+ tsconfig: project.path,
40
+ };
41
+ const plugins = entries.map((entry) => loadPluginEntry(entry, context));
42
+ let nativeBinary = null;
43
+ let nativeBackend = null;
44
+ let nativeMode = "none";
45
+ for (const plugin of plugins) {
46
+ const backend = (0, native_1.resolveNativeBackend)(plugin);
47
+ if (!backend) {
48
+ continue;
49
+ }
50
+ if (nativeMode !== "none" && nativeMode !== backend.mode) {
51
+ throw new Error(`ttsc: multiple native plugin modes requested (${nativeMode}, ${backend.mode})`);
52
+ }
53
+ nativeMode = backend.mode;
54
+ nativeBackend = backend;
55
+ if (backend.binary) {
56
+ if (nativeBinary !== null && nativeBinary !== backend.binary) {
57
+ throw new Error(`ttsc: multiple native plugin binaries requested (${nativeBinary}, ${backend.binary})`);
58
+ }
59
+ nativeBinary = backend.binary;
60
+ }
61
+ }
62
+ return {
63
+ compatibilityFallback: false,
64
+ nativeBackend,
65
+ nativeBinary,
66
+ nativeMode,
67
+ plugins,
68
+ project,
69
+ };
70
+ }
71
+ function applyPluginTransforms(plugins, context) {
72
+ let code = context.code;
73
+ for (const plugin of plugins) {
74
+ if (!plugin.transformOutput) {
75
+ continue;
76
+ }
77
+ code = plugin.transformOutput({ ...context, code });
78
+ }
79
+ return code;
80
+ }
81
+ function loadPluginEntry(entry, context) {
82
+ const specifier = entry.transform;
83
+ if (typeof specifier !== "string" || specifier.length === 0) {
84
+ throw new Error(`ttsc: plugin entry is missing a string "transform" field`);
85
+ }
86
+ const request = resolvePluginRequest(specifier, context.projectRoot);
87
+ const mod = require(request);
88
+ const candidate = mod.createTtscPlugin ??
89
+ mod.default ??
90
+ mod.plugin ??
91
+ mod;
92
+ if (typeof candidate === "function") {
93
+ return candidate(entry, context);
94
+ }
95
+ if (candidate &&
96
+ typeof candidate === "object" &&
97
+ typeof candidate.name === "string") {
98
+ return candidate;
99
+ }
100
+ throw new Error(`ttsc: plugin "${specifier}" does not export a valid ttsc plugin`);
101
+ }
102
+ function resolvePluginRequest(specifier, projectRoot) {
103
+ if (node_path_1.default.isAbsolute(specifier)) {
104
+ return specifier;
105
+ }
106
+ if (isRelativePluginSpecifier(specifier)) {
107
+ return node_path_1.default.resolve(projectRoot, specifier);
108
+ }
109
+ try {
110
+ return require.resolve(specifier, { paths: [projectRoot] });
111
+ }
112
+ catch (error) {
113
+ const sourceFallback = resolveSourceCheckoutPlugin(specifier, projectRoot);
114
+ if (sourceFallback) {
115
+ return sourceFallback;
116
+ }
117
+ throw error;
118
+ }
119
+ }
120
+ function isRelativePluginSpecifier(specifier) {
121
+ return (specifier === "." ||
122
+ specifier === ".." ||
123
+ specifier.startsWith("./") ||
124
+ specifier.startsWith("../") ||
125
+ specifier.startsWith(".\\") ||
126
+ specifier.startsWith("..\\"));
127
+ }
128
+ function resolveSourceCheckoutPlugin(specifier, projectRoot) {
129
+ const normalized = specifier.replace(/\\/g, "/");
130
+ const match = normalized.match(/^(.*)\/lib\/transform$/);
131
+ if (!match) {
132
+ return null;
133
+ }
134
+ try {
135
+ const packageJson = require.resolve(`${match[1]}/package.json`, {
136
+ paths: [projectRoot],
137
+ });
138
+ const packageRoot = node_path_1.default.dirname(packageJson);
139
+ const candidates = [
140
+ node_path_1.default.join(packageRoot, "lib", "transform.js"),
141
+ node_path_1.default.join(packageRoot, "src", "transform.ts"),
142
+ node_path_1.default.join(packageRoot, "bin", "ttsc-plugin.cjs"),
143
+ ];
144
+ return candidates.find((candidate) => node_fs_1.default.existsSync(candidate)) ?? null;
145
+ }
146
+ catch {
147
+ return null;
148
+ }
149
+ }
150
+ //# sourceMappingURL=plugin.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plugin.js","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":";;;;;;;;AAAA,sDAAyB;AACzB,0DAA6B;AAE7B,qCAIkB;AAClB,uCAImB;AA2DnB,sBAAyD,MAAS;IAChE,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,4BAAmC,OAA2B;IAC5D,MAAM,OAAO,GAAG,IAAA,2BAAiB,EAAC;QAChC,GAAG,EAAE,OAAO,CAAC,GAAG;QAChB,IAAI,EAAE,OAAO,CAAC,IAAI;QAClB,QAAQ,EAAE,OAAO,CAAC,QAAQ;KAC3B,CAAC,CAAC;IACH,MAAM,OAAO,GACX,OAAO,CAAC,OAAO,KAAK,KAAK;QACvB,CAAC,CAAC,EAAE;QACJ,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,OAAO,IAAI,OAAO,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAC9D,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,OAAO,KAAK,KAAK,CACnC,CAAC;IACR,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACzB,OAAO;YACL,qBAAqB,EAAE,KAAK;YAC5B,aAAa,EAAE,IAAI;YACnB,YAAY,EAAE,IAAI;YAClB,UAAU,EAAE,MAAM;YAClB,OAAO,EAAE,EAAE;YACX,OAAO;SACR,CAAC;IACJ,CAAC;IAED,MAAM,OAAO,GAA6B;QACxC,MAAM,EAAE,OAAO,CAAC,MAAM;QACtB,GAAG,EAAE,mBAAI,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,IAAI,OAAO,CAAC,GAAG,EAAE,CAAC;QAC/C,WAAW,EAAE,OAAO,CAAC,IAAI;QACzB,QAAQ,EAAE,OAAO,CAAC,IAAI;KACvB,CAAC;IACF,MAAM,O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,CAAC,IAAI,CAAC,WAAW,EAAE,KAAK,EAAE,cAAc,CAAC;YAC7C,mBAAI,CAAC,IAAI,CAAC,WAAW,EAAE,KAAK,EAAE,iBAAiB,CAAC;SACjD,CAAC;QACF,OAAO,UAAU,CAAC,IAAI,CAAC,CAAC,SAAS,EAAE,EAAE,CAAC,iBAAE,CAAC,UAAU,CAAC,SAAS,CAAC,CAAC,IAAI,IAAI,CAAC;IAC1E,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC"}
@@ -0,0 +1,32 @@
1
+ export interface ProjectLocatorOptions {
2
+ cwd?: string;
3
+ file?: string;
4
+ tsconfig?: string;
5
+ }
6
+ export interface ProjectPluginConfig {
7
+ enabled?: boolean;
8
+ transform?: string;
9
+ [key: string]: unknown;
10
+ }
11
+ export interface ParsedProjectConfig {
12
+ compilerOptions: {
13
+ outDir?: string;
14
+ plugins: ProjectPluginConfig[];
15
+ };
16
+ path: string;
17
+ root: string;
18
+ }
19
+ export declare function resolveWorkingDirectory(cwd?: string): string;
20
+ export declare function resolveAbsolutePath(cwd: string, target: string): string;
21
+ export declare function resolveProjectConfig(opts?: ProjectLocatorOptions): string;
22
+ export declare function resolveProjectRoot(opts?: ProjectLocatorOptions): string;
23
+ export declare function defaultCacheDirectory(projectRoot: string, tool: string): string;
24
+ export declare function readProjectConfig(opts?: ProjectLocatorOptions): ParsedProjectConfig;
25
+ export interface CacheKeyInput {
26
+ file: string;
27
+ tsconfig: string;
28
+ version: string;
29
+ mode?: string;
30
+ extra?: readonly string[];
31
+ }
32
+ export declare function createTransformCacheKey(input: CacheKeyInput): string;