@tanstack/start-plugin-core 1.167.35 → 1.169.0

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 (187) hide show
  1. package/dist/esm/import-protection/adapterUtils.d.ts +27 -0
  2. package/dist/esm/import-protection/adapterUtils.js +31 -0
  3. package/dist/esm/import-protection/adapterUtils.js.map +1 -0
  4. package/dist/esm/import-protection/analysis.d.ts +36 -0
  5. package/dist/esm/import-protection/analysis.js +407 -0
  6. package/dist/esm/import-protection/analysis.js.map +1 -0
  7. package/dist/esm/{import-protection-plugin → import-protection}/ast.js +1 -1
  8. package/dist/esm/import-protection/ast.js.map +1 -0
  9. package/dist/esm/import-protection/constants.d.ts +11 -0
  10. package/dist/esm/{import-protection-plugin → import-protection}/constants.js +7 -2
  11. package/dist/esm/import-protection/constants.js.map +1 -0
  12. package/dist/esm/{import-protection-plugin → import-protection}/defaults.js +1 -1
  13. package/dist/esm/import-protection/defaults.js.map +1 -0
  14. package/dist/esm/{import-protection-plugin → import-protection}/extensionlessAbsoluteIdResolver.js +2 -2
  15. package/dist/esm/import-protection/extensionlessAbsoluteIdResolver.js.map +1 -0
  16. package/dist/esm/{import-protection-plugin → import-protection}/matchers.js +1 -1
  17. package/dist/esm/import-protection/matchers.js.map +1 -0
  18. package/dist/esm/{import-protection-plugin/rewriteDeniedImports.d.ts → import-protection/rewrite.d.ts} +0 -4
  19. package/dist/esm/import-protection/rewrite.js +121 -0
  20. package/dist/esm/import-protection/rewrite.js.map +1 -0
  21. package/dist/esm/{import-protection-plugin → import-protection}/sourceLocation.d.ts +32 -3
  22. package/dist/esm/{import-protection-plugin → import-protection}/sourceLocation.js +65 -10
  23. package/dist/esm/import-protection/sourceLocation.js.map +1 -0
  24. package/dist/esm/{import-protection-plugin → import-protection}/trace.d.ts +0 -1
  25. package/dist/esm/{import-protection-plugin → import-protection}/trace.js +1 -1
  26. package/dist/esm/import-protection/trace.js.map +1 -0
  27. package/dist/esm/{import-protection-plugin → import-protection}/utils.d.ts +18 -1
  28. package/dist/esm/{import-protection-plugin → import-protection}/utils.js +13 -20
  29. package/dist/esm/import-protection/utils.js.map +1 -0
  30. package/dist/esm/import-protection/virtualModules.d.ts +25 -0
  31. package/dist/esm/{import-protection-plugin → import-protection}/virtualModules.js +5 -117
  32. package/dist/esm/import-protection/virtualModules.js.map +1 -0
  33. package/dist/esm/index.d.ts +1 -5
  34. package/dist/esm/index.js +2 -4
  35. package/dist/esm/post-build.d.ts +9 -0
  36. package/dist/esm/post-build.js +37 -0
  37. package/dist/esm/post-build.js.map +1 -0
  38. package/dist/esm/prerender.d.ts +11 -0
  39. package/dist/esm/prerender.js +159 -0
  40. package/dist/esm/prerender.js.map +1 -0
  41. package/dist/esm/rsbuild/dev-server.d.ts +21 -0
  42. package/dist/esm/rsbuild/dev-server.js +76 -0
  43. package/dist/esm/rsbuild/dev-server.js.map +1 -0
  44. package/dist/esm/rsbuild/import-protection.d.ts +10 -0
  45. package/dist/esm/rsbuild/import-protection.js +775 -0
  46. package/dist/esm/rsbuild/import-protection.js.map +1 -0
  47. package/dist/esm/rsbuild/index.d.ts +4 -0
  48. package/dist/esm/rsbuild/index.js +3 -0
  49. package/dist/esm/rsbuild/normalized-client-build.d.ts +18 -0
  50. package/dist/esm/rsbuild/normalized-client-build.js +207 -0
  51. package/dist/esm/rsbuild/normalized-client-build.js.map +1 -0
  52. package/dist/esm/rsbuild/planning.d.ts +52 -0
  53. package/dist/esm/rsbuild/planning.js +108 -0
  54. package/dist/esm/rsbuild/planning.js.map +1 -0
  55. package/dist/esm/rsbuild/plugin.d.ts +4 -0
  56. package/dist/esm/rsbuild/plugin.js +344 -0
  57. package/dist/esm/rsbuild/plugin.js.map +1 -0
  58. package/dist/esm/rsbuild/post-build.d.ts +6 -0
  59. package/dist/esm/rsbuild/post-build.js +57 -0
  60. package/dist/esm/rsbuild/post-build.js.map +1 -0
  61. package/dist/esm/rsbuild/schema.d.ts +3372 -0
  62. package/dist/esm/rsbuild/schema.js +12 -0
  63. package/dist/esm/rsbuild/schema.js.map +1 -0
  64. package/dist/esm/rsbuild/start-compiler-host.d.ts +20 -0
  65. package/dist/esm/rsbuild/start-compiler-host.js +150 -0
  66. package/dist/esm/rsbuild/start-compiler-host.js.map +1 -0
  67. package/dist/esm/rsbuild/start-router-plugin.d.ts +18 -0
  68. package/dist/esm/rsbuild/start-router-plugin.js +63 -0
  69. package/dist/esm/rsbuild/start-router-plugin.js.map +1 -0
  70. package/dist/esm/rsbuild/swc-rsc.d.ts +14 -0
  71. package/dist/esm/rsbuild/swc-rsc.js +93 -0
  72. package/dist/esm/rsbuild/swc-rsc.js.map +1 -0
  73. package/dist/esm/rsbuild/types.d.ts +17 -0
  74. package/dist/esm/rsbuild/types.js +0 -0
  75. package/dist/esm/rsbuild/virtual-modules.d.ts +53 -0
  76. package/dist/esm/rsbuild/virtual-modules.js +287 -0
  77. package/dist/esm/rsbuild/virtual-modules.js.map +1 -0
  78. package/dist/esm/schema.d.ts +43 -43
  79. package/dist/esm/start-compiler/compiler.d.ts +1 -1
  80. package/dist/esm/start-compiler/compiler.js +80 -9
  81. package/dist/esm/start-compiler/compiler.js.map +1 -1
  82. package/dist/esm/start-compiler/handleCreateServerFn.js +9 -0
  83. package/dist/esm/start-compiler/handleCreateServerFn.js.map +1 -1
  84. package/dist/esm/start-compiler/host.js +5 -1
  85. package/dist/esm/start-compiler/host.js.map +1 -1
  86. package/dist/esm/start-compiler/types.d.ts +1 -0
  87. package/dist/esm/utils.d.ts +1 -0
  88. package/dist/esm/utils.js +10 -1
  89. package/dist/esm/utils.js.map +1 -1
  90. package/dist/esm/{import-protection-plugin → vite/import-protection-plugin}/plugin.js +41 -92
  91. package/dist/esm/vite/import-protection-plugin/plugin.js.map +1 -0
  92. package/dist/esm/{import-protection-plugin → vite/import-protection-plugin}/types.d.ts +5 -5
  93. package/dist/esm/vite/import-protection-plugin/virtualModules.d.ts +8 -0
  94. package/dist/esm/vite/import-protection-plugin/virtualModules.js +49 -0
  95. package/dist/esm/vite/import-protection-plugin/virtualModules.js.map +1 -0
  96. package/dist/esm/vite/index.d.ts +5 -0
  97. package/dist/esm/vite/index.js +4 -0
  98. package/dist/esm/vite/plugin.js +1 -1
  99. package/dist/esm/vite/plugin.js.map +1 -1
  100. package/dist/esm/vite/post-server-build.js +14 -32
  101. package/dist/esm/vite/post-server-build.js.map +1 -1
  102. package/dist/esm/vite/prerender.d.ts +2 -2
  103. package/dist/esm/vite/prerender.js +17 -147
  104. package/dist/esm/vite/prerender.js.map +1 -1
  105. package/dist/esm/vite/schema.d.ts +23 -23
  106. package/dist/esm/vite/start-compiler-plugin/hot-update.d.ts +2 -0
  107. package/dist/esm/vite/start-compiler-plugin/hot-update.js +16 -0
  108. package/dist/esm/vite/start-compiler-plugin/hot-update.js.map +1 -0
  109. package/dist/esm/vite/start-compiler-plugin/module-specifier.js +9 -4
  110. package/dist/esm/vite/start-compiler-plugin/module-specifier.js.map +1 -1
  111. package/dist/esm/vite/start-compiler-plugin/plugin.js +86 -13
  112. package/dist/esm/vite/start-compiler-plugin/plugin.js.map +1 -1
  113. package/package.json +32 -4
  114. package/src/import-protection/INTERNALS.md +266 -0
  115. package/src/import-protection/adapterUtils.ts +94 -0
  116. package/src/import-protection/analysis.ts +853 -0
  117. package/src/{import-protection-plugin → import-protection}/constants.ts +7 -0
  118. package/src/import-protection/rewrite.ts +229 -0
  119. package/src/{import-protection-plugin → import-protection}/sourceLocation.ts +125 -9
  120. package/src/{import-protection-plugin → import-protection}/trace.ts +0 -1
  121. package/src/{import-protection-plugin → import-protection}/utils.ts +36 -21
  122. package/src/{import-protection-plugin → import-protection}/virtualModules.ts +30 -177
  123. package/src/index.ts +1 -8
  124. package/src/post-build.ts +64 -0
  125. package/src/prerender.ts +292 -0
  126. package/src/rsbuild/INTERNALS-import-protection.md +169 -0
  127. package/src/rsbuild/dev-server.ts +129 -0
  128. package/src/rsbuild/import-protection.ts +1599 -0
  129. package/src/rsbuild/index.ts +4 -0
  130. package/src/rsbuild/normalized-client-build.ts +346 -0
  131. package/src/rsbuild/planning.ts +234 -0
  132. package/src/rsbuild/plugin.ts +754 -0
  133. package/src/rsbuild/post-build.ts +96 -0
  134. package/src/rsbuild/schema.ts +31 -0
  135. package/src/rsbuild/start-compiler-host.ts +250 -0
  136. package/src/rsbuild/start-router-plugin.ts +86 -0
  137. package/src/rsbuild/swc-rsc.ts +166 -0
  138. package/src/rsbuild/types.ts +20 -0
  139. package/src/rsbuild/virtual-modules.ts +565 -0
  140. package/src/start-compiler/compiler.ts +153 -19
  141. package/src/start-compiler/handleCreateServerFn.ts +18 -0
  142. package/src/start-compiler/types.ts +1 -0
  143. package/src/utils.ts +14 -0
  144. package/src/vite/import-protection-plugin/INTERNALS.md +187 -0
  145. package/src/{import-protection-plugin → vite/import-protection-plugin}/plugin.ts +73 -158
  146. package/src/{import-protection-plugin → vite/import-protection-plugin}/types.ts +5 -5
  147. package/src/vite/import-protection-plugin/virtualModules.ts +122 -0
  148. package/src/vite/index.ts +8 -0
  149. package/src/vite/plugin.ts +1 -1
  150. package/src/vite/post-server-build.ts +14 -57
  151. package/src/vite/prerender.ts +19 -260
  152. package/src/vite/start-compiler-plugin/hot-update.ts +24 -0
  153. package/src/vite/start-compiler-plugin/module-specifier.ts +15 -5
  154. package/src/vite/start-compiler-plugin/plugin.ts +193 -18
  155. package/dist/esm/import-protection-plugin/ast.js.map +0 -1
  156. package/dist/esm/import-protection-plugin/constants.d.ts +0 -6
  157. package/dist/esm/import-protection-plugin/constants.js.map +0 -1
  158. package/dist/esm/import-protection-plugin/defaults.js.map +0 -1
  159. package/dist/esm/import-protection-plugin/extensionlessAbsoluteIdResolver.js.map +0 -1
  160. package/dist/esm/import-protection-plugin/matchers.js.map +0 -1
  161. package/dist/esm/import-protection-plugin/plugin.js.map +0 -1
  162. package/dist/esm/import-protection-plugin/postCompileUsage.d.ts +0 -13
  163. package/dist/esm/import-protection-plugin/postCompileUsage.js +0 -63
  164. package/dist/esm/import-protection-plugin/postCompileUsage.js.map +0 -1
  165. package/dist/esm/import-protection-plugin/rewriteDeniedImports.js +0 -205
  166. package/dist/esm/import-protection-plugin/rewriteDeniedImports.js.map +0 -1
  167. package/dist/esm/import-protection-plugin/sourceLocation.js.map +0 -1
  168. package/dist/esm/import-protection-plugin/trace.js.map +0 -1
  169. package/dist/esm/import-protection-plugin/utils.js.map +0 -1
  170. package/dist/esm/import-protection-plugin/virtualModules.d.ts +0 -78
  171. package/dist/esm/import-protection-plugin/virtualModules.js.map +0 -1
  172. package/dist/esm/start-compiler/load-module.d.ts +0 -14
  173. package/dist/esm/start-compiler/load-module.js +0 -18
  174. package/dist/esm/start-compiler/load-module.js.map +0 -1
  175. package/src/import-protection-plugin/INTERNALS.md +0 -700
  176. package/src/import-protection-plugin/postCompileUsage.ts +0 -100
  177. package/src/import-protection-plugin/rewriteDeniedImports.ts +0 -379
  178. package/src/start-compiler/load-module.ts +0 -31
  179. /package/dist/esm/{import-protection-plugin → import-protection}/ast.d.ts +0 -0
  180. /package/dist/esm/{import-protection-plugin → import-protection}/defaults.d.ts +0 -0
  181. /package/dist/esm/{import-protection-plugin → import-protection}/extensionlessAbsoluteIdResolver.d.ts +0 -0
  182. /package/dist/esm/{import-protection-plugin → import-protection}/matchers.d.ts +0 -0
  183. /package/dist/esm/{import-protection-plugin → vite/import-protection-plugin}/plugin.d.ts +0 -0
  184. /package/src/{import-protection-plugin → import-protection}/ast.ts +0 -0
  185. /package/src/{import-protection-plugin → import-protection}/defaults.ts +0 -0
  186. /package/src/{import-protection-plugin → import-protection}/extensionlessAbsoluteIdResolver.ts +0 -0
  187. /package/src/{import-protection-plugin → import-protection}/matchers.ts +0 -0
