@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,69 +1,87 @@
1
1
  import { normalizePath } from 'vite'
2
2
 
3
- import { resolveViteId } from '../utils'
4
- import { VITE_ENVIRONMENT_NAMES } from '../constants'
5
- import { ImportGraph, buildTrace, formatViolation } from './trace'
3
+ import { resolveViteId } from '../../utils'
4
+ import { VITE_ENVIRONMENT_NAMES } from '../../constants'
5
+ import {
6
+ ImportGraph,
7
+ buildTrace,
8
+ formatViolation,
9
+ } from '../../import-protection/trace'
6
10
  import {
7
11
  getDefaultImportProtectionRules,
8
12
  getMarkerSpecifiers,
9
- } from './defaults'
10
- import { compileMatchers, matchesAny } from './matchers'
13
+ } from '../../import-protection/defaults'
14
+ import { compileMatchers, matchesAny } from '../../import-protection/matchers'
15
+ import {
16
+ getImportProtectionEnvType,
17
+ getImportProtectionRelativePath,
18
+ getImportProtectionRulesForEnvironment,
19
+ shouldCheckImportProtectionImporter,
20
+ } from '../../import-protection/adapterUtils'
11
21
  import {
12
22
  buildResolutionCandidates,
13
23
  buildSourceCandidates,
14
24
  canonicalizeResolvedId,
25
+ checkFileDenial,
15
26
  clearNormalizeFilePathCache,
16
27
  debugLog,
17
28
  dedupePatterns,
29
+ dedupeViolationKey,
18
30
  escapeRegExp,
19
- extractImportSources,
20
31
  getOrCreate,
32
+ isFileExcluded,
21
33
  isInsideDirectory,
22
34
  matchesDebugFilter,
23
35
  normalizeFilePath,
24
36
  relativizePath,
25
37
  shouldDeferViolation,
26
- } from './utils'
38
+ } from '../../import-protection/utils'
27
39
  import {
28
- collectMockExportNamesBySource,
29
- collectNamedExports,
30
- rewriteDeniedImports,
31
- } from './rewriteDeniedImports'
32
- import {
33
- MOCK_BUILD_PREFIX,
34
- generateDevSelfDenialModule,
35
- generateSelfContainedMockModule,
36
- getResolvedVirtualModuleMatchers,
37
- loadResolvedVirtualModule,
38
- makeMockEdgeModuleId,
39
- mockRuntimeModuleIdFromViolation,
40
- resolveInternalVirtualModuleId,
41
- resolvedMarkerVirtualModuleId,
42
- } from './virtualModules'
43
- import { ExtensionlessAbsoluteIdResolver } from './extensionlessAbsoluteIdResolver'
40
+ getImportSources,
41
+ getMockExportNamesBySource,
42
+ getNamedExports,
43
+ } from '../../import-protection/analysis'
44
+ import { rewriteDeniedImports } from '../../import-protection/rewrite'
45
+ import { ExtensionlessAbsoluteIdResolver } from '../../import-protection/extensionlessAbsoluteIdResolver'
44
46
  import {
45
47
  IMPORT_PROTECTION_DEBUG,
46
48
  SERVER_FN_LOOKUP_QUERY,
47
49
  VITE_BROWSER_VIRTUAL_PREFIX,
48
- } from './constants'
50
+ } from '../../import-protection/constants'
49
51
  import {
50
52
  ImportLocCache,
51
53
  addTraceImportLocations,
52
54
  buildCodeSnippet,
53
55
  buildLineIndex,
56
+ createImportSpecifierLocationIndex,
54
57
  findImportStatementLocationFromTransformed,
55
58
  findPostCompileUsageLocation,
59
+ normalizeSourceMap,
56
60
  pickOriginalCodeFromSourcesContent,
57
- } from './sourceLocation'
61
+ } from '../../import-protection/sourceLocation'
62
+ import {
63
+ MOCK_BUILD_PREFIX,
64
+ generateDevSelfDenialModule,
65
+ generateSelfContainedMockModule,
66
+ getResolvedVirtualModuleMatchers,
67
+ loadResolvedVirtualModule,
68
+ makeMockEdgeModuleId,
69
+ mockRuntimeModuleIdFromViolation,
70
+ resolveInternalVirtualModuleId,
71
+ resolvedMarkerVirtualModuleId,
72
+ } from './virtualModules'
58
73
  import type { PluginOption, ViteDevServer } from 'vite'
