@tanstack/start-plugin-core 1.163.3 → 1.163.4

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 (47) hide show
  1. package/dist/esm/constants.d.ts +1 -0
  2. package/dist/esm/constants.js +2 -0
  3. package/dist/esm/constants.js.map +1 -1
  4. package/dist/esm/import-protection-plugin/ast.d.ts +3 -0
  5. package/dist/esm/import-protection-plugin/ast.js +8 -0
  6. package/dist/esm/import-protection-plugin/ast.js.map +1 -0
  7. package/dist/esm/import-protection-plugin/constants.d.ts +6 -0
  8. package/dist/esm/import-protection-plugin/constants.js +24 -0
  9. package/dist/esm/import-protection-plugin/constants.js.map +1 -0
  10. package/dist/esm/import-protection-plugin/extensionlessAbsoluteIdResolver.d.ts +22 -0
  11. package/dist/esm/import-protection-plugin/extensionlessAbsoluteIdResolver.js +95 -0
  12. package/dist/esm/import-protection-plugin/extensionlessAbsoluteIdResolver.js.map +1 -0
  13. package/dist/esm/import-protection-plugin/plugin.d.ts +2 -13
  14. package/dist/esm/import-protection-plugin/plugin.js +684 -299
  15. package/dist/esm/import-protection-plugin/plugin.js.map +1 -1
  16. package/dist/esm/import-protection-plugin/postCompileUsage.js +4 -2
  17. package/dist/esm/import-protection-plugin/postCompileUsage.js.map +1 -1
  18. package/dist/esm/import-protection-plugin/rewriteDeniedImports.d.ts +4 -5
  19. package/dist/esm/import-protection-plugin/rewriteDeniedImports.js +225 -3
  20. package/dist/esm/import-protection-plugin/rewriteDeniedImports.js.map +1 -1
  21. package/dist/esm/import-protection-plugin/sourceLocation.d.ts +4 -7
  22. package/dist/esm/import-protection-plugin/sourceLocation.js +18 -73
  23. package/dist/esm/import-protection-plugin/sourceLocation.js.map +1 -1
  24. package/dist/esm/import-protection-plugin/types.d.ts +94 -0
  25. package/dist/esm/import-protection-plugin/utils.d.ts +33 -1
  26. package/dist/esm/import-protection-plugin/utils.js +69 -3
  27. package/dist/esm/import-protection-plugin/utils.js.map +1 -1
  28. package/dist/esm/import-protection-plugin/virtualModules.d.ts +30 -2
  29. package/dist/esm/import-protection-plugin/virtualModules.js +66 -23
  30. package/dist/esm/import-protection-plugin/virtualModules.js.map +1 -1
  31. package/dist/esm/start-compiler-plugin/plugin.d.ts +2 -1
  32. package/dist/esm/start-compiler-plugin/plugin.js +1 -2
  33. package/dist/esm/start-compiler-plugin/plugin.js.map +1 -1
  34. package/package.json +4 -4
  35. package/src/constants.ts +2 -0
  36. package/src/import-protection-plugin/INTERNALS.md +462 -60
  37. package/src/import-protection-plugin/ast.ts +7 -0
  38. package/src/import-protection-plugin/constants.ts +25 -0
  39. package/src/import-protection-plugin/extensionlessAbsoluteIdResolver.ts +121 -0
  40. package/src/import-protection-plugin/plugin.ts +1080 -597
  41. package/src/import-protection-plugin/postCompileUsage.ts +8 -2
  42. package/src/import-protection-plugin/rewriteDeniedImports.ts +141 -9
  43. package/src/import-protection-plugin/sourceLocation.ts +19 -89
  44. package/src/import-protection-plugin/types.ts +103 -0
  45. package/src/import-protection-plugin/utils.ts +123 -4
  46. package/src/import-protection-plugin/virtualModules.ts +117 -31
  47. package/src/start-compiler-plugin/plugin.ts +7 -2