@@ -1,700 +0,0 @@
1
- # Import Protection Plugin — Internal Technical Documentation
2
-
3
- ## Overview
4
-
5
- The import protection plugin prevents server-only code from leaking into client
6
- bundles (and vice versa). It operates as a Vite plugin that intercepts module
7
- resolution, detects violations, and either errors or replaces the offending
8
- module with a safe mock.
9
-
10
- The plugin must handle **two axes of configuration**:
11
-
12
- | | **Dev** (`vite dev`) | **Build** (`vite build`) |
13
- | --------- | -------------------------------------------------------------------- | -------------------------------------------------------------- |
14
- | **Error** | `ctx.error()` immediately in `resolveId` | Defer to `generateBundle`; error if mock survived tree-shaking |
15
- | **Mock** | Defer to transform-cache; warn if reachable via post-transform graph | Defer to `generateBundle`; warn if mock survived tree-shaking |
16
-
17
- ## Plugin Architecture
18
-
19
- `importProtectionPlugin()` returns **two** Vite plugins:
20
-
21
- ### 1. `tanstack-start-core:import-protection` (enforce: `'pre'`)
22
-
23
- The main enforcement plugin. Hooks: `applyToEnvironment`, `configResolved`,
24
- `configureServer`, `buildStart`, `hotUpdate`, `resolveId`, `load`, and
25
- `generateBundle`. All violation detection starts in `resolveId`.
26
-
27
- ### 2. `tanstack-start-core:import-protection-transform-cache`
28
-
29
- Runs after all `enforce: 'pre'` hooks (including the Start compiler). Caches
30
- transformed code and composed sourcemaps for accurate source-location mapping
31
- in violation messages. Also resolves post-transform imports and triggers
32
- `processPendingViolations()` for the dev mock deferral path.
33
-
34
- In both build and dev modes, this plugin performs **self-denial**: when a file
35
- matches a deny pattern in the current environment (e.g. a `.server.ts` file
36
- transformed in the client environment), its entire content is replaced with a
37
- mock module. This is the core mechanism for preventing cross-environment cache
38
- contamination — `resolveId` never returns virtual module IDs for file-based
39
- violations, so there is nothing for third-party resolver caches
40
- (e.g. `vite-tsconfig-paths`) to leak across environments. In dev mode, the
41
- mock imports a `mock-runtime` module for runtime diagnostics; in build mode,
42
- the mock is fully self-contained.
43
-
44
- See the [Self-Denial Transform](#self-denial-transform-in-detail) section below
45
- for a detailed walkthrough with code examples.
46
-
47
- ## Violation Types
48
-
49
- | Type | Trigger | Example |
50
- | ----------- | --------------------------------------------------------------------------------------- | ------------------------------------------------------- |
51
- | `file` | Resolved path matches a deny glob (e.g. `**/*.server.*`) | `import './db.server'` in client env |
52
- | `specifier` | Import specifier matches a deny pattern | `import '@tanstack/react-start/server'` in client env |
53
- | `marker` | File imports `'server-only'` or `'client-only'` marker, then is loaded in the wrong env | File with `import 'server-only'` resolved in client env |
54
-
55
- ## The False-Positive Problem
56
-
57
- ### Barrel re-exports
58
-
59
- A common pattern:
60
-
61
- ```text
62
- db/index.ts → export { getUsers } from './db.server'
63
- export { userColumns } from './shared'
64
-
65
- route.tsx → import { getUsers, userColumns } from '../db'
66
- // getUsers used only in createServerFn().handler()
67
- // userColumns used in JSX
68
- ```
69
-
70
- At `resolveId` time, `db/index.ts` imports `./db.server` — the plugin sees a
71
- `.server` file imported in the client environment. But the Start compiler strips
72
- the `getUsers` usage from the client bundle (it's inside a server fn handler),
73
- and Rollup's tree-shaking then eliminates the `./db.server` dependency entirely.
74
-
75
- **No server code actually leaks.** But without deferral, the plugin fires at
76
- `resolveId` time — before tree-shaking — producing a false positive.
77
-
78
- ### Pre-transform resolves (server-fn-lookup)
79
-
80
- During dev, Vite's `fetchModule(?SERVER_FN_LOOKUP)` call triggers resolves for
81
- analysing a module's exports. These are tracked via `serverFnLookupModules` and
82
- `isPreTransformResolve`. In dev mock mode, pre-transform violations are
83
- deferred like all other violations (verified via edge-survival and graph
84
- reachability). In dev error mode, they are silenced because no deferred
85
- verification path exists.
86
-
87
- ## Violation Handling Flow
88
-
89
- ### Central functions
90
-
91
- - **`handleViolation()`**: Formats + reports (or silences) the violation. Returns
92
- a resolve result so `resolveId` can substitute the offending import: for
93
- file-based violations, returns the physical file path (self-denial handles
94
- the rest in transform); for specifier/marker violations, returns a mock-edge
95
- module ID. May also return `undefined` (suppressed by `onViolation` or
96
- silent+error in dev) or throw via `ctx.error()` (dev+error).
97
- - **`reportOrDeferViolation()`**: Dispatch layer. Either defers (stores for later
98
- verification) or reports immediately, depending on `shouldDefer`.
99
-
100
- ### `shouldDefer` logic
101
-
102
- ```ts
103
- shouldDefer = isBuild || isDevMock
104
- ```
105
-
106
- - **Dev mock**: ALL violations (including pre-transform resolves) are deferred
107
- to `pendingViolations` → verified via edge-survival and post-transform graph
108
- reachability in `processPendingViolations()`. Pre-transform violations are
109
- tagged with `fromPreTransformResolve` so the pending-violation processor
110
- knows to wait for post-transform data before emitting.
111
- - **Build (both mock and error)**: Defer to `deferredBuildViolations` → verify
112
- via tree-shaking survival in `generateBundle`.
113
-
114
- All three violation types (file, specifier, AND marker) are deferred in build
115
- mode. Marker violations through barrels can also be false positives — e.g., if
116
- `foo.ts` has `import 'server-only'` and is re-exported through a barrel but
117
- never used in client code, tree-shaking eliminates `foo.ts` entirely. To enable
118
- `generateBundle` tracking for markers, `resolveId` returns the unique build
119
- mock ID instead of the marker module. This is transparent because marker imports
120
- are bare (`import 'server-only'` — no bindings), and the mock module is equally
121
- side-effect-free.
122
-
123
- ## Dev Mode Strategy
124
-
125
- ### Dev + Error
126
-
127
- Violations fire immediately via `ctx.error()` in `resolveId`. No tree-shaking
128
- is available, so false positives for barrel patterns are expected and accepted.
129
- (Dev + error is typically used only during explicit validation.)
130
-
131
- Pre-transform resolves (e.g. server-fn-lookup) are silenced in error mode
132
- because they fire before the Start compiler runs — imports inside `.server()`
133
- callbacks haven't been stripped yet, and error mode has no deferred verification
134
- path.
135
-
136
- ### Dev + Mock
137
-
138
- 1. `resolveId` detects the violation and calls `reportOrDeferViolation()`.
139
- 2. For **file-based violations**: `handleViolation()` returns the **physical
140
- file path** (same as build mode). The self-denial transform in the
141
- transform-cache plugin will replace the file's content with a dev mock
142
- module that imports `mock-runtime` for runtime diagnostics.
143
- 3. For **specifier/marker violations**: `handleViolation()` returns a mock-edge
144
- module ID so the dev server can serve a Proxy-based mock.
145
- 4. The violation is stored in `pendingViolations` keyed by the importer's file
146
- path.
147
- 5. The transform-cache plugin, after resolving post-transform imports, calls
148
- `processPendingViolations()`.
149
- 6. `processPendingViolations()` first applies **edge-survival**: if
150
- post-transform import data is available for the importer, it checks whether
151
- the denied import survived the Start compiler transform. Imports stripped
152
- by the compiler (e.g. inside `.server()` callbacks) are discarded. For
153
- pre-transform violations (`fromPreTransformResolve`), the function waits
154
- until post-transform data is available before proceeding.
155
- 7. After edge-survival, `processPendingViolations()` checks graph reachability
156
- from entry points using only post-transform edges. If the violating
157
- importer is reachable → confirm (warn). If unreachable → discard. If
158
- unknown → keep pending or emit conservatively (warm-start fallback).
159
-
160
- Warm-start stability guardrails:
161
-
162
- - Resolve-time edges discovered through pre-transform paths
163
- (`isPreTransformResolve`, especially `SERVER_FN_LOOKUP`) are **not** recorded
164
- into the reachability graph.
165
- - In `'unknown'` reachability status, pre-transform pending violations are
166
- kept pending (not fallback-emitted) until non-lookup transform evidence is
167
- available.
168
-
169
- This approach can't fully eliminate barrel false-positives in dev because
170
- there's no tree-shaking. The barrel's import of `.server` always resolves,
171
- and the barrel is reachable. This is a known and accepted limitation.
172
-
173
- ### Dev mock modules
174
-
175
- Dev violations are handled differently depending on their type:
176
-
177
- - **File-based violations** use **self-denial** (same mechanism as build mode):
178
- the denied file's content is replaced by the transform-cache plugin with a
179
- mock that imports `mock-runtime` for runtime diagnostics. The export list
180
- comes from the **denied file's AST** (what it exports), generated by
181
- `generateDevSelfDenialModule()`. This approach avoids the cold-start problem
182
- where the importer's AST is unavailable at `resolveId` time, and prevents
183
- cross-environment cache contamination from third-party resolver plugins.
184
-
185
- - **Specifier/marker violations** use **mock-edge modules**: each violation
186
- gets a per-importer mock edge module that explicitly exports the names the
187
- importer expects (extracted lazily by `resolveExportsForDeniedSpecifier()`
188
- which parses the importer's AST) and delegates to a **runtime mock module**
189
- that contains a recursive Proxy and optional runtime diagnostics.
190
-
191
- ### Mock edge modules (in detail)
192
-
193
- A **mock edge module** is a lightweight, auto-generated virtual module that sits
194
- between an importing file and a base mock module. Its purpose is to provide
195
- **explicit ESM named exports** so that bundlers (Rollup, Rolldown) and Vite's dev
196
- server can correctly resolve `import { foo } from './denied.server'` — even
197
- though the real module has been replaced by a mock.
198
-
199
- #### Why they exist
200
-
201
- The base mock module (`\0tanstack-start-import-protection:mock`) exports only a
202
- `default` export — a recursive Proxy. But consumers of the denied module may use
203
- named imports:
204
-
205
- ```typescript
206
- import { getSecret, initDb } from './credentials.server'
207
- ```
208
-
209
- Without explicit named exports, this would fail: the bundler would complain that
210
- `getSecret` and `initDb` don't exist on the mock module. Using
211
- `syntheticNamedExports: true` in Rollup could solve this, but Rolldown (which
212
- Vite can now use) doesn't support it. Mock edge modules solve the problem
213
- portably by generating real ESM export statements.
214
-
215
- #### Structure
216
-
217
- Each mock edge module is identified by a virtual ID:
218
-
219
- ```text
220
- \0tanstack-start-import-protection:mock-edge:<BASE64_PAYLOAD>
221
- ```
222
-
223
- The Base64URL payload encodes two fields:
224
-
225
- - `exports` — the list of named export identifiers the importer needs
226
- - `runtimeId` — the module ID of the backing mock to import from
227
-
228
- When Vite's `load` hook encounters this virtual ID, `loadMockEdgeModule()`
229
- decodes the payload and generates code like this:
230
-
231
- ```typescript
232
- // Generated mock edge module for exports: ["getSecret", "initDb"]
233
- import mock from '\0tanstack-start-import-protection:mock'
234
- export const getSecret = mock.getSecret
235
- export const initDb = mock.initDb
236
- export default mock
237
- ```
238
-
239
- Each `mock.getSecret` access returns the Proxy itself (the Proxy's `get` trap
240
- returns `mock` for any property), so the named exports are valid callable/
241
- constructable values that won't crash at runtime.
242
-
243
- #### How exports are determined
244
-
245
- The export list comes from **parsing the importer's AST** — not the denied
246
- file's AST. The function `resolveExportsForDeniedSpecifier()` performs this:
247
-
248
- 1. Gets the importer's code from the transform cache or `getModuleInfo`.
249
- 2. Calls `collectMockExportNamesBySource()` to extract which names the
250
- importer uses from each import source (parses the AST internally):
251
- - **Named imports**: `import { getSecret, initDb } from './creds.server'`
252
- → `['getSecret', 'initDb']`
253
- - **Namespace member access**: `import * as creds from './creds.server'` then
254
- `creds.getSecret()` → `['getSecret']`
255
- - **Default import member access**: `import creds from './creds.server'` then
256
- `creds.getSecret` → `['getSecret']`
257
- - **Re-exports**: `export { getSecret } from './creds.server'`
258
- → `['getSecret']`
259
- 3. Caches the result per importer so multiple violations from the same file
260
- don't re-parse.
261
-
262
- This "importer-driven" approach means the mock edge module only exports the
263
- names the consumer actually references — not all exports from the denied file.
264
-
265
- #### Dev vs build mock edge modules
266
-
267
- | Aspect | Dev mock edge | Build mock edge |
268
- | ---------------- | ---------------------------------------------------------------- | ---------------------------------------------------------------- |
269
- | **Backing mock** | `mock-runtime:BASE64` (runtime diagnostics) or `mock` (silent) | `mock:build:N` (per-violation unique, silent) |
270
- | **Purpose** | Serve mocks in dev server + runtime warnings | Track tree-shaking survival in `generateBundle` |
271
- | **Created for** | Specifier/marker violations only (file uses self-denial instead) | Specifier/marker violations only (file uses self-denial instead) |
272
- | **Uniqueness** | Per-importer per-specifier | Per-violation (unique counter) |
273
-
274
- In **dev**, the backing `runtimeId` is a `mock-runtime:BASE64` module that
275
- includes diagnostic metadata (environment, import path, trace). When the mock
276
- is accessed in the browser, it logs a console warning or error. For SSR or
277
- when `mockAccess` is `'off'`, the backing mock is the shared silent
278
- `MOCK_MODULE_ID`.
279
-
280
- In **build**, the backing mock uses a unique counter ID
281
- (`mock:build:0`, `mock:build:1`, ...) so `generateBundle` can check whether
282
- each specific violation's mock survived tree-shaking.
283
-
284
- #### Handling non-identifier export names
285
-
286
- ES2022 allows string-keyed exports like `export { x as "foo-bar" }`. Mock edge
287
- modules handle these via an intermediate variable:
288
-
289
- ```typescript
290
- import mock from '\0tanstack-start-import-protection:mock'
291
- export const validName = mock.validName
292
- const __tss_str_0 = mock['foo-bar']
293
- export { __tss_str_0 as 'foo-bar' }
294
- ```
295
-
296
- The `default` export name is always filtered out (handled separately as
297
- `export default mock`).
298
-
299
- ### File-based violations: self-denial (dev and build)
300
-
301
- File-based violations (e.g. `import './db.server'` in client env) use
302
- self-denial in **both** dev and build modes. `handleViolation()` returns the
303
- physical file path, and the transform-cache plugin replaces the file's
304
- contents with a mock module.
305
-
306
- - **Build mode**: Uses `generateSelfContainedMockModule()` — a fully
307
- self-contained mock with an inlined Proxy factory (no `import` statements).
308
- This is important because build-mode mocks must be tree-shakeable.
309
-
310
- - **Dev mode**: Uses `generateDevSelfDenialModule()` — a mock that imports
311
- `mock-runtime` for runtime diagnostics (console warnings/errors when the
312
- mock is accessed in the browser). The `mock-runtime` module ID encodes
313
- violation metadata (environment, import path, trace).
314
-
315
- Self-denial avoids creating virtual mock-edge module IDs that could
316
- contaminate third-party resolver caches across Vite environments.
317
-
318
- ### Non-file violations: mock-edge modules (dev and build)
319
-
320
- Specifier and marker violations use **mock-edge modules** because the denied
321
- specifier doesn't resolve to a physical file that could be "self-denied."
322
-
323
- - **Build mode**: Each violation gets a **per-violation mock edge module**
324
- wrapping a unique base mock module
325
- (`\0tanstack-start-import-protection:mock:build:N`). The edge module
326
- re-exports the named exports the importer expects, ensuring compatibility
327
- with both Rollup and Rolldown (which doesn't support
328
- `syntheticNamedExports`).
329
-
330
- - **Dev mode**: Each violation gets a **per-importer mock edge module**
331
- wrapping a `mock-runtime` module for runtime diagnostics.
332
-
333
- ## Build Mode Strategy
334
-
335
- ### The "mock first, verify later" pattern
336
-
337
- Both mock and error build modes follow the same pattern:
338
-
339
- 1. **`resolveId`**: Call `handleViolation({ silent: true })`.
340
- - For **file-based violations**: Returns the physical file path. The
341
- self-denial transform will replace its content.
342
- - For **specifier/marker violations**: Generates a **unique per-violation
343
- mock-edge module** wrapping a base mock module
344
- (`\0tanstack-start-import-protection:mock:build:N`). Stores the violation +
345
- mock-edge ID in `env.deferredBuildViolations`. Returns the mock-edge ID.
346
-
347
- 2. **`transform`** (self-denial): For file-based violations, the transform-cache
348
- plugin detects that the current file is denied in this environment and
349
- replaces its content with a self-contained mock module
350
- (`generateSelfContainedMockModule()`).
351
-
352
- 3. **`load`**: For base mock modules, returns a silent Proxy-based mock. For
353
- mock-edge modules, returns code that imports from the base mock and
354
- re-exports the expected named bindings (e.g. `export const Foo = mock.Foo`).
355
-
356
- 4. **Tree-shaking**: The bundler processes the bundle normally. If no binding from
357
- the mock module is actually used at runtime, the modules are eliminated.
358
-
359
- 5. **`generateBundle`**: Inspect the output chunks. For each deferred violation,
360
- check whether its unique mock module ID appears in any chunk's `modules`.
361
- - **Survived** → real violation (the import wasn't tree-shaken away).
362
- - Error mode: `ctx.error()` — fail the build.
363
- - Mock mode: `ctx.warn()` — emit a warning.
364
- - **Eliminated** → false positive (tree-shaking removed it). Suppress
365
- silently.
366
-
367
- ### Why unique mock IDs per violation?
368
-
369
- The original `RESOLVED_MOCK_MODULE_ID` is a single shared virtual module used
370
- for all mock-mode violations. If multiple violations are deferred, we need to
371
- know _which specific ones_ survived tree-shaking. A shared ID would tell us
372
- "something survived" but not which violation it corresponds to. Each violation
373
- gets a unique mock-edge module (wrapping a unique base mock
374
- `...mock:build:0`, `...mock:build:1`, etc.) to provide this granularity.
375
-
376
- ### Why mocking doesn't affect tree-shaking
377
-
378
- From the consumer's perspective, the import bindings are identical whether they
379
- point to the real module or the mock. The bundler tree-shakes based on binding
380
- usage, not module content. If a binding from the barrel's re-export of `.server`
381
- is unused after the Start compiler strips server fn handlers, tree-shaking
382
- eliminates it regardless of whether it points to real DB code or a Proxy mock.
383
-
384
- ### Per-environment operation
385
-
386
- `generateBundle` runs once per Vite environment (client, SSR, etc.). Each
387
- environment has its own `EnvState` with its own `deferredBuildViolations` array.
388
- The check only inspects chunks from THAT environment's bundle, ensuring correct
389
- per-environment verification.
390
-
391
- ## Marker violations vs. file/specifier violations
392
-
393
- | | Marker | Specifier | File |
394
- | ----------------------------- | --------------------------------------------------- | -------------------------------------------- | --------------------------------------------- |
395
- | **What triggers it** | The _importer_ has a conflicting directive | The _import specifier_ matches a deny rule | The _resolved path_ matches a deny glob |
396
- | **resolveId returns (dev)** | Marker module ID (`RESOLVED_MARKER_*`) \* | Mock edge module ID (per-importer) | Physical file path (self-denial in transform) |
397
- | **resolveId returns (build)** | Unique build mock ID | Unique build mock ID | Physical file path (self-denial in transform) |
398
- | **Can be tree-shaken away?** | Yes — if the importer is eliminated by tree-shaking | Yes — if no binding from the target survives | Yes — if no binding from the target survives |
399
- | **Deferred in build?** | Yes — deferred to `generateBundle` | Yes — deferred to `generateBundle` | Yes — deferred to `generateBundle` |
400
- | **Deferred in dev mock?** | Yes — deferred to pending graph | Yes — deferred to pending graph | Yes — deferred to pending graph |
401
-
402
- \* In dev mock, `handleViolation` internally returns the physical file path
403
- (stored as the deferred result in `pendingViolations`), but `resolveId` ignores
404
- it for markers and falls through to return the marker module ID. The file path
405
- is only used for deferral bookkeeping, not for module resolution.
406
-
407
- ## State Management
408
-
409
- ### `EnvState` (per Vite environment)
410
-
411
- Key fields for violation handling:
412
-
413
- - `seenViolations: Set<string>` — deduplication of logged violations.
414
- - `pendingViolations: Map<string, Array<PendingViolation>>` — dev mock
415
- deferral. Keyed by importer file path.
416
- - `deferredBuildViolations: Array<DeferredBuildViolation>` — build mode
417
- deferral. Each entry has `{ info, mockModuleId }`.
418
-
419
- Per-build/watch iteration, `buildStart` clears `pendingViolations` and resets
420
- `deferredBuildViolations` so deferred entries don't leak across rebuilds.
421
-
422
- - `graph: ImportGraph` — import dependency graph for reachability checks.
423
- - `serverFnLookupModules: Set<string>` — modules transitively loaded during
424
- server-fn analysis (false-positive suppression).
425
-
426
- ### `SharedState` (cross-environment)
427
-
428
- - `fileMarkerKind: Map<string, 'server' | 'client'>` — cached per-file marker
429
- detection. A file's directive is inherent to the file, not the environment.
430
-
431
- ## Virtual Module IDs
432
-
433
- | ID Pattern | Usage |
434
- | -------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------- |
435
- | `\0tanstack-start-import-protection:mock` | Shared silent mock (dev mock only) |
436
- | `\0tanstack-start-import-protection:mock:build:N` | Per-violation build mock (unique counter) |
437
- | `\0tanstack-start-import-protection:mock-edge:BASE64` | Per-importer mock with explicit named exports (specifier/marker violations, dev + build) |
438
- | `\0tanstack-start-import-protection:mock-runtime:BASE64` | Runtime diagnostic mock (dev client, console warnings) |
439
- | `\0tanstack-start-import-protection:marker:*` | Marker module (empty `export {}`). Suffixed `server-only` or `client-only`; derived from `MARKER_PREFIX` in `virtualModules.ts` |
440
-
441
- ## Key Design Decisions
442
-
443
- ### Why not just skip `.server` resolves in barrels?
444
-
445
- The plugin doesn't know at `resolveId` time whether the barrel's re-export will
446
- survive tree-shaking. It can't inspect the consumer's usage — it only sees the
447
- barrel importing `.server`. Skipping it would miss real violations where the
448
- barrel's `.server` re-export IS used in client code.
449
-
450
- ### How marker violations are deferred in build
451
-
452
- Marker violations normally resolve to `RESOLVED_MARKER_*` (empty `export {}`),
453
- not a mock. To enable `generateBundle` tracking, in build mode `resolveId`
454
- returns the unique build mock ID instead of the marker module. This works
455
- because:
456
-
457
- 1. The marker import is bare (`import 'server-only'` — no bindings), so
458
- swapping the resolution to a mock module is transparent.
459
- 2. The mock module is side-effect-free, just like the marker module.
460
- 3. If the importer file survives tree-shaking, the mock module survives →
461
- `generateBundle` fires the violation. If the importer is tree-shaken away
462
- (e.g., barrel re-export of a marker-protected file that's never used), the
463
- mock is eliminated → violation suppressed.
464
-
465
- ### Why accept false positives in dev?
466
-
467
- Dev mode uses Vite's ESM dev server — no bundling, no tree-shaking. The barrel
468
- file's import of `.server` always resolves and is always reachable from entry
469
- points. The graph-reachability check in `processPendingViolations` can only
470
- eliminate violations where the _importer_ becomes unreachable after the Start
471
- compiler transforms it, not where individual bindings are unused.
472
-
473
- This is an accepted trade-off: dev mock mode warns about potential issues,
474
- build mode provides definitive answers via tree-shaking.
475
-
476
- ## E2E Test Structure
477
-
478
- Tests live in `e2e/react-start/import-protection/` and
479
- `e2e/react-start/import-protection-custom-config/`:
480
-
481
- ### Main test suite (`import-protection/`)
482
-
483
- - **Mock mode** (default): `globalSetup` builds the app, captures build warnings
484
- to `violations.build.json`, starts a dev server capturing to
485
- `violations.dev.json`. Tests read these JSON files and assert.
486
- - **Error mode** (`BEHAVIOR=error`): `globalSetup` runs the build expecting
487
- failure, captures exit code + output. Tests assert the build failed with the
488
- expected violation.
489
- - **False-positive test**: The `barrel-reexport` test case verifies that a barrel
490
- re-exporting from both a `.server` file and a marker-protected file (`foo.ts`
491
- with `import 'server-only'`), where all server-only bindings are tree-shaken
492
- away, produces **zero** violations in the build log.
493
-
494
- ### Custom-config test suite (`import-protection-custom-config/`)
495
-
496
- Uses non-default deny patterns (`**/*.backend.*` / `**/*.frontend.*`) to verify
497
- that import protection works with user-configured file patterns. This ensures
498
- the plugin doesn't hardcode any assumption about `.server`/`.client` naming
499
- conventions. The `vite.config.ts` provides custom `client.files` and
500
- `server.files` arrays; no `vite-tsconfig-paths` is used.
501
-
502
- ## Self-Denial Transform (In Detail)
503
-
504
- The self-denial transform is the mechanism by which the plugin prevents cross-
505
- environment contamination without creating virtual module IDs that could leak
506
- through third-party resolver caches. It applies to **file-based violations in
507
- both dev and build modes**.
508
-
509
- ### The problem it solves
510
-
511
- In Vite 7+, client and SSR environments run within the same Vite process. Some
512
- plugins (e.g. `vite-tsconfig-paths`) maintain a global resolution cache shared
513
- across environments. If the import-protection plugin resolves a specifier to a
514
- virtual mock module ID (e.g.
515
- `\0tanstack-start-import-protection:mock-edge:...`) in the client environment,
516
- that virtual ID can leak into the SSR environment's cache. When the SSR
517
- environment later resolves the same specifier, it finds the cached virtual ID
518
- instead of the real file — breaking the server.
519
-
520
- In dev mode, self-denial also solves a **cold-start problem**: on cold start
521
- (no `.vite` cache), the importer's AST is unavailable when `resolveId` runs
522
- (neither the transform cache nor `getModuleInfo` have content yet). If mock-edge
523
- module IDs were generated at `resolveId` time, the export list would be empty
524
- (since it's derived from parsing the importer), producing a mock with no named
525
- exports — causing runtime errors like
526
- `does not provide an export named: 'getSecret'`.
527
-
528
- ### How it works
529
-
530
- Instead of returning a virtual module ID from `resolveId`, the self-denial
531
- transform works at the **transform** stage:
532
-
533
- 1. **`resolveId`**: For file-based violations, `handleViolation()` returns the
534
- **physical file path**. The import resolves normally. No virtual ID is
535
- created.
536
-
537
- 2. **transform-cache plugin**: When the transform-cache hook processes a file,
538
- it checks whether the file matches any deny pattern for the current
539
- environment using `checkFileDenial()`. If the file is denied, the plugin:
540
-
541
- a. Extracts the file's named exports using `collectNamedExports()` (parses
542
- the AST internally).
543
- b. **Build mode**: Generates a self-contained mock module via
544
- `generateSelfContainedMockModule()` — no imports, inlined Proxy factory.
545
- c. **Dev mode**: Generates a dev mock module via
546
- `generateDevSelfDenialModule()` — imports `mock-runtime` for runtime
547
- diagnostics (console warnings/errors when the mock is accessed).
548
- d. Returns the mock code as the transform result, completely replacing the
549
- original file content.
550
-
551
- 3. **Fallback**: If AST parsing fails (e.g. due to syntax errors in the denied
552
- file), `exportNames` defaults to `[]` (empty) and the mock module has no
553
- named exports:
554
- - Build: `generateSelfContainedMockModule([])`.
555
- - Dev: `generateDevSelfDenialModule([], runtimeId)` where `runtimeId` is
556
- computed via `mockRuntimeModuleIdFromViolation()` (which may internally
557
- fall back to the shared silent `MOCK_MODULE_ID` for SSR or when
558
- `mockAccess` is `'off'`).
559
-
560
- ### Code transformation example
561
-
562
- **Original file** (`src/lib/credentials.server.ts`):
563
-
564
- ```typescript
565
- import { db } from './database'
566
-
567
- export function getSecret(): string {
568
- return db.query('SELECT secret FROM config LIMIT 1')
569
- }
570
-
571
- export const API_KEY = process.env.API_KEY!
572
-
573
- export default { getSecret, API_KEY }
574
- ```
575
-
576
- **After self-denial transform in build mode** (client environment):
577
-
578
- ```typescript
579
- /* @__NO_SIDE_EFFECTS__ */
580
- function createMock() {
581
- const handler = {
582
- get: (_, prop) => {
583
- if (prop === Symbol.toPrimitive) return () => 'MOCK'
584
- if (typeof prop === 'symbol') return undefined
585
- return mock
586
- },
587
- apply: () => mock,
588
- construct: () => mock,
589
- }
590
- const mock = /* @__PURE__ */ new Proxy(function () {}, handler)
591
- return mock
592
- }
593
- const mock = /* @__PURE__ */ createMock()
594
- export const getSecret = mock.getSecret
595
- export const API_KEY = mock.API_KEY
596
- export default mock
597
- ```
598
-
599
- **After self-denial transform in dev mode** (client environment):
600
-
601
- ```typescript
602
- import mock from '\0tanstack-start-import-protection:mock-runtime:eyJ...'
603
- export const getSecret = mock.getSecret
604
- export const API_KEY = mock.API_KEY
605
- export default mock
606
- ```
607
-
608
- The `mock-runtime` module ID encodes violation metadata (environment, importer,
609
- specifier, trace) as a Base64URL payload. When the mock is accessed in the
610
- browser, it logs a console warning or error with this metadata.
611
-
612
- ### Key properties
613
-
614
- **Build-mode mock** (`generateSelfContainedMockModule`):
615
-
616
- - **Self-contained**: No `import` statements — the Proxy factory is inlined.
617
- - **Tree-shakeable**: All exports are marked pure (`@__NO_SIDE_EFFECTS__`,
618
- `@__PURE__`), so the bundler can eliminate unused exports.
619
-
620
- **Dev-mode mock** (`generateDevSelfDenialModule`):
621
-
622
- - **Runtime diagnostics**: Imports `mock-runtime` which provides console
623
- warnings/errors when the mock is accessed in the browser.
624
- - **Lightweight**: No inlined Proxy factory — delegates to the mock-runtime
625
- virtual module.
626
-
627
- **Both modes**:
628
-
629
- - **Same export interface**: Named exports match the original file's exports,
630
- so consumers' import bindings remain valid.
631
- - **Safe at runtime**: Every access returns the same Proxy, which is callable,
632
- constructable, and returns "MOCK" when coerced to a string.
633
- - **Export accuracy from denied file**: Unlike mock-edge modules (which parse
634
- the importer's AST), self-denial parses the **denied file's own AST**.
635
- This provides a safe over-approximation — all exports the file offers — and
636
- avoids the cold-start problem where the importer hasn't been transformed yet.
637
-
638
- ### Why self-denial instead of virtual modules for file-based violations
639
-
640
- | Approach | Virtual mock ID | Self-denial |
641
- | ------------------ | -------------------------------------------------------------- | -------------------------------------------------------------- |
642
- | `resolveId` return | `\0mock-edge:BASE64` (virtual) | Physical file path (resolves normally) |
643
- | Cache risk | Virtual ID stored in shared resolver cache → leaks to SSR | Physical path cached → correct in both environments |
644
- | Module identity | New virtual module per violation | Same physical file, different content per environment |
645
- | Export accuracy | From importer's AST (what it imports) | From denied file's AST (what it exports) |
646
- | Cold-start safety | Fails — importer AST unavailable on cold start → empty exports | Safe — denied file's source code always available in transform |
647
-
648
- ### When self-denial is NOT used
649
-
650
- Self-denial only applies to **file-based** violations.
651
-
652
- - **Specifier violations** (e.g. `import '@tanstack/react-start/server'`):
653
- These are bare specifiers, not file paths. They use virtual mock-edge module
654
- IDs because the specifier doesn't resolve to a physical file that could be
655
- "self-denied."
656
- - **Marker violations**: Use virtual mock IDs in build mode for
657
- `generateBundle` tracking (see marker section above). In dev mode, markers
658
- resolve to the marker module ID directly.
659
-
660
- ## Module Dependency Architecture
661
-
662
- The import-protection plugin is structured to avoid heavy transitive
663
- dependencies in its utility modules:
664
-
665
- ```text
666
- constants.ts ← lightweight (no heavy imports)
667
- ├── IMPORT_PROTECTION_DEBUG
668
- ├── KNOWN_SOURCE_EXTENSIONS
669
- ├── SERVER_FN_LOOKUP_QUERY ← imports SERVER_FN_LOOKUP from parent constants.ts
670
- └── VITE_BROWSER_VIRTUAL_PREFIX
671
-
672
- defaults.ts ← lightweight (type-only imports)
673
- ├── getDefaultImportProtectionRules
674
- └── getMarkerSpecifiers
675
-
676
- matchers.ts ← picomatch only
677
- ├── compileMatcher
678
- ├── compileMatchers
679
- └── matchesAny
680
-
681
- utils.ts ← vite + node:path + local constants
682
- ├── normalizeFilePath, stripViteQuery, ...
683
- └── extractImportSources, dedupePatterns, ...
684
-
685
- trace.ts ← imports from utils only
686
- ├── ImportGraph
687
- ├── buildTrace
688
- └── formatViolation
689
-
690
- virtualModules.ts ← imports ../utils (parent), ../constants (parent)
691
- ├── generateSelfContainedMockModule, generateDevSelfDenialModule
692
- ├── loadMockEdgeModule, loadMockRuntimeModule
693
- └── MOCK_MODULE_ID, MOCK_EDGE_PREFIX, ...
694
-
695
- plugin.ts ← main plugin, imports everything above
696
- ```
697
-
698
- The `SERVER_FN_LOOKUP` constant lives in the shared parent `constants.ts`
699
- (not in `start-compiler-plugin/plugin.ts`) to avoid pulling in
700
- `@tanstack/start-server-core` when unit-testing import-protection modules.