@ttsc/paths 0.5.0-dev.20260429
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/LICENSE +21 -0
- package/README.md +70 -0
- package/go-plugin/go.mod +26 -0
- package/go-plugin/main.go +33 -0
- package/go-plugin/paths/paths.go +635 -0
- package/index.cjs +18 -0
- package/package.json +36 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Jeongho Nam
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# `@ttsc/paths`
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
[](https://github.com/samchon/ttsc/blob/master/LICENSE)
|
|
6
|
+
[](https://www.npmjs.com/package/@ttsc/paths)
|
|
7
|
+
[](https://www.npmjs.com/package/@ttsc/paths)
|
|
8
|
+
[](https://github.com/samchon/ttsc/actions?query=workflow%3Atest)
|
|
9
|
+
[](https://discord.gg/E94XhzrUCZ)
|
|
10
|
+
|
|
11
|
+
`@ttsc/paths` rewrites emitted module specifiers that match `compilerOptions.paths` into relative JavaScript paths.
|
|
12
|
+
|
|
13
|
+
## Setup
|
|
14
|
+
|
|
15
|
+
Install `ttsc`, TypeScript-Go, and the paths plugin:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install -D ttsc @typescript/native-preview @ttsc/paths
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Open your project's `tsconfig.json`, then configure `paths`, `rootDir`, `outDir`, and this plugin under `compilerOptions`. If the file already has `compilerOptions`, merge these fields into the existing object:
|
|
22
|
+
|
|
23
|
+
```jsonc
|
|
24
|
+
{
|
|
25
|
+
"compilerOptions": {
|
|
26
|
+
"paths": {
|
|
27
|
+
"@/*": ["./src/*"],
|
|
28
|
+
"@lib/*": ["./src/modules/*"]
|
|
29
|
+
},
|
|
30
|
+
"rootDir": "src",
|
|
31
|
+
"outDir": "dist",
|
|
32
|
+
"plugins": [
|
|
33
|
+
{ "transform": "@ttsc/paths" }
|
|
34
|
+
]
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Run your normal `ttsc` command:
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
npx ttsc
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
An emitted import such as `import { value } from "@lib/value"` becomes a relative JavaScript import such as `import { value } from "./modules/value.js"`.
|
|
46
|
+
|
|
47
|
+
## Notes
|
|
48
|
+
|
|
49
|
+
No separate plugin options are required. `@ttsc/paths` reads the same `compilerOptions.paths`, `rootDir`, and `outDir` values that `ttsc` uses for the project.
|
|
50
|
+
|
|
51
|
+
```jsonc
|
|
52
|
+
{
|
|
53
|
+
"compilerOptions": {
|
|
54
|
+
"paths": {
|
|
55
|
+
"@lib/*": ["./src/modules/*"]
|
|
56
|
+
},
|
|
57
|
+
"rootDir": "src",
|
|
58
|
+
"outDir": "dist",
|
|
59
|
+
"plugins": [
|
|
60
|
+
// Keep lint first.
|
|
61
|
+
{ "transform": "@ttsc/lint", "rules": { "no-var": "error" } },
|
|
62
|
+
|
|
63
|
+
// Output plugins run after emit, in order.
|
|
64
|
+
{ "transform": "@ttsc/banner", "banner": "/*! @license MIT */" },
|
|
65
|
+
{ "transform": "@ttsc/paths" },
|
|
66
|
+
{ "transform": "@ttsc/strip", "calls": ["console.log"] }
|
|
67
|
+
]
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
```
|
package/go-plugin/go.mod
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
module github.com/samchon/ttsc/packages/paths/go-plugin
|
|
2
|
+
|
|
3
|
+
go 1.26
|
|
4
|
+
|
|
5
|
+
require (
|
|
6
|
+
github.com/microsoft/typescript-go/shim/ast v0.0.0
|
|
7
|
+
github.com/microsoft/typescript-go/shim/bundled v0.0.0
|
|
8
|
+
github.com/microsoft/typescript-go/shim/compiler v0.0.0
|
|
9
|
+
github.com/microsoft/typescript-go/shim/core v0.0.0
|
|
10
|
+
github.com/microsoft/typescript-go/shim/diagnosticwriter v0.0.0
|
|
11
|
+
github.com/microsoft/typescript-go/shim/parser v0.0.0
|
|
12
|
+
github.com/microsoft/typescript-go/shim/tsoptions v0.0.0
|
|
13
|
+
github.com/microsoft/typescript-go/shim/tspath v0.0.0
|
|
14
|
+
github.com/microsoft/typescript-go/shim/vfs/cachedvfs v0.0.0
|
|
15
|
+
github.com/microsoft/typescript-go/shim/vfs/osvfs v0.0.0
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
require (
|
|
19
|
+
github.com/go-json-experiment/json v0.0.0-20260214004413-d219187c3433 // indirect
|
|
20
|
+
github.com/klauspost/cpuid/v2 v2.2.10 // indirect
|
|
21
|
+
github.com/microsoft/typescript-go v0.0.0-20260424234512-515d036f927a // indirect
|
|
22
|
+
github.com/zeebo/xxh3 v1.1.0 // indirect
|
|
23
|
+
golang.org/x/sync v0.20.0 // indirect
|
|
24
|
+
golang.org/x/sys v0.42.0 // indirect
|
|
25
|
+
golang.org/x/text v0.35.0 // indirect
|
|
26
|
+
)
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
package main
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"fmt"
|
|
5
|
+
"os"
|
|
6
|
+
|
|
7
|
+
"github.com/samchon/ttsc/packages/paths/go-plugin/paths"
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
const version = "0.0.1"
|
|
11
|
+
|
|
12
|
+
func main() {
|
|
13
|
+
os.Exit(run(os.Args[1:]))
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
func run(args []string) int {
|
|
17
|
+
if len(args) == 0 {
|
|
18
|
+
fmt.Fprintln(os.Stderr, "@ttsc/paths: command required (expected output|version)")
|
|
19
|
+
return 2
|
|
20
|
+
}
|
|
21
|
+
switch args[0] {
|
|
22
|
+
case "-v", "--version", "version":
|
|
23
|
+
fmt.Fprintf(os.Stdout, "@ttsc/paths %s\n", version)
|
|
24
|
+
return 0
|
|
25
|
+
case "check":
|
|
26
|
+
return 0
|
|
27
|
+
case "output":
|
|
28
|
+
return paths.RunOutput(args[1:])
|
|
29
|
+
default:
|
|
30
|
+
fmt.Fprintf(os.Stderr, "@ttsc/paths: unknown command %q\n", args[0])
|
|
31
|
+
return 2
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,635 @@
|
|
|
1
|
+
package paths
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"encoding/json"
|
|
5
|
+
"errors"
|
|
6
|
+
"flag"
|
|
7
|
+
"fmt"
|
|
8
|
+
"os"
|
|
9
|
+
"path/filepath"
|
|
10
|
+
"sort"
|
|
11
|
+
"strings"
|
|
12
|
+
|
|
13
|
+
shimast "github.com/microsoft/typescript-go/shim/ast"
|
|
14
|
+
"github.com/microsoft/typescript-go/shim/bundled"
|
|
15
|
+
shimcompiler "github.com/microsoft/typescript-go/shim/compiler"
|
|
16
|
+
shimcore "github.com/microsoft/typescript-go/shim/core"
|
|
17
|
+
shimdw "github.com/microsoft/typescript-go/shim/diagnosticwriter"
|
|
18
|
+
shimparser "github.com/microsoft/typescript-go/shim/parser"
|
|
19
|
+
"github.com/microsoft/typescript-go/shim/tsoptions"
|
|
20
|
+
shimtspath "github.com/microsoft/typescript-go/shim/tspath"
|
|
21
|
+
"github.com/microsoft/typescript-go/shim/vfs/cachedvfs"
|
|
22
|
+
"github.com/microsoft/typescript-go/shim/vfs/osvfs"
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
const modePaths = "ttsc-paths"
|
|
26
|
+
|
|
27
|
+
type pluginEntry struct {
|
|
28
|
+
Config map[string]any `json:"config"`
|
|
29
|
+
Mode string `json:"mode"`
|
|
30
|
+
Name string `json:"name"`
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
type program struct {
|
|
34
|
+
cwd string
|
|
35
|
+
parsed *tsoptions.ParsedCommandLine
|
|
36
|
+
tsProgram *shimcompiler.Program
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
type pathsResolver struct {
|
|
40
|
+
basePath string
|
|
41
|
+
outDir string
|
|
42
|
+
patterns []pathsPattern
|
|
43
|
+
rootDir string
|
|
44
|
+
sourceFiles map[string]string
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
type pathsPattern struct {
|
|
48
|
+
pattern string
|
|
49
|
+
targets []string
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
type textEdit struct {
|
|
53
|
+
start int
|
|
54
|
+
end int
|
|
55
|
+
text string
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
func RunOutput(args []string) int {
|
|
59
|
+
fs := flag.NewFlagSet("output", flag.ContinueOnError)
|
|
60
|
+
fs.SetOutput(os.Stderr)
|
|
61
|
+
file := fs.String("file", "", "emitted file to transform")
|
|
62
|
+
out := fs.String("out", "", "write transformed text to this file instead of updating --file")
|
|
63
|
+
cwd := fs.String("cwd", "", "project directory")
|
|
64
|
+
outDir := fs.String("outDir", "", "emit directory override")
|
|
65
|
+
pluginsJSON := fs.String("plugins-json", "", "ttsc plugin manifest JSON")
|
|
66
|
+
_ = fs.String("rewrite-mode", modePaths, "native mode")
|
|
67
|
+
tsconfig := fs.String("tsconfig", "tsconfig.json", "project tsconfig")
|
|
68
|
+
if err := fs.Parse(args); err != nil {
|
|
69
|
+
return 2
|
|
70
|
+
}
|
|
71
|
+
if *file == "" {
|
|
72
|
+
fmt.Fprintln(os.Stderr, "@ttsc/paths: output requires --file")
|
|
73
|
+
return 2
|
|
74
|
+
}
|
|
75
|
+
if err := requireConfig(*pluginsJSON); err != nil {
|
|
76
|
+
fmt.Fprintln(os.Stderr, err)
|
|
77
|
+
return 2
|
|
78
|
+
}
|
|
79
|
+
resolvedCwd, err := resolveCwd(*cwd)
|
|
80
|
+
if err != nil {
|
|
81
|
+
fmt.Fprintln(os.Stderr, err)
|
|
82
|
+
return 2
|
|
83
|
+
}
|
|
84
|
+
prog, parseDiags, err := loadProgram(resolvedCwd, *tsconfig, *outDir)
|
|
85
|
+
if err != nil {
|
|
86
|
+
fmt.Fprintf(os.Stderr, "@ttsc/paths: %v\n", err)
|
|
87
|
+
return 2
|
|
88
|
+
}
|
|
89
|
+
if len(parseDiags) > 0 {
|
|
90
|
+
shimdw.FormatASTDiagnosticsWithColorAndContext(os.Stderr, parseDiags, resolvedCwd)
|
|
91
|
+
return 2
|
|
92
|
+
}
|
|
93
|
+
text, err := os.ReadFile(*file)
|
|
94
|
+
if err != nil {
|
|
95
|
+
fmt.Fprintf(os.Stderr, "@ttsc/paths: read %s: %v\n", *file, err)
|
|
96
|
+
return 2
|
|
97
|
+
}
|
|
98
|
+
patched, err := Apply(prog, *file, string(text))
|
|
99
|
+
if err != nil {
|
|
100
|
+
fmt.Fprintln(os.Stderr, err)
|
|
101
|
+
return 2
|
|
102
|
+
}
|
|
103
|
+
target := *file
|
|
104
|
+
if *out != "" {
|
|
105
|
+
target = *out
|
|
106
|
+
}
|
|
107
|
+
if err := os.MkdirAll(filepath.Dir(target), 0o755); err != nil {
|
|
108
|
+
fmt.Fprintf(os.Stderr, "@ttsc/paths: mkdir: %v\n", err)
|
|
109
|
+
return 2
|
|
110
|
+
}
|
|
111
|
+
if err := os.WriteFile(target, []byte(patched), 0o644); err != nil {
|
|
112
|
+
fmt.Fprintf(os.Stderr, "@ttsc/paths: write %s: %v\n", target, err)
|
|
113
|
+
return 2
|
|
114
|
+
}
|
|
115
|
+
return 0
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
func Apply(prog *program, fileName string, text string) (string, error) {
|
|
119
|
+
resolver := newPathsResolver(prog)
|
|
120
|
+
return resolver.apply(fileName, text)
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
func loadProgram(cwd, tsconfigPath string, outDir string) (*program, []*shimast.Diagnostic, error) {
|
|
124
|
+
if !filepath.IsAbs(cwd) {
|
|
125
|
+
abs, err := filepath.Abs(cwd)
|
|
126
|
+
if err != nil {
|
|
127
|
+
return nil, nil, fmt.Errorf("cwd: %w", err)
|
|
128
|
+
}
|
|
129
|
+
cwd = abs
|
|
130
|
+
}
|
|
131
|
+
resolved := tsconfigPath
|
|
132
|
+
if !filepath.IsAbs(resolved) {
|
|
133
|
+
resolved = filepath.Join(cwd, resolved)
|
|
134
|
+
}
|
|
135
|
+
fs := bundled.WrapFS(cachedvfs.From(osvfs.FS()))
|
|
136
|
+
host := shimcompiler.NewCompilerHost(cwd, fs, bundled.LibPath(), nil, nil)
|
|
137
|
+
parsed, parseDiags := tsoptions.GetParsedCommandLineOfConfigFile(
|
|
138
|
+
resolved,
|
|
139
|
+
&shimcore.CompilerOptions{},
|
|
140
|
+
nil,
|
|
141
|
+
host,
|
|
142
|
+
nil,
|
|
143
|
+
)
|
|
144
|
+
if parsed == nil {
|
|
145
|
+
return nil, nil, fmt.Errorf("tsoptions: parsed command line was nil for %s", resolved)
|
|
146
|
+
}
|
|
147
|
+
if len(parseDiags) > 0 {
|
|
148
|
+
return nil, parseDiags, nil
|
|
149
|
+
}
|
|
150
|
+
if len(parsed.Errors) > 0 {
|
|
151
|
+
return nil, parsed.Errors, nil
|
|
152
|
+
}
|
|
153
|
+
if outDir != "" {
|
|
154
|
+
overrideOutDir(cwd, parsed, outDir)
|
|
155
|
+
}
|
|
156
|
+
tsProgram := shimcompiler.NewProgram(shimcompiler.ProgramOptions{
|
|
157
|
+
Config: parsed,
|
|
158
|
+
SingleThreaded: shimcore.TSTrue,
|
|
159
|
+
Host: host,
|
|
160
|
+
UseSourceOfProjectReference: true,
|
|
161
|
+
})
|
|
162
|
+
if tsProgram == nil {
|
|
163
|
+
return nil, nil, errors.New("compiler.NewProgram returned nil")
|
|
164
|
+
}
|
|
165
|
+
return &program{cwd: cwd, parsed: parsed, tsProgram: tsProgram}, nil, nil
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
func (p *program) userSourceFiles() []*shimast.SourceFile {
|
|
169
|
+
out := make([]*shimast.SourceFile, 0)
|
|
170
|
+
for _, f := range p.tsProgram.SourceFiles() {
|
|
171
|
+
if f == nil || f.IsDeclarationFile {
|
|
172
|
+
continue
|
|
173
|
+
}
|
|
174
|
+
out = append(out, f)
|
|
175
|
+
}
|
|
176
|
+
return out
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
func newPathsResolver(prog *program) *pathsResolver {
|
|
180
|
+
resolver := &pathsResolver{sourceFiles: map[string]string{}}
|
|
181
|
+
if prog == nil || prog.parsed == nil || prog.parsed.ParsedConfig == nil || prog.parsed.ParsedConfig.CompilerOptions == nil {
|
|
182
|
+
return resolver
|
|
183
|
+
}
|
|
184
|
+
options := prog.parsed.ParsedConfig.CompilerOptions
|
|
185
|
+
resolver.basePath = options.GetPathsBasePath(prog.cwd)
|
|
186
|
+
resolver.outDir = normalizeOptionalPath(options.OutDir, prog.cwd)
|
|
187
|
+
resolver.rootDir = normalizeOptionalPath(options.RootDir, prog.cwd)
|
|
188
|
+
files := prog.userSourceFiles()
|
|
189
|
+
if resolver.rootDir == "" {
|
|
190
|
+
resolver.rootDir = commonSourceDir(files)
|
|
191
|
+
}
|
|
192
|
+
for _, file := range files {
|
|
193
|
+
if file == nil {
|
|
194
|
+
continue
|
|
195
|
+
}
|
|
196
|
+
name := normalizePath(file.FileName())
|
|
197
|
+
resolver.sourceFiles[name] = name
|
|
198
|
+
resolver.sourceFiles[stripKnownSourceExtension(name)] = name
|
|
199
|
+
}
|
|
200
|
+
if options.Paths != nil {
|
|
201
|
+
for pattern, targets := range options.Paths.Entries() {
|
|
202
|
+
resolver.patterns = append(resolver.patterns, pathsPattern{
|
|
203
|
+
pattern: pattern,
|
|
204
|
+
targets: append([]string(nil), targets...),
|
|
205
|
+
})
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
sort.SliceStable(resolver.patterns, func(i, j int) bool {
|
|
209
|
+
return pathsPatternRank(resolver.patterns[i].pattern) > pathsPatternRank(resolver.patterns[j].pattern)
|
|
210
|
+
})
|
|
211
|
+
return resolver
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
func (r *pathsResolver) apply(fileName string, text string) (string, error) {
|
|
215
|
+
if r == nil || len(r.patterns) == 0 || !isPathsOutput(fileName) {
|
|
216
|
+
return text, nil
|
|
217
|
+
}
|
|
218
|
+
file := parseModuleSpecifierFile(fileName, text)
|
|
219
|
+
if file == nil {
|
|
220
|
+
return text, nil
|
|
221
|
+
}
|
|
222
|
+
edits := make([]textEdit, 0)
|
|
223
|
+
addEdit := func(lit *shimast.Node) {
|
|
224
|
+
if lit == nil || lit.Kind != shimast.KindStringLiteral {
|
|
225
|
+
return
|
|
226
|
+
}
|
|
227
|
+
specifier := lit.Text()
|
|
228
|
+
rewritten, ok := r.rewriteSpecifier(fileName, specifier)
|
|
229
|
+
if !ok || rewritten == specifier {
|
|
230
|
+
return
|
|
231
|
+
}
|
|
232
|
+
start, end, quote, ok := stringLiteralRange(text, lit)
|
|
233
|
+
if !ok {
|
|
234
|
+
return
|
|
235
|
+
}
|
|
236
|
+
edits = append(edits, textEdit{
|
|
237
|
+
start: start,
|
|
238
|
+
end: end,
|
|
239
|
+
text: quoteJSString(quote, rewritten),
|
|
240
|
+
})
|
|
241
|
+
}
|
|
242
|
+
var walk func(*shimast.Node)
|
|
243
|
+
walk = func(node *shimast.Node) {
|
|
244
|
+
if node == nil {
|
|
245
|
+
return
|
|
246
|
+
}
|
|
247
|
+
switch node.Kind {
|
|
248
|
+
case shimast.KindImportDeclaration:
|
|
249
|
+
addEdit(node.AsImportDeclaration().ModuleSpecifier)
|
|
250
|
+
case shimast.KindExportDeclaration:
|
|
251
|
+
addEdit(node.AsExportDeclaration().ModuleSpecifier)
|
|
252
|
+
case shimast.KindImportEqualsDeclaration:
|
|
253
|
+
ref := node.AsImportEqualsDeclaration().ModuleReference
|
|
254
|
+
if ref != nil && ref.Kind == shimast.KindExternalModuleReference {
|
|
255
|
+
addEdit(ref.AsExternalModuleReference().Expression)
|
|
256
|
+
}
|
|
257
|
+
case shimast.KindImportType:
|
|
258
|
+
arg := node.AsImportTypeNode().Argument
|
|
259
|
+
if arg != nil && arg.Kind == shimast.KindLiteralType {
|
|
260
|
+
addEdit(arg.AsLiteralTypeNode().Literal)
|
|
261
|
+
}
|
|
262
|
+
case shimast.KindModuleDeclaration:
|
|
263
|
+
addEdit(node.AsModuleDeclaration().Name())
|
|
264
|
+
case shimast.KindCallExpression:
|
|
265
|
+
call := node.AsCallExpression()
|
|
266
|
+
if call != nil && (isRequireCall(call) || isDynamicImportCall(call)) && call.Arguments != nil && len(call.Arguments.Nodes) > 0 {
|
|
267
|
+
addEdit(call.Arguments.Nodes[0])
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
node.ForEachChild(func(child *shimast.Node) bool {
|
|
271
|
+
walk(child)
|
|
272
|
+
return false
|
|
273
|
+
})
|
|
274
|
+
}
|
|
275
|
+
for _, stmt := range file.Statements.Nodes {
|
|
276
|
+
walk(stmt)
|
|
277
|
+
}
|
|
278
|
+
return applyTextEdits(text, edits), nil
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
func (r *pathsResolver) rewriteSpecifier(outputFile string, specifier string) (string, bool) {
|
|
282
|
+
if isExternalModuleNameRelative(specifier) || strings.HasPrefix(specifier, "/") {
|
|
283
|
+
return specifier, false
|
|
284
|
+
}
|
|
285
|
+
for _, pattern := range r.patterns {
|
|
286
|
+
capture, ok := matchPathPattern(pattern.pattern, specifier)
|
|
287
|
+
if !ok || len(pattern.targets) == 0 {
|
|
288
|
+
continue
|
|
289
|
+
}
|
|
290
|
+
for _, targetPattern := range pattern.targets {
|
|
291
|
+
target := strings.ReplaceAll(targetPattern, "*", capture)
|
|
292
|
+
source, ok := r.resolveTargetSource(target)
|
|
293
|
+
if !ok {
|
|
294
|
+
continue
|
|
295
|
+
}
|
|
296
|
+
targetOutput := r.outputPathForSource(source)
|
|
297
|
+
relative, err := filepath.Rel(filepath.Dir(normalizePath(outputFile)), targetOutput)
|
|
298
|
+
if err != nil {
|
|
299
|
+
return specifier, false
|
|
300
|
+
}
|
|
301
|
+
relative = filepath.ToSlash(relative)
|
|
302
|
+
if relative == "." {
|
|
303
|
+
relative = "./" + filepath.Base(targetOutput)
|
|
304
|
+
}
|
|
305
|
+
if !strings.HasPrefix(relative, ".") {
|
|
306
|
+
relative = "./" + relative
|
|
307
|
+
}
|
|
308
|
+
return relative, true
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
return specifier, false
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
func (r *pathsResolver) resolveTargetSource(target string) (string, bool) {
|
|
315
|
+
base := r.basePath
|
|
316
|
+
if base == "" {
|
|
317
|
+
base = "."
|
|
318
|
+
}
|
|
319
|
+
raw := normalizePath(filepath.Join(base, target))
|
|
320
|
+
candidates := []string{raw}
|
|
321
|
+
if stripKnownSourceExtension(raw) == raw {
|
|
322
|
+
for _, ext := range knownResolvableExtensions() {
|
|
323
|
+
candidates = append(candidates, raw+ext)
|
|
324
|
+
}
|
|
325
|
+
for _, ext := range knownResolvableExtensions() {
|
|
326
|
+
candidates = append(candidates, filepath.ToSlash(filepath.Join(raw, "index"+ext)))
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
for _, candidate := range candidates {
|
|
330
|
+
normalized := normalizePath(candidate)
|
|
331
|
+
if source, ok := r.sourceFiles[normalized]; ok {
|
|
332
|
+
return source, true
|
|
333
|
+
}
|
|
334
|
+
if source, ok := r.sourceFiles[stripKnownSourceExtension(normalized)]; ok {
|
|
335
|
+
return source, true
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
return "", false
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
func (r *pathsResolver) outputPathForSource(source string) string {
|
|
342
|
+
outputExt := outputExtensionForSource(source)
|
|
343
|
+
if r.outDir == "" {
|
|
344
|
+
return changeExtension(source, outputExt)
|
|
345
|
+
}
|
|
346
|
+
if r.rootDir != "" {
|
|
347
|
+
if rel, err := filepath.Rel(r.rootDir, source); err == nil && !strings.HasPrefix(rel, "..") && !filepath.IsAbs(rel) {
|
|
348
|
+
return normalizePath(filepath.Join(r.outDir, changeExtension(rel, outputExt)))
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
return normalizePath(filepath.Join(r.outDir, filepath.Base(changeExtension(source, outputExt))))
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
func requireConfig(pluginsJSON string) error {
|
|
355
|
+
if strings.TrimSpace(pluginsJSON) == "" {
|
|
356
|
+
return fmt.Errorf("@ttsc/paths: missing --plugins-json")
|
|
357
|
+
}
|
|
358
|
+
var entries []pluginEntry
|
|
359
|
+
if err := json.Unmarshal([]byte(pluginsJSON), &entries); err != nil {
|
|
360
|
+
return fmt.Errorf("@ttsc/paths: invalid --plugins-json: %w", err)
|
|
361
|
+
}
|
|
362
|
+
for _, entry := range entries {
|
|
363
|
+
if entry.Mode == modePaths || entry.Name == "@ttsc/paths" {
|
|
364
|
+
return nil
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
return fmt.Errorf("@ttsc/paths: plugin entry not found")
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
func resolveCwd(override string) (string, error) {
|
|
371
|
+
if override != "" {
|
|
372
|
+
abs, err := filepath.Abs(override)
|
|
373
|
+
if err != nil {
|
|
374
|
+
return "", fmt.Errorf("@ttsc/paths: --cwd: %w", err)
|
|
375
|
+
}
|
|
376
|
+
return abs, nil
|
|
377
|
+
}
|
|
378
|
+
wd, err := os.Getwd()
|
|
379
|
+
if err != nil {
|
|
380
|
+
return "", fmt.Errorf("@ttsc/paths: cwd: %w", err)
|
|
381
|
+
}
|
|
382
|
+
return wd, nil
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
func overrideOutDir(cwd string, parsed *tsoptions.ParsedCommandLine, outDir string) {
|
|
386
|
+
if parsed == nil || parsed.ParsedConfig == nil || parsed.ParsedConfig.CompilerOptions == nil {
|
|
387
|
+
return
|
|
388
|
+
}
|
|
389
|
+
if filepath.IsAbs(outDir) {
|
|
390
|
+
parsed.ParsedConfig.CompilerOptions.OutDir = filepath.ToSlash(outDir)
|
|
391
|
+
return
|
|
392
|
+
}
|
|
393
|
+
parsed.ParsedConfig.CompilerOptions.OutDir = filepath.ToSlash(filepath.Join(cwd, outDir))
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
func parseModuleSpecifierFile(fileName string, text string) *shimast.SourceFile {
|
|
397
|
+
normalized := normalizePath(fileName)
|
|
398
|
+
if !filepath.IsAbs(normalized) {
|
|
399
|
+
if abs, err := filepath.Abs(normalized); err == nil {
|
|
400
|
+
normalized = normalizePath(abs)
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
opts := shimast.SourceFileParseOptions{FileName: normalized}
|
|
404
|
+
kind := shimcore.ScriptKindJS
|
|
405
|
+
if isDeclarationOutput(fileName) {
|
|
406
|
+
kind = shimcore.ScriptKindTS
|
|
407
|
+
}
|
|
408
|
+
return shimparser.ParseSourceFile(opts, text, kind)
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
func isRequireCall(call *shimast.CallExpression) bool {
|
|
412
|
+
if call == nil || call.Expression == nil || call.Expression.Kind != shimast.KindIdentifier {
|
|
413
|
+
return false
|
|
414
|
+
}
|
|
415
|
+
return call.Expression.Text() == "require"
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
func isDynamicImportCall(call *shimast.CallExpression) bool {
|
|
419
|
+
if call == nil || call.Expression == nil || call.Expression.Kind != shimast.KindImportKeyword {
|
|
420
|
+
return false
|
|
421
|
+
}
|
|
422
|
+
return call.Arguments != nil && len(call.Arguments.Nodes) == 1
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
func stringLiteralRange(text string, node *shimast.Node) (int, int, byte, bool) {
|
|
426
|
+
start := clamp(node.Pos(), 0, len(text))
|
|
427
|
+
end := clamp(node.End(), start, len(text))
|
|
428
|
+
for start < end && text[start] != '"' && text[start] != '\'' {
|
|
429
|
+
start++
|
|
430
|
+
}
|
|
431
|
+
if start >= end {
|
|
432
|
+
return 0, 0, 0, false
|
|
433
|
+
}
|
|
434
|
+
quote := text[start]
|
|
435
|
+
for end > start+1 && text[end-1] != quote {
|
|
436
|
+
end--
|
|
437
|
+
}
|
|
438
|
+
if end <= start+1 {
|
|
439
|
+
return 0, 0, 0, false
|
|
440
|
+
}
|
|
441
|
+
return start, end, quote, true
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
func quoteJSString(quote byte, value string) string {
|
|
445
|
+
var b strings.Builder
|
|
446
|
+
b.WriteByte(quote)
|
|
447
|
+
for _, r := range value {
|
|
448
|
+
switch r {
|
|
449
|
+
case '\\':
|
|
450
|
+
b.WriteString(`\\`)
|
|
451
|
+
case '\n':
|
|
452
|
+
b.WriteString(`\n`)
|
|
453
|
+
case '\r':
|
|
454
|
+
b.WriteString(`\r`)
|
|
455
|
+
case '\t':
|
|
456
|
+
b.WriteString(`\t`)
|
|
457
|
+
default:
|
|
458
|
+
if byte(r) == quote && r < utf8RuneSelf {
|
|
459
|
+
b.WriteByte('\\')
|
|
460
|
+
b.WriteByte(byte(r))
|
|
461
|
+
} else {
|
|
462
|
+
b.WriteRune(r)
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
b.WriteByte(quote)
|
|
467
|
+
return b.String()
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
const utf8RuneSelf = 0x80
|
|
471
|
+
|
|
472
|
+
func applyTextEdits(text string, edits []textEdit) string {
|
|
473
|
+
if len(edits) == 0 {
|
|
474
|
+
return text
|
|
475
|
+
}
|
|
476
|
+
sort.SliceStable(edits, func(i, j int) bool {
|
|
477
|
+
if edits[i].start == edits[j].start {
|
|
478
|
+
return edits[i].end > edits[j].end
|
|
479
|
+
}
|
|
480
|
+
return edits[i].start > edits[j].start
|
|
481
|
+
})
|
|
482
|
+
out := text
|
|
483
|
+
lastStart := len(text) + 1
|
|
484
|
+
for _, edit := range edits {
|
|
485
|
+
if edit.start < 0 || edit.end < edit.start || edit.start > len(out) {
|
|
486
|
+
continue
|
|
487
|
+
}
|
|
488
|
+
if edit.end > lastStart {
|
|
489
|
+
edit.end = lastStart
|
|
490
|
+
}
|
|
491
|
+
if edit.end > len(out) {
|
|
492
|
+
edit.end = len(out)
|
|
493
|
+
}
|
|
494
|
+
out = out[:edit.start] + edit.text + out[edit.end:]
|
|
495
|
+
lastStart = edit.start
|
|
496
|
+
}
|
|
497
|
+
return out
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
func matchPathPattern(pattern string, specifier string) (string, bool) {
|
|
501
|
+
star := strings.Index(pattern, "*")
|
|
502
|
+
if star < 0 {
|
|
503
|
+
return "", pattern == specifier
|
|
504
|
+
}
|
|
505
|
+
prefix := pattern[:star]
|
|
506
|
+
suffix := pattern[star+1:]
|
|
507
|
+
if !strings.HasPrefix(specifier, prefix) || !strings.HasSuffix(specifier, suffix) {
|
|
508
|
+
return "", false
|
|
509
|
+
}
|
|
510
|
+
return specifier[len(prefix) : len(specifier)-len(suffix)], true
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
func commonSourceDir(files []*shimast.SourceFile) string {
|
|
514
|
+
var common string
|
|
515
|
+
for _, file := range files {
|
|
516
|
+
if file == nil {
|
|
517
|
+
continue
|
|
518
|
+
}
|
|
519
|
+
dir := filepath.Dir(normalizePath(file.FileName()))
|
|
520
|
+
if common == "" {
|
|
521
|
+
common = dir
|
|
522
|
+
continue
|
|
523
|
+
}
|
|
524
|
+
common = commonPathPrefix(common, dir)
|
|
525
|
+
}
|
|
526
|
+
return common
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
func commonPathPrefix(a string, b string) string {
|
|
530
|
+
aParts := strings.Split(normalizePath(a), "/")
|
|
531
|
+
bParts := strings.Split(normalizePath(b), "/")
|
|
532
|
+
n := len(aParts)
|
|
533
|
+
if len(bParts) < n {
|
|
534
|
+
n = len(bParts)
|
|
535
|
+
}
|
|
536
|
+
i := 0
|
|
537
|
+
for i < n && aParts[i] == bParts[i] {
|
|
538
|
+
i++
|
|
539
|
+
}
|
|
540
|
+
if i == 0 {
|
|
541
|
+
return ""
|
|
542
|
+
}
|
|
543
|
+
return strings.Join(aParts[:i], "/")
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
func normalizeOptionalPath(value string, cwd string) string {
|
|
547
|
+
if value == "" {
|
|
548
|
+
return ""
|
|
549
|
+
}
|
|
550
|
+
if filepath.IsAbs(value) {
|
|
551
|
+
return normalizePath(value)
|
|
552
|
+
}
|
|
553
|
+
return normalizePath(filepath.Join(cwd, value))
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
func normalizePath(value string) string {
|
|
557
|
+
return filepath.ToSlash(shimtspath.NormalizePath(value))
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
func stripKnownSourceExtension(value string) string {
|
|
561
|
+
for _, ext := range []string{".d.ts", ".d.mts", ".d.cts", ".tsx", ".ts", ".mts", ".cts", ".jsx", ".js", ".json"} {
|
|
562
|
+
if strings.HasSuffix(value, ext) {
|
|
563
|
+
return strings.TrimSuffix(value, ext)
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
return value
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
func outputExtensionForSource(source string) string {
|
|
570
|
+
switch strings.ToLower(filepath.Ext(source)) {
|
|
571
|
+
case ".mts":
|
|
572
|
+
return ".mjs"
|
|
573
|
+
case ".cts":
|
|
574
|
+
return ".cjs"
|
|
575
|
+
case ".json":
|
|
576
|
+
return ".json"
|
|
577
|
+
default:
|
|
578
|
+
return ".js"
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
func changeExtension(value string, ext string) string {
|
|
583
|
+
return strings.TrimSuffix(value, filepath.Ext(value)) + ext
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
func isExternalModuleNameRelative(specifier string) bool {
|
|
587
|
+
return strings.HasPrefix(specifier, "./") ||
|
|
588
|
+
strings.HasPrefix(specifier, "../") ||
|
|
589
|
+
specifier == "." ||
|
|
590
|
+
specifier == ".."
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
func isPathsOutput(fileName string) bool {
|
|
594
|
+
return isJavaScriptOutput(fileName) || isDeclarationOutput(fileName)
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
func isJavaScriptOutput(fileName string) bool {
|
|
598
|
+
switch strings.ToLower(filepath.Ext(fileName)) {
|
|
599
|
+
case ".js", ".mjs", ".cjs":
|
|
600
|
+
return true
|
|
601
|
+
default:
|
|
602
|
+
return false
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
func isDeclarationOutput(fileName string) bool {
|
|
607
|
+
lower := strings.ToLower(fileName)
|
|
608
|
+
return strings.HasSuffix(lower, ".d.ts") ||
|
|
609
|
+
strings.HasSuffix(lower, ".d.mts") ||
|
|
610
|
+
strings.HasSuffix(lower, ".d.cts")
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
func knownResolvableExtensions() []string {
|
|
614
|
+
return []string{".ts", ".tsx", ".mts", ".cts", ".js", ".jsx", ".json"}
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
func pathsPatternRank(pattern string) int {
|
|
618
|
+
star := strings.Index(pattern, "*")
|
|
619
|
+
if star < 0 {
|
|
620
|
+
return 1_000_000 + len(pattern)
|
|
621
|
+
}
|
|
622
|
+
prefix := len(pattern[:star])
|
|
623
|
+
suffix := len(pattern[star+1:])
|
|
624
|
+
return prefix*1_000 + suffix*10 + len(pattern)
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
func clamp(value int, min int, max int) int {
|
|
628
|
+
if value < min {
|
|
629
|
+
return min
|
|
630
|
+
}
|
|
631
|
+
if value > max {
|
|
632
|
+
return max
|
|
633
|
+
}
|
|
634
|
+
return value
|
|
635
|
+
}
|
package/index.cjs
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
"use strict";
|
|
3
|
+
|
|
4
|
+
const path = require("node:path");
|
|
5
|
+
|
|
6
|
+
module.exports = function createTtscPaths() {
|
|
7
|
+
return {
|
|
8
|
+
name: "@ttsc/paths",
|
|
9
|
+
native: {
|
|
10
|
+
mode: "ttsc-paths",
|
|
11
|
+
source: {
|
|
12
|
+
dir: path.resolve(__dirname, "go-plugin"),
|
|
13
|
+
},
|
|
14
|
+
contractVersion: 1,
|
|
15
|
+
capabilities: ["output"],
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ttsc/paths",
|
|
3
|
+
"version": "0.5.0-dev.20260429",
|
|
4
|
+
"description": "First-party ttsc plugin that rewrites emitted module specifiers from tsconfig paths.",
|
|
5
|
+
"main": "index.cjs",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": "./index.cjs",
|
|
8
|
+
"./package.json": "./package.json"
|
|
9
|
+
},
|
|
10
|
+
"keywords": [
|
|
11
|
+
"ttsc",
|
|
12
|
+
"paths",
|
|
13
|
+
"typescript",
|
|
14
|
+
"tsgo"
|
|
15
|
+
],
|
|
16
|
+
"files": [
|
|
17
|
+
"README.md",
|
|
18
|
+
"index.cjs",
|
|
19
|
+
"go-plugin/go.mod",
|
|
20
|
+
"go-plugin/main.go",
|
|
21
|
+
"go-plugin/paths"
|
|
22
|
+
],
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"ttsc": "^0.4.4"
|
|
25
|
+
},
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "https://github.com/samchon/ttsc"
|
|
29
|
+
},
|
|
30
|
+
"author": "Jeongho Nam",
|
|
31
|
+
"license": "MIT",
|
|
32
|
+
"bugs": {
|
|
33
|
+
"url": "https://github.com/samchon/ttsc/issues"
|
|
34
|
+
},
|
|
35
|
+
"homepage": "https://github.com/samchon/ttsc/tree/master/packages/paths#readme"
|
|
36
|
+
}
|