59
- import type { CompiledMatcher } from './matchers'
60
- import type { Loc, TraceStep, ViolationInfo } from './trace'
74
+ import type {
75
+ Loc,
76
+ TraceStep,
77
+ ViolationInfo,
78
+ } from '../../import-protection/trace'
61
79
  import type {
62
80
  SourceMapLike,
63
81
  TransformResult,
64
82
  TransformResultProvider,
65
- } from './sourceLocation'
66
- import type { ImportProtectionOptions } from '../schema'
83
+ } from '../../import-protection/sourceLocation'
84
+ import type { ImportProtectionOptions } from '../../schema'
67
85
  import type {
68
86
  EnvRules,
69
87
  EnvState,
@@ -85,31 +103,7 @@ export function importProtectionPlugin(
85
103
  const extensionlessIdResolver = new ExtensionlessAbsoluteIdResolver()
86
104
  const resolveExtensionlessAbsoluteId = (id: string) =>
87
105
  extensionlessIdResolver.resolve(id)
88
-
89
- const importPatternCache = new Map<string, Array<RegExp>>()
90
-
91
- function findFirstImportSpecifierIndex(code: string, source: string): number {
92
- let patterns = importPatternCache.get(source)
93
- if (!patterns) {
94
- const escaped = escapeRegExp(source)
95
- patterns = [
96
- new RegExp(`\\bimport\\s+(['"])${escaped}\\1`),
97
- new RegExp(`\\bfrom\\s+(['"])${escaped}\\1`),
98
- new RegExp(`\\bimport\\s*\\(\\s*(['"])${escaped}\\1\\s*\\)`),
99
- ]
100
- importPatternCache.set(source, patterns)
101
- }
102
-
103
- let best = -1
104
- for (const re of patterns) {
105
- const m = re.exec(code)
106
- if (!m) continue
107
- const idx = m.index + m[0].indexOf(source)
108
- if (idx === -1) continue
109
- if (best === -1 || idx < best) best = idx
110
- }
111
- return best
112
- }
106
+ const importSpecifierLocationIndex = createImportSpecifierLocationIndex()
113
107
 
114
108
  /**
115
109
  * Build an import trace using Vite's per-environment module graph, which
@@ -265,7 +259,7 @@ export function importProtectionPlugin(
265
259
  provider,
266
260
  trace,
267
261
  env.importLocCache,
268
- findFirstImportSpecifierIndex,
262
+ importSpecifierLocationIndex.find,
269
263
  )
270
264
 
271
265
  if (trace.length > 0) {
@@ -361,7 +355,7 @@ export function importProtectionPlugin(
361
355
  importer,
362
356
  candidate,
363
357
  env.importLocCache,
364
- findFirstImportSpecifierIndex,
358
+ importSpecifierLocationIndex.find,
365
359
  ))
366
360
  if (loc) return loc
367
361
  }
@@ -383,7 +377,6 @@ export function importProtectionPlugin(
383
377
  importer: string,
384
378
  source: string,
385
379
  resolvedId: string,
386
- relativePath: string,
387
380
  traceOverride?: Array<TraceStep>,
388
381
  ): Promise<ViolationInfo | undefined> {
389
382
  const normalizedResolvedId = normalizeFilePath(resolvedId)
@@ -406,21 +399,11 @@ export function importProtectionPlugin(
406
399
  {
407
400
  type: 'marker',
408
401
  resolved: normalizedResolvedId,
409
- message: buildMarkerViolationMessage(relativePath, markerKind),
410
402
  },
411
403
  traceOverride,
412
404
  )
413
405
  }
414
406
 
415
- function buildMarkerViolationMessage(
416
- relativePath: string,
417
- markerKind: 'server' | 'client' | undefined,
418
- ): string {
419
- return markerKind === 'server'
420
- ? `Module "${relativePath}" is marked server-only but is imported in the client environment`
421
- : `Module "${relativePath}" is marked client-only but is imported in the server environment`
422
- }
423
-
424
407
  async function buildFileViolationInfo(
425
408
  provider: TransformResultProvider,
426
409
  env: EnvState,
@@ -433,8 +416,6 @@ export function importProtectionPlugin(
433
416
  pattern: string | RegExp,
434
417
  traceOverride?: Array<TraceStep>,
435
418
  ): Promise<ViolationInfo> {
436
- const relativePath = getRelativePath(resolvedPath)
437
-
438
419
  return buildViolationInfo(
439
420
  provider,
440
421
  env,
@@ -447,44 +428,13 @@ export function importProtectionPlugin(
447
428
  type: 'file',
448
429
  pattern,
449
430
  resolved: resolvedPath,
450
- message: `Import "${source}" (resolved to "${relativePath}") is denied in the ${envType} environment`,
451
431
  },
452
432
  traceOverride,
453
433
  )
454
434
  }
455
435
 
456
- function getEnvType(envName: string): 'client' | 'server' {
457
- return config.envTypeMap.get(envName) ?? 'server'
458
- }
459
-
460
436
  function getRulesForEnvironment(envName: string): EnvRules {
461
- const type = getEnvType(envName)
462
- return type === 'client'
463
- ? config.compiledRules.client
464
- : config.compiledRules.server
465
- }
466
-
467
- /**
468
- * Check if a relative path matches any denied file pattern for the given
469
- * environment, respecting `excludeFiles`. Returns the matching pattern
470
- * or `undefined` if the file is not denied.
471
- */
472
- function checkFileDenial(
473
- relativePath: string,
474
- matchers: {
475
- files: Array<CompiledMatcher>
476
- excludeFiles: Array<CompiledMatcher>
477
- },
478
- ): CompiledMatcher | undefined {
479
- if (
480
- matchers.excludeFiles.length > 0 &&
481
- matchesAny(relativePath, matchers.excludeFiles)
482
- ) {
483
- return undefined
484
- }
485
- return matchers.files.length > 0
486
- ? matchesAny(relativePath, matchers.files)
487
- : undefined
437
+ return getImportProtectionRulesForEnvironment(config, envName) as EnvRules
488
438
  }
489
439
 
490
440
  const environmentNames = new Set<string>([
@@ -589,7 +539,7 @@ export function importProtectionPlugin(
589
539
  return []
590
540
 
591
541
  try {
592
- parsedBySource = collectMockExportNamesBySource(importerCode)
542
+ parsedBySource = getMockExportNamesBySource(importerCode)
593
543
 
594
544
  // Also index by resolved physical IDs so later lookups match.
595
545
  await recordMockExportsForImporter(
@@ -680,18 +630,23 @@ export function importProtectionPlugin(
680
630
 
681
631
  if (namesBySource.size === 0) return
682
632
 
633
+ const resolvedAliases = new Map<string, Array<string>>()
683
634
  for (const [source, names] of namesBySource) {
684
635
  try {
685
636
  const resolvedId = await resolveSource(source)
686
637
  if (!resolvedId) continue
687
638
 
688
- namesBySource.set(normalizeFilePath(resolvedId), names)
689
- namesBySource.set(resolveExtensionlessAbsoluteId(resolvedId), names)
639
+ resolvedAliases.set(normalizeFilePath(resolvedId), names)
640
+ resolvedAliases.set(resolveExtensionlessAbsoluteId(resolvedId), names)
690
641
  } catch {
691
642
  // Best-effort only
692
643
  }
693
644
  }
694
645
 
646
+ for (const [source, names] of resolvedAliases) {
647
+ namesBySource.set(source, names)
648
+ }
649
+
695
650
  const existing = env.mockExportsByImporter.get(importerFile)
696
651
  if (!existing) {
697
652
  env.mockExportsByImporter.set(importerFile, namesBySource)
@@ -712,34 +667,15 @@ export function importProtectionPlugin(
712
667
 
713
668
  const shouldCheckImporterCache = new Map<string, boolean>()
714
669
  function shouldCheckImporter(importer: string): boolean {
715
- let result = shouldCheckImporterCache.get(importer)
716
- if (result !== undefined) return result
717
-
718
- const relativePath = relativizePath(importer, config.root)
719
-
720
- // Excluded or ignored importers are never checked.
721
- const excluded =
722
- (config.excludeMatchers.length > 0 &&
723
- matchesAny(relativePath, config.excludeMatchers)) ||
724
- (config.ignoreImporterMatchers.length > 0 &&
725
- matchesAny(relativePath, config.ignoreImporterMatchers))
726
-
727
- if (excluded) {
728
- result = false
729
- } else if (config.includeMatchers.length > 0) {
730
- result = !!matchesAny(relativePath, config.includeMatchers)
731
- } else if (config.srcDirectory) {
732
- result = isInsideDirectory(importer, config.srcDirectory)
733
- } else {
734
- result = true
735
- }
736
-
737
- shouldCheckImporterCache.set(importer, result)
738
- return result
670
+ return shouldCheckImportProtectionImporter(
671
+ config,
672
+ importer,
673
+ shouldCheckImporterCache,
674
+ )
739
675
  }
740
676
 
741
677
  function dedupeKey(info: ViolationInfo): string {
742
- return `${info.type}:${info.importer}:${info.specifier}:${info.resolved ?? ''}`
678
+ return dedupeViolationKey(info)
743
679
  }
744
680
 
745
681
  function hasSeen(env: EnvState, key: string): boolean {
@@ -750,7 +686,7 @@ export function importProtectionPlugin(
750
686
  }
751
687
 
752
688
  function getRelativePath(absolutePath: string): string {
753
- return relativizePath(normalizePath(absolutePath), config.root)
689
+ return getImportProtectionRelativePath(config.root, absolutePath)
754
690
  }
755
691
 
756
692
  /** Reset all caches on an EnvState (called from buildStart). */
@@ -1354,7 +1290,6 @@ export function importProtectionPlugin(
1354
1290
  // Clear memoization caches that grow unboundedly across builds
1355
1291
  clearNormalizeFilePathCache()
1356
1292
  extensionlessIdResolver.clear()
1357
- importPatternCache.clear()
1358
1293
  shouldCheckImporterCache.clear()
1359
1294
 
1360
1295
  // Clear per-env caches
@@ -1391,7 +1326,7 @@ export function importProtectionPlugin(
1391
1326
  async resolveId(source, importer, _options) {
1392
1327
  const envName = this.environment.name
1393
1328
  const env = getEnv(envName)
1394
- const envType = getEnvType(envName)
1329
+ const envType = getImportProtectionEnvType(config, envName)
1395
1330
  const provider = env.transformResultProvider
1396
1331
  const isScanResolve = !!(_options as Record<string, unknown>).scan
1397
1332
 
@@ -1500,10 +1435,6 @@ export function importProtectionPlugin(
1500
1435
  source,
1501
1436
  {
1502
1437
  type: 'marker',
1503
- message: buildMarkerViolationMessage(
1504
- getRelativePath(normalizedImporter),
1505
- markerKind,
1506
- ),
1507
1438
  },
1508
1439
  )
1509
1440
  const markerResult = await reportOrDeferViolation(
@@ -1563,7 +1494,6 @@ export function importProtectionPlugin(
1563
1494
  importerFile,
1564
1495
  specifier,
1565
1496
  normalizedImporter,
1566
- getRelativePath(normalizedImporter),
1567
1497
  )
1568
1498
  if (markerInfo) {
1569
1499
  deferViolation(
@@ -1610,7 +1540,6 @@ export function importProtectionPlugin(
1610
1540
  {
1611
1541
  type: 'specifier',
1612
1542
  pattern: specifierMatch.pattern,
1613
- message: `Import "${source}" is denied in the ${envType} environment`,
1614
1543
  },
1615
1544
  )
1616
1545
 
@@ -1678,15 +1607,10 @@ export function importProtectionPlugin(
1678
1607
  // exports `index.client.js`) are not treated as user-authored
1679
1608
  // environment boundaries. Users can override `excludeFiles` per
1680
1609
  // environment to narrow or widen this exclusion.
1681
- const isExcludedFile =
1682
- matchers.excludeFiles.length > 0 &&
1683
- matchesAny(relativePath, matchers.excludeFiles)
1610
+ const isExcludedFile = isFileExcluded(relativePath, matchers)
1684
1611
 
1685
1612
  if (!isExcludedFile) {
1686
- const fileMatch =
1687
- matchers.files.length > 0
1688
- ? matchesAny(relativePath, matchers.files)
1689
- : undefined
1613
+ const fileMatch = checkFileDenial(relativePath, matchers)
1690
1614
 
1691
1615
  if (fileMatch) {
1692
1616
  const info = await buildFileViolationInfo(
@@ -1719,7 +1643,6 @@ export function importProtectionPlugin(
1719
1643
  importer,
1720
1644
  source,
1721
1645
  resolved,
1722
- relativePath,
1723
1646
  )
1724
1647
  if (markerInfo) {
1725
1648
  return reportOrDeferViolation(
@@ -1891,7 +1814,7 @@ export function importProtectionPlugin(
1891
1814
  async handler(code, id) {
1892
1815
  const envName = this.environment.name
1893
1816
  const file = normalizeFilePath(id)
1894
- const envType = getEnvType(envName)
1817
+ const envType = getImportProtectionEnvType(config, envName)
1895
1818
  const matchers = getRulesForEnvironment(envName)
1896
1819
  const isBuild = config.command === 'build'
1897
1820
 
@@ -1930,7 +1853,7 @@ export function importProtectionPlugin(
1930
1853
  // Falls back to empty list on non-standard syntax.
1931
1854
  let exportNames: Array<string> = []
1932
1855
  try {
1933
- exportNames = collectNamedExports(code)
1856
+ exportNames = getNamedExports(code)
1934
1857
  } catch {
1935
1858
  // Parsing may fail on non-standard syntax
1936
1859
  }
@@ -1952,7 +1875,6 @@ export function importProtectionPlugin(
1952
1875
  specifier: relativizePath(file, config.root),
1953
1876
  resolved: file,
1954
1877
  pattern: selfFileMatch.pattern,
1955
- message: `File "${relativizePath(file, config.root)}" is denied in the ${envType} environment`,
1956
1878
  trace: [],
1957
1879
  },
1958
1880
  config.mockAccess,
@@ -2009,7 +1931,7 @@ export function importProtectionPlugin(
2009
1931
  // Dev mode: resolve imports, populate graph, detect violations,
2010
1932
  // and rewrite denied imports.
2011
1933
  const isDevMock = config.effectiveBehavior === 'mock'
2012
- const importSources = extractImportSources(code)
1934
+ const importSources = getImportSources(code)
2013
1935
  const resolvedChildren = new Set<string>()
2014
1936
  const deniedSourceReplacements = new Map<string, string>()
2015
1937
  for (const src of importSources) {
@@ -2116,14 +2038,7 @@ export function importProtectionPlugin(
2116
2038
  }
2117
2039
 
2118
2040
  const normalizedMap = rewritten.map
2119
- ? {
2120
- ...rewritten.map,
2121
- version: Number(rewritten.map.version),
2122
- sourcesContent:
2123
- rewritten.map.sourcesContent?.map(
2124
- (s: string | null) => s ?? '',
2125
- ) ?? [],
2126
- }
2041
+ ? normalizeSourceMap(rewritten.map)
2127
2042
  : {
2128
2043
  version: 3,
2129
2044
  file: id,
@@ -1,12 +1,12 @@
1
- import type { CompileStartFrameworkOptions, GetConfigFn } from '../types'
2
- import type { ImportProtectionBehavior } from '../schema'
3
- import type { CompiledMatcher } from './matchers'
4
- import type { ImportGraph, ViolationInfo } from './trace'
1
+ import type { CompileStartFrameworkOptions, GetConfigFn } from '../../types'
2
+ import type { ImportProtectionBehavior } from '../../schema'
3
+ import type { CompiledMatcher } from '../../import-protection/matchers'
4
+ import type { ImportGraph, ViolationInfo } from '../../import-protection/trace'
5
5
  import type {
6
6
  ImportLocCache,
7
7
  TransformResult,
8
8
  TransformResultProvider,
9
- } from './sourceLocation'
9
+ } from '../../import-protection/sourceLocation'
10
10
 
11
11
  /** Compiled deny/exclude patterns for one environment (client or server). */
12
12
  export interface EnvRules {
@@ -0,0 +1,122 @@
1
+ import { resolveViteId } from '../../utils'
2
+ import {
3
+ MARKER_PREFIX,
4
+ MOCK_BUILD_PREFIX,
5
+ MOCK_EDGE_PREFIX,
6
+ MOCK_MODULE_ID,
7
+ MOCK_RUNTIME_PREFIX,
8
+ RUNTIME_SUGGESTION_TEXT,
9
+ generateDevSelfDenialModule,
10
+ generateSelfContainedMockModule,
11
+ loadMarkerModule,
12
+ loadMockEdgeModule,
13
+ loadMockRuntimeModule,
14
+ loadSilentMockModule,
15
+ makeMockEdgeModuleId,
16
+ mockRuntimeModuleIdFromViolation,
17
+ } from '../../import-protection/virtualModules'
18
+ import { VITE_BROWSER_VIRTUAL_PREFIX } from '../../import-protection/constants'
19
+
20
+ const RESOLVED_MOCK_MODULE_ID = resolveViteId(MOCK_MODULE_ID)
21
+ const RESOLVED_MOCK_BUILD_PREFIX = resolveViteId(MOCK_BUILD_PREFIX)
22
+ const RESOLVED_MOCK_EDGE_PREFIX = resolveViteId(MOCK_EDGE_PREFIX)
23
+ const RESOLVED_MOCK_RUNTIME_PREFIX = resolveViteId(MOCK_RUNTIME_PREFIX)
24
+ const RESOLVED_MARKER_PREFIX = resolveViteId(MARKER_PREFIX)
25
+ const RESOLVED_MARKER_SERVER_ONLY = resolveViteId(`${MARKER_PREFIX}server-only`)
26
+ const RESOLVED_MARKER_CLIENT_ONLY = resolveViteId(`${MARKER_PREFIX}client-only`)
27
+
28
+ export function resolvedMarkerVirtualModuleId(
29
+ kind: 'server' | 'client',
30
+ ): string {
31
+ return kind === 'server'
32
+ ? RESOLVED_MARKER_SERVER_ONLY
33
+ : RESOLVED_MARKER_CLIENT_ONLY
34
+ }
35
+
36
+ export function getResolvedVirtualModuleMatchers(): ReadonlyArray<string> {
37
+ return RESOLVED_VIRTUAL_MODULE_MATCHERS
38
+ }
39
+
40
+ const RESOLVED_VIRTUAL_MODULE_MATCHERS = [
41
+ RESOLVED_MOCK_MODULE_ID,
42
+ RESOLVED_MOCK_BUILD_PREFIX,
43
+ RESOLVED_MOCK_EDGE_PREFIX,
44
+ RESOLVED_MOCK_RUNTIME_PREFIX,
45
+ RESOLVED_MARKER_PREFIX,
46
+ ] as const
47
+
48
+ const RESOLVE_PREFIX_PAIRS = [
49
+ [MOCK_EDGE_PREFIX, RESOLVED_MOCK_EDGE_PREFIX],
50
+ [MOCK_RUNTIME_PREFIX, RESOLVED_MOCK_RUNTIME_PREFIX],
51
+ [MOCK_BUILD_PREFIX, RESOLVED_MOCK_BUILD_PREFIX],
52
+ [MARKER_PREFIX, RESOLVED_MARKER_PREFIX],
53
+ ] as const
54
+
55
+ export function resolveInternalVirtualModuleId(
56
+ source: string,
57
+ ): string | undefined {
58
+ if (source.startsWith(VITE_BROWSER_VIRTUAL_PREFIX)) {
59
+ return resolveInternalVirtualModuleId(
60
+ `\0${source.slice(VITE_BROWSER_VIRTUAL_PREFIX.length)}`,
61
+ )
62
+ }
63
+
64
+ if (source === MOCK_MODULE_ID || source === RESOLVED_MOCK_MODULE_ID) {
65
+ return RESOLVED_MOCK_MODULE_ID
66
+ }
67
+
68
+ for (const [unresolvedPrefix, resolvedPrefix] of RESOLVE_PREFIX_PAIRS) {
69
+ if (source.startsWith(unresolvedPrefix)) {
70
+ return resolveViteId(source)
71
+ }
72
+
73
+ if (source.startsWith(resolvedPrefix)) {
74
+ return source
75
+ }
76
+ }
77
+
78
+ return undefined
79
+ }
80
+
81
+ export function loadResolvedVirtualModule(
82
+ id: string,
83
+ ): { code: string } | undefined {
84
+ if (id === RESOLVED_MOCK_MODULE_ID) {
85
+ return loadSilentMockModule()
86
+ }
87
+
88
+ if (id.startsWith(RESOLVED_MOCK_BUILD_PREFIX)) {
89
+ return loadSilentMockModule()
90
+ }
91
+
92
+ if (id.startsWith(RESOLVED_MOCK_EDGE_PREFIX)) {
93
+ return loadMockEdgeModule(id.slice(RESOLVED_MOCK_EDGE_PREFIX.length))
94
+ }
95
+
96
+ if (id.startsWith(RESOLVED_MOCK_RUNTIME_PREFIX)) {
97
+ return loadMockRuntimeModule(id.slice(RESOLVED_MOCK_RUNTIME_PREFIX.length))
98
+ }
99
+
100
+ if (id.startsWith(RESOLVED_MARKER_PREFIX)) {
101
+ return loadMarkerModule()
102
+ }
103
+
104
+ return undefined
105
+ }
106
+
107
+ export {
108
+ MARKER_PREFIX,
109
+ MOCK_BUILD_PREFIX,
110
+ MOCK_EDGE_PREFIX,
111
+ MOCK_MODULE_ID,
112
+ MOCK_RUNTIME_PREFIX,
113
+ RUNTIME_SUGGESTION_TEXT,
114
+ generateDevSelfDenialModule,
115
+ generateSelfContainedMockModule,
116
+ loadMarkerModule,
117
+ loadMockEdgeModule,
118
+ loadMockRuntimeModule,
119
+ loadSilentMockModule,
120
+ makeMockEdgeModuleId,
121
+ mockRuntimeModuleIdFromViolation,
122
+ }
@@ -0,0 +1,8 @@
1
+ export type {
2
+ TanStackStartVitePluginCoreOptions,
3
+ ViteRscForwardSsrResolverStrategy,
4
+ } from './types'
5
+ export type { TanStackStartViteInputConfig } from './schema'
6
+ export { START_ENVIRONMENT_NAMES, VITE_ENVIRONMENT_NAMES } from '../constants'
7
+ export { createVirtualModule } from './createVirtualModule'
8
+ export { tanStackStartVite } from './plugin'
@@ -5,12 +5,12 @@ import {
5
5
  createStartConfigContext,
6
6
  } from '../config-context'
7
7
  import { START_ENVIRONMENT_NAMES } from '../constants'
8
- import { importProtectionPlugin } from '../import-protection-plugin/plugin'
9
8
  import {
10
9
  createServerFnBasePath,
11
10
  normalizePublicBase,
12
11
  shouldRewriteDevBasepath,
13
12
  } from '../planning'
13
+ import { importProtectionPlugin } from './import-protection-plugin/plugin'
14
14
  import { startCompilerPlugin } from './start-compiler-plugin/plugin'
15
15
  import { loadEnvPlugin } from './load-env-plugin/plugin'
16
16
  import {