@ttsc/paths 0.13.1 → 0.14.0-dev.20260528.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/driver/paths.go +21 -14
  2. package/package.json +1 -1
package/driver/paths.go CHANGED
@@ -32,6 +32,7 @@ func (plugin) ApplyProgram(prog *driver.Program, _ driver.PluginContext) error {
32
32
  // module specifiers across an entire program.
33
33
  type rewriter struct {
34
34
  basePath string
35
+ jsxPreserve bool
35
36
  outDir string
36
37
  patterns []pathPattern
37
38
  rootDir string
@@ -45,6 +46,11 @@ type pathPattern struct {
45
46
  targets []string
46
47
  }
47
48
 
49
+ var sourceLookupExtensions = []string{
50
+ ".ts", ".tsx", ".mts", ".cts",
51
+ ".js", ".jsx", ".mjs", ".cjs",
52
+ }
53
+
48
54
  // newRewriter builds a rewriter from the program's compiler options.
49
55
  // Patterns are sorted by decreasing specificity (longer literal prefix first)
50
56
  // so the most-specific match wins on overlapping patterns.
@@ -55,6 +61,7 @@ func newRewriter(prog *driver.Program) *rewriter {
55
61
  }
56
62
  options := prog.ParsedConfig.ParsedConfig.CompilerOptions
57
63
  out.basePath = filepath.Clean(options.GetPathsBasePath(prog.Host.GetCurrentDirectory()))
64
+ out.jsxPreserve = options.Jsx == shimcore.JsxEmitPreserve
58
65
  out.outDir = optionalPath(options.OutDir, prog.Host.GetCurrentDirectory())
59
66
  out.rootDir = optionalPath(options.RootDir, prog.Host.GetCurrentDirectory())
60
67
  files := prog.SourceFiles()
@@ -64,7 +71,6 @@ func newRewriter(prog *driver.Program) *rewriter {
64
71
  for _, file := range files {
65
72
  name := normalizePath(file.FileName())
66
73
  out.sourceFiles[name] = name
67
- out.sourceFiles[stripKnownSourceExtension(name)] = name
68
74
  }
69
75
  if options.Paths != nil {
70
76
  for key, targets := range options.Paths.Entries() {
@@ -200,22 +206,19 @@ func (r *rewriter) resolveSource(specifier string) (string, bool) {
200
206
  }
201
207
 
202
208
  // lookupSource checks whether candidate (a normalized path, possibly without
203
- // extension) corresponds to a known source file. It tries the exact path, the
204
- // extension-stripped stem, stem with each TS extension, and index files.
209
+ // extension) corresponds to a known source file. It tries the exact path, stem
210
+ // with each known TypeScript/JavaScript source extension, and index files.
205
211
  func (r *rewriter) lookupSource(candidate string) (string, bool) {
206
212
  if source, ok := r.sourceFiles[normalizePath(candidate)]; ok {
207
213
  return source, true
208
214
  }
209
215
  stem := stripKnownSourceExtension(normalizePath(candidate))
210
- if source, ok := r.sourceFiles[stem]; ok {
211
- return source, true
212
- }
213
- for _, ext := range []string{".ts", ".tsx", ".mts", ".cts"} {
216
+ for _, ext := range sourceLookupExtensions {
214
217
  if source, ok := r.sourceFiles[stem+ext]; ok {
215
218
  return source, true
216
219
  }
217
220
  }
218
- for _, ext := range []string{".ts", ".tsx", ".mts", ".cts"} {
221
+ for _, ext := range sourceLookupExtensions {
219
222
  if source, ok := r.sourceFiles[normalizePath(filepath.Join(stem, "index"+ext))]; ok {
220
223
  return source, true
221
224
  }
@@ -234,18 +237,22 @@ func (r *rewriter) outputPathForSource(source string) string {
234
237
  if err != nil || isOutsideRelativePath(rel) {
235
238
  return ""
236
239
  }
237
- return normalizePath(filepath.Join(r.outDir, replaceSourceExtension(rel, emittedJavaScriptExtension(rel))))
240
+ return normalizePath(filepath.Join(r.outDir, replaceSourceExtension(rel, emittedJavaScriptExtension(rel, r.jsxPreserve))))
238
241
  }
239
242
 
240
243
  // emittedJavaScriptExtension returns the JavaScript file extension that TypeScript
241
- // emits for a given source path: ".mjs" for ".mts", ".cjs" for ".cts", ".js"
242
- // for all other extensions.
243
- func emittedJavaScriptExtension(source string) string {
244
+ // emits for a given source path.
245
+ func emittedJavaScriptExtension(source string, jsxPreserve bool) string {
244
246
  switch strings.ToLower(filepath.Ext(source)) {
245
- case ".mts":
247
+ case ".mts", ".mjs":
246
248
  return ".mjs"
247
- case ".cts":
249
+ case ".cts", ".cjs":
248
250
  return ".cjs"
251
+ case ".tsx", ".jsx":
252
+ if jsxPreserve {
253
+ return ".jsx"
254
+ }
255
+ return ".js"
249
256
  default:
250
257
  return ".js"
251
258
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ttsc/paths",
3
- "version": "0.13.1",
3
+ "version": "0.14.0-dev.20260528.1",
4
4
  "description": "First-party ttsc plugin that rewrites emitted module specifiers from tsconfig paths.",
5
5
  "main": "src/index.cjs",
6
6
  "exports": {