ttsc 0.7.0 → 0.7.2
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.
- package/README.md +3 -5
- package/cmd/platform/main.go +1 -1
- package/cmd/ttsc/api_transform.go +64 -0
- package/cmd/ttsc/main.go +4 -18
- package/driver/program.go +2 -2
- package/lib/TtscCompiler.d.ts +20 -0
- package/lib/TtscCompiler.js +49 -0
- package/lib/TtscCompiler.js.map +1 -1
- package/lib/compiler/internal/runSingleFileEmit.d.ts +3 -0
- package/lib/compiler/internal/{runTransform.js → runSingleFileEmit.js} +10 -8
- package/lib/compiler/internal/runSingleFileEmit.js.map +1 -0
- package/lib/compiler/internal/transformProjectInMemory.d.ts +7 -0
- package/lib/compiler/internal/transformProjectInMemory.js +275 -0
- package/lib/compiler/internal/transformProjectInMemory.js.map +1 -0
- package/lib/launcher/internal/prepareExecution.js +89 -8
- package/lib/launcher/internal/prepareExecution.js.map +1 -1
- package/lib/launcher/internal/runTtsc.js +2 -76
- package/lib/launcher/internal/runTtsc.js.map +1 -1
- package/lib/structures/ITtscCompilerTransformation.d.ts +35 -0
- package/lib/structures/ITtscCompilerTransformation.js +3 -0
- package/lib/structures/ITtscCompilerTransformation.js.map +1 -0
- package/lib/structures/TtscPluginStage.d.ts +7 -4
- package/lib/structures/index.d.ts +1 -0
- package/lib/structures/index.js +1 -0
- package/lib/structures/index.js.map +1 -1
- package/lib/structures/internal/TtscCommonOptions.d.ts +1 -1
- package/lib/structures/internal/{TtscTransformOptions.d.ts → TtscSingleFileEmitOptions.d.ts} +4 -4
- package/lib/structures/internal/{TtscTransformOptions.js → TtscSingleFileEmitOptions.js} +1 -1
- package/lib/structures/internal/TtscSingleFileEmitOptions.js.map +1 -0
- package/package.json +8 -8
- package/src/TtscCompiler.ts +64 -0
- package/src/compiler/internal/{runTransform.ts → runSingleFileEmit.ts} +11 -7
- package/src/compiler/internal/transformProjectInMemory.ts +368 -0
- package/src/launcher/internal/prepareExecution.ts +114 -8
- package/src/launcher/internal/runTtsc.ts +6 -79
- package/src/structures/ITtscCompilerTransformation.ts +46 -0
- package/src/structures/TtscPluginStage.ts +7 -4
- package/src/structures/index.ts +1 -0
- package/src/structures/internal/TtscCommonOptions.ts +1 -1
- package/src/structures/internal/{TtscTransformOptions.ts → TtscSingleFileEmitOptions.ts} +4 -4
- package/test/compiler.test.ts +196 -8
- package/cmd/ttsc/transform.go +0 -160
- package/lib/compiler/internal/runTransform.d.ts +0 -3
- package/lib/compiler/internal/runTransform.js.map +0 -1
- package/lib/structures/internal/TtscTransformOptions.js.map +0 -1
package/test/compiler.test.ts
CHANGED
|
@@ -10,7 +10,11 @@ const tsgo = resolveTsgo({ cwd: path.resolve(__dirname, "..") }).binary;
|
|
|
10
10
|
|
|
11
11
|
test("TtscCompiler.compile returns output without writing project files", () => {
|
|
12
12
|
const root = createProject();
|
|
13
|
-
const compiler = new TtscCompiler({
|
|
13
|
+
const compiler = new TtscCompiler({
|
|
14
|
+
binary: tsgo,
|
|
15
|
+
cwd: root,
|
|
16
|
+
plugins: false,
|
|
17
|
+
});
|
|
14
18
|
|
|
15
19
|
const result = compiler.compile();
|
|
16
20
|
|
|
@@ -27,7 +31,11 @@ test("TtscCompiler can disable project plugin loading", () => {
|
|
|
27
31
|
const root = createProject({
|
|
28
32
|
plugins: [{ transform: "./missing-plugin.cjs" }],
|
|
29
33
|
});
|
|
30
|
-
const compiler = new TtscCompiler({
|
|
34
|
+
const compiler = new TtscCompiler({
|
|
35
|
+
binary: tsgo,
|
|
36
|
+
cwd: root,
|
|
37
|
+
plugins: false,
|
|
38
|
+
});
|
|
31
39
|
|
|
32
40
|
const result = compiler.compile();
|
|
33
41
|
|
|
@@ -49,11 +57,121 @@ test("TtscCompiler.compile applies configured source plugins without project out
|
|
|
49
57
|
assert.equal(fs.existsSync(path.join(root, "dist")), false);
|
|
50
58
|
});
|
|
51
59
|
|
|
60
|
+
test("TtscCompiler.transform returns TypeScript source without project files", () => {
|
|
61
|
+
const root = createProject();
|
|
62
|
+
const compiler = new TtscCompiler({
|
|
63
|
+
binary: tsgo,
|
|
64
|
+
cwd: root,
|
|
65
|
+
plugins: false,
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
const result = compiler.transform();
|
|
69
|
+
|
|
70
|
+
assert.equal(result.type, "success");
|
|
71
|
+
assert.match(
|
|
72
|
+
result.typescript["src/main.ts"],
|
|
73
|
+
/const message: string = "api-ok"/,
|
|
74
|
+
);
|
|
75
|
+
assert.match(
|
|
76
|
+
result.typescript["src/main.ts"],
|
|
77
|
+
/console\.log\(\s*message\s*\)/,
|
|
78
|
+
);
|
|
79
|
+
assert.equal(result.typescript["dist/main.js"], undefined);
|
|
80
|
+
assert.equal(result.typescript["dist/main.d.ts"], undefined);
|
|
81
|
+
assert.equal(fs.existsSync(path.join(root, "dist")), false);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
test("TtscCompiler.transform returns every included TypeScript source file", () => {
|
|
85
|
+
const root = createProject({
|
|
86
|
+
files: {
|
|
87
|
+
"src/helpers.ts":
|
|
88
|
+
"export const helper = (value: string): string => value.toUpperCase();\n",
|
|
89
|
+
"src/nested/model.ts": "export interface Model { value: string }\n",
|
|
90
|
+
},
|
|
91
|
+
source:
|
|
92
|
+
'import { helper } from "./helpers";\nconst message: string = helper("api-ok");\nconsole.log(message);\n',
|
|
93
|
+
});
|
|
94
|
+
const compiler = new TtscCompiler({
|
|
95
|
+
binary: tsgo,
|
|
96
|
+
cwd: root,
|
|
97
|
+
plugins: false,
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
const result = compiler.transform();
|
|
101
|
+
|
|
102
|
+
assert.equal(result.type, "success");
|
|
103
|
+
assert.match(result.typescript["src/main.ts"], /helper\("api-ok"\)/);
|
|
104
|
+
assert.match(result.typescript["src/helpers.ts"], /toUpperCase/);
|
|
105
|
+
assert.match(result.typescript["src/nested/model.ts"], /interface Model/);
|
|
106
|
+
for (const key of Object.keys(result.typescript)) {
|
|
107
|
+
assert.equal(key.startsWith("dist/"), false);
|
|
108
|
+
assert.equal(/\.(?:js|cjs|mjs|d\.ts|map)$/.test(key), false);
|
|
109
|
+
}
|
|
110
|
+
assert.equal(fs.existsSync(path.join(root, "dist")), false);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
test("TtscCompiler.transform applies configured source plugins to TypeScript output", () => {
|
|
114
|
+
const root = createProject({
|
|
115
|
+
plugins: [{ transform: "./plugin.cjs" }],
|
|
116
|
+
source: 'export const value = goUpper("plugin");\nconsole.log(value);\n',
|
|
117
|
+
});
|
|
118
|
+
writeCompilerPlugin(root);
|
|
119
|
+
const compiler = new TtscCompiler({ binary: tsgo, cwd: root });
|
|
120
|
+
|
|
121
|
+
const result = compiler.transform();
|
|
122
|
+
|
|
123
|
+
assert.equal(result.type, "success");
|
|
124
|
+
assert.match(
|
|
125
|
+
result.typescript["src/main.ts"],
|
|
126
|
+
/export const value = "PLUGIN"/,
|
|
127
|
+
);
|
|
128
|
+
assert.match(result.typescript["src/main.ts"], /console\.log\(value\)/);
|
|
129
|
+
assert.equal(result.typescript["dist/main.js"], undefined);
|
|
130
|
+
assert.equal(fs.existsSync(path.join(root, "dist")), false);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
test("TtscCompiler.transform rejects plugin output that is not TypeScript source", () => {
|
|
134
|
+
const root = createProject({
|
|
135
|
+
plugins: [{ transform: "./plugin.cjs" }],
|
|
136
|
+
source: 'export const value = goUpper("plugin");\nconsole.log(value);\n',
|
|
137
|
+
});
|
|
138
|
+
writeBrokenTransformPlugin(root);
|
|
139
|
+
const compiler = new TtscCompiler({ binary: tsgo, cwd: root });
|
|
140
|
+
|
|
141
|
+
const result = compiler.transform();
|
|
142
|
+
|
|
143
|
+
assert.equal(result.type, "exception");
|
|
144
|
+
assert.match(result.error.message, /did not return a TypeScript source map/);
|
|
145
|
+
assert.equal(fs.existsSync(path.join(root, "dist")), false);
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
test("TtscCompiler.transform returns failure on compiler diagnostics", () => {
|
|
149
|
+
const root = createProject({
|
|
150
|
+
source: 'const value: number = "not-a-number";\nconsole.log(value);\n',
|
|
151
|
+
});
|
|
152
|
+
const compiler = new TtscCompiler({
|
|
153
|
+
binary: tsgo,
|
|
154
|
+
cwd: root,
|
|
155
|
+
plugins: false,
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
const result = compiler.transform();
|
|
159
|
+
|
|
160
|
+
assert.equal(result.type, "failure");
|
|
161
|
+
assert.equal(result.diagnostics[0].code, 2322);
|
|
162
|
+
assert.match(result.typescript["src/main.ts"], /not-a-number/);
|
|
163
|
+
assert.equal(fs.existsSync(path.join(root, "dist")), false);
|
|
164
|
+
});
|
|
165
|
+
|
|
52
166
|
test("TtscCompiler.compile returns structured diagnostics", () => {
|
|
53
167
|
const root = createProject({
|
|
54
168
|
source: 'const value: number = "not-a-number";\nconsole.log(value);\n',
|
|
55
169
|
});
|
|
56
|
-
const compiler = new TtscCompiler({
|
|
170
|
+
const compiler = new TtscCompiler({
|
|
171
|
+
binary: tsgo,
|
|
172
|
+
cwd: root,
|
|
173
|
+
plugins: false,
|
|
174
|
+
});
|
|
57
175
|
|
|
58
176
|
const result = compiler.compile();
|
|
59
177
|
|
|
@@ -76,7 +194,11 @@ test("TtscCompiler.compile does not accept per-call context overrides", () => {
|
|
|
76
194
|
const other = createProject({
|
|
77
195
|
plugins: [{ transform: "./missing-plugin.cjs" }],
|
|
78
196
|
});
|
|
79
|
-
const compiler = new TtscCompiler({
|
|
197
|
+
const compiler = new TtscCompiler({
|
|
198
|
+
binary: tsgo,
|
|
199
|
+
cwd: root,
|
|
200
|
+
plugins: false,
|
|
201
|
+
});
|
|
80
202
|
|
|
81
203
|
const result = compiler.compile({
|
|
82
204
|
binary: path.join(other, "missing-tsgo"),
|
|
@@ -119,6 +241,11 @@ function createProject(options = {}) {
|
|
|
119
241
|
'const message: string = "api-ok";\nconsole.log(message);\n',
|
|
120
242
|
"utf8",
|
|
121
243
|
);
|
|
244
|
+
for (const [file, content] of Object.entries(options.files ?? {})) {
|
|
245
|
+
const location = path.join(root, file);
|
|
246
|
+
fs.mkdirSync(path.dirname(location), { recursive: true });
|
|
247
|
+
fs.writeFileSync(location, content, "utf8");
|
|
248
|
+
}
|
|
122
249
|
fs.writeFileSync(
|
|
123
250
|
path.join(root, "tsconfig.json"),
|
|
124
251
|
JSON.stringify(
|
|
@@ -163,6 +290,40 @@ function writeSourcePlugin(root) {
|
|
|
163
290
|
);
|
|
164
291
|
}
|
|
165
292
|
|
|
293
|
+
function writeBrokenTransformPlugin(root) {
|
|
294
|
+
fs.writeFileSync(
|
|
295
|
+
path.join(root, "plugin.cjs"),
|
|
296
|
+
'module.exports = { name: "broken-transform-fixture", source: "./plugin-go" };\n',
|
|
297
|
+
"utf8",
|
|
298
|
+
);
|
|
299
|
+
fs.mkdirSync(path.join(root, "plugin-go"), { recursive: true });
|
|
300
|
+
fs.writeFileSync(
|
|
301
|
+
path.join(root, "plugin-go", "go.mod"),
|
|
302
|
+
"module example.com/brokentransformfixture\n\ngo 1.26\n",
|
|
303
|
+
"utf8",
|
|
304
|
+
);
|
|
305
|
+
fs.writeFileSync(
|
|
306
|
+
path.join(root, "plugin-go", "main.go"),
|
|
307
|
+
[
|
|
308
|
+
"package main",
|
|
309
|
+
"",
|
|
310
|
+
"import (",
|
|
311
|
+
'\t"fmt"',
|
|
312
|
+
'\t"os"',
|
|
313
|
+
")",
|
|
314
|
+
"",
|
|
315
|
+
"func main() {",
|
|
316
|
+
'\tif len(os.Args) > 1 && os.Args[1] == "transform" {',
|
|
317
|
+
'\t\tfmt.Println(`{"output":{"dist/main.js":"console.log(\\"wrong\\");\\n"}}`)',
|
|
318
|
+
"\t\treturn",
|
|
319
|
+
"\t}",
|
|
320
|
+
"}",
|
|
321
|
+
"",
|
|
322
|
+
].join("\n"),
|
|
323
|
+
"utf8",
|
|
324
|
+
);
|
|
325
|
+
}
|
|
326
|
+
|
|
166
327
|
function writeCompilerPlugin(root) {
|
|
167
328
|
fs.writeFileSync(
|
|
168
329
|
path.join(root, "plugin.cjs"),
|
|
@@ -181,6 +342,7 @@ function writeCompilerPlugin(root) {
|
|
|
181
342
|
"package main",
|
|
182
343
|
"",
|
|
183
344
|
"import (",
|
|
345
|
+
'\t"encoding/json"',
|
|
184
346
|
'\t"flag"',
|
|
185
347
|
'\t"fmt"',
|
|
186
348
|
'\t"os"',
|
|
@@ -195,6 +357,8 @@ function writeCompilerPlugin(root) {
|
|
|
195
357
|
"\tswitch args[0] {",
|
|
196
358
|
'\tcase "build":',
|
|
197
359
|
"\t\treturn build(args[1:])",
|
|
360
|
+
'\tcase "transform":',
|
|
361
|
+
"\t\treturn transformSource(args[1:])",
|
|
198
362
|
'\tcase "check", "version":',
|
|
199
363
|
"\t\treturn 0",
|
|
200
364
|
"\tdefault:",
|
|
@@ -204,7 +368,7 @@ function writeCompilerPlugin(root) {
|
|
|
204
368
|
"",
|
|
205
369
|
"func build(args []string) int {",
|
|
206
370
|
'\tfs := flag.NewFlagSet("build", flag.ContinueOnError)',
|
|
207
|
-
|
|
371
|
+
"\tfs.SetOutput(os.Stderr)",
|
|
208
372
|
'\tcwd := fs.String("cwd", "", "")',
|
|
209
373
|
'\toutDir := fs.String("outDir", "dist", "")',
|
|
210
374
|
'\t_ = fs.String("tsconfig", "", "")',
|
|
@@ -215,19 +379,43 @@ function writeCompilerPlugin(root) {
|
|
|
215
379
|
'\t_ = fs.Bool("noEmit", false, "")',
|
|
216
380
|
"\tif err := fs.Parse(args); err != nil { return 2 }",
|
|
217
381
|
"\troot := *cwd",
|
|
218
|
-
|
|
382
|
+
'\tif root == "" { root, _ = os.Getwd() }',
|
|
219
383
|
'\tinput, err := os.ReadFile(filepath.Join(root, "src", "main.ts"))',
|
|
220
|
-
|
|
384
|
+
"\tif err != nil { fmt.Fprintln(os.Stderr, err); return 2 }",
|
|
221
385
|
'\tvalue := "PLUGIN"',
|
|
222
386
|
'\tif !strings.Contains(string(input), `goUpper("plugin")`) { value = "UNKNOWN" }',
|
|
223
387
|
'\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
388
|
'\tfile := filepath.Join(*outDir, "main.js")',
|
|
225
|
-
|
|
389
|
+
'\tif !filepath.IsAbs(*outDir) { file = filepath.Join(root, *outDir, "main.js") }',
|
|
226
390
|
"\tif err := os.MkdirAll(filepath.Dir(file), 0o755); err != nil { fmt.Fprintln(os.Stderr, err); return 2 }",
|
|
227
391
|
"\tif err := os.WriteFile(file, []byte(output), 0o644); err != nil { fmt.Fprintln(os.Stderr, err); return 2 }",
|
|
228
392
|
"\treturn 0",
|
|
229
393
|
"}",
|
|
230
394
|
"",
|
|
395
|
+
"type transformResult struct {",
|
|
396
|
+
'\tTypeScript map[string]string `json:"typescript"`',
|
|
397
|
+
"}",
|
|
398
|
+
"",
|
|
399
|
+
"func transformSource(args []string) int {",
|
|
400
|
+
'\tfs := flag.NewFlagSet("transform", flag.ContinueOnError)',
|
|
401
|
+
"\tfs.SetOutput(os.Stderr)",
|
|
402
|
+
'\tcwd := fs.String("cwd", "", "")',
|
|
403
|
+
'\t_ = fs.String("tsconfig", "", "")',
|
|
404
|
+
'\t_ = fs.String("plugins-json", "", "")',
|
|
405
|
+
"\tif err := fs.Parse(args); err != nil { return 2 }",
|
|
406
|
+
"\troot := *cwd",
|
|
407
|
+
'\tif root == "" { root, _ = os.Getwd() }',
|
|
408
|
+
'\tinput, err := os.ReadFile(filepath.Join(root, "src", "main.ts"))',
|
|
409
|
+
"\tif err != nil { fmt.Fprintln(os.Stderr, err); return 2 }",
|
|
410
|
+
'\tvalue := "PLUGIN"',
|
|
411
|
+
'\tif !strings.Contains(string(input), `goUpper("plugin")`) { value = "UNKNOWN" }',
|
|
412
|
+
'\toutput := fmt.Sprintf("export const value = %q;\\nconsole.log(value);\\n", value)',
|
|
413
|
+
'\tdata, err := json.Marshal(transformResult{TypeScript: map[string]string{"src/main.ts": output}})',
|
|
414
|
+
"\tif err != nil { fmt.Fprintln(os.Stderr, err); return 2 }",
|
|
415
|
+
"\tfmt.Fprintln(os.Stdout, string(data))",
|
|
416
|
+
"\treturn 0",
|
|
417
|
+
"}",
|
|
418
|
+
"",
|
|
231
419
|
].join("\n"),
|
|
232
420
|
"utf8",
|
|
233
421
|
);
|
package/cmd/ttsc/transform.go
DELETED
|
@@ -1,160 +0,0 @@
|
|
|
1
|
-
package main
|
|
2
|
-
|
|
3
|
-
import (
|
|
4
|
-
"bytes"
|
|
5
|
-
"flag"
|
|
6
|
-
"fmt"
|
|
7
|
-
"os"
|
|
8
|
-
"path/filepath"
|
|
9
|
-
"strings"
|
|
10
|
-
|
|
11
|
-
shimcompiler "github.com/microsoft/typescript-go/shim/compiler"
|
|
12
|
-
|
|
13
|
-
"github.com/samchon/ttsc/packages/ttsc/driver"
|
|
14
|
-
)
|
|
15
|
-
|
|
16
|
-
// runTransform implements `ttsc transform` — a single-file transform used by
|
|
17
|
-
// bundler plugin adapters (unplugin, vite, webpack, rollup, esbuild, rspack,
|
|
18
|
-
// farm). Behavior:
|
|
19
|
-
//
|
|
20
|
-
// - Loads the enclosing tsconfig so tsgo resolves imports and types.
|
|
21
|
-
// - Runs tsgo Emit, filtering WriteFile so only the requested source
|
|
22
|
-
// file's JS leaves the binary.
|
|
23
|
-
// - Applies the ttsc rewrite + import cleanup just like `build --emit`.
|
|
24
|
-
// - Writes the result to stdout (or `--out` when provided).
|
|
25
|
-
//
|
|
26
|
-
// The contract matches a per-file bundler transform hook: bundlers
|
|
27
|
-
// hand the plugin a source file path, the plugin shells out here, and the
|
|
28
|
-
// returned text replaces the file in the bundle graph.
|
|
29
|
-
func runTransform(args []string) int {
|
|
30
|
-
fs := flag.NewFlagSet("transform", flag.ContinueOnError)
|
|
31
|
-
fs.SetOutput(stderr)
|
|
32
|
-
file := fs.String("file", "", "absolute or cwd-relative path of the .ts file to transform")
|
|
33
|
-
tsconfigPath := fs.String("tsconfig", "tsconfig.json", "tsconfig.json owning --file")
|
|
34
|
-
cwdOverride := fs.String("cwd", "", "override the working directory")
|
|
35
|
-
out := fs.String("out", "", "write output JS to PATH (default: stdout)")
|
|
36
|
-
if err := fs.Parse(args); err != nil {
|
|
37
|
-
return 2
|
|
38
|
-
}
|
|
39
|
-
if *file == "" {
|
|
40
|
-
fmt.Fprintln(stderr, "ttsc transform: --file is required")
|
|
41
|
-
return 2
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
cwd := *cwdOverride
|
|
45
|
-
if cwd == "" {
|
|
46
|
-
var err error
|
|
47
|
-
cwd, err = os.Getwd()
|
|
48
|
-
if err != nil {
|
|
49
|
-
fmt.Fprintf(stderr, "ttsc transform: cwd: %v\n", err)
|
|
50
|
-
return 2
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
prog, diags, err := driver.LoadProgram(cwd, *tsconfigPath, driver.LoadProgramOptions{
|
|
55
|
-
ForceEmit: true,
|
|
56
|
-
})
|
|
57
|
-
if err != nil {
|
|
58
|
-
fmt.Fprintf(stderr, "ttsc transform: %v\n", err)
|
|
59
|
-
return 2
|
|
60
|
-
}
|
|
61
|
-
if len(diags) > 0 {
|
|
62
|
-
driver.WritePrettyDiagnostics(stderr, diags, cwd)
|
|
63
|
-
return 2
|
|
64
|
-
}
|
|
65
|
-
defer prog.Close()
|
|
66
|
-
if diags := prog.Diagnostics(); len(diags) > 0 {
|
|
67
|
-
driver.WritePrettyDiagnostics(stderr, diags, cwd)
|
|
68
|
-
return 2
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
absFile := *file
|
|
72
|
-
if !filepath.IsAbs(absFile) {
|
|
73
|
-
absFile = filepath.Join(cwd, absFile)
|
|
74
|
-
}
|
|
75
|
-
absFile = filepath.ToSlash(absFile)
|
|
76
|
-
|
|
77
|
-
rewrites := driver.NewRewriteSet()
|
|
78
|
-
|
|
79
|
-
target := prog.SourceFile(absFile)
|
|
80
|
-
if target == nil {
|
|
81
|
-
fmt.Fprintf(stderr, "ttsc transform: source file is not in program: %s\n", absFile)
|
|
82
|
-
return 2
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
// Capture the requested source file's JS output.
|
|
86
|
-
var captured []byte
|
|
87
|
-
capture := func(name, text string, _ *shimcompiler.WriteFileData) error {
|
|
88
|
-
rel := filepath.ToSlash(name)
|
|
89
|
-
if !isJavaScriptOutput(rel) {
|
|
90
|
-
return nil
|
|
91
|
-
}
|
|
92
|
-
captured = []byte(text)
|
|
93
|
-
return nil
|
|
94
|
-
}
|
|
95
|
-
_, eDiags, err := prog.EmitFile(rewrites, target, capture)
|
|
96
|
-
if err != nil {
|
|
97
|
-
fmt.Fprintf(stderr, "ttsc transform: emit: %v\n", err)
|
|
98
|
-
return 3
|
|
99
|
-
}
|
|
100
|
-
for _, d := range eDiags {
|
|
101
|
-
fmt.Fprintln(stderr, " -", d.String())
|
|
102
|
-
}
|
|
103
|
-
if captured == nil {
|
|
104
|
-
fmt.Fprintf(stderr, "ttsc transform: no output produced for %s\n", absFile)
|
|
105
|
-
return 3
|
|
106
|
-
}
|
|
107
|
-
if *out == "" {
|
|
108
|
-
if _, werr := bytes.NewReader(captured).WriteTo(stdout); werr != nil {
|
|
109
|
-
fmt.Fprintf(stderr, "ttsc transform: write stdout: %v\n", werr)
|
|
110
|
-
return 3
|
|
111
|
-
}
|
|
112
|
-
return 0
|
|
113
|
-
}
|
|
114
|
-
if dir := filepath.Dir(*out); dir != "" {
|
|
115
|
-
if err := os.MkdirAll(dir, 0o755); err != nil {
|
|
116
|
-
fmt.Fprintf(stderr, "ttsc transform: mkdir: %v\n", err)
|
|
117
|
-
return 3
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
if err := os.WriteFile(*out, captured, 0o644); err != nil {
|
|
121
|
-
fmt.Fprintf(stderr, "ttsc transform: write %s: %v\n", *out, err)
|
|
122
|
-
return 3
|
|
123
|
-
}
|
|
124
|
-
return 0
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
// sharedSourceStemSegments counts the shared trailing path segments between the
|
|
128
|
-
// emitted output path and the original source path after stripping extensions.
|
|
129
|
-
// `src/foo/bar.ts` ↔ `dist/foo/bar.js` returns 2, while unrelated paths return
|
|
130
|
-
// 0. `ttsc transform` uses the highest score to pick the emitted JS that
|
|
131
|
-
// belongs to the requested source file even when a project contains multiple
|
|
132
|
-
// `index.ts` files.
|
|
133
|
-
func sharedSourceStemSegments(outPath, srcPath string) int {
|
|
134
|
-
trim := func(p string) []string {
|
|
135
|
-
p = strings.TrimSuffix(p, filepath.Ext(p))
|
|
136
|
-
return strings.Split(p, "/")
|
|
137
|
-
}
|
|
138
|
-
a, b := trim(outPath), trim(srcPath)
|
|
139
|
-
n := len(a)
|
|
140
|
-
if len(b) < n {
|
|
141
|
-
n = len(b)
|
|
142
|
-
}
|
|
143
|
-
shared := 0
|
|
144
|
-
for i := 1; i <= n; i++ {
|
|
145
|
-
if a[len(a)-i] != b[len(b)-i] {
|
|
146
|
-
break
|
|
147
|
-
}
|
|
148
|
-
shared++
|
|
149
|
-
}
|
|
150
|
-
return shared
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
func isJavaScriptOutput(name string) bool {
|
|
154
|
-
switch strings.ToLower(filepath.Ext(name)) {
|
|
155
|
-
case ".js", ".mjs", ".cjs":
|
|
156
|
-
return true
|
|
157
|
-
default:
|
|
158
|
-
return false
|
|
159
|
-
}
|
|
160
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"runTransform.js","sourceRoot":"","sources":["../../../src/compiler/internal/runTransform.ts"],"names":[],"mappings":"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"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"TtscTransformOptions.js","sourceRoot":"","sources":["../../../src/structures/internal/TtscTransformOptions.ts"],"names":[],"mappings":""}
|