@tanstack/start-plugin-core 1.167.34 → 1.168.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 (197) 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 +1 -1
  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 +12 -19
  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 +4 -0
  34. package/dist/esm/index.js +3 -1
  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/normalized-client-build.d.ts +18 -0
  48. package/dist/esm/rsbuild/normalized-client-build.js +207 -0
  49. package/dist/esm/rsbuild/normalized-client-build.js.map +1 -0
  50. package/dist/esm/rsbuild/planning.d.ts +52 -0
  51. package/dist/esm/rsbuild/planning.js +108 -0
  52. package/dist/esm/rsbuild/planning.js.map +1 -0
  53. package/dist/esm/rsbuild/plugin.d.ts +4 -0
  54. package/dist/esm/rsbuild/plugin.js +344 -0
  55. package/dist/esm/rsbuild/plugin.js.map +1 -0
  56. package/dist/esm/rsbuild/post-build.d.ts +6 -0
  57. package/dist/esm/rsbuild/post-build.js +57 -0
  58. package/dist/esm/rsbuild/post-build.js.map +1 -0
  59. package/dist/esm/rsbuild/schema.d.ts +3372 -0
  60. package/dist/esm/rsbuild/schema.js +12 -0
  61. package/dist/esm/rsbuild/schema.js.map +1 -0
  62. package/dist/esm/rsbuild/start-compiler-host.d.ts +20 -0
  63. package/dist/esm/rsbuild/start-compiler-host.js +150 -0
  64. package/dist/esm/rsbuild/start-compiler-host.js.map +1 -0
  65. package/dist/esm/rsbuild/start-router-plugin.d.ts +18 -0
  66. package/dist/esm/rsbuild/start-router-plugin.js +63 -0
  67. package/dist/esm/rsbuild/start-router-plugin.js.map +1 -0
  68. package/dist/esm/rsbuild/swc-rsc.d.ts +14 -0
  69. package/dist/esm/rsbuild/swc-rsc.js +93 -0
  70. package/dist/esm/rsbuild/swc-rsc.js.map +1 -0
  71. package/dist/esm/rsbuild/types.d.ts +17 -0
  72. package/dist/esm/rsbuild/types.js +0 -0
  73. package/dist/esm/rsbuild/virtual-modules.d.ts +53 -0
  74. package/dist/esm/rsbuild/virtual-modules.js +287 -0
  75. package/dist/esm/rsbuild/virtual-modules.js.map +1 -0
  76. package/dist/esm/schema.d.ts +43 -43
  77. package/dist/esm/schema.js +1 -1
  78. package/dist/esm/start-compiler/compiler.d.ts +1 -1
  79. package/dist/esm/start-compiler/compiler.js +80 -9
  80. package/dist/esm/start-compiler/compiler.js.map +1 -1
  81. package/dist/esm/start-compiler/handleCreateServerFn.js +9 -0
  82. package/dist/esm/start-compiler/handleCreateServerFn.js.map +1 -1
  83. package/dist/esm/start-compiler/host.js +5 -1
  84. package/dist/esm/start-compiler/host.js.map +1 -1
  85. package/dist/esm/start-compiler/types.d.ts +1 -0
  86. package/dist/esm/start-manifest-plugin/manifestBuilder.d.ts +3 -6
  87. package/dist/esm/start-manifest-plugin/manifestBuilder.js +34 -81
  88. package/dist/esm/start-manifest-plugin/manifestBuilder.js.map +1 -1
  89. package/dist/esm/utils.d.ts +1 -0
  90. package/dist/esm/utils.js +4 -1
  91. package/dist/esm/utils.js.map +1 -1
  92. package/dist/esm/{import-protection-plugin → vite/import-protection-plugin}/plugin.js +41 -92
  93. package/dist/esm/vite/import-protection-plugin/plugin.js.map +1 -0
  94. package/dist/esm/{import-protection-plugin → vite/import-protection-plugin}/types.d.ts +5 -5
  95. package/dist/esm/vite/import-protection-plugin/virtualModules.d.ts +8 -0
  96. package/dist/esm/vite/import-protection-plugin/virtualModules.js +49 -0
  97. package/dist/esm/vite/import-protection-plugin/virtualModules.js.map +1 -0
  98. package/dist/esm/vite/plugin.js +4 -12
  99. package/dist/esm/vite/plugin.js.map +1 -1
  100. package/dist/esm/vite/plugins.d.ts +1 -5
  101. package/dist/esm/vite/plugins.js +2 -17
  102. package/dist/esm/vite/plugins.js.map +1 -1
  103. package/dist/esm/vite/post-server-build.js +14 -32
  104. package/dist/esm/vite/post-server-build.js.map +1 -1
  105. package/dist/esm/vite/prerender.d.ts +2 -2
  106. package/dist/esm/vite/prerender.js +17 -147
  107. package/dist/esm/vite/prerender.js.map +1 -1
  108. package/dist/esm/vite/schema.d.ts +23 -23
  109. package/dist/esm/vite/start-compiler-plugin/hot-update.d.ts +2 -0
  110. package/dist/esm/vite/start-compiler-plugin/hot-update.js +16 -0
  111. package/dist/esm/vite/start-compiler-plugin/hot-update.js.map +1 -0
  112. package/dist/esm/vite/start-compiler-plugin/module-specifier.js +9 -4
  113. package/dist/esm/vite/start-compiler-plugin/module-specifier.js.map +1 -1
  114. package/dist/esm/vite/start-compiler-plugin/plugin.js +86 -13
  115. package/dist/esm/vite/start-compiler-plugin/plugin.js.map +1 -1
  116. package/dist/esm/vite/start-manifest-plugin/normalized-client-build.js +2 -2
  117. package/dist/esm/vite/start-manifest-plugin/normalized-client-build.js.map +1 -1
  118. package/dist/esm/vite/start-manifest-plugin/plugin.d.ts +1 -2
  119. package/dist/esm/vite/start-manifest-plugin/plugin.js +48 -14
  120. package/dist/esm/vite/start-manifest-plugin/plugin.js.map +1 -1
  121. package/package.json +17 -4
  122. package/src/import-protection/INTERNALS.md +266 -0
  123. package/src/import-protection/adapterUtils.ts +94 -0
  124. package/src/import-protection/analysis.ts +853 -0
  125. package/src/{import-protection-plugin → import-protection}/constants.ts +7 -0
  126. package/src/import-protection/rewrite.ts +229 -0
  127. package/src/{import-protection-plugin → import-protection}/sourceLocation.ts +125 -9
  128. package/src/{import-protection-plugin → import-protection}/trace.ts +0 -1
  129. package/src/{import-protection-plugin → import-protection}/utils.ts +35 -20
  130. package/src/{import-protection-plugin → import-protection}/virtualModules.ts +30 -177
  131. package/src/index.ts +5 -0
  132. package/src/post-build.ts +64 -0
  133. package/src/prerender.ts +292 -0
  134. package/src/rsbuild/INTERNALS-import-protection.md +169 -0
  135. package/src/rsbuild/dev-server.ts +129 -0
  136. package/src/rsbuild/import-protection.ts +1600 -0
  137. package/src/rsbuild/normalized-client-build.ts +346 -0
  138. package/src/rsbuild/planning.ts +234 -0
  139. package/src/rsbuild/plugin.ts +754 -0
  140. package/src/rsbuild/post-build.ts +96 -0
  141. package/src/rsbuild/schema.ts +31 -0
  142. package/src/rsbuild/start-compiler-host.ts +250 -0
  143. package/src/rsbuild/start-router-plugin.ts +86 -0
  144. package/src/rsbuild/swc-rsc.ts +166 -0
  145. package/src/rsbuild/types.ts +20 -0
  146. package/src/rsbuild/virtual-modules.ts +565 -0
  147. package/src/start-compiler/compiler.ts +153 -19
  148. package/src/start-compiler/handleCreateServerFn.ts +18 -0
  149. package/src/start-compiler/types.ts +1 -0
  150. package/src/start-manifest-plugin/manifestBuilder.ts +53 -116
  151. package/src/utils.ts +4 -0
  152. package/src/vite/import-protection-plugin/INTERNALS.md +187 -0
  153. package/src/{import-protection-plugin → vite/import-protection-plugin}/plugin.ts +73 -158
  154. package/src/{import-protection-plugin → vite/import-protection-plugin}/types.ts +5 -5
  155. package/src/vite/import-protection-plugin/virtualModules.ts +122 -0
  156. package/src/vite/plugin.ts +1 -18
  157. package/src/vite/plugins.ts +2 -33
  158. package/src/vite/post-server-build.ts +14 -57
  159. package/src/vite/prerender.ts +19 -260
  160. package/src/vite/start-compiler-plugin/hot-update.ts +24 -0
  161. package/src/vite/start-compiler-plugin/module-specifier.ts +15 -5
  162. package/src/vite/start-compiler-plugin/plugin.ts +193 -18
  163. package/src/vite/start-manifest-plugin/normalized-client-build.ts +15 -16
  164. package/src/vite/start-manifest-plugin/plugin.ts +121 -38
  165. package/dist/esm/import-protection-plugin/ast.js.map +0 -1
  166. package/dist/esm/import-protection-plugin/constants.d.ts +0 -6
  167. package/dist/esm/import-protection-plugin/constants.js.map +0 -1
  168. package/dist/esm/import-protection-plugin/defaults.js.map +0 -1
  169. package/dist/esm/import-protection-plugin/extensionlessAbsoluteIdResolver.js.map +0 -1
  170. package/dist/esm/import-protection-plugin/matchers.js.map +0 -1
  171. package/dist/esm/import-protection-plugin/plugin.js.map +0 -1
  172. package/dist/esm/import-protection-plugin/postCompileUsage.d.ts +0 -13
  173. package/dist/esm/import-protection-plugin/postCompileUsage.js +0 -63
  174. package/dist/esm/import-protection-plugin/postCompileUsage.js.map +0 -1
  175. package/dist/esm/import-protection-plugin/rewriteDeniedImports.js +0 -205
  176. package/dist/esm/import-protection-plugin/rewriteDeniedImports.js.map +0 -1
  177. package/dist/esm/import-protection-plugin/sourceLocation.js.map +0 -1
  178. package/dist/esm/import-protection-plugin/trace.js.map +0 -1
  179. package/dist/esm/import-protection-plugin/utils.js.map +0 -1
  180. package/dist/esm/import-protection-plugin/virtualModules.d.ts +0 -78
  181. package/dist/esm/import-protection-plugin/virtualModules.js.map +0 -1
  182. package/dist/esm/start-compiler/load-module.d.ts +0 -14
  183. package/dist/esm/start-compiler/load-module.js +0 -18
  184. package/dist/esm/start-compiler/load-module.js.map +0 -1
  185. package/src/import-protection-plugin/INTERNALS.md +0 -700
  186. package/src/import-protection-plugin/postCompileUsage.ts +0 -100
  187. package/src/import-protection-plugin/rewriteDeniedImports.ts +0 -379
  188. package/src/start-compiler/load-module.ts +0 -31
  189. /package/dist/esm/{import-protection-plugin → import-protection}/ast.d.ts +0 -0
  190. /package/dist/esm/{import-protection-plugin → import-protection}/defaults.d.ts +0 -0
  191. /package/dist/esm/{import-protection-plugin → import-protection}/extensionlessAbsoluteIdResolver.d.ts +0 -0
  192. /package/dist/esm/{import-protection-plugin → import-protection}/matchers.d.ts +0 -0
  193. /package/dist/esm/{import-protection-plugin → vite/import-protection-plugin}/plugin.d.ts +0 -0
  194. /package/src/{import-protection-plugin → import-protection}/ast.ts +0 -0
  195. /package/src/{import-protection-plugin → import-protection}/defaults.ts +0 -0
  196. /package/src/{import-protection-plugin → import-protection}/extensionlessAbsoluteIdResolver.ts +0 -0
  197. /package/src/{import-protection-plugin → import-protection}/matchers.ts +0 -0
