ttsc 0.14.2 → 0.15.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.
- package/driver/emit_plugin.go +165 -0
- package/driver/plugins.go +37 -0
- package/go.mod +0 -3
- package/go.sum +0 -4
- package/lib/compiler/internal/resolveEmittedJavaScript.js +25 -4
- package/lib/compiler/internal/resolveEmittedJavaScript.js.map +1 -1
- package/lib/launcher/internal/prepareExecution.d.ts +4 -1
- package/lib/launcher/internal/prepareExecution.js +13 -17
- package/lib/launcher/internal/prepareExecution.js.map +1 -1
- package/lib/launcher/internal/registerRuntimeHooks.js +43 -8
- package/lib/launcher/internal/registerRuntimeHooks.js.map +1 -1
- package/lib/launcher/internal/runTtsx.js +46 -386
- package/lib/launcher/internal/runTtsx.js.map +1 -1
- package/lib/launcher/internal/runtimeHookPreload.d.ts +1 -0
- package/lib/launcher/internal/runtimeHookPreload.js +16 -0
- package/lib/launcher/internal/runtimeHookPreload.js.map +1 -0
- package/lib/launcher/internal/runtimeHooks.d.ts +16 -38
- package/lib/launcher/internal/runtimeHooks.js +672 -119
- package/lib/launcher/internal/runtimeHooks.js.map +1 -1
- package/lib/plugin/internal/buildSourcePlugin.js +120 -16
- package/lib/plugin/internal/buildSourcePlugin.js.map +1 -1
- package/lib/plugin/internal/loadProjectPlugins.js +15 -4
- package/lib/plugin/internal/loadProjectPlugins.js.map +1 -1
- package/package.json +8 -8
- package/shim/ast/parent.go +31 -0
- package/shim/compiler/emitassemble.go +30 -0
- package/shim/compiler/emithook.go +22 -0
- package/shim/compiler/go.mod +9 -0
- package/shim/compiler/go.sum +22 -0
- package/shim/core/modulekind.go +10 -0
- package/shim/printer/emithost.go +24 -0
- package/shim/printer/go.mod +9 -0
- package/shim/printer/go.sum +22 -0
- package/shim/transformers/go.mod +14 -0
- package/shim/transformers/go.sum +20 -0
- package/shim/transformers/shim.go +15 -0
- package/src/compiler/internal/resolveEmittedJavaScript.ts +35 -4
- package/src/launcher/internal/prepareExecution.ts +18 -21
- package/src/launcher/internal/registerRuntimeHooks.ts +53 -8
- package/src/launcher/internal/runTtsx.ts +61 -453
- package/src/launcher/internal/runtimeHookPreload.ts +14 -0
- package/src/launcher/internal/runtimeHooks.ts +794 -136
- package/src/plugin/internal/buildSourcePlugin.ts +147 -16
- package/src/plugin/internal/loadProjectPlugins.ts +16 -4
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// gen_shims:hand-maintained
|
|
2
|
+
//
|
|
3
|
+
// Emit-pipeline assembly parts: pick the files tsgo would emit and resolve
|
|
4
|
+
// their output paths, so ttsc's driver can drive emit per file with a plugin
|
|
5
|
+
// transformer inserted ahead of the builtin chain.
|
|
6
|
+
package compiler
|
|
7
|
+
|
|
8
|
+
import (
|
|
9
|
+
_ "unsafe"
|
|
10
|
+
|
|
11
|
+
innerast "github.com/microsoft/typescript-go/internal/ast"
|
|
12
|
+
innercompiler "github.com/microsoft/typescript-go/internal/compiler"
|
|
13
|
+
innercore "github.com/microsoft/typescript-go/internal/core"
|
|
14
|
+
inneroutputpaths "github.com/microsoft/typescript-go/internal/outputpaths"
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
// GetSourceFilesToEmit returns the source files tsgo would emit for the program
|
|
18
|
+
// (excludes .d.ts and external-library files), linked from the internal package.
|
|
19
|
+
//
|
|
20
|
+
//go:linkname GetSourceFilesToEmit github.com/microsoft/typescript-go/internal/compiler.getSourceFilesToEmit
|
|
21
|
+
func GetSourceFilesToEmit(host innercompiler.SourceFileMayBeEmittedHost, targetSourceFile *innerast.SourceFile, forceDtsEmit bool) []*innerast.SourceFile
|
|
22
|
+
|
|
23
|
+
// OutputPaths holds the resolved output file paths for one source file.
|
|
24
|
+
type OutputPaths = inneroutputpaths.OutputPaths
|
|
25
|
+
|
|
26
|
+
// GetOutputPathsFor resolves the .js / .d.ts / map output paths for a source
|
|
27
|
+
// file (honoring rootDir/outDir), the same call tsgo's emitter makes.
|
|
28
|
+
func GetOutputPathsFor(sourceFile *innerast.SourceFile, options *innercore.CompilerOptions, host inneroutputpaths.OutputPathsHost, forceDtsEmit bool) *inneroutputpaths.OutputPaths {
|
|
29
|
+
return inneroutputpaths.GetOutputPathsFor(sourceFile, options, host, forceDtsEmit)
|
|
30
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// gen_shims:hand-maintained
|
|
2
|
+
//
|
|
3
|
+
// Exposes tsgo's emit-stage internals so ttsc can assemble the emit pipeline
|
|
4
|
+
// from real tsgo parts (no tsgo source copy/edit): obtain the builtin
|
|
5
|
+
// transformer chain for a file and prepend a plugin transformer that shares the
|
|
6
|
+
// same EmitContext, so module-transform aliases plugin-generated imports.
|
|
7
|
+
package compiler
|
|
8
|
+
|
|
9
|
+
import (
|
|
10
|
+
_ "unsafe"
|
|
11
|
+
|
|
12
|
+
innerast "github.com/microsoft/typescript-go/internal/ast"
|
|
13
|
+
innerprinter "github.com/microsoft/typescript-go/internal/printer"
|
|
14
|
+
innertransformers "github.com/microsoft/typescript-go/internal/transformers"
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
// GetScriptTransformers returns tsgo's builtin emit transformer chain
|
|
18
|
+
// (type-erase, import-elision, runtime-syntax, module-transform, ...) for one
|
|
19
|
+
// source file, linked from the internal package via go:linkname.
|
|
20
|
+
//
|
|
21
|
+
//go:linkname GetScriptTransformers github.com/microsoft/typescript-go/internal/compiler.getScriptTransformers
|
|
22
|
+
func GetScriptTransformers(emitContext *innerprinter.EmitContext, host innerprinter.EmitHost, sourceFile *innerast.SourceFile) []*innertransformers.Transformer
|
package/shim/compiler/go.mod
CHANGED
|
@@ -3,3 +3,12 @@ module github.com/microsoft/typescript-go/shim/compiler
|
|
|
3
3
|
go 1.26
|
|
4
4
|
|
|
5
5
|
require github.com/microsoft/typescript-go v0.0.0-20260429010842-56ab4af42157
|
|
6
|
+
|
|
7
|
+
require (
|
|
8
|
+
github.com/go-json-experiment/json v0.0.0-20260214004413-d219187c3433 // indirect
|
|
9
|
+
github.com/klauspost/cpuid/v2 v2.2.10 // indirect
|
|
10
|
+
github.com/zeebo/xxh3 v1.1.0 // indirect
|
|
11
|
+
golang.org/x/sync v0.20.0 // indirect
|
|
12
|
+
golang.org/x/sys v0.43.0 // indirect
|
|
13
|
+
golang.org/x/text v0.36.0 // indirect
|
|
14
|
+
)
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
github.com/go-json-experiment/json v0.0.0-20260214004413-d219187c3433 h1:vymEbVwYFP/L05h5TKQxvkXoKxNvTpjxYKdF1Nlwuao=
|
|
2
|
+
github.com/go-json-experiment/json v0.0.0-20260214004413-d219187c3433/go.mod h1:tphK2c80bpPhMOI4v6bIc2xWywPfbqi1Z06+RcrMkDg=
|
|
3
|
+
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
|
4
|
+
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
|
5
|
+
github.com/klauspost/cpuid/v2 v2.2.10 h1:tBs3QSyvjDyFTq3uoc/9xFpCuOsJQFNPiAhYdw2skhE=
|
|
6
|
+
github.com/klauspost/cpuid/v2 v2.2.10/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
|
|
7
|
+
github.com/microsoft/typescript-go v0.0.0-20260429010842-56ab4af42157 h1:llOMPhKDiQ+UEJiAaQuuWaJOTg5V7tNNTEf9JPWoYSY=
|
|
8
|
+
github.com/microsoft/typescript-go v0.0.0-20260429010842-56ab4af42157/go.mod h1:m8YA0PMC7ti0GW0RI05D6fEcjQeu98XVS+FWF+VDW2k=
|
|
9
|
+
github.com/peter-evans/patience v0.3.0 h1:rX0JdJeepqdQl1Sk9c9uvorjYYzL2TfgLX1adqYm9cA=
|
|
10
|
+
github.com/peter-evans/patience v0.3.0/go.mod h1:Kmxu5sY1NmBLFSStvXjX1wS9mIv7wMcP/ubucyMOAu0=
|
|
11
|
+
github.com/zeebo/assert v1.3.0 h1:g7C04CbJuIDKNPFHmsk4hwZDO5O+kntRxzaUoNXj+IQ=
|
|
12
|
+
github.com/zeebo/assert v1.3.0/go.mod h1:Pq9JiuJQpG8JLJdtkwrJESF0Foym2/D9XMU5ciN/wJ0=
|
|
13
|
+
github.com/zeebo/xxh3 v1.1.0 h1:s7DLGDK45Dyfg7++yxI0khrfwq9661w9EN78eP/UZVs=
|
|
14
|
+
github.com/zeebo/xxh3 v1.1.0/go.mod h1:IisAie1LELR4xhVinxWS5+zf1lA4p0MW4T+w+W07F5s=
|
|
15
|
+
golang.org/x/sync v0.20.0 h1:e0PTpb7pjO8GAtTs2dQ6jYa5BWYlMuX047Dco/pItO4=
|
|
16
|
+
golang.org/x/sync v0.20.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
|
|
17
|
+
golang.org/x/sys v0.43.0 h1:Rlag2XtaFTxp19wS8MXlJwTvoh8ArU6ezoyFsMyCTNI=
|
|
18
|
+
golang.org/x/sys v0.43.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
|
|
19
|
+
golang.org/x/text v0.36.0 h1:JfKh3XmcRPqZPKevfXVpI1wXPTqbkE5f7JA92a55Yxg=
|
|
20
|
+
golang.org/x/text v0.36.0/go.mod h1:NIdBknypM8iqVmPiuco0Dh6P5Jcdk8lJL0CUebqK164=
|
|
21
|
+
gotest.tools/v3 v3.5.2 h1:7koQfIKdy+I8UTetycgUqXWSDwpgv193Ka+qRsmBY8Q=
|
|
22
|
+
gotest.tools/v3 v3.5.2/go.mod h1:LtdLGcnqToBH83WByAAi/wiwSFCArdFIUV/xxN4pcjA=
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// gen_shims:hand-maintained
|
|
2
|
+
//
|
|
3
|
+
// Re-exports the ModuleKind enum so plugins / ttsc can type emit-format values
|
|
4
|
+
// (e.g. an EmitHost.GetEmitModuleFormatOfFile result).
|
|
5
|
+
package core
|
|
6
|
+
|
|
7
|
+
import innercore "github.com/microsoft/typescript-go/internal/core"
|
|
8
|
+
|
|
9
|
+
// ModuleKind is tsgo's module-format enum (CommonJS, ESNext, NodeNext, ...).
|
|
10
|
+
type ModuleKind = innercore.ModuleKind
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// gen_shims:hand-maintained
|
|
2
|
+
//
|
|
3
|
+
// Re-exports the emit host/resolver interfaces so ttsc's driver can implement
|
|
4
|
+
// its own EmitHost (delegating to driver.Program) and hand it to
|
|
5
|
+
// compiler.GetScriptTransformers when assembling the emit pipeline.
|
|
6
|
+
package printer
|
|
7
|
+
|
|
8
|
+
import innerprinter "github.com/microsoft/typescript-go/internal/printer"
|
|
9
|
+
|
|
10
|
+
// EmitHost is the per-emit host interface tsgo's transformers query (Options,
|
|
11
|
+
// SourceFiles, GetEmitResolver, GetEmitModuleFormatOfFile, WriteFile, ...).
|
|
12
|
+
type EmitHost = innerprinter.EmitHost
|
|
13
|
+
|
|
14
|
+
// EmitResolver resolves emit-time facts about nodes (referenced imports,
|
|
15
|
+
// declaration flags, ...) under the checker mutex.
|
|
16
|
+
type EmitResolver = innerprinter.EmitResolver
|
|
17
|
+
|
|
18
|
+
// EmitTextWriter is the sink the printer emits into; String() yields the text.
|
|
19
|
+
type EmitTextWriter = innerprinter.EmitTextWriter
|
|
20
|
+
|
|
21
|
+
// NewTextWriter creates a fresh writer for one emit (newLine e.g. "\n").
|
|
22
|
+
func NewTextWriter(newLine string, indentSize int) EmitTextWriter {
|
|
23
|
+
return innerprinter.NewTextWriter(newLine, indentSize)
|
|
24
|
+
}
|
package/shim/printer/go.mod
CHANGED
|
@@ -3,3 +3,12 @@ module github.com/microsoft/typescript-go/shim/printer
|
|
|
3
3
|
go 1.26
|
|
4
4
|
|
|
5
5
|
require github.com/microsoft/typescript-go v0.0.0-20260429010842-56ab4af42157
|
|
6
|
+
|
|
7
|
+
require (
|
|
8
|
+
github.com/go-json-experiment/json v0.0.0-20260214004413-d219187c3433 // indirect
|
|
9
|
+
github.com/klauspost/cpuid/v2 v2.2.10 // indirect
|
|
10
|
+
github.com/zeebo/xxh3 v1.1.0 // indirect
|
|
11
|
+
golang.org/x/sync v0.20.0 // indirect
|
|
12
|
+
golang.org/x/sys v0.43.0 // indirect
|
|
13
|
+
golang.org/x/text v0.36.0 // indirect
|
|
14
|
+
)
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
github.com/go-json-experiment/json v0.0.0-20260214004413-d219187c3433 h1:vymEbVwYFP/L05h5TKQxvkXoKxNvTpjxYKdF1Nlwuao=
|
|
2
|
+
github.com/go-json-experiment/json v0.0.0-20260214004413-d219187c3433/go.mod h1:tphK2c80bpPhMOI4v6bIc2xWywPfbqi1Z06+RcrMkDg=
|
|
3
|
+
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
|
4
|
+
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
|
5
|
+
github.com/klauspost/cpuid/v2 v2.2.10 h1:tBs3QSyvjDyFTq3uoc/9xFpCuOsJQFNPiAhYdw2skhE=
|
|
6
|
+
github.com/klauspost/cpuid/v2 v2.2.10/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
|
|
7
|
+
github.com/microsoft/typescript-go v0.0.0-20260429010842-56ab4af42157 h1:llOMPhKDiQ+UEJiAaQuuWaJOTg5V7tNNTEf9JPWoYSY=
|
|
8
|
+
github.com/microsoft/typescript-go v0.0.0-20260429010842-56ab4af42157/go.mod h1:m8YA0PMC7ti0GW0RI05D6fEcjQeu98XVS+FWF+VDW2k=
|
|
9
|
+
github.com/peter-evans/patience v0.3.0 h1:rX0JdJeepqdQl1Sk9c9uvorjYYzL2TfgLX1adqYm9cA=
|
|
10
|
+
github.com/peter-evans/patience v0.3.0/go.mod h1:Kmxu5sY1NmBLFSStvXjX1wS9mIv7wMcP/ubucyMOAu0=
|
|
11
|
+
github.com/zeebo/assert v1.3.0 h1:g7C04CbJuIDKNPFHmsk4hwZDO5O+kntRxzaUoNXj+IQ=
|
|
12
|
+
github.com/zeebo/assert v1.3.0/go.mod h1:Pq9JiuJQpG8JLJdtkwrJESF0Foym2/D9XMU5ciN/wJ0=
|
|
13
|
+
github.com/zeebo/xxh3 v1.1.0 h1:s7DLGDK45Dyfg7++yxI0khrfwq9661w9EN78eP/UZVs=
|
|
14
|
+
github.com/zeebo/xxh3 v1.1.0/go.mod h1:IisAie1LELR4xhVinxWS5+zf1lA4p0MW4T+w+W07F5s=
|
|
15
|
+
golang.org/x/sync v0.20.0 h1:e0PTpb7pjO8GAtTs2dQ6jYa5BWYlMuX047Dco/pItO4=
|
|
16
|
+
golang.org/x/sync v0.20.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
|
|
17
|
+
golang.org/x/sys v0.43.0 h1:Rlag2XtaFTxp19wS8MXlJwTvoh8ArU6ezoyFsMyCTNI=
|
|
18
|
+
golang.org/x/sys v0.43.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
|
|
19
|
+
golang.org/x/text v0.36.0 h1:JfKh3XmcRPqZPKevfXVpI1wXPTqbkE5f7JA92a55Yxg=
|
|
20
|
+
golang.org/x/text v0.36.0/go.mod h1:NIdBknypM8iqVmPiuco0Dh6P5Jcdk8lJL0CUebqK164=
|
|
21
|
+
gotest.tools/v3 v3.5.2 h1:7koQfIKdy+I8UTetycgUqXWSDwpgv193Ka+qRsmBY8Q=
|
|
22
|
+
gotest.tools/v3 v3.5.2/go.mod h1:LtdLGcnqToBH83WByAAi/wiwSFCArdFIUV/xxN4pcjA=
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
module github.com/microsoft/typescript-go/shim/transformers
|
|
2
|
+
|
|
3
|
+
go 1.26
|
|
4
|
+
|
|
5
|
+
require github.com/microsoft/typescript-go v0.0.0-20260429010842-56ab4af42157
|
|
6
|
+
|
|
7
|
+
require (
|
|
8
|
+
github.com/go-json-experiment/json v0.0.0-20260214004413-d219187c3433 // indirect
|
|
9
|
+
github.com/klauspost/cpuid/v2 v2.2.10 // indirect
|
|
10
|
+
github.com/zeebo/xxh3 v1.1.0 // indirect
|
|
11
|
+
golang.org/x/sync v0.20.0 // indirect
|
|
12
|
+
golang.org/x/sys v0.43.0 // indirect
|
|
13
|
+
golang.org/x/text v0.36.0 // indirect
|
|
14
|
+
)
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
github.com/go-json-experiment/json v0.0.0-20260214004413-d219187c3433 h1:vymEbVwYFP/L05h5TKQxvkXoKxNvTpjxYKdF1Nlwuao=
|
|
2
|
+
github.com/go-json-experiment/json v0.0.0-20260214004413-d219187c3433/go.mod h1:tphK2c80bpPhMOI4v6bIc2xWywPfbqi1Z06+RcrMkDg=
|
|
3
|
+
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
|
4
|
+
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
|
5
|
+
github.com/klauspost/cpuid/v2 v2.2.10 h1:tBs3QSyvjDyFTq3uoc/9xFpCuOsJQFNPiAhYdw2skhE=
|
|
6
|
+
github.com/klauspost/cpuid/v2 v2.2.10/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
|
|
7
|
+
github.com/microsoft/typescript-go v0.0.0-20260429010842-56ab4af42157 h1:llOMPhKDiQ+UEJiAaQuuWaJOTg5V7tNNTEf9JPWoYSY=
|
|
8
|
+
github.com/microsoft/typescript-go v0.0.0-20260429010842-56ab4af42157/go.mod h1:m8YA0PMC7ti0GW0RI05D6fEcjQeu98XVS+FWF+VDW2k=
|
|
9
|
+
github.com/zeebo/assert v1.3.0 h1:g7C04CbJuIDKNPFHmsk4hwZDO5O+kntRxzaUoNXj+IQ=
|
|
10
|
+
github.com/zeebo/assert v1.3.0/go.mod h1:Pq9JiuJQpG8JLJdtkwrJESF0Foym2/D9XMU5ciN/wJ0=
|
|
11
|
+
github.com/zeebo/xxh3 v1.1.0 h1:s7DLGDK45Dyfg7++yxI0khrfwq9661w9EN78eP/UZVs=
|
|
12
|
+
github.com/zeebo/xxh3 v1.1.0/go.mod h1:IisAie1LELR4xhVinxWS5+zf1lA4p0MW4T+w+W07F5s=
|
|
13
|
+
golang.org/x/sync v0.20.0 h1:e0PTpb7pjO8GAtTs2dQ6jYa5BWYlMuX047Dco/pItO4=
|
|
14
|
+
golang.org/x/sync v0.20.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
|
|
15
|
+
golang.org/x/sys v0.43.0 h1:Rlag2XtaFTxp19wS8MXlJwTvoh8ArU6ezoyFsMyCTNI=
|
|
16
|
+
golang.org/x/sys v0.43.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
|
|
17
|
+
golang.org/x/text v0.36.0 h1:JfKh3XmcRPqZPKevfXVpI1wXPTqbkE5f7JA92a55Yxg=
|
|
18
|
+
golang.org/x/text v0.36.0/go.mod h1:NIdBknypM8iqVmPiuco0Dh6P5Jcdk8lJL0CUebqK164=
|
|
19
|
+
gotest.tools/v3 v3.5.2 h1:7koQfIKdy+I8UTetycgUqXWSDwpgv193Ka+qRsmBY8Q=
|
|
20
|
+
gotest.tools/v3 v3.5.2/go.mod h1:LtdLGcnqToBH83WByAAi/wiwSFCArdFIUV/xxN4pcjA=
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// gen_shims:hand-maintained
|
|
2
|
+
//
|
|
3
|
+
// Re-exports tsgo's emit transformer type so ttsc can build a plugin
|
|
4
|
+
// transformer and run it ahead of the builtin emit chain. Plugins (typia,
|
|
5
|
+
// nestia) return real AST nodes; ttsc inserts their transformer first in the
|
|
6
|
+
// same EmitContext as the builtins, so tsgo's module-transform aliases imports
|
|
7
|
+
// itself, no text-splice needed.
|
|
8
|
+
package transformers
|
|
9
|
+
|
|
10
|
+
import innertransformers "github.com/microsoft/typescript-go/internal/transformers"
|
|
11
|
+
|
|
12
|
+
// Transformer is one stage of tsgo's emit transformer chain
|
|
13
|
+
// (TransformSourceFile mutates the SourceFile AST in place within the emit
|
|
14
|
+
// EmitContext).
|
|
15
|
+
type Transformer = innertransformers.Transformer
|
|
@@ -34,18 +34,49 @@ export function resolveEmittedJavaScript(options: {
|
|
|
34
34
|
return exact;
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
+
// Score the pre-computed emit list first (cheap). When it yields nothing —
|
|
38
|
+
// because the list is incomplete (a native transform host such as typia emits
|
|
39
|
+
// without printing the `--listEmittedFiles` lines) or because the emit landed
|
|
40
|
+
// at a path the exact mirror did not predict (tsgo shifts every output path
|
|
41
|
+
// when the program pulls a raw-`.ts` dependency that sits outside `rootDir`,
|
|
42
|
+
// so it strips the common source root rather than `rootDir`) — fall back to a
|
|
43
|
+
// full recursive scan of `outDir`. Trailing-stem scoring still pins the right
|
|
44
|
+
// file regardless of how deep the shifted prefix is.
|
|
45
|
+
const primary = bestStemMatch(
|
|
46
|
+
options.emittedFiles ?? listJavaScriptFiles(options.outDir),
|
|
47
|
+
options.sourceFile,
|
|
48
|
+
);
|
|
49
|
+
if (primary !== null && fs.existsSync(primary)) {
|
|
50
|
+
return primary;
|
|
51
|
+
}
|
|
52
|
+
if (options.emittedFiles !== undefined) {
|
|
53
|
+
const fromDir = bestStemMatch(
|
|
54
|
+
listJavaScriptFiles(options.outDir),
|
|
55
|
+
options.sourceFile,
|
|
56
|
+
);
|
|
57
|
+
if (fromDir !== null && fs.existsSync(fromDir)) {
|
|
58
|
+
return fromDir;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Highest trailing-stem-scoring JavaScript output among `files`, or `null`. */
|
|
65
|
+
function bestStemMatch(
|
|
66
|
+
files: readonly string[],
|
|
67
|
+
sourceFile: string,
|
|
68
|
+
): string | null {
|
|
37
69
|
let best: string | null = null;
|
|
38
70
|
let bestScore = 0;
|
|
39
|
-
for (const file of
|
|
40
|
-
listJavaScriptFiles(options.outDir)) {
|
|
71
|
+
for (const file of files) {
|
|
41
72
|
if (!isJavaScriptOutput(file)) continue;
|
|
42
|
-
const score = sharedSourceStemSegments(file,
|
|
73
|
+
const score = sharedSourceStemSegments(file, sourceFile);
|
|
43
74
|
if (score > bestScore) {
|
|
44
75
|
best = file;
|
|
45
76
|
bestScore = score;
|
|
46
77
|
}
|
|
47
78
|
}
|
|
48
|
-
return best
|
|
79
|
+
return best;
|
|
49
80
|
}
|
|
50
81
|
|
|
51
82
|
/**
|
|
@@ -27,8 +27,11 @@ export function prepareExecution(
|
|
|
27
27
|
): {
|
|
28
28
|
cleanupDir: string;
|
|
29
29
|
emitDir: string;
|
|
30
|
+
emittedFiles?: readonly string[];
|
|
30
31
|
entryFile: string;
|
|
31
|
-
|
|
32
|
+
moduleOption?: string;
|
|
33
|
+
projectRoot: string;
|
|
34
|
+
rootDir: string;
|
|
32
35
|
} {
|
|
33
36
|
const context = createProjectContext(
|
|
34
37
|
path.resolve(options.cwd ?? process.cwd()),
|
|
@@ -46,12 +49,14 @@ export function prepareExecution(
|
|
|
46
49
|
if (emittedEntry === null) {
|
|
47
50
|
throw new Error(`ttsx: emitted entry not found for ${entryFile}`);
|
|
48
51
|
}
|
|
49
|
-
const output = fs.readFileSync(emittedEntry, "utf8");
|
|
50
52
|
return {
|
|
51
53
|
cleanupDir: context.processDir,
|
|
52
54
|
emitDir: context.emitDir,
|
|
55
|
+
emittedFiles: context.emittedFiles ?? undefined,
|
|
53
56
|
entryFile: emittedEntry,
|
|
54
|
-
|
|
57
|
+
moduleOption: context.moduleOption,
|
|
58
|
+
projectRoot: context.root,
|
|
59
|
+
rootDir: context.runtimeRootDir,
|
|
55
60
|
};
|
|
56
61
|
} catch (error) {
|
|
57
62
|
removeRuntimeOutput(context.processDir);
|
|
@@ -87,6 +92,16 @@ function createProjectContext(
|
|
|
87
92
|
emitDir: project.compilerOptions.outDir
|
|
88
93
|
? virtualPath(virtualRoot, project.compilerOptions.outDir)
|
|
89
94
|
: virtualPath(virtualRoot, resolveRuntimeSourceRoot(project, filename)),
|
|
95
|
+
// The source-tree root the emit mirrors (tsgo strips this prefix). Used to
|
|
96
|
+
// map a source `.ts` back to its emitted `.js` when the runtime hooks serve
|
|
97
|
+
// the built entry under its source URL.
|
|
98
|
+
runtimeRootDir: resolveRuntimeSourceRoot(project, filename),
|
|
99
|
+
// The tsconfig `module` option, so the runtime hooks classify each served
|
|
100
|
+
// file's format the same way tsgo chose when emitting it.
|
|
101
|
+
moduleOption:
|
|
102
|
+
typeof project.compilerOptions.module === "string"
|
|
103
|
+
? project.compilerOptions.module
|
|
104
|
+
: undefined,
|
|
90
105
|
built: false,
|
|
91
106
|
emittedFiles: undefined as string[] | undefined,
|
|
92
107
|
};
|
|
@@ -261,21 +276,3 @@ function virtualPath(root: string, absolute: string): string {
|
|
|
261
276
|
const relative = path.relative(parsed.root, path.resolve(absolute));
|
|
262
277
|
return path.join(root, label, relative);
|
|
263
278
|
}
|
|
264
|
-
|
|
265
|
-
/**
|
|
266
|
-
* Heuristic: classify emitted JS as ESM when it contains top-level `import` or
|
|
267
|
-
* `export` statements but none of the well-known CJS patterns. The CJS checks
|
|
268
|
-
* run first so that re-exported CJS bundles with both `require` calls and an
|
|
269
|
-
* `export` declaration are conservatively treated as CJS.
|
|
270
|
-
*/
|
|
271
|
-
function looksLikeESM(output: string): boolean {
|
|
272
|
-
if (
|
|
273
|
-
/\bObject\.defineProperty\(exports\b/.test(output) ||
|
|
274
|
-
/\bmodule\.exports\b/.test(output) ||
|
|
275
|
-
/\brequire\(/.test(output) ||
|
|
276
|
-
/\bexports\./.test(output)
|
|
277
|
-
) {
|
|
278
|
-
return false;
|
|
279
|
-
}
|
|
280
|
-
return /^\s*(import|export)\s/m.test(output);
|
|
281
|
-
}
|
|
@@ -1,14 +1,59 @@
|
|
|
1
|
-
import { register } from "node:module";
|
|
2
1
|
import path from "node:path";
|
|
3
2
|
import { pathToFileURL } from "node:url";
|
|
4
3
|
|
|
4
|
+
import { entryModuleFormat, installRuntimeHooks } from "./runtimeHooks";
|
|
5
|
+
|
|
5
6
|
/**
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
7
|
+
* Bootstrap `ttsx` spawns as the child's main module: it installs the runtime
|
|
8
|
+
* module hooks, then loads the TypeScript entry _from source_.
|
|
9
|
+
*
|
|
10
|
+
* Why load the entry here instead of passing it as Node's main entry: a
|
|
11
|
+
* CommonJS `require("./x")` chain only reaches `module.registerHooks` when the
|
|
12
|
+
* entry itself is loaded through a CommonJS `require`. Running the `.ts` as
|
|
13
|
+
* Node's ESM-first main entry leaves inner `require`s on the native loader,
|
|
14
|
+
* where they cannot resolve a sibling `.ts`. So the parent invokes `node
|
|
15
|
+
* registerRuntimeHooks.js <entry> [argv...]` and we `require()` (or dynamically
|
|
16
|
+
* `import()`) the entry after the hooks are live.
|
|
9
17
|
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* and the sibling hooks file.
|
|
18
|
+
* `process.argv` is rewritten to `[node, <entry>, ...argv]` so the program sees
|
|
19
|
+
* the same argv it would under a direct `node <entry>` invocation.
|
|
13
20
|
*/
|
|
14
|
-
|
|
21
|
+
const entry = process.argv[2];
|
|
22
|
+
if (entry === undefined) {
|
|
23
|
+
process.stderr.write("ttsx: internal error — no entry passed to bootstrap\n");
|
|
24
|
+
process.exit(2);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const forwarded = process.argv.slice(3);
|
|
28
|
+
process.argv = [process.argv[0]!, entry, ...forwarded];
|
|
29
|
+
|
|
30
|
+
installRuntimeHooks();
|
|
31
|
+
|
|
32
|
+
const kind = entryModuleFormat(path.resolve(entry));
|
|
33
|
+
|
|
34
|
+
function fail(error: unknown): never {
|
|
35
|
+
// Match Node's own uncaught-exception surfacing for a thrown entry error.
|
|
36
|
+
process.stderr.write(
|
|
37
|
+
`${error instanceof Error ? (error.stack ?? error.message) : String(error)}\n`,
|
|
38
|
+
);
|
|
39
|
+
process.exit(1);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// A genuine dynamic `import()`. Authored directly it would be downlevelled to a
|
|
43
|
+
// `require()` shim under this package's CommonJS emit, which cannot load a
|
|
44
|
+
// `file:` URL or an ESM module — so build it through `Function` to keep it an
|
|
45
|
+
// `import` at runtime.
|
|
46
|
+
const dynamicImport = new Function(
|
|
47
|
+
"specifier",
|
|
48
|
+
"return import(specifier);",
|
|
49
|
+
) as (specifier: string) => Promise<unknown>;
|
|
50
|
+
|
|
51
|
+
if (kind === "module") {
|
|
52
|
+
dynamicImport(pathToFileURL(path.resolve(entry)).href).catch(fail);
|
|
53
|
+
} else {
|
|
54
|
+
try {
|
|
55
|
+
require(path.resolve(entry));
|
|
56
|
+
} catch (error) {
|
|
57
|
+
fail(error);
|
|
58
|
+
}
|
|
59
|
+
}
|