rsbuild-plugin-react-router 0.1.1 → 0.3.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 (99) hide show
  1. package/README.md +185 -204
  2. package/dist/451.js +1087 -0
  3. package/dist/bounded-cache.d.ts +1 -0
  4. package/dist/build-manifest.d.ts +1 -1
  5. package/dist/build-output-transforms.d.ts +30 -0
  6. package/dist/concurrency.d.ts +2 -0
  7. package/dist/config-imports.d.ts +3 -0
  8. package/dist/constants.d.ts +3 -0
  9. package/dist/dev-generation.d.ts +25 -0
  10. package/dist/dev-runtime-artifacts.d.ts +39 -0
  11. package/dist/dev-runtime-compilation.d.ts +32 -0
  12. package/dist/dev-runtime-controller.d.ts +14 -0
  13. package/dist/dev-runtime-session.d.ts +34 -0
  14. package/dist/dev-server.d.ts +11 -2
  15. package/dist/export-utils.d.ts +15 -7
  16. package/dist/index.cjs +3085 -1272
  17. package/dist/index.d.ts +4 -1
  18. package/dist/index.js +2039 -1299
  19. package/dist/lazy-compilation.d.ts +5 -0
  20. package/dist/manifest.d.ts +32 -9
  21. package/dist/modify-browser-manifest.d.ts +30 -13
  22. package/dist/parallel-route-transform-protocol.d.ts +25 -0
  23. package/dist/parallel-route-transform-worker.d.ts +1 -0
  24. package/dist/parallel-route-transform-worker.js +38 -0
  25. package/dist/parallel-route-transforms.d.ts +14 -0
  26. package/dist/performance.d.ts +26 -0
  27. package/dist/plugin-utils.d.ts +2 -12
  28. package/dist/prerender-build.d.ts +32 -0
  29. package/dist/prerender.d.ts +28 -2
  30. package/dist/react-router-config.d.ts +8 -1
  31. package/dist/route-artifacts.d.ts +33 -0
  32. package/dist/route-ast.d.ts +21 -0
  33. package/dist/route-chunks.d.ts +9 -1
  34. package/dist/route-component-transform.d.ts +5 -0
  35. package/dist/route-export-pruning.d.ts +6 -0
  36. package/dist/route-export-resolution.d.ts +5 -0
  37. package/dist/route-transform-tasks.d.ts +47 -0
  38. package/dist/route-watch.d.ts +27 -0
  39. package/dist/server-build-plan.d.ts +22 -0
  40. package/dist/server-utils.d.ts +3 -2
  41. package/dist/templates/entry.client.js +3 -3
  42. package/dist/templates/entry.server.cjs +11 -8
  43. package/dist/templates/entry.server.js +5 -5
  44. package/dist/typegen.d.ts +2 -0
  45. package/dist/types.d.ts +30 -12
  46. package/dist/virtual-modules.d.ts +2 -0
  47. package/dist/warnings/warn-on-client-source-maps.d.ts +1 -0
  48. package/dist/yuku.d.ts +15 -0
  49. package/package.json +26 -22
  50. package/src/bounded-cache.ts +18 -0
  51. package/src/build-manifest.ts +168 -0
  52. package/src/build-output-transforms.ts +273 -0
  53. package/src/concurrency.ts +34 -0
  54. package/src/config-imports.ts +45 -0
  55. package/src/constants.ts +91 -0
  56. package/src/dev-generation.ts +700 -0
  57. package/src/dev-runtime-artifacts.ts +207 -0
  58. package/src/dev-runtime-compilation.ts +92 -0
  59. package/src/dev-runtime-controller.ts +462 -0
  60. package/src/dev-runtime-session.ts +184 -0
  61. package/src/dev-server.ts +58 -0
  62. package/src/export-utils.ts +219 -0
  63. package/src/index.ts +1025 -0
  64. package/src/lazy-compilation.ts +94 -0
  65. package/src/manifest.ts +480 -0
  66. package/src/modify-browser-manifest.ts +245 -0
  67. package/src/parallel-route-transform-protocol.ts +35 -0
  68. package/src/parallel-route-transform-worker.ts +82 -0
  69. package/src/parallel-route-transforms.ts +332 -0
  70. package/src/performance.ts +254 -0
  71. package/src/plugin-utils.ts +82 -0
  72. package/src/prerender-build.ts +623 -0
  73. package/src/prerender.ts +367 -0
  74. package/src/react-router-config.ts +220 -0
  75. package/src/route-artifacts.ts +155 -0
  76. package/src/route-ast.ts +163 -0
  77. package/src/route-chunks.ts +857 -0
  78. package/src/route-component-transform.ts +314 -0
  79. package/src/route-config.ts +106 -0
  80. package/src/route-export-pruning.ts +668 -0
  81. package/src/route-export-resolution.ts +329 -0
  82. package/src/route-transform-tasks.ts +249 -0
  83. package/src/route-watch.ts +322 -0
  84. package/src/server-build-plan.ts +91 -0
  85. package/src/server-utils.ts +210 -0
  86. package/src/ssr-externals.ts +59 -0
  87. package/src/templates/context.ts +12 -0
  88. package/src/templates/entry.client.tsx +12 -0
  89. package/src/templates/entry.server.tsx +76 -0
  90. package/src/typegen.ts +49 -0
  91. package/src/types.ts +82 -0
  92. package/src/validation/validate-plugin-order.ts +76 -0
  93. package/src/virtual-modules.ts +30 -0
  94. package/src/warnings/warn-on-client-source-maps.ts +96 -0
  95. package/src/yuku.ts +67 -0
  96. package/dist/906.js +0 -1
  97. package/dist/946.js +0 -1
  98. package/dist/babel.d.ts +0 -8
  99. package/dist/rslib-runtime.js +0 -23