@@ -0,0 +1,266 @@
1
+ # Import Protection - Shared Internals
2
+
3
+ ## Overview
4
+
5
+ `src/import-protection/` contains the adapter-independent core for TanStack
6
+ Start import protection.
7
+
8
+ Its current structure is:
9
+
10
+ - parse module code once per `TransformResult`
11
+ - derive a read-only `ImportAnalysis` object from that AST
12
+ - reuse that analysis for source extraction, import-specifier locations, mock
13
+ export discovery, named export discovery, and usage lookup
14
+ - keep AST mutation in a separate rewrite pass
15
+ - keep adapter lifecycle/state-machine behavior outside this directory
16
+
17
+ The shared layer is responsible for correctness-sensitive logic that must stay
18
+ aligned between Vite and Rsbuild.
19
+
20
+ ## Shared Module Layout
21
+
22
+ ### `analysis.ts`
23
+
24
+ The central shared analysis module.
25
+
26
+ It builds and caches `ImportAnalysis` on `TransformResult.analysis`.
27
+
28
+ `ImportAnalysis` contains:
29
+
30
+ - parsed AST
31
+ - line index
32
+ - import sources in source order
33
+ - import specifier literal locations
34
+ - imported bindings by source
35
+ - mock export names by source
36
+ - named exports
37
+ - usage-position cache
38
+
39
+ It exposes both result-based and code-based helpers:
40
+
41
+ - `getOrCreateImportAnalysis(result)`
42
+ - `getImportSourcesFromResult(result)`
43
+ - `getImportSources(code)`
44
+ - `getImportSpecifierLocationFromResult(result, source)`
45
+ - `getMockExportNamesBySourceFromResult(result)`
46
+ - `getMockExportNamesBySource(code)`
47
+ - `getNamedExportsFromResult(result)`
48
+ - `getNamedExports(code)`
49
+ - `findPostCompileUsagePosFromResult(result, source)`
50
+ - `findPostCompileUsagePos(code, source)`
51
+ - `findOriginalUnsafeUsagePosFromResult(result, source, envType)`
52
+ - `findOriginalUnsafeUsagePos(code, source, envType)`
53
+ - `isValidExportName(name)`
54
+
55
+ ### `adapterUtils.ts`
56
+
57
+ Contains small adapter-facing shared helpers for decisions that are identical in
58
+ Vite and Rsbuild.
59
+
60
+ It currently owns:
61
+
62
+ - environment type selection from `envTypeMap`
63
+ - environment rule selection from `compiledRules`
64
+ - normalized root-relative path calculation
65
+ - importer filtering via include/exclude/ignore/srcDirectory rules
66
+
67
+ This keeps the adapters from duplicating the same targeting logic while still
68
+ leaving lifecycle-specific behavior in the adapter implementations.
69
+
70
+ ### `rewrite.ts`
71
+
72
+ Contains the actual AST mutation pass for denied static imports and re-exports.
73
+
74
+ It is intentionally separate from `analysis.ts`.
75
+
76
+ Why:
77
+
78
+ - analysis is read-only and cacheable
79
+ - rewrite mutates AST structure and generates code/maps
80
+ - keeping them separate prevents adapter code from depending on mutation when it
81
+ only needs inspection
82
+
83
+ `rewriteDeniedImports()` rewrites denied static import/re-export edges to mock
84
+ module imports while preserving explicit bindings expected by the importer.
85
+
86
+ ### `sourceLocation.ts`
87
+
88
+ Builds source locations and snippets from transformed code plus sourcemaps.
89
+
90
+ It reuses shared analysis for two important lookups:
91
+
92
+ - import-specifier positions in transformed code
93
+ - post-compile and original-source usage locations
94
+
95
+ It also owns:
96
+
97
+ - line-index helpers
98
+ - sourcemap normalization
99
+ - `sourcesContent` original-source selection
100
+ - import-location caching for traces
101
+ - code snippet formatting
102
+
103
+ ### `virtualModules.ts`
104
+
105
+ Owns the shared mock module generators and payload encoding.
106
+
107
+ This includes:
108
+
109
+ - silent mock module code
110
+ - runtime diagnostic mock module code
111
+ - mock-edge module code with explicit named exports
112
+ - self-denial module generation for denied files
113
+ - marker module loading
114
+
115
+ The adapter-specific transport of these modules is not handled here.
116
+
117
+ ### `defaults.ts`, `matchers.ts`, `utils.ts`, `trace.ts`
118
+
119
+ These remain shared support layers:
120
+
121
+ - `defaults.ts`: default deny/marker rules
122
+ - `matchers.ts`: compiled glob/regex matcher utilities
123
+ - `utils.ts`: path normalization, candidate generation, messages, defer logic,
124
+ generic helpers
125
+ - `trace.ts`: import graph, traces, snippet-aware message formatting
126
+
127
+ ## Analysis Lifetime And Cache Model
128
+
129
+ The key cache decision is: analysis is cached per `TransformResult`, not
130
+ globally by source text.
131
+
132
+ That means:
133
+
134
+ - Vite can attach analysis to transform-cache results for the current module
135
+ version
136
+ - Rsbuild can attach analysis to transform results rebuilt from compilation or
137
+ preloaded during transform
138
+ - cache invalidation follows the host tool's normal module invalidation model
139
+ - long dev/HMR sessions do not accumulate an unbounded process-global AST cache
140
+
141
+ The analysis object is read-only after creation, except for the internal
142
+ `usageByKey` memoization map.
143
+
144
+ ## Import Source Extraction
145
+
146
+ `getImportSources()` is now AST-driven and returns sources in encountered order.
147
+
148
+ It includes:
149
+
150
+ - `import ... from 'x'`
151
+ - `export ... from 'x'`
152
+ - `export * from 'x'`
153
+ - `import('x')`
154
+ - dynamic import call forms supported by Babel's AST shape
155
+
156
+ Adapter code that needs source extraction should call into `analysis.ts`
157
+ directly.
158
+
159
+ ## Mock Export Discovery
160
+
161
+ Mock-edge modules need to know which named exports the importer expects.
162
+
163
+ `analysis.ts` derives that from the importer AST by recording:
164
+
165
+ - named import specifiers
166
+ - namespace member access
167
+ - default-import member access
168
+ - re-export specifiers
169
+
170
+ This produces `mockExportNamesBySource`, which is a safe over-approximation of
171
+ the named exports the adapter should expose on a mock-edge module.
172
+
173
+ ## Named Export Discovery
174
+
175
+ Self-denial needs the denied file's export surface.
176
+
177
+ `getNamedExports()` and `getNamedExportsFromResult()` collect valid named
178
+ exports from:
179
+
180
+ - function/class declarations
181
+ - variable declarations, including destructuring patterns
182
+ - export specifiers and aliases
183
+
184
+ `default` is excluded from the named export list because the mock generators
185
+ handle default export separately.
186
+
187
+ ## Usage Lookup And Safe Boundaries
188
+
189
+ Usage lookup is shared across adapters through `analysis.ts`.
190
+
191
+ The walker:
192
+
193
+ - tracks lexical/program/block/function bindings manually
194
+ - respects shadowing across nested scopes, block bindings, function params, and
195
+ `catch` params
196
+ - treats certain compiler-recognized boundaries as safe when looking for
197
+ original unsafe usage
198
+
199
+ Safe boundary recognition is environment-specific:
200
+
201
+ - client env safe boundaries:
202
+ - `createServerFn().handler(...)`
203
+ - `createMiddleware(...).server(...)`
204
+ - `createIsomorphicFn().server(...)`
205
+ - `createServerOnlyFn(...)`
206
+ - server env safe boundaries:
207
+ - `createClientOnlyFn(...)`
208
+ - `createIsomorphicFn().client(...)`
209
+
210
+ This is the core shared logic that keeps both adapters aligned on cases like
211
+ `boundary-safe`, `factory-safe`, and `compiler-leak`.
212
+
213
+ ## Rewriting Versus Analysis
214
+
215
+ Analysis and rewrite are intentionally separate.
216
+
217
+ Reasons:
218
+
219
+ - adapters frequently need inspection without mutation
220
+ - mutation wants a fresh, isolated AST/codegen pass
221
+ - read-only analysis is much easier to cache safely
222
+ - source-location helpers and usage lookup should not depend on whether a module
223
+ was rewritten
224
+
225
+ The intended flow is:
226
+
227
+ 1. build or reuse `ImportAnalysis`
228
+ 2. inspect/import-locate/trace as needed
229
+ 3. if a denied-specifier rewrite is needed, run `rewriteDeniedImports()`
230
+
231
+ ## Adapter Contract
232
+
233
+ The shared layer deliberately does not decide:
234
+
235
+ - when a violation is discovered
236
+ - when it is reported versus deferred
237
+ - how dev/build lifecycle hooks work
238
+ - how virtual module ids/files are transported by the bundler
239
+ - how final compilation truth is obtained
240
+
241
+ Those are adapter responsibilities.
242
+
243
+ The shared layer does provide the primitives both adapters rely on:
244
+
245
+ - rule helpers
246
+ - AST parsing
247
+ - analysis
248
+ - rewrite
249
+ - virtual module code generation
250
+ - source locations/snippets
251
+ - trace formatting
252
+
253
+ ## Maintainer Guidance
254
+
255
+ When making future import-protection changes:
256
+
257
+ 1. If the behavior is about parsing, source extraction, export discovery, usage
258
+ lookup, source locations, rewrite mechanics, or mock codegen, it probably
259
+ belongs in `src/import-protection/`.
260
+ 2. If the behavior is about hook timing, deferral, compilation truth, pending
261
+ queues, or environment-specific virtual-module transport, it probably belongs
262
+ in the adapter docs/code.
263
+ 3. Prefer extending `ImportAnalysis` over adding new one-off parsers or wrapper
264
+ helpers.
265
+ 4. Avoid reintroducing global source-text caches; cache on `TransformResult`
266
+ instead.
@@ -0,0 +1,94 @@
1
+ import { matchesAny } from './matchers'
2
+ import { isInsideDirectory, normalizeFilePath, relativizePath } from './utils'
3
+ import type { CompiledMatcher } from './matchers'
4
+
5
+ export type ImportProtectionEnvType = 'client' | 'server'
6
+
7
+ export interface ImportProtectionEnvRules {
8
+ specifiers: Array<CompiledMatcher>
9
+ files: Array<CompiledMatcher>
10
+ excludeFiles: Array<CompiledMatcher>
11
+ }
12
+
13
+ /**
14
+ * Shared subset of adapter config used to decide which environment rules apply
15
+ * and whether an importer should be checked at all.
16
+ */
17
+ export interface ImportProtectionAdapterConfig {
18
+ root: string
19
+ srcDirectory: string
20
+ compiledRules: {
21
+ client: ImportProtectionEnvRules
22
+ server: ImportProtectionEnvRules
23
+ }
24
+ includeMatchers: Array<CompiledMatcher>
25
+ excludeMatchers: Array<CompiledMatcher>
26
+ ignoreImporterMatchers: Array<CompiledMatcher>
27
+ envTypeMap: Map<string, ImportProtectionEnvType>
28
+ }
29
+
30
+ export function getImportProtectionRelativePath(
31
+ root: string,
32
+ absolutePath: string,
33
+ ): string {
34
+ return relativizePath(normalizeFilePath(absolutePath), root)
35
+ }
36
+
37
+ export function getImportProtectionEnvType(
38
+ config: Pick<ImportProtectionAdapterConfig, 'envTypeMap'>,
39
+ envName: string,
40
+ ): ImportProtectionEnvType {
41
+ return config.envTypeMap.get(envName) ?? 'server'
42
+ }
43
+
44
+ export function getImportProtectionRulesForEnvironment(
45
+ config: Pick<ImportProtectionAdapterConfig, 'compiledRules' | 'envTypeMap'>,
46
+ envName: string,
47
+ ): ImportProtectionEnvRules {
48
+ return getImportProtectionEnvType(config, envName) === 'client'
49
+ ? config.compiledRules.client
50
+ : config.compiledRules.server
51
+ }
52
+
53
+ export function shouldCheckImportProtectionImporter(
54
+ config: Pick<
55
+ ImportProtectionAdapterConfig,
56
+ | 'root'
57
+ | 'srcDirectory'
58
+ | 'includeMatchers'
59
+ | 'excludeMatchers'
60
+ | 'ignoreImporterMatchers'
61
+ >,
62
+ importer: string,
63
+ cache?: Map<string, boolean>,
64
+ ): boolean {
65
+ const normalizedImporter = normalizeFilePath(importer)
66
+
67
+ if (cache) {
68
+ const cached = cache.get(normalizedImporter)
69
+ if (cached !== undefined) {
70
+ return cached
71
+ }
72
+ }
73
+
74
+ const relativePath = relativizePath(normalizedImporter, config.root)
75
+
76
+ let result: boolean
77
+ if (
78
+ (config.excludeMatchers.length > 0 &&
79
+ matchesAny(relativePath, config.excludeMatchers)) ||
80
+ (config.ignoreImporterMatchers.length > 0 &&
81
+ matchesAny(relativePath, config.ignoreImporterMatchers))
82
+ ) {
83
+ result = false
84
+ } else if (config.includeMatchers.length > 0) {
85
+ result = !!matchesAny(relativePath, config.includeMatchers)
86
+ } else if (config.srcDirectory) {
87
+ result = isInsideDirectory(normalizedImporter, config.srcDirectory)
88
+ } else {
89
+ result = true
90
+ }
91
+
92
+ cache?.set(normalizedImporter, result)
93
+ return result
94
+ }