create-payload-app 4.0.0-internal.e55ccef → 4.0.0-internal.f11981a

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 (104) hide show
  1. package/README.md +5 -0
  2. package/dist/commands.d.ts +4 -0
  3. package/dist/commands.d.ts.map +1 -0
  4. package/dist/commands.js +5 -0
  5. package/dist/commands.js.map +1 -0
  6. package/dist/lib/ast/package-json.d.ts +9 -0
  7. package/dist/lib/ast/package-json.d.ts.map +1 -1
  8. package/dist/lib/ast/package-json.js +34 -8
  9. package/dist/lib/ast/package-json.js.map +1 -1
  10. package/dist/lib/ast/package-json.spec.js +73 -1
  11. package/dist/lib/ast/package-json.spec.js.map +1 -1
  12. package/dist/lib/configure-payload-tsconfig.d.ts +5 -0
  13. package/dist/lib/configure-payload-tsconfig.d.ts.map +1 -0
  14. package/dist/lib/configure-payload-tsconfig.js +26 -0
  15. package/dist/lib/configure-payload-tsconfig.js.map +1 -0
  16. package/dist/lib/configure-payload-tsconfig.spec.js +135 -0
  17. package/dist/lib/configure-payload-tsconfig.spec.js.map +1 -0
  18. package/dist/lib/create-project.spec.js +9 -0
  19. package/dist/lib/create-project.spec.js.map +1 -1
  20. package/dist/lib/init-next.d.ts.map +1 -1
  21. package/dist/lib/init-next.js +5 -34
  22. package/dist/lib/init-next.js.map +1 -1
  23. package/dist/lib/init-tanstack-default-template.spec.js +119 -0
  24. package/dist/lib/init-tanstack-default-template.spec.js.map +1 -0
  25. package/dist/lib/init-tanstack.d.ts +18 -0
  26. package/dist/lib/init-tanstack.d.ts.map +1 -0
  27. package/dist/lib/init-tanstack.js +150 -0
  28. package/dist/lib/init-tanstack.js.map +1 -0
  29. package/dist/lib/init-tanstack.spec.js +394 -0
  30. package/dist/lib/init-tanstack.spec.js.map +1 -0
  31. package/dist/lib/tanstack/detect.d.ts +5 -0
  32. package/dist/lib/tanstack/detect.d.ts.map +1 -0
  33. package/dist/lib/tanstack/detect.js +131 -0
  34. package/dist/lib/tanstack/detect.js.map +1 -0
  35. package/dist/lib/tanstack/detect.spec.js +227 -0
  36. package/dist/lib/tanstack/detect.spec.js.map +1 -0
  37. package/dist/lib/tanstack/prepare.d.ts +23 -0
  38. package/dist/lib/tanstack/prepare.d.ts.map +1 -0
  39. package/dist/lib/tanstack/prepare.js +114 -0
  40. package/dist/lib/tanstack/prepare.js.map +1 -0
  41. package/dist/lib/tanstack/prepare.spec.js +288 -0
  42. package/dist/lib/tanstack/prepare.spec.js.map +1 -0
  43. package/dist/lib/tanstack/template-files.d.ts +50 -0
  44. package/dist/lib/tanstack/template-files.d.ts.map +1 -0
  45. package/dist/lib/tanstack/template-files.js +64 -0
  46. package/dist/lib/tanstack/template-files.js.map +1 -0
  47. package/dist/lib/tanstack/transform-root-route.d.ts +7 -0
  48. package/dist/lib/tanstack/transform-root-route.d.ts.map +1 -0
  49. package/dist/lib/tanstack/transform-root-route.js +697 -0
  50. package/dist/lib/tanstack/transform-root-route.js.map +1 -0
  51. package/dist/lib/tanstack/transform-root-route.spec.js +448 -0
  52. package/dist/lib/tanstack/transform-root-route.spec.js.map +1 -0
  53. package/dist/lib/tanstack/transform-router.d.ts +5 -0
  54. package/dist/lib/tanstack/transform-router.d.ts.map +1 -0
  55. package/dist/lib/tanstack/transform-router.js +301 -0
  56. package/dist/lib/tanstack/transform-router.js.map +1 -0
  57. package/dist/lib/tanstack/transform-router.spec.js +264 -0
  58. package/dist/lib/tanstack/transform-router.spec.js.map +1 -0
  59. package/dist/lib/tanstack/transform-vite-config.d.ts +14 -0
  60. package/dist/lib/tanstack/transform-vite-config.d.ts.map +1 -0
  61. package/dist/lib/tanstack/transform-vite-config.js +533 -0
  62. package/dist/lib/tanstack/transform-vite-config.js.map +1 -0
  63. package/dist/lib/tanstack/transform-vite-config.spec.js +450 -0
  64. package/dist/lib/tanstack/transform-vite-config.spec.js.map +1 -0
  65. package/dist/lib/templates.d.ts.map +1 -1
  66. package/dist/lib/templates.js +6 -0
  67. package/dist/lib/templates.js.map +1 -1
  68. package/dist/lib/update-payload-in-project.d.ts +18 -3
  69. package/dist/lib/update-payload-in-project.d.ts.map +1 -1
  70. package/dist/lib/update-payload-in-project.js +93 -18
  71. package/dist/lib/update-payload-in-project.js.map +1 -1
  72. package/dist/lib/update-payload-in-project.spec.js +258 -0
  73. package/dist/lib/update-payload-in-project.spec.js.map +1 -0
  74. package/dist/main.d.ts +20 -1
  75. package/dist/main.d.ts.map +1 -1
  76. package/dist/main.js +120 -15
  77. package/dist/main.js.map +1 -1
  78. package/dist/main.spec.js +102 -0
  79. package/dist/main.spec.js.map +1 -0
  80. package/dist/scripts/pack-template-files.js +18 -0
  81. package/dist/scripts/pack-template-files.js.map +1 -1
  82. package/dist/template/src/app/(frontend)/styles.css +1 -1
  83. package/dist/template/src/app/(payload)/layout.tsx +1 -1
  84. package/dist/template-tanstack/routes/_payload/admin.$.tsx +8 -0
  85. package/dist/template-tanstack/routes/_payload/admin.index.tsx +8 -0
  86. package/dist/template-tanstack/routes/_payload/api.$.ts +10 -0
  87. package/dist/template-tanstack/routes/_payload/importMap.js +32 -0
  88. package/dist/template-tanstack/routes/_payload/server.functions.ts +49 -0
  89. package/dist/template-tanstack/routes/_payload.tsx +13 -0
  90. package/dist/template-tanstack/src/collections/Folders.ts +16 -0
  91. package/dist/template-tanstack/src/collections/Media.ts +20 -0
  92. package/dist/template-tanstack/src/collections/Tags.ts +16 -0
  93. package/dist/template-tanstack/src/collections/Users.ts +14 -0
  94. package/dist/template-tanstack/src/payload-foundation.css +10 -0
  95. package/dist/template-tanstack/src/payload.config.ts +40 -0
  96. package/dist/types.d.ts +22 -0
  97. package/dist/types.d.ts.map +1 -1
  98. package/dist/types.js.map +1 -1
  99. package/dist/utils/messages.d.ts +1 -0
  100. package/dist/utils/messages.d.ts.map +1 -1
  101. package/dist/utils/messages.js +7 -1
  102. package/dist/utils/messages.js.map +1 -1
  103. package/package.json +4 -4
  104. /package/dist/template/src/app/(payload)/{custom.scss → custom.css} +0 -0
