@vistagenic/vista 0.2.16 → 0.3.2

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 (153) hide show
  1. package/bin/vista.js +184 -177
  2. package/dist/ai/agent.d.ts +37 -0
  3. package/dist/ai/agent.js +385 -0
  4. package/dist/ai/embeddings.d.ts +11 -0
  5. package/dist/ai/embeddings.js +71 -0
  6. package/dist/ai/index.d.ts +10 -0
  7. package/dist/ai/index.js +26 -0
  8. package/dist/ai/memory.d.ts +16 -0
  9. package/dist/ai/memory.js +39 -0
  10. package/dist/ai/observability.d.ts +21 -0
  11. package/dist/ai/observability.js +69 -0
  12. package/dist/ai/providers/anthropic.d.ts +2 -0
  13. package/dist/ai/providers/anthropic.js +190 -0
  14. package/dist/ai/providers/base.d.ts +7 -0
  15. package/dist/ai/providers/base.js +85 -0
  16. package/dist/ai/providers/gemini.d.ts +2 -0
  17. package/dist/ai/providers/gemini.js +194 -0
  18. package/dist/ai/providers/index.d.ts +5 -0
  19. package/dist/ai/providers/index.js +21 -0
  20. package/dist/ai/providers/mock.d.ts +8 -0
  21. package/dist/ai/providers/mock.js +77 -0
  22. package/dist/ai/providers/openai.d.ts +2 -0
  23. package/dist/ai/providers/openai.js +211 -0
  24. package/dist/ai/rag.d.ts +35 -0
  25. package/dist/ai/rag.js +110 -0
  26. package/dist/ai/react/index.d.ts +1 -0
  27. package/dist/ai/react/index.js +17 -0
  28. package/dist/ai/react/react-server.d.ts +1 -0
  29. package/dist/ai/react/react-server.js +17 -0
  30. package/dist/ai/react/use-agent.d.ts +23 -0
  31. package/dist/ai/react/use-agent.js +138 -0
  32. package/dist/ai/stream.d.ts +24 -0
  33. package/dist/ai/stream.js +109 -0
  34. package/dist/ai/tool.d.ts +11 -0
  35. package/dist/ai/tool.js +47 -0
  36. package/dist/ai/types.d.ts +117 -0
  37. package/dist/ai/types.js +5 -0
  38. package/dist/auth/core.d.ts +140 -0
  39. package/dist/auth/core.js +165 -0
  40. package/dist/auth/index.d.ts +20 -8
  41. package/dist/auth/index.js +369 -16
  42. package/dist/auth/react-server.d.ts +1 -0
  43. package/dist/auth/react-server.js +10 -0
  44. package/dist/auth/react.d.ts +18 -0
  45. package/dist/auth/react.js +71 -0
  46. package/dist/bin/build-rsc.js +602 -604
  47. package/dist/bin/build.js +389 -388
  48. package/dist/bin/deploy-output.d.ts +2 -7
  49. package/dist/bin/deploy-output.js +3 -76
  50. package/dist/bin/deploy.d.ts +7 -0
  51. package/dist/bin/deploy.js +102 -0
  52. package/dist/bin/dev-error-overlay-snippet.js +778 -552
  53. package/dist/bin/devtools-indicator-snippet.js +387 -387
  54. package/dist/bin/generate.js +510 -248
  55. package/dist/build/manifest.d.ts +169 -139
  56. package/dist/build/manifest.js +423 -367
  57. package/dist/build/rsc/client-manifest.d.ts +62 -52
  58. package/dist/build/rsc/client-manifest.js +295 -193
  59. package/dist/build/rsc/client-reference-plugin.d.ts +37 -37
  60. package/dist/build/rsc/client-reference-plugin.js +160 -160
  61. package/dist/build/rsc/native-scanner.d.ts +135 -123
  62. package/dist/build/rsc/native-scanner.js +203 -170
  63. package/dist/build/rsc/react-client-reference-manifest.d.ts +28 -22
  64. package/dist/build/rsc/react-client-reference-manifest.js +240 -219
  65. package/dist/build/rsc/rsc-renderer.d.ts +99 -99
  66. package/dist/build/rsc/rsc-renderer.js +263 -263
  67. package/dist/build/rsc/server-component-loader.d.ts +19 -19
  68. package/dist/build/rsc/server-component-loader.js +91 -91
  69. package/dist/build/rsc/server-manifest.d.ts +17 -0
  70. package/dist/build/rsc/server-manifest.js +20 -0
  71. package/dist/build/standalone.js +337 -334
  72. package/dist/build/webpack/plugins/vista-flight-plugin.js +5 -5
  73. package/dist/client/dynamic.react-server.d.ts +2 -0
  74. package/dist/client/dynamic.react-server.js +23 -0
  75. package/dist/client/link.react-server.d.ts +2 -0
  76. package/dist/client/link.react-server.js +11 -0
  77. package/dist/client/navigation.react-server.d.ts +1 -0
  78. package/dist/client/navigation.react-server.js +17 -0
  79. package/dist/client/router.react-server.d.ts +1 -0
  80. package/dist/client/router.react-server.js +17 -0
  81. package/dist/client/rsc-router.react-server.d.ts +1 -0
  82. package/dist/client/rsc-router.react-server.js +17 -0
  83. package/dist/client/script.react-server.d.ts +2 -0
  84. package/dist/client/script.react-server.js +23 -0
  85. package/dist/components/client-island.d.ts +34 -34
  86. package/dist/components/client-island.js +75 -75
  87. package/dist/config.d.ts +17 -0
  88. package/dist/config.js +60 -1
  89. package/dist/deploy/adapters/cloudflare.d.ts +2 -0
  90. package/dist/deploy/adapters/cloudflare.js +124 -0
  91. package/dist/deploy/adapters/docker.d.ts +2 -0
  92. package/dist/deploy/adapters/docker.js +112 -0
  93. package/dist/deploy/adapters/index.d.ts +15 -0
  94. package/dist/deploy/adapters/index.js +24 -0
  95. package/dist/deploy/adapters/netlify.d.ts +2 -0
  96. package/dist/deploy/adapters/netlify.js +115 -0
  97. package/dist/deploy/adapters/render.d.ts +2 -0
  98. package/dist/deploy/adapters/render.js +95 -0
  99. package/dist/deploy/adapters/vercel.d.ts +7 -0
  100. package/dist/deploy/adapters/vercel.js +164 -0
  101. package/dist/deploy/cli-runner.d.ts +8 -0
  102. package/dist/deploy/cli-runner.js +58 -0
  103. package/dist/deploy/detect.d.ts +6 -0
  104. package/dist/deploy/detect.js +77 -0
  105. package/dist/deploy/index.d.ts +24 -0
  106. package/dist/deploy/index.js +112 -0
  107. package/dist/deploy/preflight.d.ts +8 -0
  108. package/dist/deploy/preflight.js +80 -0
  109. package/dist/deploy/types.d.ts +48 -0
  110. package/dist/deploy/types.js +2 -0
  111. package/dist/deploy/utils.d.ts +18 -0
  112. package/dist/deploy/utils.js +70 -0
  113. package/dist/dev-error.d.ts +14 -0
  114. package/dist/dev-error.js +1763 -705
  115. package/dist/index.d.ts +23 -21
  116. package/dist/index.js +61 -57
  117. package/dist/server/cookie-parse.d.ts +4 -0
  118. package/dist/server/cookie-parse.js +14 -0
  119. package/dist/server/engine.js +7 -26
  120. package/dist/server/index.d.ts +109 -102
  121. package/dist/server/index.js +315 -286
  122. package/dist/server/middleware-runner.d.ts +125 -57
  123. package/dist/server/middleware-runner.js +615 -218
  124. package/dist/server/middleware-security.d.ts +36 -0
  125. package/dist/server/middleware-security.js +183 -0
  126. package/dist/server/module-compile-hook.js +695 -662
  127. package/dist/server/route-handler-registry.d.ts +65 -0
  128. package/dist/server/route-handler-registry.js +195 -0
  129. package/dist/server/route-patterns.d.ts +67 -0
  130. package/dist/server/route-patterns.js +186 -0
  131. package/dist/server/rsc-engine.js +75 -92
  132. package/dist/server/rsc-module-system.d.ts +33 -33
  133. package/dist/server/rsc-module-system.js +87 -87
  134. package/dist/server/rsc-upstream.js +892 -888
  135. package/dist/server/static-generator.js +10 -10
  136. package/dist/server/typed-api-runtime.d.ts +18 -0
  137. package/dist/server/typed-api-runtime.js +635 -507
  138. package/dist/server/vista-import-map.js +134 -123
  139. package/dist/stack/index.d.ts +34 -32
  140. package/dist/stack/index.js +49 -45
  141. package/dist/stack/server/caller.d.ts +13 -0
  142. package/dist/stack/server/caller.js +42 -0
  143. package/dist/stack/server/executor.d.ts +40 -36
  144. package/dist/stack/server/executor.js +222 -174
  145. package/dist/stack/server/index.d.ts +11 -10
  146. package/dist/stack/server/index.js +24 -23
  147. package/dist/stack/server/procedure.d.ts +20 -18
  148. package/dist/stack/server/procedure.js +66 -58
  149. package/dist/stack/server/types.d.ts +113 -100
  150. package/dist/theme/react-server.d.ts +10 -0
  151. package/dist/theme/react-server.js +16 -0
  152. package/package.json +28 -3
  153. package/LICENSE +0 -21