@@ -0,0 +1,219 @@
1
+ import { readFile, stat } from 'node:fs/promises';
2
+ import { langFromPath, parse } from 'yuku-parser';
3
+ import { setBoundedCacheEntry } from './bounded-cache.js';
4
+ import {
5
+ getExportedName,
6
+ getIdentifierNamesFromPattern,
7
+ getProgram,
8
+ type AnyNode,
9
+ type ProgramNode,
10
+ } from './route-ast.js';
11
+
12
+ type ExportInfo = {
13
+ readonly exportNames: readonly string[];
14
+ readonly exportAllModules: readonly string[];
15
+ };
16
+
17
+ type RouteModuleAnalysis = {
18
+ readonly code: string;
19
+ readonly exports: readonly string[];
20
+ readonly exportAllModules: readonly string[];
21
+ };
22
+
23
+ type RouteModuleAnalysisCacheEntry = {
24
+ mtimeMs: number;
25
+ size: number;
26
+ analysis: Promise<RouteModuleAnalysis>;
27
+ };
28
+
29
+ const exportInfoCache = new Map<string, Promise<ExportInfo>>();
30
+ const routeModuleAnalysisCache = new Map<
31
+ string,
32
+ RouteModuleAnalysisCacheEntry
33
+ >();
34
+
35
+ const MAX_EXPORT_UTILS_CACHE_ENTRIES = 2048;
36
+
37
+ const cachePromiseOnReject = <T>(
38
+ promise: Promise<T>,
39
+ invalidate: () => void
40
+ ): Promise<T> =>
41
+ promise.catch(error => {
42
+ invalidate();
43
+ throw error;
44
+ });
45
+
46
+ const parseProgram = (code: string, resourcePath?: string): ProgramNode => {
47
+ const result = parse(code, {
48
+ sourceType: 'module',
49
+ lang: resourcePath ? langFromPath(resourcePath) : 'tsx',
50
+ });
51
+ const errors = result.diagnostics.filter(
52
+ diagnostic => diagnostic.severity === 'error'
53
+ );
54
+ if (errors.length > 0) {
55
+ throw new Error(errors.map(error => error.message).join('\n'));
56
+ }
57
+ return getProgram(result);
58
+ };
59
+
60
+ const isTypeOnlyExport = (node: AnyNode): boolean =>
61
+ node.exportKind === 'type' ||
62
+ node.type === 'TSExportAssignment' ||
63
+ node.declaration?.declare === true ||
64
+ (node.type === 'ExportDefaultDeclaration' &&
65
+ node.declaration?.type === 'TSInterfaceDeclaration');
66
+
67
+ export const collectProgramExportNames = (program: ProgramNode): string[] => {
68
+ const exportNames = new Set<string>();
69
+ for (const statement of program.body ?? []) {
70
+ if (isTypeOnlyExport(statement)) {
71
+ continue;
72
+ }
73
+
74
+ if (statement.type === 'ExportAllDeclaration') {
75
+ const exported = getExportedName(statement.exported);
76
+ if (exported) {
77
+ exportNames.add(exported);
78
+ }
79
+ continue;
80
+ }
81
+
82
+ if (statement.type === 'ExportDefaultDeclaration') {
83
+ exportNames.add('default');
84
+ continue;
85
+ }
86
+
87
+ if (statement.type !== 'ExportNamedDeclaration') {
88
+ continue;
89
+ }
90
+ const declaration = statement.declaration;
91
+ if (declaration) {
92
+ if (declaration.type === 'VariableDeclaration') {
93
+ for (const declarator of declaration.declarations ?? []) {
94
+ for (const name of getIdentifierNamesFromPattern(declarator.id)) {
95
+ exportNames.add(name);
96
+ }
97
+ }
98
+ } else if (
99
+ (declaration.type === 'FunctionDeclaration' ||
100
+ declaration.type === 'ClassDeclaration' ||
101
+ declaration.type === 'TSEnumDeclaration') &&
102
+ declaration.id?.name
103
+ ) {
104
+ exportNames.add(declaration.id.name);
105
+ }
106
+ continue;
107
+ }
108
+
109
+ for (const specifier of statement.specifiers ?? []) {
110
+ if (specifier.exportKind === 'type') {
111
+ continue;
112
+ }
113
+ const exported = getExportedName(specifier.exported);
114
+ if (exported) {
115
+ exportNames.add(exported);
116
+ }
117
+ }
118
+ }
119
+ return Array.from(exportNames);
120
+ };
121
+
122
+ const collectExportAllModules = (program: AnyNode): string[] => {
123
+ const modules: string[] = [];
124
+ for (const statement of program.body ?? []) {
125
+ if (
126
+ statement.type !== 'ExportAllDeclaration' ||
127
+ isTypeOnlyExport(statement)
128
+ ) {
129
+ continue;
130
+ }
131
+ if (statement.exported) {
132
+ continue;
133
+ }
134
+ const source = statement.source?.value;
135
+ if (typeof source === 'string') {
136
+ modules.push(source);
137
+ }
138
+ }
139
+ return modules;
140
+ };
141
+
142
+ export const getExportNames = async (
143
+ code: string
144
+ ): Promise<readonly string[]> => {
145
+ return (await getExportNamesAndExportAll(code)).exportNames;
146
+ };
147
+
148
+ export const getExportNamesAndExportAll = async (
149
+ code: string
150
+ ): Promise<ExportInfo> => {
151
+ const cached = exportInfoCache.get(code);
152
+ if (cached) {
153
+ return cached;
154
+ }
155
+
156
+ const exportInfo = (async () => {
157
+ const program = parseProgram(code);
158
+ return {
159
+ exportNames: collectProgramExportNames(program),
160
+ exportAllModules: collectExportAllModules(program),
161
+ };
162
+ })();
163
+
164
+ let trackedExportInfo: Promise<ExportInfo>;
165
+ trackedExportInfo = cachePromiseOnReject(exportInfo, () => {
166
+ if (exportInfoCache.get(code) === trackedExportInfo) {
167
+ exportInfoCache.delete(code);
168
+ }
169
+ });
170
+
171
+ setBoundedCacheEntry(
172
+ exportInfoCache,
173
+ code,
174
+ trackedExportInfo,
175
+ MAX_EXPORT_UTILS_CACHE_ENTRIES
176
+ );
177
+ return trackedExportInfo;
178
+ };
179
+
180
+ export const getRouteModuleAnalysis = async (
181
+ resourcePath: string
182
+ ): Promise<RouteModuleAnalysis> => {
183
+ const stats = await stat(resourcePath);
184
+ const cached = routeModuleAnalysisCache.get(resourcePath);
185
+ if (cached?.mtimeMs === stats.mtimeMs && cached.size === stats.size) {
186
+ return cached.analysis;
187
+ }
188
+
189
+ const analysis = (async () => {
190
+ const source = await readFile(resourcePath, 'utf8');
191
+ const program = parseProgram(source, resourcePath);
192
+ return {
193
+ code: source,
194
+ exports: collectProgramExportNames(program),
195
+ exportAllModules: collectExportAllModules(program),
196
+ };
197
+ })();
198
+
199
+ let trackedAnalysis: Promise<RouteModuleAnalysis>;
200
+ trackedAnalysis = cachePromiseOnReject(analysis, () => {
201
+ if (
202
+ routeModuleAnalysisCache.get(resourcePath)?.analysis === trackedAnalysis
203
+ ) {
204
+ routeModuleAnalysisCache.delete(resourcePath);
205
+ }
206
+ });
207
+
208
+ setBoundedCacheEntry(
209
+ routeModuleAnalysisCache,
210
+ resourcePath,
211
+ {
212
+ mtimeMs: stats.mtimeMs,
213
+ size: stats.size,
214
+ analysis: trackedAnalysis,
215
+ },
216
+ MAX_EXPORT_UTILS_CACHE_ENTRIES
217
+ );
218
+ return trackedAnalysis;
219
+ };