@@ -0,0 +1,121 @@
1
+ import { basename, dirname, extname, isAbsolute } from 'node:path'
2
+ import { resolveModulePath } from 'exsolve'
3
+
4
+ import { KNOWN_SOURCE_EXTENSIONS } from './constants'
5
+ import { normalizeFilePath } from './utils'
6
+
7
+ const FILE_RESOLUTION_EXTENSIONS = [...KNOWN_SOURCE_EXTENSIONS]
8
+
9
+ type DepKey = `file:${string}` | `dir:${string}`
10
+
11
+ /**
12
+ * Canonicalize extensionless absolute IDs like `/src/foo.server` to the
13
+ * physical file when possible.
14
+ *
15
+ * Keeps a small cache plus a reverse index so we can invalidate on HMR
16
+ * updates without clearing the whole map.
17
+ */
18
+ export class ExtensionlessAbsoluteIdResolver {
19
+ private entries = new Map<string, { value: string; deps: Set<DepKey> }>()
20
+ private keysByDep = new Map<DepKey, Set<string>>()
21
+
22
+ clear(): void {
23
+ this.entries.clear()
24
+ this.keysByDep.clear()
25
+ }
26
+
27
+ /**
28
+ * Invalidate any cached entries that might be affected by changes to `id`.
29
+ * We invalidate both the file and its containing directory.
30
+ */
31
+ invalidateByFile(id: string): void {
32
+ const file = normalizeFilePath(id)
33
+ this.invalidateDep(`file:${file}`)
34
+ if (isAbsolute(file)) {
35
+ this.invalidateDep(`dir:${dirname(file)}`)
36
+ }
37
+ }
38
+
39
+ resolve(id: string): string {
40
+ const key = normalizeFilePath(id)
41
+ const cached = this.entries.get(key)
42
+ if (cached) return cached.value
43
+
44
+ let result = key
45
+ let resolvedPhysical: string | undefined
46
+
47
+ if (isAbsolute(key)) {
48
+ const ext = extname(key)
49
+ if (!FILE_RESOLUTION_EXTENSIONS.includes(ext)) {
50
+ const resolved = resolveModulePath(`./${basename(key)}`, {
51
+ from: dirname(key),
52
+ extensions: FILE_RESOLUTION_EXTENSIONS,
53
+ try: true,
54
+ })
55
+ if (resolved) {
56
+ resolvedPhysical = resolved
57
+ result = normalizeFilePath(resolved)
58
+ }
59
+ }
60
+ }
61
+
62
+ const resolvedFile = resolvedPhysical
63
+ ? normalizeFilePath(resolvedPhysical)
64
+ : undefined
65
+
66
+ const deps = this.buildDepsForKey(key, resolvedFile)
67
+ this.entries.set(key, { value: result, deps })
68
+ this.indexDeps(key, deps)
69
+ return result
70
+ }
71
+
72
+ private invalidateDep(dep: DepKey): void {
73
+ const keys = this.keysByDep.get(dep)
74
+ if (!keys) return
75
+
76
+ // Copy because deleting keys mutates indexes.
77
+ for (const key of Array.from(keys)) {
78
+ this.deleteKey(key)
79
+ }
80
+ }
81
+
82
+ private buildDepsForKey(key: string, resolvedFile: string | undefined) {
83
+ const deps = new Set<DepKey>()
84
+ deps.add(`file:${key}`)
85
+
86
+ if (isAbsolute(key)) {
87
+ deps.add(`dir:${dirname(key)}`)
88
+ }
89
+ if (resolvedFile) {
90
+ deps.add(`file:${resolvedFile}`)
91
+ }
92
+
93
+ return deps
94
+ }
95
+
96
+ private indexDeps(key: string, deps: Set<DepKey>): void {
97
+ for (const dep of deps) {
98
+ let keys = this.keysByDep.get(dep)
99
+ if (!keys) {
100
+ keys = new Set<string>()
101
+ this.keysByDep.set(dep, keys)
102
+ }
103
+ keys.add(key)
104
+ }
105
+ }
106
+
107
+ private deleteKey(key: string): void {
108
+ const entry = this.entries.get(key)
109
+ this.entries.delete(key)
110
+ if (!entry) return
111
+
112
+ for (const dep of entry.deps) {
113
+ const keys = this.keysByDep.get(dep)
114
+ if (!keys) continue
115
+ keys.delete(key)
116
+ if (keys.size === 0) {
117
+ this.keysByDep.delete(dep)
118
+ }
119
+ }
120
+ }
121
+ }