@@ -0,0 +1,697 @@
1
+ import { Node, Project, QuoteKind, SyntaxKind } from 'ts-morph';
2
+ const PAYLOAD_CLIENT_MODULE = '@payloadcms/tanstack-start/client';
3
+ const ROUTER_MODULE = '@tanstack/react-router';
4
+ export function transformTanStackRootRoute({ content, kind }) {
5
+ const project = new Project({
6
+ manipulationSettings: {
7
+ quoteKind: QuoteKind.Single
8
+ },
9
+ useInMemoryFileSystem: true
10
+ });
11
+ const sourceFile = project.createSourceFile('__root.tsx', content);
12
+ const createRootRouteImport = getNamedImport({
13
+ importedName: 'createRootRoute',
14
+ moduleSpecifier: ROUTER_MODULE,
15
+ sourceFile
16
+ });
17
+ if (!createRootRouteImport) {
18
+ return failure('Could not identify the createRootRoute import.');
19
+ }
20
+ const createRootRouteCalls = sourceFile.getDescendantsOfKind(SyntaxKind.CallExpression).filter((call)=>call.getExpression().getText() === createRootRouteImport.localName);
21
+ if (createRootRouteCalls.length !== 1) {
22
+ return failure('Expected exactly one createRootRoute() call.');
23
+ }
24
+ const createRootRouteCall = createRootRouteCalls[0];
25
+ if (createRootRouteCall.getArguments().length !== 1) {
26
+ return failure('createRootRoute() must receive exactly one argument.');
27
+ }
28
+ const rootObject = createRootRouteCall.getArguments()[0];
29
+ if (!Node.isObjectLiteralExpression(rootObject)) {
30
+ return failure('createRootRoute() must receive an object literal.');
31
+ }
32
+ if (rootObject.getProperties().some(Node.isSpreadAssignment)) {
33
+ return failure('Root route object spreads cannot be transformed safely.');
34
+ }
35
+ return kind === 'start' ? transformStartRoot({
36
+ content,
37
+ rootObject,
38
+ sourceFile
39
+ }) : transformRouterOnlyRoot({
40
+ content,
41
+ rootObject,
42
+ sourceFile
43
+ });
44
+ }
45
+ function bindingNameIncludes({ localName, node }) {
46
+ if (Node.isIdentifier(node)) {
47
+ return node.getText() === localName;
48
+ }
49
+ if (Node.isArrayBindingPattern(node) || Node.isObjectBindingPattern(node)) {
50
+ return node.getElements().some((element)=>Node.isBindingElement(element) && bindingNameIncludes({
51
+ localName,
52
+ node: element.getNameNode()
53
+ }));
54
+ }
55
+ return false;
56
+ }
57
+ function ensureNamedImport({ importedName, moduleSpecifier, sourceFile }) {
58
+ const declaration = sourceFile.getImportDeclarations().find((candidate)=>candidate.getModuleSpecifierValue() === moduleSpecifier && !candidate.isTypeOnly() && !candidate.getNamespaceImport());
59
+ if (declaration?.getNamedImports().some((namedImport)=>namedImport.getName() === importedName && !namedImport.getAliasNode())) {
60
+ return;
61
+ }
62
+ if (declaration) {
63
+ declaration.addNamedImport(importedName);
64
+ } else {
65
+ sourceFile.addImportDeclaration({
66
+ moduleSpecifier,
67
+ namedImports: [
68
+ importedName
69
+ ]
70
+ });
71
+ }
72
+ }
73
+ function failure(reason) {
74
+ return {
75
+ reason,
76
+ success: false
77
+ };
78
+ }
79
+ function getAppStylesheetImport({ requireSideEffect, sourceFile }) {
80
+ const imports = sourceFile.getImportDeclarations().filter((declaration)=>{
81
+ const moduleSpecifier = declaration.getModuleSpecifierValue();
82
+ const isStylesheet = requireSideEffect ? /\.css$/.test(moduleSpecifier) : /\.css\?url$/.test(moduleSpecifier);
83
+ if (!isStylesheet) {
84
+ return false;
85
+ }
86
+ const hasNamedOrNamespaceImport = declaration.getNamedImports().length > 0 || Boolean(declaration.getNamespaceImport());
87
+ return requireSideEffect ? !declaration.getDefaultImport() && !hasNamedOrNamespaceImport : Boolean(declaration.getDefaultImport()) && !hasNamedOrNamespaceImport;
88
+ });
89
+ if (imports.length !== 1) {
90
+ return undefined;
91
+ }
92
+ const declaration = imports[0];
93
+ return {
94
+ declaration,
95
+ localName: declaration.getDefaultImport()?.getText(),
96
+ moduleSpecifier: declaration.getModuleSpecifierValue()
97
+ };
98
+ }
99
+ function getFunction({ functionName, sourceFile }) {
100
+ const functions = sourceFile.getFunctions().filter((declaration)=>declaration.getName() === functionName);
101
+ return functions.length === 1 ? functions[0] : undefined;
102
+ }
103
+ function getHeadContentNode({ functionDeclaration, localName }) {
104
+ const matches = functionDeclaration.getDescendantsOfKind(SyntaxKind.JsxSelfClosingElement).filter((element)=>element.getTagNameNode().getText() === localName);
105
+ return matches.length === 1 ? matches[0] : undefined;
106
+ }
107
+ function getLocalBindings({ localName, sourceFile }) {
108
+ const bindings = [];
109
+ for (const declaration of sourceFile.getImportDeclarations()){
110
+ const moduleSpecifier = declaration.getModuleSpecifierValue();
111
+ const defaultImport = declaration.getDefaultImport();
112
+ if (defaultImport?.getText() === localName) {
113
+ bindings.push({
114
+ importKind: 'default',
115
+ isTypeOnly: declaration.isTypeOnly(),
116
+ kind: 'import',
117
+ moduleSpecifier
118
+ });
119
+ }
120
+ const namespaceImport = declaration.getNamespaceImport();
121
+ if (namespaceImport?.getText() === localName) {
122
+ bindings.push({
123
+ importKind: 'namespace',
124
+ isTypeOnly: declaration.isTypeOnly(),
125
+ kind: 'import',
126
+ moduleSpecifier
127
+ });
128
+ }
129
+ for (const namedImport of declaration.getNamedImports()){
130
+ const namedImportLocalName = namedImport.getAliasNode()?.getText() ?? namedImport.getName();
131
+ if (namedImportLocalName === localName) {
132
+ bindings.push({
133
+ importedName: namedImport.getName(),
134
+ importKind: 'named',
135
+ isTypeOnly: declaration.isTypeOnly() || namedImport.isTypeOnly(),
136
+ kind: 'import',
137
+ moduleSpecifier
138
+ });
139
+ }
140
+ }
141
+ }
142
+ for (const declaration of sourceFile.getVariableDeclarations()){
143
+ if (bindingNameIncludes({
144
+ localName,
145
+ node: declaration.getNameNode()
146
+ })) {
147
+ bindings.push({
148
+ kind: 'variable'
149
+ });
150
+ }
151
+ }
152
+ const hasOtherDeclaration = [
153
+ ...sourceFile.getClasses(),
154
+ ...sourceFile.getEnums(),
155
+ ...sourceFile.getFunctions(),
156
+ ...sourceFile.getModules()
157
+ ].some((declaration)=>declaration.getName() === localName);
158
+ if (hasOtherDeclaration) {
159
+ bindings.push({
160
+ kind: 'other'
161
+ });
162
+ }
163
+ return bindings;
164
+ }
165
+ function getNamedImport({ importedName, moduleSpecifier, sourceFile }) {
166
+ const imports = sourceFile.getImportDeclarations().flatMap((declaration)=>declaration.getModuleSpecifierValue() === moduleSpecifier ? declaration.getNamedImports().filter((namedImport)=>namedImport.getName() === importedName).map((namedImport)=>({
167
+ declaration,
168
+ localName: namedImport.getAliasNode()?.getText() ?? namedImport.getName()
169
+ })) : []);
170
+ if (imports.length !== 1) {
171
+ return undefined;
172
+ }
173
+ const imported = imports[0];
174
+ const bindings = getLocalBindings({
175
+ localName: imported.localName,
176
+ sourceFile
177
+ });
178
+ const binding = bindings[0];
179
+ if (bindings.length !== 1 || binding?.kind !== 'import' || binding.importKind !== 'named' || binding.importedName !== importedName || binding.isTypeOnly || binding.moduleSpecifier !== moduleSpecifier) {
180
+ return undefined;
181
+ }
182
+ return imported;
183
+ }
184
+ function resolveRouterNamedImport({ importedName, sourceFile }) {
185
+ const matchingImports = sourceFile.getImportDeclarations().flatMap((declaration)=>declaration.getModuleSpecifierValue() === ROUTER_MODULE && !declaration.isTypeOnly() ? declaration.getNamedImports().filter((namedImport)=>namedImport.getName() === importedName && !namedImport.isTypeOnly()).map((namedImport)=>namedImport.getAliasNode()?.getText() ?? namedImport.getName()) : []);
186
+ if (matchingImports.length > 1) {
187
+ return {
188
+ reason: `The TanStack router module imports ${importedName} more than once.`,
189
+ success: false
190
+ };
191
+ }
192
+ const localName = matchingImports[0] ?? importedName;
193
+ const bindings = getLocalBindings({
194
+ localName,
195
+ sourceFile
196
+ });
197
+ const binding = bindings[0];
198
+ const isCompatible = binding?.kind === 'import' && binding.importKind === 'named' && binding.importedName === importedName && !binding.isTypeOnly && binding.moduleSpecifier === ROUTER_MODULE;
199
+ if (bindings.length > 0 && (bindings.length !== 1 || !isCompatible)) {
200
+ return {
201
+ reason: `Identifier "${localName}" is already bound incompatibly.`,
202
+ success: false
203
+ };
204
+ }
205
+ return {
206
+ importedName,
207
+ isPresent: matchingImports.length === 1,
208
+ localName,
209
+ success: true
210
+ };
211
+ }
212
+ function getSideEffectStylesheetImports(sourceFile) {
213
+ return sourceFile.getImportDeclarations().filter((declaration)=>/\.css$/.test(declaration.getModuleSpecifierValue()) && !declaration.getDefaultImport() && !declaration.getNamespaceImport() && declaration.getNamedImports().length === 0);
214
+ }
215
+ function getPropertyName(node) {
216
+ if (Node.isPropertyAssignment(node) || Node.isShorthandPropertyAssignment(node) || Node.isMethodDeclaration(node) || Node.isGetAccessorDeclaration(node) || Node.isSetAccessorDeclaration(node)) {
217
+ return node.getName();
218
+ }
219
+ }
220
+ function getPropertyAssignment({ object, propertyName }) {
221
+ const properties = object.getProperties().filter((property)=>getPropertyName(property) === propertyName);
222
+ return properties.length === 1 && Node.isPropertyAssignment(properties[0]) ? properties[0] : undefined;
223
+ }
224
+ function getRootHeadLinks(rootObject) {
225
+ const headProperty = getPropertyAssignment({
226
+ object: rootObject,
227
+ propertyName: 'head'
228
+ });
229
+ const headInitializer = headProperty?.getInitializer();
230
+ if (!headInitializer || !Node.isArrowFunction(headInitializer)) {
231
+ return {
232
+ reason: 'The root head declaration cannot be transformed safely.',
233
+ success: false
234
+ };
235
+ }
236
+ const body = headInitializer.getBody();
237
+ const headObject = Node.isParenthesizedExpression(body) ? body.getExpression() : body;
238
+ if (!Node.isObjectLiteralExpression(headObject)) {
239
+ return {
240
+ reason: 'The root head declaration cannot be transformed safely.',
241
+ success: false
242
+ };
243
+ }
244
+ const linksProperty = getPropertyAssignment({
245
+ object: headObject,
246
+ propertyName: 'links'
247
+ });
248
+ const links = linksProperty?.getInitializer();
249
+ if (!links || !Node.isArrayLiteralExpression(links) || links.getElements().some(Node.isSpreadElement)) {
250
+ return {
251
+ reason: 'The root head links cannot be transformed safely.',
252
+ success: false
253
+ };
254
+ }
255
+ return {
256
+ links,
257
+ success: true
258
+ };
259
+ }
260
+ function getShellState({ expectedComponent, rootObject, withPayloadRootLocalName }) {
261
+ const properties = rootObject.getProperties().filter((property)=>getPropertyName(property) === 'shellComponent');
262
+ if (properties.length === 0) {
263
+ if (!expectedComponent) {
264
+ return {
265
+ componentName: 'RootDocument',
266
+ isWrapped: false,
267
+ success: true
268
+ };
269
+ }
270
+ return {
271
+ reason: 'The existing shellComponent cannot be transformed safely.',
272
+ success: false
273
+ };
274
+ }
275
+ const property = properties[0];
276
+ if (properties.length !== 1 || !property || !Node.isPropertyAssignment(property)) {
277
+ return {
278
+ reason: 'The existing shellComponent cannot be transformed safely.',
279
+ success: false
280
+ };
281
+ }
282
+ const initializer = property.getInitializer();
283
+ if (Node.isIdentifier(initializer) && (!expectedComponent || initializer.getText() === expectedComponent)) {
284
+ return {
285
+ componentName: initializer.getText(),
286
+ isWrapped: false,
287
+ property,
288
+ success: true
289
+ };
290
+ }
291
+ if (Node.isCallExpression(initializer) && initializer.getExpression().getText() === withPayloadRootLocalName && initializer.getArguments().length === 1 && Node.isIdentifier(initializer.getArguments()[0]) && (!expectedComponent || initializer.getArguments()[0].getText() === expectedComponent)) {
292
+ return {
293
+ componentName: initializer.getArguments()[0].getText(),
294
+ isWrapped: true,
295
+ property,
296
+ success: true
297
+ };
298
+ }
299
+ return {
300
+ reason: 'The existing shellComponent cannot be transformed safely.',
301
+ success: false
302
+ };
303
+ }
304
+ function getStylesheetAttribute({ name, element }) {
305
+ const attribute = element.getAttributes().find((candidate)=>Node.isJsxAttribute(candidate) && candidate.getNameNode().getText() === name);
306
+ if (!attribute || !Node.isJsxAttribute(attribute)) {
307
+ return undefined;
308
+ }
309
+ const initializer = attribute.getInitializer();
310
+ if (Node.isStringLiteral(initializer)) {
311
+ return initializer.getLiteralValue();
312
+ }
313
+ if (Node.isJsxExpression(initializer)) {
314
+ return initializer.getExpression()?.getText();
315
+ }
316
+ }
317
+ function transformRouterOnlyRoot({ content, rootObject, sourceFile }) {
318
+ const withPayloadRootImport = getNamedImport({
319
+ importedName: 'withPayloadRoot',
320
+ moduleSpecifier: PAYLOAD_CLIENT_MODULE,
321
+ sourceFile
322
+ });
323
+ const withPayloadRootLocalName = withPayloadRootImport?.localName ?? 'withPayloadRoot';
324
+ const shellState = getShellState({
325
+ rootObject,
326
+ withPayloadRootLocalName
327
+ });
328
+ if (!shellState.success) {
329
+ return shellState;
330
+ }
331
+ if (shellState.property && !shellState.isWrapped) {
332
+ return failure('The existing shellComponent cannot be transformed safely.');
333
+ }
334
+ if (shellState.isWrapped && getSideEffectStylesheetImports(sourceFile).length > 0) {
335
+ return failure('Side-effect stylesheet imports cannot be isolated from admin.');
336
+ }
337
+ const headContentImport = resolveRouterNamedImport({
338
+ importedName: 'HeadContent',
339
+ sourceFile
340
+ });
341
+ if (!headContentImport.success) {
342
+ return headContentImport;
343
+ }
344
+ const scriptsImport = resolveRouterNamedImport({
345
+ importedName: 'Scripts',
346
+ sourceFile
347
+ });
348
+ if (!scriptsImport.success) {
349
+ return scriptsImport;
350
+ }
351
+ const destinationValidation = validateRouterOnlyDestinations({
352
+ isAlreadyWrapped: shellState.isWrapped,
353
+ sourceFile,
354
+ withPayloadRootLocalName
355
+ });
356
+ if (!destinationValidation.success) {
357
+ return destinationValidation;
358
+ }
359
+ if (shellState.isWrapped) {
360
+ const stylesheetImport = getAppStylesheetImport({
361
+ requireSideEffect: false,
362
+ sourceFile
363
+ });
364
+ const rootDocument = getFunction({
365
+ functionName: 'RootDocument',
366
+ sourceFile
367
+ });
368
+ if (!stylesheetImport || stylesheetImport.localName !== 'appCss' || !rootDocument || !hasValidAppStylesheetLink({
369
+ functionDeclaration: rootDocument,
370
+ headContentLocalName: headContentImport.localName,
371
+ localName: 'appCss'
372
+ })) {
373
+ return failure('The existing Payload root shell does not match the supported configuration.');
374
+ }
375
+ const hasWithPayloadImport = Boolean(withPayloadRootImport);
376
+ const hasAllRequiredImports = hasWithPayloadImport && headContentImport.isPresent && scriptsImport.isPresent;
377
+ if (!hasAllRequiredImports) {
378
+ if (!hasWithPayloadImport) {
379
+ ensureNamedImport({
380
+ importedName: 'withPayloadRoot',
381
+ moduleSpecifier: PAYLOAD_CLIENT_MODULE,
382
+ sourceFile
383
+ });
384
+ }
385
+ if (!headContentImport.isPresent) {
386
+ ensureNamedImport({
387
+ importedName: 'HeadContent',
388
+ moduleSpecifier: ROUTER_MODULE,
389
+ sourceFile
390
+ });
391
+ }
392
+ if (!scriptsImport.isPresent) {
393
+ ensureNamedImport({
394
+ importedName: 'Scripts',
395
+ moduleSpecifier: ROUTER_MODULE,
396
+ sourceFile
397
+ });
398
+ }
399
+ sourceFile.formatText({
400
+ indentSize: 2
401
+ });
402
+ return {
403
+ content: sourceFile.getFullText(),
404
+ modified: true,
405
+ success: true
406
+ };
407
+ }
408
+ return {
409
+ content,
410
+ modified: false,
411
+ success: true
412
+ };
413
+ }
414
+ const sideEffectStylesheets = getSideEffectStylesheetImports(sourceFile);
415
+ if (sideEffectStylesheets.length !== 1) {
416
+ return failure('Expected exactly one side-effect stylesheet import.');
417
+ }
418
+ const stylesheetImport = sideEffectStylesheets[0];
419
+ const componentProperties = rootObject.getProperties().filter((property)=>getPropertyName(property) === 'component');
420
+ if (componentProperties.length !== 1) {
421
+ return failure('The Router-only root must contain exactly one component option.');
422
+ }
423
+ stylesheetImport.setDefaultImport('appCss');
424
+ stylesheetImport.setModuleSpecifier(`${stylesheetImport.getModuleSpecifierValue()}?url`);
425
+ rootObject.addPropertyAssignment({
426
+ name: 'shellComponent',
427
+ initializer: `${withPayloadRootLocalName}(RootDocument)`
428
+ });
429
+ if (!withPayloadRootImport) {
430
+ ensureNamedImport({
431
+ importedName: 'withPayloadRoot',
432
+ moduleSpecifier: PAYLOAD_CLIENT_MODULE,
433
+ sourceFile
434
+ });
435
+ }
436
+ if (!headContentImport.isPresent) {
437
+ ensureNamedImport({
438
+ importedName: 'HeadContent',
439
+ moduleSpecifier: ROUTER_MODULE,
440
+ sourceFile
441
+ });
442
+ }
443
+ if (!scriptsImport.isPresent) {
444
+ ensureNamedImport({
445
+ importedName: 'Scripts',
446
+ moduleSpecifier: ROUTER_MODULE,
447
+ sourceFile
448
+ });
449
+ }
450
+ sourceFile.addStatements(`
451
+ function RootDocument({ children }: { children: React.ReactNode }) {
452
+ return (
453
+ <html lang="en" suppressHydrationWarning>
454
+ <head>
455
+ <link href={appCss} rel="stylesheet" />
456
+ <${headContentImport.localName} />
457
+ </head>
458
+ <body>
459
+ {children}
460
+ <${scriptsImport.localName} />
461
+ </body>
462
+ </html>
463
+ )
464
+ }`);
465
+ sourceFile.formatText({
466
+ indentSize: 2
467
+ });
468
+ return {
469
+ content: sourceFile.getFullText(),
470
+ modified: true,
471
+ success: true
472
+ };
473
+ }
474
+ function transformStartRoot({ content, rootObject, sourceFile }) {
475
+ if (getSideEffectStylesheetImports(sourceFile).length > 0) {
476
+ return failure('Side-effect stylesheet imports cannot be isolated from admin.');
477
+ }
478
+ const withPayloadRootImport = getNamedImport({
479
+ importedName: 'withPayloadRoot',
480
+ moduleSpecifier: PAYLOAD_CLIENT_MODULE,
481
+ sourceFile
482
+ });
483
+ const withPayloadRootLocalName = withPayloadRootImport?.localName ?? 'withPayloadRoot';
484
+ const shellState = getShellState({
485
+ expectedComponent: undefined,
486
+ rootObject,
487
+ withPayloadRootLocalName
488
+ });
489
+ if (!shellState.success || !shellState.property) {
490
+ return shellState.success ? failure('The existing shellComponent cannot be transformed safely.') : shellState;
491
+ }
492
+ const withPayloadValidation = validateNamedDestination({
493
+ importedName: 'withPayloadRoot',
494
+ localName: withPayloadRootLocalName,
495
+ moduleSpecifier: PAYLOAD_CLIENT_MODULE,
496
+ sourceFile
497
+ });
498
+ if (!withPayloadValidation.success) {
499
+ return withPayloadValidation;
500
+ }
501
+ const hasWithPayloadImport = Boolean(withPayloadRootImport);
502
+ const headContentImport = getNamedImport({
503
+ importedName: 'HeadContent',
504
+ moduleSpecifier: ROUTER_MODULE,
505
+ sourceFile
506
+ });
507
+ const scriptsImport = getNamedImport({
508
+ importedName: 'Scripts',
509
+ moduleSpecifier: ROUTER_MODULE,
510
+ sourceFile
511
+ });
512
+ if (!headContentImport || !scriptsImport) {
513
+ return failure('Could not identify the Start document imports.');
514
+ }
515
+ const stylesheetImport = getAppStylesheetImport({
516
+ requireSideEffect: false,
517
+ sourceFile
518
+ });
519
+ if (!stylesheetImport?.localName) {
520
+ return failure('Could not identify the app stylesheet import.');
521
+ }
522
+ const stylesheetBindings = getLocalBindings({
523
+ localName: stylesheetImport.localName,
524
+ sourceFile
525
+ });
526
+ const stylesheetBinding = stylesheetBindings[0];
527
+ if (stylesheetBindings.length !== 1 || stylesheetBinding?.kind !== 'import' || stylesheetBinding.importKind !== 'default' || stylesheetBinding.isTypeOnly || stylesheetBinding.moduleSpecifier !== stylesheetImport.moduleSpecifier) {
528
+ return failure(`Identifier "${stylesheetImport.localName}" is already bound incompatibly.`);
529
+ }
530
+ const rootDocument = getFunction({
531
+ functionName: shellState.componentName,
532
+ sourceFile
533
+ });
534
+ if (!rootDocument) {
535
+ return failure('Could not identify the Start root document component.');
536
+ }
537
+ const headContentNode = getHeadContentNode({
538
+ functionDeclaration: rootDocument,
539
+ localName: headContentImport.localName
540
+ });
541
+ if (!headContentNode) {
542
+ return failure('Could not identify HeadContent in the Start root document.');
543
+ }
544
+ const headLinks = getRootHeadLinks(rootObject);
545
+ if (!headLinks.success) {
546
+ return headLinks;
547
+ }
548
+ let stylesheetIndex = -1;
549
+ for (const [index, element] of headLinks.links.getElements().entries()){
550
+ if (!Node.isObjectLiteralExpression(element)) {
551
+ return failure('The app stylesheet link cannot be relocated safely.');
552
+ }
553
+ if (element.getProperties().some(Node.isSpreadAssignment)) {
554
+ return failure('The app stylesheet link cannot be relocated safely.');
555
+ }
556
+ const rel = getStaticObjectPropertyValue({
557
+ object: element,
558
+ propertyName: 'rel'
559
+ });
560
+ if (rel !== 'stylesheet') {
561
+ continue;
562
+ }
563
+ const hrefProperty = getPropertyAssignment({
564
+ object: element,
565
+ propertyName: 'href'
566
+ });
567
+ if (hrefProperty?.getInitializer()?.getText() !== stylesheetImport.localName || stylesheetIndex !== -1) {
568
+ return failure('The app stylesheet link cannot be relocated safely.');
569
+ }
570
+ stylesheetIndex = index;
571
+ }
572
+ const hasStylesheetLink = hasValidAppStylesheetLink({
573
+ functionDeclaration: rootDocument,
574
+ headContentLocalName: headContentImport.localName,
575
+ localName: stylesheetImport.localName
576
+ });
577
+ if (stylesheetIndex === -1 && !hasStylesheetLink) {
578
+ return failure('The app stylesheet link cannot be relocated safely.');
579
+ }
580
+ if (shellState.isWrapped && stylesheetIndex === -1 && hasStylesheetLink && hasWithPayloadImport) {
581
+ return {
582
+ content,
583
+ modified: false,
584
+ success: true
585
+ };
586
+ }
587
+ if (stylesheetIndex !== -1) {
588
+ headLinks.links.removeElement(stylesheetIndex);
589
+ }
590
+ if (!hasStylesheetLink) {
591
+ headContentNode.replaceWithText(`<link href={${stylesheetImport.localName}} rel="stylesheet" />\n<${headContentImport.localName} />`);
592
+ }
593
+ if (!shellState.isWrapped) {
594
+ shellState.property.setInitializer(`${withPayloadRootLocalName}(${shellState.componentName})`);
595
+ }
596
+ if (!withPayloadRootImport) {
597
+ ensureNamedImport({
598
+ importedName: 'withPayloadRoot',
599
+ moduleSpecifier: PAYLOAD_CLIENT_MODULE,
600
+ sourceFile
601
+ });
602
+ }
603
+ sourceFile.formatText({
604
+ indentSize: 2
605
+ });
606
+ return {
607
+ content: sourceFile.getFullText(),
608
+ modified: true,
609
+ success: true
610
+ };
611
+ }
612
+ function getStaticObjectPropertyValue({ object, propertyName }) {
613
+ const property = getPropertyAssignment({
614
+ object,
615
+ propertyName
616
+ });
617
+ const initializer = property?.getInitializer();
618
+ return Node.isStringLiteral(initializer) ? initializer.getLiteralValue() : undefined;
619
+ }
620
+ function hasValidAppStylesheetLink({ functionDeclaration, headContentLocalName, localName }) {
621
+ const links = functionDeclaration.getDescendantsOfKind(SyntaxKind.JsxSelfClosingElement).filter((element)=>element.getTagNameNode().getText() === 'link' && getStylesheetAttribute({
622
+ name: 'href',
623
+ element
624
+ }) === localName && getStylesheetAttribute({
625
+ name: 'rel',
626
+ element
627
+ }) === 'stylesheet');
628
+ const headContentElements = functionDeclaration.getDescendantsOfKind(SyntaxKind.JsxSelfClosingElement).filter((element)=>element.getTagNameNode().getText() === headContentLocalName);
629
+ if (links.length !== 1 || headContentElements.length !== 1) {
630
+ return false;
631
+ }
632
+ const link = links[0];
633
+ const headContent = headContentElements[0];
634
+ const linkParent = link.getFirstAncestorByKind(SyntaxKind.JsxElement);
635
+ const headContentParent = headContent.getFirstAncestorByKind(SyntaxKind.JsxElement);
636
+ return linkParent?.getStart() === headContentParent?.getStart() && linkParent?.getOpeningElement().getTagNameNode().getText() === 'head' && link.getStart() < headContent.getStart();
637
+ }
638
+ function validateNamedDestination({ importedName, localName, moduleSpecifier, sourceFile }) {
639
+ const bindings = getLocalBindings({
640
+ localName,
641
+ sourceFile
642
+ });
643
+ const binding = bindings[0];
644
+ const isCompatible = binding?.kind === 'import' && binding.importKind === 'named' && binding.importedName === importedName && !binding.isTypeOnly && binding.moduleSpecifier === moduleSpecifier;
645
+ if (bindings.length > 0 && (bindings.length !== 1 || !isCompatible)) {
646
+ return {
647
+ reason: `Identifier "${localName}" is already bound incompatibly.`,
648
+ success: false
649
+ };
650
+ }
651
+ return {
652
+ success: true
653
+ };
654
+ }
655
+ function validateRouterOnlyDestinations({ isAlreadyWrapped, sourceFile, withPayloadRootLocalName }) {
656
+ const destinations = [
657
+ {
658
+ importedName: 'withPayloadRoot',
659
+ localName: withPayloadRootLocalName,
660
+ moduleSpecifier: PAYLOAD_CLIENT_MODULE
661
+ }
662
+ ];
663
+ for (const destination of destinations){
664
+ const validation = validateNamedDestination({
665
+ ...destination,
666
+ sourceFile
667
+ });
668
+ if (!validation.success) {
669
+ return validation;
670
+ }
671
+ }
672
+ for (const localName of [
673
+ 'appCss',
674
+ 'RootDocument'
675
+ ]){
676
+ const bindings = getLocalBindings({
677
+ localName,
678
+ sourceFile
679
+ });
680
+ if (bindings.length === 0) {
681
+ continue;
682
+ }
683
+ const binding = bindings[0];
684
+ const isCompatible = isAlreadyWrapped && bindings.length === 1 && (localName === 'RootDocument' ? binding?.kind === 'other' : binding?.kind === 'import' && binding.importKind === 'default' && !binding.isTypeOnly && /\.css\?url$/.test(binding.moduleSpecifier));
685
+ if (!isCompatible) {
686
+ return {
687
+ reason: `Identifier "${localName}" is already bound incompatibly.`,
688
+ success: false
689
+ };
690
+ }
691
+ }
692
+ return {
693
+ success: true
694
+ };
695
+ }
696
+
697
+ //# sourceMappingURL=transform-root-route.js.map