@@ -1,662 +1,695 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.installModuleCompileHook = installModuleCompileHook;
7
- const fs_1 = __importDefault(require("fs"));
8
- const path_1 = __importDefault(require("path"));
9
- const core_1 = require("@swc/core");
10
- const url_1 = require("url");
11
- const runtime_actions_1 = require("./runtime-actions");
12
- const cache_1 = require("./cache");
13
- const CjsModule = require('module');
14
- let compileHookInstalled = false;
15
- let originalCompile = null;
16
- let activeCompileRoots = [];
17
- let activeClientModuleProxyFactory = null;
18
- let activeRuntimeActionsSpecifier = require.resolve('./runtime-actions');
19
- let activeCacheRuntimeSpecifier = require.resolve('./cache');
20
- let activeCacheComponentsEnabled = false;
21
- function normalizeModulePath(filePath) {
22
- return filePath.replace(/\\/g, '/').toLowerCase();
23
- }
24
- function stripLeadingCommentsAndWhitespace(source) {
25
- let remaining = source;
26
- while (true) {
27
- const trimmed = remaining.trimStart();
28
- if (trimmed.startsWith('//')) {
29
- const newlineIndex = trimmed.indexOf('\n');
30
- remaining = newlineIndex === -1 ? '' : trimmed.slice(newlineIndex + 1);
31
- continue;
32
- }
33
- if (trimmed.startsWith('/*')) {
34
- const commentEndIndex = trimmed.indexOf('*/');
35
- if (commentEndIndex === -1) {
36
- return trimmed;
37
- }
38
- remaining = trimmed.slice(commentEndIndex + 2);
39
- continue;
40
- }
41
- return trimmed;
42
- }
43
- }
44
- function hasDirective(source, directive) {
45
- const trimmed = stripLeadingCommentsAndWhitespace(source);
46
- return trimmed.startsWith(`'${directive}'`) || trimmed.startsWith(`"${directive}"`);
47
- }
48
- function containsDirectiveLiteral(source, directive) {
49
- return source.includes(`'${directive}'`) || source.includes(`"${directive}"`);
50
- }
51
- function readOriginalSource(filename, fallback) {
52
- try {
53
- return fs_1.default.readFileSync(filename, 'utf-8');
54
- }
55
- catch {
56
- return fallback;
57
- }
58
- }
59
- function createDirectiveError(filename, message) {
60
- return new Error(`[vista:cache] ${path_1.default.basename(filename)}: ${message}`);
61
- }
62
- function isProjectModule(filename, roots) {
63
- const normalized = normalizeModulePath(filename);
64
- const matchesRoot = roots.some((root) => {
65
- const rootPrefix = normalizeModulePath(`${root}${path_1.default.sep}`);
66
- return normalized.startsWith(rootPrefix);
67
- });
68
- const isStandaloneProjectModule = roots.some((root) => {
69
- const normalizedRoot = normalizeModulePath(root);
70
- return (normalizedRoot.includes('/.vista/standalone/') &&
71
- (normalized === normalizedRoot || normalized.startsWith(`${normalizedRoot}/`)));
72
- });
73
- if (!matchesRoot)
74
- return false;
75
- if (normalized.includes('/node_modules/'))
76
- return false;
77
- if (normalized.includes('/.vista/') && !isStandaloneProjectModule)
78
- return false;
79
- if (normalized.includes('/.flash/'))
80
- return false;
81
- return /\.[cm]?[jt]sx?$/i.test(normalized);
82
- }
83
- function isStringDirectiveStatement(statement, directive) {
84
- return (statement?.type === 'ExpressionStatement' &&
85
- (statement.directive === directive ||
86
- (statement.expression?.type === 'StringLiteral' &&
87
- statement.expression?.value === directive)));
88
- }
89
- function hasServerDirectiveInFunctionLike(node) {
90
- return (node?.body?.type === 'BlockStatement' &&
91
- Array.isArray(node.body.stmts) &&
92
- node.body.stmts.length > 0 &&
93
- isStringDirectiveStatement(node.body.stmts[0], 'use server'));
94
- }
95
- function hasCacheDirectiveInFunctionLike(node) {
96
- return (node?.body?.type === 'BlockStatement' &&
97
- Array.isArray(node.body.stmts) &&
98
- node.body.stmts.length > 0 &&
99
- isStringDirectiveStatement(node.body.stmts[0], 'use cache'));
100
- }
101
- function createSpan() {
102
- return { start: 0, end: 0 };
103
- }
104
- function createIdentifier(value) {
105
- return {
106
- type: 'Identifier',
107
- value,
108
- optional: false,
109
- ctxt: 0,
110
- span: createSpan(),
111
- };
112
- }
113
- function createStringLiteral(value) {
114
- return {
115
- type: 'StringLiteral',
116
- value,
117
- raw: JSON.stringify(value),
118
- span: createSpan(),
119
- };
120
- }
121
- function createRuntimeMemberExpression(runtimeIdentifier, propertyName) {
122
- return {
123
- type: 'MemberExpression',
124
- object: createIdentifier(runtimeIdentifier),
125
- property: {
126
- type: 'Identifier',
127
- value: propertyName,
128
- span: createSpan(),
129
- },
130
- span: createSpan(),
131
- };
132
- }
133
- function createRegisterReferenceCall(targetExpression, id, exportName = 'default') {
134
- return {
135
- type: 'CallExpression',
136
- ctxt: 0,
137
- span: createSpan(),
138
- callee: createRuntimeMemberExpression('__vistaServerActionsRuntime', 'registerInlineServerReference'),
139
- arguments: [
140
- { spread: null, expression: targetExpression },
141
- { spread: null, expression: createStringLiteral(id) },
142
- { spread: null, expression: createStringLiteral(exportName) },
143
- ],
144
- typeArguments: null,
145
- };
146
- }
147
- function createRegistrationStatement(identifierName, id, exportName = 'default') {
148
- return {
149
- type: 'ExpressionStatement',
150
- span: createSpan(),
151
- expression: createRegisterReferenceCall(createIdentifier(identifierName), id, exportName),
152
- };
153
- }
154
- function createUseCacheWrapCall(targetExpression, filename, exportName) {
155
- return {
156
- type: 'CallExpression',
157
- ctxt: 0,
158
- span: createSpan(),
159
- callee: createRuntimeMemberExpression('__vistaCacheRuntime', 'wrapModuleUseCacheExport'),
160
- arguments: [
161
- { spread: null, expression: targetExpression },
162
- { spread: null, expression: createStringLiteral(filename) },
163
- { spread: null, expression: createStringLiteral(exportName) },
164
- ],
165
- typeArguments: null,
166
- };
167
- }
168
- function createUseCacheAssignmentStatement(identifierName, filename, exportName) {
169
- return {
170
- type: 'ExpressionStatement',
171
- span: createSpan(),
172
- expression: {
173
- type: 'AssignmentExpression',
174
- span: createSpan(),
175
- operator: '=',
176
- left: createIdentifier(identifierName),
177
- right: createUseCacheWrapCall(createIdentifier(identifierName), filename, exportName),
178
- },
179
- };
180
- }
181
- function createRuntimeRequireDeclaration(runtimeIdentifier, runtimeSpecifier) {
182
- return {
183
- type: 'VariableDeclaration',
184
- span: createSpan(),
185
- ctxt: 0,
186
- kind: 'const',
187
- declare: false,
188
- declarations: [
189
- {
190
- type: 'VariableDeclarator',
191
- span: createSpan(),
192
- definite: false,
193
- id: {
194
- ...createIdentifier(runtimeIdentifier),
195
- typeAnnotation: null,
196
- },
197
- init: {
198
- type: 'CallExpression',
199
- ctxt: 0,
200
- span: createSpan(),
201
- callee: createIdentifier('require'),
202
- arguments: [
203
- {
204
- spread: null,
205
- expression: createStringLiteral(runtimeSpecifier),
206
- },
207
- ],
208
- typeArguments: null,
209
- },
210
- },
211
- ],
212
- };
213
- }
214
- function processFunctionLikeDeclaration(statement, filename, state, nextStatements) {
215
- if (statement?.body?.type !== 'BlockStatement') {
216
- return false;
217
- }
218
- statement.body.stmts = processStatementList(statement.body.stmts || [], filename, state);
219
- nextStatements.push(statement);
220
- const identifierName = statement.identifier?.value;
221
- if (identifierName && hasServerDirectiveInFunctionLike(statement)) {
222
- const actionId = (0, runtime_actions_1.createInlineServerActionId)(filename, state.nextOrdinal++, identifierName);
223
- nextStatements.push(createRegistrationStatement(identifierName, actionId, identifierName));
224
- state.serverActionsTransformed = true;
225
- }
226
- if (identifierName && hasCacheDirectiveInFunctionLike(statement)) {
227
- nextStatements.push(createUseCacheAssignmentStatement(identifierName, filename, identifierName));
228
- state.useCacheTransformed = true;
229
- }
230
- return true;
231
- }
232
- function processStatementList(statements, filename, state) {
233
- const nextStatements = [];
234
- for (const statement of statements) {
235
- if (!statement || typeof statement !== 'object') {
236
- nextStatements.push(statement);
237
- continue;
238
- }
239
- if (statement.type === 'FunctionDeclaration') {
240
- processFunctionLikeDeclaration(statement, filename, state, nextStatements);
241
- continue;
242
- }
243
- if (statement.type === 'ExportDefaultDeclaration' && statement.decl) {
244
- if (statement.decl.body?.type === 'BlockStatement') {
245
- statement.decl.body.stmts = processStatementList(statement.decl.body.stmts || [], filename, state);
246
- }
247
- else if ((statement.decl.type === 'FunctionExpression' || statement.decl.type === 'ArrowFunctionExpression') &&
248
- statement.decl.body?.type === 'BlockStatement') {
249
- statement.decl.body.stmts = processStatementList(statement.decl.body.stmts || [], filename, state);
250
- }
251
- if ((statement.decl.type === 'FunctionDeclaration' ||
252
- statement.decl.type === 'FunctionExpression' ||
253
- statement.decl.type === 'ArrowFunctionExpression') &&
254
- hasCacheDirectiveInFunctionLike(statement.decl)) {
255
- statement.decl = createUseCacheWrapCall(statement.decl, filename, statement.decl.identifier?.value || 'default');
256
- state.useCacheTransformed = true;
257
- }
258
- nextStatements.push(statement);
259
- continue;
260
- }
261
- if (statement.type === 'ExportDeclaration' && statement.declaration) {
262
- if (statement.declaration.type === 'FunctionDeclaration') {
263
- statement.declaration.body.stmts = processStatementList(statement.declaration.body?.stmts || [], filename, state);
264
- const identifierName = statement.declaration.identifier?.value;
265
- nextStatements.push(statement);
266
- if (identifierName && hasServerDirectiveInFunctionLike(statement.declaration)) {
267
- const actionId = (0, runtime_actions_1.createInlineServerActionId)(filename, state.nextOrdinal++, identifierName);
268
- nextStatements.push(createRegistrationStatement(identifierName, actionId, identifierName));
269
- state.serverActionsTransformed = true;
270
- }
271
- if (identifierName && hasCacheDirectiveInFunctionLike(statement.declaration)) {
272
- nextStatements.push(createUseCacheAssignmentStatement(identifierName, filename, identifierName));
273
- state.useCacheTransformed = true;
274
- }
275
- continue;
276
- }
277
- else if (statement.declaration.type === 'VariableDeclaration' &&
278
- Array.isArray(statement.declaration.declarations)) {
279
- for (const declaration of statement.declaration.declarations) {
280
- if (!declaration?.init || typeof declaration.init !== 'object') {
281
- continue;
282
- }
283
- const declaratorName = declaration.id?.type === 'Identifier'
284
- ? declaration.id.value
285
- : `anonymous_${state.nextOrdinal}`;
286
- declaration.init = processExpression(declaration.init, filename, state, declaratorName);
287
- }
288
- }
289
- nextStatements.push(statement);
290
- continue;
291
- }
292
- if (statement.type === 'VariableDeclaration' && Array.isArray(statement.declarations)) {
293
- for (const declaration of statement.declarations) {
294
- if (!declaration?.init || typeof declaration.init !== 'object') {
295
- continue;
296
- }
297
- const declaratorName = declaration.id?.type === 'Identifier'
298
- ? declaration.id.value
299
- : `anonymous_${state.nextOrdinal}`;
300
- declaration.init = processExpression(declaration.init, filename, state, declaratorName);
301
- }
302
- nextStatements.push(statement);
303
- continue;
304
- }
305
- if (statement.type === 'ExpressionStatement' && statement.expression) {
306
- statement.expression = processExpression(statement.expression, filename, state);
307
- nextStatements.push(statement);
308
- continue;
309
- }
310
- if (statement.type === 'ReturnStatement' && statement.argument) {
311
- statement.argument = processExpression(statement.argument, filename, state);
312
- nextStatements.push(statement);
313
- continue;
314
- }
315
- if (statement.type === 'BlockStatement') {
316
- statement.stmts = processStatementList(statement.stmts || [], filename, state);
317
- nextStatements.push(statement);
318
- continue;
319
- }
320
- if (statement.type === 'IfStatement') {
321
- if (statement.consequent?.type === 'BlockStatement') {
322
- statement.consequent.stmts = processStatementList(statement.consequent.stmts || [], filename, state);
323
- }
324
- if (statement.alternate?.type === 'BlockStatement') {
325
- statement.alternate.stmts = processStatementList(statement.alternate.stmts || [], filename, state);
326
- }
327
- nextStatements.push(statement);
328
- continue;
329
- }
330
- if (statement.type === 'TryStatement') {
331
- if (statement.block?.type === 'BlockStatement') {
332
- statement.block.stmts = processStatementList(statement.block.stmts || [], filename, state);
333
- }
334
- if (statement.handler?.body?.type === 'BlockStatement') {
335
- statement.handler.body.stmts = processStatementList(statement.handler.body.stmts || [], filename, state);
336
- }
337
- if (statement.finalizer?.type === 'BlockStatement') {
338
- statement.finalizer.stmts = processStatementList(statement.finalizer.stmts || [], filename, state);
339
- }
340
- nextStatements.push(statement);
341
- continue;
342
- }
343
- if (statement.type === 'SwitchStatement' && Array.isArray(statement.cases)) {
344
- for (const switchCase of statement.cases) {
345
- switchCase.consequent = processStatementList(switchCase.consequent || [], filename, state);
346
- }
347
- nextStatements.push(statement);
348
- continue;
349
- }
350
- if ((statement.type === 'WhileStatement' ||
351
- statement.type === 'DoWhileStatement' ||
352
- statement.type === 'ForStatement' ||
353
- statement.type === 'ForInStatement' ||
354
- statement.type === 'ForOfStatement') &&
355
- statement.body?.type === 'BlockStatement') {
356
- statement.body.stmts = processStatementList(statement.body.stmts || [], filename, state);
357
- nextStatements.push(statement);
358
- continue;
359
- }
360
- nextStatements.push(statement);
361
- }
362
- return nextStatements;
363
- }
364
- function processExpression(expression, filename, state, hint = 'action') {
365
- if (!expression || typeof expression !== 'object') {
366
- return expression;
367
- }
368
- if (expression.type === 'ArrowFunctionExpression' || expression.type === 'FunctionExpression') {
369
- if (expression.body?.type === 'BlockStatement') {
370
- expression.body.stmts = processStatementList(expression.body.stmts || [], filename, state);
371
- }
372
- if (hasServerDirectiveInFunctionLike(expression)) {
373
- const actionId = (0, runtime_actions_1.createInlineServerActionId)(filename, state.nextOrdinal++, expression.identifier?.value || hint);
374
- state.serverActionsTransformed = true;
375
- return createRegisterReferenceCall(expression, actionId, expression.identifier?.value || hint);
376
- }
377
- if (hasCacheDirectiveInFunctionLike(expression)) {
378
- state.useCacheTransformed = true;
379
- return createUseCacheWrapCall(expression, filename, expression.identifier?.value || hint);
380
- }
381
- return expression;
382
- }
383
- if (expression.type === 'AssignmentExpression') {
384
- expression.left = processExpression(expression.left, filename, state, `${hint}_target`);
385
- expression.right = processExpression(expression.right, filename, state, hint);
386
- return expression;
387
- }
388
- if (expression.type === 'CallExpression') {
389
- expression.callee = processExpression(expression.callee, filename, state, `${hint}_callee`);
390
- expression.arguments = (expression.arguments || []).map((argument, index) => argument && argument.expression
391
- ? {
392
- ...argument,
393
- expression: processExpression(argument.expression, filename, state, `${hint}_${index}`),
394
- }
395
- : argument);
396
- return expression;
397
- }
398
- if (expression.type === 'NewExpression') {
399
- expression.callee = processExpression(expression.callee, filename, state, `${hint}_callee`);
400
- expression.arguments = (expression.arguments || []).map((argument, index) => argument && argument.expression
401
- ? {
402
- ...argument,
403
- expression: processExpression(argument.expression, filename, state, `${hint}_${index}`),
404
- }
405
- : argument);
406
- return expression;
407
- }
408
- if (expression.type === 'ArrayExpression') {
409
- expression.elements = (expression.elements || []).map((element, index) => element && element.expression
410
- ? {
411
- ...element,
412
- expression: processExpression(element.expression, filename, state, `${hint}_${index}`),
413
- }
414
- : element);
415
- return expression;
416
- }
417
- if (expression.type === 'MemberExpression') {
418
- expression.object = processExpression(expression.object, filename, state, `${hint}_object`);
419
- if (expression.property && expression.computed) {
420
- expression.property = processExpression(expression.property, filename, state, `${hint}_property`);
421
- }
422
- return expression;
423
- }
424
- if (expression.type === 'ObjectExpression') {
425
- expression.properties = (expression.properties || []).map((property) => {
426
- if (property?.type === 'KeyValueProperty') {
427
- property.value = processExpression(property.value, filename, state, property.key?.value || property.key?.name || hint);
428
- }
429
- return property;
430
- });
431
- return expression;
432
- }
433
- if (expression.type === 'ObjectPattern') {
434
- expression.properties = (expression.properties || []).map((property) => {
435
- if (property?.type === 'KeyValuePatternProperty' && property.value) {
436
- property.value = processExpression(property.value, filename, state, hint);
437
- }
438
- if (property?.type === 'AssignmentPatternProperty' && property.value) {
439
- property.value = processExpression(property.value, filename, state, hint);
440
- }
441
- return property;
442
- });
443
- return expression;
444
- }
445
- if (expression.type === 'ConditionalExpression') {
446
- expression.test = processExpression(expression.test, filename, state, hint);
447
- expression.consequent = processExpression(expression.consequent, filename, state, hint);
448
- expression.alternate = processExpression(expression.alternate, filename, state, hint);
449
- return expression;
450
- }
451
- if (expression.type === 'SequenceExpression') {
452
- expression.expressions = (expression.expressions || []).map((entry, index) => processExpression(entry, filename, state, `${hint}_${index}`));
453
- return expression;
454
- }
455
- if (expression.type === 'BinaryExpression' ||
456
- expression.type === 'LogicalExpression' ||
457
- expression.type === 'AssignmentPattern') {
458
- expression.left = processExpression(expression.left, filename, state, `${hint}_left`);
459
- expression.right = processExpression(expression.right, filename, state, `${hint}_right`);
460
- return expression;
461
- }
462
- if (expression.type === 'UnaryExpression' || expression.type === 'UpdateExpression') {
463
- expression.argument = processExpression(expression.argument, filename, state, hint);
464
- return expression;
465
- }
466
- if (expression.type === 'AwaitExpression' || expression.type === 'SpreadElement') {
467
- expression.argument = processExpression(expression.argument, filename, state, hint);
468
- return expression;
469
- }
470
- if (expression.type === 'YieldExpression') {
471
- if (expression.argument) {
472
- expression.argument = processExpression(expression.argument, filename, state, hint);
473
- }
474
- return expression;
475
- }
476
- if (expression.type === 'ParenExpression') {
477
- expression.expression = processExpression(expression.expression, filename, state, hint);
478
- return expression;
479
- }
480
- if (expression.type === 'TemplateLiteral') {
481
- expression.expressions = (expression.expressions || []).map((entry, index) => processExpression(entry, filename, state, `${hint}_${index}`));
482
- return expression;
483
- }
484
- if (expression.type === 'TaggedTemplateExpression') {
485
- expression.tag = processExpression(expression.tag, filename, state, `${hint}_tag`);
486
- expression.template = processExpression(expression.template, filename, state, `${hint}_template`);
487
- return expression;
488
- }
489
- return expression;
490
- }
491
- function injectRuntimeRequire(moduleAst, runtimeIdentifier, runtimeSpecifier) {
492
- const body = Array.isArray(moduleAst.body) ? moduleAst.body : [];
493
- let insertionIndex = 0;
494
- while (insertionIndex < body.length && isStringDirectiveStatement(body[insertionIndex], 'use strict')) {
495
- insertionIndex += 1;
496
- }
497
- body.splice(insertionIndex, 0, createRuntimeRequireDeclaration(runtimeIdentifier, runtimeSpecifier));
498
- moduleAst.body = body;
499
- }
500
- function getParserOptions(filename) {
501
- const extension = path_1.default.extname(filename).toLowerCase();
502
- const isTypeScript = extension === '.ts' || extension === '.tsx' || extension === '.mts' || extension === '.cts';
503
- const jsxEnabled = isTypeScript ? extension === '.tsx' : true;
504
- return {
505
- syntax: isTypeScript ? 'typescript' : 'ecmascript',
506
- tsx: isTypeScript ? jsxEnabled : undefined,
507
- jsx: !isTypeScript ? jsxEnabled : undefined,
508
- };
509
- }
510
- function transformInlineServerActions(content, filename) {
511
- if ((!content.includes("'use server'") && !content.includes('"use server"')) &&
512
- (!content.includes("'use cache'") && !content.includes('"use cache"'))) {
513
- return content;
514
- }
515
- try {
516
- const moduleAst = (0, core_1.parseSync)(content, getParserOptions(filename));
517
- const state = {
518
- serverActionsTransformed: false,
519
- useCacheTransformed: false,
520
- nextOrdinal: 0,
521
- };
522
- moduleAst.body = processStatementList(moduleAst.body || [], filename, state);
523
- if (!state.serverActionsTransformed && !state.useCacheTransformed) {
524
- return content;
525
- }
526
- if (state.serverActionsTransformed) {
527
- injectRuntimeRequire(moduleAst, '__vistaServerActionsRuntime', activeRuntimeActionsSpecifier);
528
- }
529
- if (state.useCacheTransformed) {
530
- injectRuntimeRequire(moduleAst, '__vistaCacheRuntime', activeCacheRuntimeSpecifier);
531
- }
532
- return (0, core_1.printSync)(moduleAst).code;
533
- }
534
- catch (error) {
535
- if (process.env.VISTA_DEBUG) {
536
- console.error(`[vista:compile] Inline action transform failed for ${filename}:`, error);
537
- }
538
- return content;
539
- }
540
- }
541
- function wrapUseCacheModuleExports(moduleExports, filename) {
542
- if (typeof moduleExports === 'function') {
543
- const wrappedDefault = (0, cache_1.wrapModuleUseCacheExport)(moduleExports, filename, 'default');
544
- for (const propertyName of Reflect.ownKeys(moduleExports)) {
545
- if (propertyName === 'default' ||
546
- propertyName === 'length' ||
547
- propertyName === 'name' ||
548
- propertyName === 'prototype') {
549
- continue;
550
- }
551
- const descriptor = Object.getOwnPropertyDescriptor(moduleExports, propertyName);
552
- const currentValue = descriptor && 'value' in descriptor
553
- ? descriptor.value
554
- : descriptor?.get
555
- ? descriptor.get.call(moduleExports)
556
- : moduleExports[propertyName];
557
- Object.defineProperty(wrappedDefault, propertyName, {
558
- configurable: true,
559
- enumerable: descriptor?.enumerable ?? true,
560
- writable: true,
561
- value: (0, cache_1.wrapModuleUseCacheExport)(currentValue, filename, String(propertyName)),
562
- });
563
- }
564
- return wrappedDefault;
565
- }
566
- if (!moduleExports || typeof moduleExports !== 'object') {
567
- return moduleExports;
568
- }
569
- const wrappedExports = Object.create(Object.getPrototypeOf(moduleExports));
570
- for (const exportName of Reflect.ownKeys(moduleExports)) {
571
- const descriptor = Object.getOwnPropertyDescriptor(moduleExports, exportName);
572
- if (!descriptor) {
573
- continue;
574
- }
575
- if (exportName === '__esModule') {
576
- Object.defineProperty(wrappedExports, exportName, descriptor);
577
- continue;
578
- }
579
- const currentValue = 'value' in descriptor
580
- ? descriptor.value
581
- : descriptor.get
582
- ? descriptor.get.call(moduleExports)
583
- : moduleExports[exportName];
584
- Object.defineProperty(wrappedExports, exportName, {
585
- configurable: true,
586
- enumerable: descriptor.enumerable ?? true,
587
- writable: true,
588
- value: (0, cache_1.wrapModuleUseCacheExport)(currentValue, filename, String(exportName)),
589
- });
590
- }
591
- return wrappedExports;
592
- }
593
- function transpileProjectSource(source, filename, fallback) {
594
- try {
595
- return (0, core_1.transformSync)(source, {
596
- filename,
597
- sourceMaps: 'inline',
598
- module: {
599
- type: 'commonjs',
600
- },
601
- jsc: {
602
- target: 'es2020',
603
- parser: getParserOptions(filename),
604
- transform: {
605
- react: {
606
- runtime: 'automatic',
607
- },
608
- },
609
- },
610
- }).code;
611
- }
612
- catch (error) {
613
- if (process.env.VISTA_DEBUG) {
614
- console.error(`[vista:compile] Source transpile failed for ${filename}:`, error);
615
- }
616
- return fallback;
617
- }
618
- }
619
- function installModuleCompileHook(options) {
620
- activeCompileRoots = Array.from(new Set([options.cwd, path_1.default.resolve(__dirname, '..')].map((entry) => path_1.default.resolve(entry))));
621
- activeRuntimeActionsSpecifier = require.resolve('./runtime-actions');
622
- activeCacheRuntimeSpecifier = require.resolve('./cache');
623
- activeCacheComponentsEnabled = Boolean(options.cacheComponentsEnabled);
624
- if (options.createClientModuleProxy) {
625
- activeClientModuleProxyFactory = options.createClientModuleProxy;
626
- }
627
- if (compileHookInstalled) {
628
- return;
629
- }
630
- originalCompile = CjsModule.prototype._compile;
631
- CjsModule.prototype._compile = function compileVistaModule(content, filename) {
632
- if (!isProjectModule(filename, activeCompileRoots)) {
633
- return originalCompile.call(this, content, filename);
634
- }
635
- const originalSource = readOriginalSource(filename, content);
636
- const containsUseCacheDirective = containsDirectiveLiteral(originalSource, 'use cache');
637
- const hasTopLevelUseCacheDirective = hasDirective(originalSource, 'use cache');
638
- if (containsUseCacheDirective && !activeCacheComponentsEnabled) {
639
- throw createDirectiveError(filename, 'To use "use cache", enable experimental.cacheComponents.enabled in vista.config.*.');
640
- }
641
- if (containsUseCacheDirective && hasDirective(originalSource, 'use client')) {
642
- throw createDirectiveError(filename, '"use cache" cannot be used in a Client Component. Move the cached logic to a server-only module.');
643
- }
644
- if (activeClientModuleProxyFactory && hasDirective(originalSource, 'use client')) {
645
- const moduleId = (0, url_1.pathToFileURL)(filename).href;
646
- this.exports = activeClientModuleProxyFactory(moduleId);
647
- return;
648
- }
649
- const sourceToCompile = originalSource || content;
650
- const transformedSource = transformInlineServerActions(sourceToCompile, filename);
651
- const compiledContent = transpileProjectSource(transformedSource, filename, content);
652
- const compileResult = originalCompile.call(this, compiledContent, filename);
653
- if (hasDirective(originalSource, 'use server')) {
654
- (0, runtime_actions_1.registerServerActionModule)(this.exports, filename);
655
- }
656
- if (hasTopLevelUseCacheDirective) {
657
- this.exports = wrapUseCacheModuleExports(this.exports, filename);
658
- }
659
- return compileResult;
660
- };
661
- compileHookInstalled = true;
662
- }
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.installModuleCompileHook = installModuleCompileHook;
7
+ const fs_1 = __importDefault(require("fs"));
8
+ const path_1 = __importDefault(require("path"));
9
+ const core_1 = require("@swc/core");
10
+ const url_1 = require("url");
11
+ const runtime_actions_1 = require("./runtime-actions");
12
+ const cache_1 = require("./cache");
13
+ const CjsModule = require('module');
14
+ let compileHookInstalled = false;
15
+ let originalCompile = null;
16
+ let activeCompileRoots = [];
17
+ let activeClientModuleProxyFactory = null;
18
+ let activeRuntimeActionsSpecifier = require.resolve('./runtime-actions');
19
+ let activeCacheRuntimeSpecifier = require.resolve('./cache');
20
+ let activeCacheComponentsEnabled = false;
21
+ function normalizeModulePath(filePath) {
22
+ return filePath.replace(/\\/g, '/').toLowerCase();
23
+ }
24
+ function stripLeadingCommentsAndWhitespace(source) {
25
+ let remaining = source;
26
+ while (true) {
27
+ const trimmed = remaining.trimStart();
28
+ if (trimmed.startsWith('//')) {
29
+ const newlineIndex = trimmed.indexOf('\n');
30
+ remaining = newlineIndex === -1 ? '' : trimmed.slice(newlineIndex + 1);
31
+ continue;
32
+ }
33
+ if (trimmed.startsWith('/*')) {
34
+ const commentEndIndex = trimmed.indexOf('*/');
35
+ if (commentEndIndex === -1) {
36
+ return trimmed;
37
+ }
38
+ remaining = trimmed.slice(commentEndIndex + 2);
39
+ continue;
40
+ }
41
+ return trimmed;
42
+ }
43
+ }
44
+ function hasDirective(source, directive) {
45
+ const trimmed = stripLeadingCommentsAndWhitespace(source);
46
+ return trimmed.startsWith(`'${directive}'`) || trimmed.startsWith(`"${directive}"`);
47
+ }
48
+ function containsDirectiveLiteral(source, directive) {
49
+ return source.includes(`'${directive}'`) || source.includes(`"${directive}"`);
50
+ }
51
+ function readOriginalSource(filename, fallback) {
52
+ try {
53
+ return fs_1.default.readFileSync(filename, 'utf-8');
54
+ }
55
+ catch {
56
+ return fallback;
57
+ }
58
+ }
59
+ function createDirectiveError(filename, message) {
60
+ return new Error(`[vista:cache] ${path_1.default.basename(filename)}: ${message}`);
61
+ }
62
+ function isVistaFrameworkModule(filename) {
63
+ const normalized = normalizeModulePath(filename);
64
+ return (/\/packages\/vista\/(src|dist)\//.test(normalized) ||
65
+ /\/node_modules\/vista\//.test(normalized) ||
66
+ /\/node_modules\/@vistagenic\/vista\//.test(normalized));
67
+ }
68
+ function isProjectModule(filename, roots) {
69
+ const normalized = normalizeModulePath(filename);
70
+ if (!/\.[cm]?[jt]sx?$/i.test(normalized)) {
71
+ return false;
72
+ }
73
+ const matchesRoot = roots.some((root) => {
74
+ const rootPrefix = normalizeModulePath(`${root}${path_1.default.sep}`);
75
+ return normalized.startsWith(rootPrefix);
76
+ });
77
+ const isStandaloneProjectModule = roots.some((root) => {
78
+ const normalizedRoot = normalizeModulePath(root);
79
+ return (normalizedRoot.includes('/.vista/standalone/') &&
80
+ (normalized === normalizedRoot || normalized.startsWith(`${normalizedRoot}/`)));
81
+ });
82
+ if (isVistaFrameworkModule(filename)) {
83
+ return true;
84
+ }
85
+ if (!matchesRoot && !isStandaloneProjectModule)
86
+ return false;
87
+ if (normalized.includes('/node_modules/'))
88
+ return false;
89
+ if (normalized.includes('/.vista/') && !isStandaloneProjectModule)
90
+ return false;
91
+ if (normalized.includes('/.flash/'))
92
+ return false;
93
+ return true;
94
+ }
95
+ function isStringDirectiveStatement(statement, directive) {
96
+ return (statement?.type === 'ExpressionStatement' &&
97
+ (statement.directive === directive ||
98
+ (statement.expression?.type === 'StringLiteral' &&
99
+ statement.expression?.value === directive)));
100
+ }
101
+ /**
102
+ * SWC labels the body of a function/method/arrow as `FunctionBody`, while a bare
103
+ * block (`{ ... }`, loop/if/try bodies) stays `BlockStatement`. Older versions
104
+ * reported `BlockStatement` for both, so accept either shape here - otherwise
105
+ * every function-body walk silently no-ops and inline `'use server'` /
106
+ * `'use cache'` directives never get transformed.
107
+ */
108
+ function isFunctionBodyBlock(node) {
109
+ return node?.type === 'BlockStatement' || node?.type === 'FunctionBody';
110
+ }
111
+ function hasServerDirectiveInFunctionLike(node) {
112
+ return (isFunctionBodyBlock(node?.body) &&
113
+ Array.isArray(node.body.stmts) &&
114
+ node.body.stmts.length > 0 &&
115
+ isStringDirectiveStatement(node.body.stmts[0], 'use server'));
116
+ }
117
+ function hasCacheDirectiveInFunctionLike(node) {
118
+ return (isFunctionBodyBlock(node?.body) &&
119
+ Array.isArray(node.body.stmts) &&
120
+ node.body.stmts.length > 0 &&
121
+ isStringDirectiveStatement(node.body.stmts[0], 'use cache'));
122
+ }
123
+ function createSpan() {
124
+ return { start: 0, end: 0 };
125
+ }
126
+ function createIdentifier(value) {
127
+ return {
128
+ type: 'Identifier',
129
+ value,
130
+ optional: false,
131
+ ctxt: 0,
132
+ span: createSpan(),
133
+ };
134
+ }
135
+ function createStringLiteral(value) {
136
+ return {
137
+ type: 'StringLiteral',
138
+ value,
139
+ raw: JSON.stringify(value),
140
+ span: createSpan(),
141
+ };
142
+ }
143
+ function createRuntimeMemberExpression(runtimeIdentifier, propertyName) {
144
+ return {
145
+ type: 'MemberExpression',
146
+ object: createIdentifier(runtimeIdentifier),
147
+ property: {
148
+ type: 'Identifier',
149
+ value: propertyName,
150
+ span: createSpan(),
151
+ },
152
+ span: createSpan(),
153
+ };
154
+ }
155
+ function createRegisterReferenceCall(targetExpression, id, exportName = 'default') {
156
+ return {
157
+ type: 'CallExpression',
158
+ ctxt: 0,
159
+ span: createSpan(),
160
+ callee: createRuntimeMemberExpression('__vistaServerActionsRuntime', 'registerInlineServerReference'),
161
+ arguments: [
162
+ { spread: null, expression: targetExpression },
163
+ { spread: null, expression: createStringLiteral(id) },
164
+ { spread: null, expression: createStringLiteral(exportName) },
165
+ ],
166
+ typeArguments: null,
167
+ };
168
+ }
169
+ function createRegistrationStatement(identifierName, id, exportName = 'default') {
170
+ return {
171
+ type: 'ExpressionStatement',
172
+ span: createSpan(),
173
+ expression: createRegisterReferenceCall(createIdentifier(identifierName), id, exportName),
174
+ };
175
+ }
176
+ function createUseCacheWrapCall(targetExpression, filename, exportName) {
177
+ return {
178
+ type: 'CallExpression',
179
+ ctxt: 0,
180
+ span: createSpan(),
181
+ callee: createRuntimeMemberExpression('__vistaCacheRuntime', 'wrapModuleUseCacheExport'),
182
+ arguments: [
183
+ { spread: null, expression: targetExpression },
184
+ { spread: null, expression: createStringLiteral(filename) },
185
+ { spread: null, expression: createStringLiteral(exportName) },
186
+ ],
187
+ typeArguments: null,
188
+ };
189
+ }
190
+ function createUseCacheAssignmentStatement(identifierName, filename, exportName) {
191
+ return {
192
+ type: 'ExpressionStatement',
193
+ span: createSpan(),
194
+ expression: {
195
+ type: 'AssignmentExpression',
196
+ span: createSpan(),
197
+ operator: '=',
198
+ left: createIdentifier(identifierName),
199
+ right: createUseCacheWrapCall(createIdentifier(identifierName), filename, exportName),
200
+ },
201
+ };
202
+ }
203
+ function createRuntimeRequireDeclaration(runtimeIdentifier, runtimeSpecifier) {
204
+ return {
205
+ type: 'VariableDeclaration',
206
+ span: createSpan(),
207
+ ctxt: 0,
208
+ kind: 'const',
209
+ declare: false,
210
+ declarations: [
211
+ {
212
+ type: 'VariableDeclarator',
213
+ span: createSpan(),
214
+ definite: false,
215
+ id: {
216
+ ...createIdentifier(runtimeIdentifier),
217
+ typeAnnotation: null,
218
+ },
219
+ init: {
220
+ type: 'CallExpression',
221
+ ctxt: 0,
222
+ span: createSpan(),
223
+ callee: createIdentifier('require'),
224
+ arguments: [
225
+ {
226
+ spread: null,
227
+ expression: createStringLiteral(runtimeSpecifier),
228
+ },
229
+ ],
230
+ typeArguments: null,
231
+ },
232
+ },
233
+ ],
234
+ };
235
+ }
236
+ function processFunctionLikeDeclaration(statement, filename, state, nextStatements) {
237
+ if (!isFunctionBodyBlock(statement?.body)) {
238
+ // Bodyless declarations (TS overload signatures, `declare function`) still
239
+ // have to survive into the emitted module.
240
+ nextStatements.push(statement);
241
+ return false;
242
+ }
243
+ statement.body.stmts = processStatementList(statement.body.stmts || [], filename, state);
244
+ nextStatements.push(statement);
245
+ const identifierName = statement.identifier?.value;
246
+ if (identifierName && hasServerDirectiveInFunctionLike(statement)) {
247
+ const actionId = (0, runtime_actions_1.createInlineServerActionId)(filename, state.nextOrdinal++, identifierName);
248
+ nextStatements.push(createRegistrationStatement(identifierName, actionId, identifierName));
249
+ state.serverActionsTransformed = true;
250
+ }
251
+ if (identifierName && hasCacheDirectiveInFunctionLike(statement)) {
252
+ nextStatements.push(createUseCacheAssignmentStatement(identifierName, filename, identifierName));
253
+ state.useCacheTransformed = true;
254
+ }
255
+ return true;
256
+ }
257
+ function processStatementList(statements, filename, state) {
258
+ const nextStatements = [];
259
+ for (const statement of statements) {
260
+ if (!statement || typeof statement !== 'object') {
261
+ nextStatements.push(statement);
262
+ continue;
263
+ }
264
+ if (statement.type === 'FunctionDeclaration') {
265
+ processFunctionLikeDeclaration(statement, filename, state, nextStatements);
266
+ continue;
267
+ }
268
+ if (statement.type === 'ExportDefaultDeclaration' && statement.decl) {
269
+ if (isFunctionBodyBlock(statement.decl.body)) {
270
+ statement.decl.body.stmts = processStatementList(statement.decl.body.stmts || [], filename, state);
271
+ }
272
+ else if ((statement.decl.type === 'FunctionExpression' || statement.decl.type === 'ArrowFunctionExpression') &&
273
+ isFunctionBodyBlock(statement.decl.body)) {
274
+ statement.decl.body.stmts = processStatementList(statement.decl.body.stmts || [], filename, state);
275
+ }
276
+ if ((statement.decl.type === 'FunctionDeclaration' ||
277
+ statement.decl.type === 'FunctionExpression' ||
278
+ statement.decl.type === 'ArrowFunctionExpression') &&
279
+ hasCacheDirectiveInFunctionLike(statement.decl)) {
280
+ statement.decl = createUseCacheWrapCall(statement.decl, filename, statement.decl.identifier?.value || 'default');
281
+ state.useCacheTransformed = true;
282
+ }
283
+ nextStatements.push(statement);
284
+ continue;
285
+ }
286
+ if (statement.type === 'ExportDeclaration' && statement.declaration) {
287
+ if (statement.declaration.type === 'FunctionDeclaration') {
288
+ statement.declaration.body.stmts = processStatementList(statement.declaration.body?.stmts || [], filename, state);
289
+ const identifierName = statement.declaration.identifier?.value;
290
+ nextStatements.push(statement);
291
+ if (identifierName && hasServerDirectiveInFunctionLike(statement.declaration)) {
292
+ const actionId = (0, runtime_actions_1.createInlineServerActionId)(filename, state.nextOrdinal++, identifierName);
293
+ nextStatements.push(createRegistrationStatement(identifierName, actionId, identifierName));
294
+ state.serverActionsTransformed = true;
295
+ }
296
+ if (identifierName && hasCacheDirectiveInFunctionLike(statement.declaration)) {
297
+ nextStatements.push(createUseCacheAssignmentStatement(identifierName, filename, identifierName));
298
+ state.useCacheTransformed = true;
299
+ }
300
+ continue;
301
+ }
302
+ else if (statement.declaration.type === 'VariableDeclaration' &&
303
+ Array.isArray(statement.declaration.declarations)) {
304
+ for (const declaration of statement.declaration.declarations) {
305
+ if (!declaration?.init || typeof declaration.init !== 'object') {
306
+ continue;
307
+ }
308
+ const declaratorName = declaration.id?.type === 'Identifier'
309
+ ? declaration.id.value
310
+ : `anonymous_${state.nextOrdinal}`;
311
+ declaration.init = processExpression(declaration.init, filename, state, declaratorName);
312
+ }
313
+ }
314
+ nextStatements.push(statement);
315
+ continue;
316
+ }
317
+ if (statement.type === 'VariableDeclaration' && Array.isArray(statement.declarations)) {
318
+ for (const declaration of statement.declarations) {
319
+ if (!declaration?.init || typeof declaration.init !== 'object') {
320
+ continue;
321
+ }
322
+ const declaratorName = declaration.id?.type === 'Identifier'
323
+ ? declaration.id.value
324
+ : `anonymous_${state.nextOrdinal}`;
325
+ declaration.init = processExpression(declaration.init, filename, state, declaratorName);
326
+ }
327
+ nextStatements.push(statement);
328
+ continue;
329
+ }
330
+ if (statement.type === 'ExpressionStatement' && statement.expression) {
331
+ statement.expression = processExpression(statement.expression, filename, state);
332
+ nextStatements.push(statement);
333
+ continue;
334
+ }
335
+ if (statement.type === 'ReturnStatement' && statement.argument) {
336
+ statement.argument = processExpression(statement.argument, filename, state);
337
+ nextStatements.push(statement);
338
+ continue;
339
+ }
340
+ if (statement.type === 'BlockStatement') {
341
+ statement.stmts = processStatementList(statement.stmts || [], filename, state);
342
+ nextStatements.push(statement);
343
+ continue;
344
+ }
345
+ if (statement.type === 'IfStatement') {
346
+ if (statement.consequent?.type === 'BlockStatement') {
347
+ statement.consequent.stmts = processStatementList(statement.consequent.stmts || [], filename, state);
348
+ }
349
+ if (statement.alternate?.type === 'BlockStatement') {
350
+ statement.alternate.stmts = processStatementList(statement.alternate.stmts || [], filename, state);
351
+ }
352
+ nextStatements.push(statement);
353
+ continue;
354
+ }
355
+ if (statement.type === 'TryStatement') {
356
+ if (statement.block?.type === 'BlockStatement') {
357
+ statement.block.stmts = processStatementList(statement.block.stmts || [], filename, state);
358
+ }
359
+ if (statement.handler?.body?.type === 'BlockStatement') {
360
+ statement.handler.body.stmts = processStatementList(statement.handler.body.stmts || [], filename, state);
361
+ }
362
+ if (statement.finalizer?.type === 'BlockStatement') {
363
+ statement.finalizer.stmts = processStatementList(statement.finalizer.stmts || [], filename, state);
364
+ }
365
+ nextStatements.push(statement);
366
+ continue;
367
+ }
368
+ if (statement.type === 'SwitchStatement' && Array.isArray(statement.cases)) {
369
+ for (const switchCase of statement.cases) {
370
+ switchCase.consequent = processStatementList(switchCase.consequent || [], filename, state);
371
+ }
372
+ nextStatements.push(statement);
373
+ continue;
374
+ }
375
+ if ((statement.type === 'WhileStatement' ||
376
+ statement.type === 'DoWhileStatement' ||
377
+ statement.type === 'ForStatement' ||
378
+ statement.type === 'ForInStatement' ||
379
+ statement.type === 'ForOfStatement') &&
380
+ statement.body?.type === 'BlockStatement') {
381
+ statement.body.stmts = processStatementList(statement.body.stmts || [], filename, state);
382
+ nextStatements.push(statement);
383
+ continue;
384
+ }
385
+ nextStatements.push(statement);
386
+ }
387
+ return nextStatements;
388
+ }
389
+ function processExpression(expression, filename, state, hint = 'action') {
390
+ if (!expression || typeof expression !== 'object') {
391
+ return expression;
392
+ }
393
+ if (expression.type === 'ArrowFunctionExpression' || expression.type === 'FunctionExpression') {
394
+ if (isFunctionBodyBlock(expression.body)) {
395
+ expression.body.stmts = processStatementList(expression.body.stmts || [], filename, state);
396
+ }
397
+ if (hasServerDirectiveInFunctionLike(expression)) {
398
+ const actionId = (0, runtime_actions_1.createInlineServerActionId)(filename, state.nextOrdinal++, expression.identifier?.value || hint);
399
+ state.serverActionsTransformed = true;
400
+ return createRegisterReferenceCall(expression, actionId, expression.identifier?.value || hint);
401
+ }
402
+ if (hasCacheDirectiveInFunctionLike(expression)) {
403
+ state.useCacheTransformed = true;
404
+ return createUseCacheWrapCall(expression, filename, expression.identifier?.value || hint);
405
+ }
406
+ return expression;
407
+ }
408
+ if (expression.type === 'AssignmentExpression') {
409
+ expression.left = processExpression(expression.left, filename, state, `${hint}_target`);
410
+ expression.right = processExpression(expression.right, filename, state, hint);
411
+ return expression;
412
+ }
413
+ if (expression.type === 'CallExpression') {
414
+ expression.callee = processExpression(expression.callee, filename, state, `${hint}_callee`);
415
+ expression.arguments = (expression.arguments || []).map((argument, index) => argument && argument.expression
416
+ ? {
417
+ ...argument,
418
+ expression: processExpression(argument.expression, filename, state, `${hint}_${index}`),
419
+ }
420
+ : argument);
421
+ return expression;
422
+ }
423
+ if (expression.type === 'NewExpression') {
424
+ expression.callee = processExpression(expression.callee, filename, state, `${hint}_callee`);
425
+ expression.arguments = (expression.arguments || []).map((argument, index) => argument && argument.expression
426
+ ? {
427
+ ...argument,
428
+ expression: processExpression(argument.expression, filename, state, `${hint}_${index}`),
429
+ }
430
+ : argument);
431
+ return expression;
432
+ }
433
+ if (expression.type === 'ArrayExpression') {
434
+ expression.elements = (expression.elements || []).map((element, index) => element && element.expression
435
+ ? {
436
+ ...element,
437
+ expression: processExpression(element.expression, filename, state, `${hint}_${index}`),
438
+ }
439
+ : element);
440
+ return expression;
441
+ }
442
+ if (expression.type === 'MemberExpression') {
443
+ expression.object = processExpression(expression.object, filename, state, `${hint}_object`);
444
+ if (expression.property && expression.computed) {
445
+ expression.property = processExpression(expression.property, filename, state, `${hint}_property`);
446
+ }
447
+ return expression;
448
+ }
449
+ if (expression.type === 'ObjectExpression') {
450
+ expression.properties = (expression.properties || []).map((property) => {
451
+ if (property?.type === 'KeyValueProperty') {
452
+ property.value = processExpression(property.value, filename, state, property.key?.value || property.key?.name || hint);
453
+ }
454
+ return property;
455
+ });
456
+ return expression;
457
+ }
458
+ if (expression.type === 'ObjectPattern') {
459
+ expression.properties = (expression.properties || []).map((property) => {
460
+ if (property?.type === 'KeyValuePatternProperty' && property.value) {
461
+ property.value = processExpression(property.value, filename, state, hint);
462
+ }
463
+ if (property?.type === 'AssignmentPatternProperty' && property.value) {
464
+ property.value = processExpression(property.value, filename, state, hint);
465
+ }
466
+ return property;
467
+ });
468
+ return expression;
469
+ }
470
+ if (expression.type === 'ConditionalExpression') {
471
+ expression.test = processExpression(expression.test, filename, state, hint);
472
+ expression.consequent = processExpression(expression.consequent, filename, state, hint);
473
+ expression.alternate = processExpression(expression.alternate, filename, state, hint);
474
+ return expression;
475
+ }
476
+ if (expression.type === 'SequenceExpression') {
477
+ expression.expressions = (expression.expressions || []).map((entry, index) => processExpression(entry, filename, state, `${hint}_${index}`));
478
+ return expression;
479
+ }
480
+ if (expression.type === 'BinaryExpression' ||
481
+ expression.type === 'LogicalExpression' ||
482
+ expression.type === 'AssignmentPattern') {
483
+ expression.left = processExpression(expression.left, filename, state, `${hint}_left`);
484
+ expression.right = processExpression(expression.right, filename, state, `${hint}_right`);
485
+ return expression;
486
+ }
487
+ if (expression.type === 'UnaryExpression' || expression.type === 'UpdateExpression') {
488
+ expression.argument = processExpression(expression.argument, filename, state, hint);
489
+ return expression;
490
+ }
491
+ if (expression.type === 'AwaitExpression' || expression.type === 'SpreadElement') {
492
+ expression.argument = processExpression(expression.argument, filename, state, hint);
493
+ return expression;
494
+ }
495
+ if (expression.type === 'YieldExpression') {
496
+ if (expression.argument) {
497
+ expression.argument = processExpression(expression.argument, filename, state, hint);
498
+ }
499
+ return expression;
500
+ }
501
+ if (expression.type === 'ParenExpression') {
502
+ expression.expression = processExpression(expression.expression, filename, state, hint);
503
+ return expression;
504
+ }
505
+ if (expression.type === 'TemplateLiteral') {
506
+ expression.expressions = (expression.expressions || []).map((entry, index) => processExpression(entry, filename, state, `${hint}_${index}`));
507
+ return expression;
508
+ }
509
+ if (expression.type === 'TaggedTemplateExpression') {
510
+ expression.tag = processExpression(expression.tag, filename, state, `${hint}_tag`);
511
+ expression.template = processExpression(expression.template, filename, state, `${hint}_template`);
512
+ return expression;
513
+ }
514
+ return expression;
515
+ }
516
+ function injectRuntimeRequire(moduleAst, runtimeIdentifier, runtimeSpecifier) {
517
+ const body = Array.isArray(moduleAst.body) ? moduleAst.body : [];
518
+ let insertionIndex = 0;
519
+ while (insertionIndex < body.length && isStringDirectiveStatement(body[insertionIndex], 'use strict')) {
520
+ insertionIndex += 1;
521
+ }
522
+ body.splice(insertionIndex, 0, createRuntimeRequireDeclaration(runtimeIdentifier, runtimeSpecifier));
523
+ moduleAst.body = body;
524
+ }
525
+ function getParserOptions(filename) {
526
+ const extension = path_1.default.extname(filename).toLowerCase();
527
+ const isTypeScript = extension === '.ts' || extension === '.tsx' || extension === '.mts' || extension === '.cts';
528
+ const jsxEnabled = isTypeScript ? extension === '.tsx' : true;
529
+ return {
530
+ syntax: isTypeScript ? 'typescript' : 'ecmascript',
531
+ tsx: isTypeScript ? jsxEnabled : undefined,
532
+ jsx: !isTypeScript ? jsxEnabled : undefined,
533
+ };
534
+ }
535
+ function transformInlineServerActions(content, filename) {
536
+ if ((!content.includes("'use server'") && !content.includes('"use server"')) &&
537
+ (!content.includes("'use cache'") && !content.includes('"use cache"'))) {
538
+ return content;
539
+ }
540
+ try {
541
+ const moduleAst = (0, core_1.parseSync)(content, getParserOptions(filename));
542
+ const state = {
543
+ serverActionsTransformed: false,
544
+ useCacheTransformed: false,
545
+ nextOrdinal: 0,
546
+ };
547
+ moduleAst.body = processStatementList(moduleAst.body || [], filename, state);
548
+ if (!state.serverActionsTransformed && !state.useCacheTransformed) {
549
+ return content;
550
+ }
551
+ if (state.serverActionsTransformed) {
552
+ injectRuntimeRequire(moduleAst, '__vistaServerActionsRuntime', activeRuntimeActionsSpecifier);
553
+ }
554
+ if (state.useCacheTransformed) {
555
+ injectRuntimeRequire(moduleAst, '__vistaCacheRuntime', activeCacheRuntimeSpecifier);
556
+ }
557
+ return (0, core_1.printSync)(moduleAst).code;
558
+ }
559
+ catch (error) {
560
+ if (process.env.VISTA_DEBUG) {
561
+ console.error(`[vista:compile] Inline action transform failed for ${filename}:`, error);
562
+ }
563
+ return content;
564
+ }
565
+ }
566
+ function wrapUseCacheModuleExports(moduleExports, filename) {
567
+ if (typeof moduleExports === 'function') {
568
+ const wrappedDefault = (0, cache_1.wrapModuleUseCacheExport)(moduleExports, filename, 'default');
569
+ for (const propertyName of Reflect.ownKeys(moduleExports)) {
570
+ if (propertyName === 'default' ||
571
+ propertyName === 'length' ||
572
+ propertyName === 'name' ||
573
+ propertyName === 'prototype') {
574
+ continue;
575
+ }
576
+ const descriptor = Object.getOwnPropertyDescriptor(moduleExports, propertyName);
577
+ const currentValue = descriptor && 'value' in descriptor
578
+ ? descriptor.value
579
+ : descriptor?.get
580
+ ? descriptor.get.call(moduleExports)
581
+ : moduleExports[propertyName];
582
+ Object.defineProperty(wrappedDefault, propertyName, {
583
+ configurable: true,
584
+ enumerable: descriptor?.enumerable ?? true,
585
+ writable: true,
586
+ value: (0, cache_1.wrapModuleUseCacheExport)(currentValue, filename, String(propertyName)),
587
+ });
588
+ }
589
+ return wrappedDefault;
590
+ }
591
+ if (!moduleExports || typeof moduleExports !== 'object') {
592
+ return moduleExports;
593
+ }
594
+ const wrappedExports = Object.create(Object.getPrototypeOf(moduleExports));
595
+ for (const exportName of Reflect.ownKeys(moduleExports)) {
596
+ const descriptor = Object.getOwnPropertyDescriptor(moduleExports, exportName);
597
+ if (!descriptor) {
598
+ continue;
599
+ }
600
+ if (exportName === '__esModule') {
601
+ Object.defineProperty(wrappedExports, exportName, descriptor);
602
+ continue;
603
+ }
604
+ const currentValue = 'value' in descriptor
605
+ ? descriptor.value
606
+ : descriptor.get
607
+ ? descriptor.get.call(moduleExports)
608
+ : moduleExports[exportName];
609
+ Object.defineProperty(wrappedExports, exportName, {
610
+ configurable: true,
611
+ enumerable: descriptor.enumerable ?? true,
612
+ writable: true,
613
+ value: (0, cache_1.wrapModuleUseCacheExport)(currentValue, filename, String(exportName)),
614
+ });
615
+ }
616
+ return wrappedExports;
617
+ }
618
+ function transpileProjectSource(source, filename, fallback) {
619
+ try {
620
+ return (0, core_1.transformSync)(source, {
621
+ filename,
622
+ sourceMaps: 'inline',
623
+ module: {
624
+ type: 'commonjs',
625
+ },
626
+ jsc: {
627
+ target: 'es2020',
628
+ parser: getParserOptions(filename),
629
+ transform: {
630
+ react: {
631
+ runtime: 'automatic',
632
+ },
633
+ },
634
+ },
635
+ }).code;
636
+ }
637
+ catch (error) {
638
+ if (process.env.VISTA_DEBUG) {
639
+ console.error(`[vista:compile] Source transpile failed for ${filename}:`, error);
640
+ }
641
+ return fallback;
642
+ }
643
+ }
644
+ function installModuleCompileHook(options) {
645
+ const vistaRuntimeDir = path_1.default.resolve(__dirname, '..');
646
+ const vistaPackageRoot = path_1.default.resolve(__dirname, '..', '..');
647
+ activeCompileRoots = Array.from(new Set([
648
+ options.cwd,
649
+ vistaRuntimeDir,
650
+ vistaPackageRoot,
651
+ path_1.default.join(vistaPackageRoot, 'src'),
652
+ path_1.default.join(vistaPackageRoot, 'dist'),
653
+ ].map((entry) => path_1.default.resolve(entry))));
654
+ activeRuntimeActionsSpecifier = require.resolve('./runtime-actions');
655
+ activeCacheRuntimeSpecifier = require.resolve('./cache');
656
+ activeCacheComponentsEnabled = Boolean(options.cacheComponentsEnabled);
657
+ if (options.createClientModuleProxy) {
658
+ activeClientModuleProxyFactory = options.createClientModuleProxy;
659
+ }
660
+ if (compileHookInstalled) {
661
+ return;
662
+ }
663
+ originalCompile = CjsModule.prototype._compile;
664
+ CjsModule.prototype._compile = function compileVistaModule(content, filename) {
665
+ if (!isProjectModule(filename, activeCompileRoots)) {
666
+ return originalCompile.call(this, content, filename);
667
+ }
668
+ const originalSource = readOriginalSource(filename, content);
669
+ const containsUseCacheDirective = containsDirectiveLiteral(originalSource, 'use cache');
670
+ const hasTopLevelUseCacheDirective = hasDirective(originalSource, 'use cache');
671
+ if (containsUseCacheDirective && !activeCacheComponentsEnabled) {
672
+ throw createDirectiveError(filename, 'To use "use cache", enable experimental.cacheComponents.enabled in vista.config.*.');
673
+ }
674
+ if (containsUseCacheDirective && hasDirective(originalSource, 'use client')) {
675
+ throw createDirectiveError(filename, '"use cache" cannot be used in a Client Component. Move the cached logic to a server-only module.');
676
+ }
677
+ if (activeClientModuleProxyFactory && hasDirective(originalSource, 'use client')) {
678
+ const moduleId = (0, url_1.pathToFileURL)(filename).href;
679
+ this.exports = activeClientModuleProxyFactory(moduleId);
680
+ return;
681
+ }
682
+ const sourceToCompile = originalSource || content;
683
+ const transformedSource = transformInlineServerActions(sourceToCompile, filename);
684
+ const compiledContent = transpileProjectSource(transformedSource, filename, content);
685
+ const compileResult = originalCompile.call(this, compiledContent, filename);
686
+ if (hasDirective(originalSource, 'use server')) {
687
+ (0, runtime_actions_1.registerServerActionModule)(this.exports, filename);
688
+ }
689
+ if (hasTopLevelUseCacheDirective) {
690
+ this.exports = wrapUseCacheModuleExports(this.exports, filename);
691
+ }
692
+ return compileResult;
693
+ };
694
+ compileHookInstalled = true;
695
+ }