@jay-framework/dev-server 0.8.0 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +896 -7
  2. package/package.json +12 -12
package/dist/index.js CHANGED
@@ -8,10 +8,890 @@ import { createServer } from "vite";
8
8
  import { scanRoutes, routeToExpressRoute } from "@jay-framework/stack-route-scanner";
9
9
  import { runInitCallbacks, runShutdownCallbacks, clearLifecycleCallbacks, clearServiceRegistry, DevSlowlyChangingPhase, loadPageParts, renderFastChangingData, generateClientScript } from "@jay-framework/stack-server-runtime";
10
10
  import { jayRuntime } from "@jay-framework/vite-plugin";
11
+ import { createRequire } from "module";
12
+ import "@jay-framework/compiler-shared";
11
13
  import * as path from "node:path";
12
14
  import path__default from "node:path";
15
+ import "@jay-framework/compiler-jay-html";
13
16
  import * as fs from "node:fs";
14
17
  import { pathToFileURL } from "node:url";
18
+ const s$1 = createRequire(import.meta.url), e$1 = s$1("typescript"), c$1 = new Proxy(e$1, {
19
+ get(t, r) {
20
+ return t[r];
21
+ }
22
+ });
23
+ var __defProp2 = Object.defineProperty;
24
+ var __defNormalProp2 = (obj, key, value) => key in obj ? __defProp2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
25
+ var __publicField2 = (obj, key, value) => {
26
+ __defNormalProp2(obj, typeof key !== "symbol" ? key + "" : key, value);
27
+ return value;
28
+ };
29
+ const s = createRequire(import.meta.url), e = s("typescript"), c = new Proxy(e, {
30
+ get(t, r) {
31
+ return t[r];
32
+ }
33
+ });
34
+ const {
35
+ visitEachChild: visitEachChild$5,
36
+ isArrowFunction: isArrowFunction$4,
37
+ isFunctionExpression: isFunctionExpression$1,
38
+ createSourceFile: createSourceFile$1,
39
+ ScriptTarget: ScriptTarget$1,
40
+ isFunctionDeclaration: isFunctionDeclaration$3,
41
+ isMethodDeclaration,
42
+ isConstructorDeclaration,
43
+ isGetAccessorDeclaration,
44
+ isSetAccessorDeclaration,
45
+ createPrinter: createPrinter$1,
46
+ NewLineKind,
47
+ EmitHint,
48
+ setTextRange
49
+ } = c;
50
+ createPrinter$1({
51
+ newLine: NewLineKind.LineFeed
52
+ });
53
+ function isFunctionLikeDeclarationBase(node) {
54
+ return isFunctionExpression$1(node) || isArrowFunction$4(node) || isFunctionDeclaration$3(node) || isMethodDeclaration(node) || isConstructorDeclaration(node) || isGetAccessorDeclaration(node) || isSetAccessorDeclaration(node);
55
+ }
56
+ function mkTransformer(fileTransformer, config) {
57
+ return (context) => {
58
+ const { factory } = context;
59
+ return (sourceFile) => {
60
+ return fileTransformer({ factory, context, sourceFile, ...config || {} });
61
+ };
62
+ };
63
+ }
64
+ const {
65
+ isIdentifier: isIdentifier$9,
66
+ isStatement: isStatement$3,
67
+ isObjectBindingPattern,
68
+ isArrayBindingPattern,
69
+ isBindingElement,
70
+ isPropertyAccessExpression: isPropertyAccessExpression$5,
71
+ isElementAccessExpression: isElementAccessExpression$1,
72
+ isStringLiteral: isStringLiteral$8,
73
+ isParenthesizedExpression,
74
+ isAsExpression,
75
+ isObjectLiteralExpression,
76
+ isPropertyAssignment: isPropertyAssignment$1,
77
+ isShorthandPropertyAssignment,
78
+ isNumericLiteral,
79
+ isToken,
80
+ isNamespaceImport,
81
+ isNamedImports: isNamedImports$1,
82
+ isArrowFunction: isArrowFunction$2,
83
+ isFunctionExpression,
84
+ isCallExpression: isCallExpression$6,
85
+ NodeFlags,
86
+ SyntaxKind: SyntaxKind$5
87
+ } = c;
88
+ var VariableRootType = /* @__PURE__ */ ((VariableRootType2) => {
89
+ VariableRootType2[VariableRootType2["FunctionParameter"] = 0] = "FunctionParameter";
90
+ VariableRootType2[VariableRootType2["FunctionDefinition"] = 1] = "FunctionDefinition";
91
+ VariableRootType2[VariableRootType2["Literal"] = 2] = "Literal";
92
+ VariableRootType2[VariableRootType2["ImportModule"] = 3] = "ImportModule";
93
+ VariableRootType2[VariableRootType2["FunctionCall"] = 4] = "FunctionCall";
94
+ VariableRootType2[VariableRootType2["Global"] = 5] = "Global";
95
+ VariableRootType2[VariableRootType2["Other"] = 6] = "Other";
96
+ return VariableRootType2;
97
+ })(VariableRootType || {});
98
+ function mkParameterVariableRoot(param, paramIndex) {
99
+ return { kind: 0, param, paramIndex };
100
+ }
101
+ function mkFunctionVariableRoot(func) {
102
+ return { kind: 1, func };
103
+ }
104
+ function mkLiteralVariableRoot(literal) {
105
+ return { kind: 2, literal };
106
+ }
107
+ function mkImportModuleVariableRoot(module, importType) {
108
+ return { kind: 3, module, importType };
109
+ }
110
+ function mkFunctionCallVariableRoot(node) {
111
+ return { kind: 4, node };
112
+ }
113
+ function mkGlobalVariableRoot(name) {
114
+ return { kind: 5, name };
115
+ }
116
+ function mkOtherVariableRoot(node) {
117
+ return { kind: 6, node };
118
+ }
119
+ function isImportModuleVariableRoot(vr) {
120
+ return vr.kind === 3;
121
+ }
122
+ function mkVariable(members) {
123
+ return Object.fromEntries(Object.entries(members).filter(([, value]) => value !== void 0));
124
+ }
125
+ const UNKNOWN_VARIABLE = {};
126
+ const getAccessedByProperty = (binding, accessedFrom, propertyName) => {
127
+ return accessedFrom ? propertyName ? isIdentifier$9(propertyName) ? propertyName.text : void 0 : binding.text : void 0;
128
+ };
129
+ function findDeclaringStatement(node) {
130
+ if (!node)
131
+ return void 0;
132
+ else if (isStatement$3(node))
133
+ return node;
134
+ else
135
+ return findDeclaringStatement(node.parent);
136
+ }
137
+ function tsBindingNameToVariable(binding, accessedFrom, assignedFrom, propertyName, root) {
138
+ if (isIdentifier$9(binding)) {
139
+ return [
140
+ mkVariable({
141
+ name: binding.text,
142
+ accessedFrom,
143
+ accessedByProperty: getAccessedByProperty(binding, accessedFrom, propertyName),
144
+ assignedFrom,
145
+ root,
146
+ definingStatement: findDeclaringStatement(binding)
147
+ })
148
+ ];
149
+ } else if (isObjectBindingPattern(binding)) {
150
+ let variable = mkVariable({
151
+ accessedFrom,
152
+ accessedByProperty: propertyName ? isIdentifier$9(propertyName) ? propertyName.text : void 0 : void 0,
153
+ assignedFrom,
154
+ root,
155
+ definingStatement: findDeclaringStatement(binding)
156
+ });
157
+ return binding.elements.flatMap((element) => {
158
+ return tsBindingNameToVariable(element.name, variable, void 0, element.propertyName);
159
+ });
160
+ } else if (isArrayBindingPattern(binding)) {
161
+ let variable = mkVariable({
162
+ accessedFrom,
163
+ accessedByProperty: propertyName ? isIdentifier$9(propertyName) ? propertyName.text : void 0 : void 0,
164
+ assignedFrom,
165
+ root,
166
+ definingStatement: findDeclaringStatement(binding)
167
+ });
168
+ return binding.elements.flatMap((element, index) => {
169
+ return isBindingElement(element) && tsBindingNameToVariable(element.name, variable, void 0, {
170
+ kind: 80,
171
+ text: "" + index
172
+ });
173
+ }).filter((variable2) => !!variable2);
174
+ }
175
+ }
176
+ class NameBindingResolver {
177
+ constructor(parentNameResolver) {
178
+ __publicField2(this, "variables", /* @__PURE__ */ new Map());
179
+ this.parentNameResolver = parentNameResolver;
180
+ }
181
+ addVariable(name, variable) {
182
+ this.variables.set(name, variable);
183
+ }
184
+ addFunctionParams(functionDeclaration) {
185
+ functionDeclaration.parameters.map((param, paramIndex) => {
186
+ let paramVariables = tsBindingNameToVariable(
187
+ param.name,
188
+ void 0,
189
+ void 0,
190
+ void 0,
191
+ mkParameterVariableRoot(param, paramIndex)
192
+ );
193
+ paramVariables.forEach((variable) => {
194
+ if (variable.name)
195
+ this.variables.set(variable.name, variable);
196
+ });
197
+ });
198
+ }
199
+ addFunctionDeclaration(statement) {
200
+ if (statement.name) {
201
+ let functionVariable = mkVariable({
202
+ name: statement.name.text,
203
+ root: mkFunctionVariableRoot(statement),
204
+ definingStatement: statement
205
+ });
206
+ this.variables.set(statement.name.text, functionVariable);
207
+ }
208
+ }
209
+ addVariableDeclarationList(declarationList) {
210
+ const letOrConst = declarationList.flags === NodeFlags.Const ? 1 : 0;
211
+ declarationList.declarations.forEach((declaration) => {
212
+ let rightSide = declaration.initializer ? this.resolvePropertyAccessChain(declaration.initializer) : void 0;
213
+ let declaredVariable = tsBindingNameToVariable(
214
+ declaration.name,
215
+ void 0,
216
+ rightSide,
217
+ void 0
218
+ );
219
+ declaredVariable.forEach(
220
+ (variable) => this.variables.set(variable.name, { ...variable, letOrConst })
221
+ );
222
+ });
223
+ }
224
+ addVariableStatement(variableStatement) {
225
+ this.addVariableDeclarationList(variableStatement.declarationList);
226
+ }
227
+ getVariable(name) {
228
+ return this.variables.get(name) || UNKNOWN_VARIABLE;
229
+ }
230
+ resolvePropertyAccessChain(expression) {
231
+ if (isPropertyAccessExpression$5(expression)) {
232
+ const name = expression.name.text;
233
+ const identifiersFromObject = this.resolvePropertyAccessChain(expression.expression);
234
+ return { accessedFrom: identifiersFromObject, accessedByProperty: name };
235
+ } else if (isElementAccessExpression$1(expression) && isStringLiteral$8(expression.argumentExpression)) {
236
+ const name = expression.argumentExpression.text;
237
+ const identifiersFromObject = this.resolvePropertyAccessChain(expression.expression);
238
+ return { accessedFrom: identifiersFromObject, accessedByProperty: name };
239
+ } else if (isIdentifier$9(expression)) {
240
+ return this.resolveIdentifier(expression);
241
+ } else if (isParenthesizedExpression(expression)) {
242
+ return this.resolvePropertyAccessChain(expression.expression);
243
+ } else if (isAsExpression(expression)) {
244
+ return this.resolvePropertyAccessChain(expression.expression);
245
+ } else if (isObjectLiteralExpression(expression)) {
246
+ return {
247
+ properties: expression.properties.map((property) => {
248
+ if (isPropertyAssignment$1(property) && (isStringLiteral$8(property.name) || isIdentifier$9(property.name))) {
249
+ if (isIdentifier$9(property.initializer))
250
+ return {
251
+ name: property.name.text,
252
+ assignedFrom: this.resolveIdentifier(property.initializer)
253
+ };
254
+ else if (isObjectLiteralExpression(property.initializer)) {
255
+ let nestedProperty = this.resolvePropertyAccessChain(
256
+ property.initializer
257
+ );
258
+ nestedProperty.name = property.name.text;
259
+ return nestedProperty;
260
+ } else if (isArrowFunction$2(property.initializer) || isFunctionExpression(property.initializer)) {
261
+ return {
262
+ name: property.name.text,
263
+ root: mkFunctionVariableRoot(property.initializer)
264
+ };
265
+ } else
266
+ return {
267
+ name: property.name.text,
268
+ root: mkLiteralVariableRoot(property.initializer)
269
+ };
270
+ } else if (isShorthandPropertyAssignment(property))
271
+ return {
272
+ name: property.name.text,
273
+ assignedFrom: this.resolveIdentifier(property.name)
274
+ };
275
+ })
276
+ };
277
+ } else if (isArrowFunction$2(expression) || isFunctionExpression(expression)) {
278
+ return { root: mkFunctionVariableRoot(expression) };
279
+ } else if (isCallExpression$6(expression)) {
280
+ return { root: mkFunctionCallVariableRoot(expression) };
281
+ } else if (isStringLiteral$8(expression) || isNumericLiteral(expression) || isToken(expression) && expression.kind === SyntaxKind$5.TrueKeyword || isToken(expression) && expression.kind === SyntaxKind$5.FalseKeyword) {
282
+ return { root: mkLiteralVariableRoot(expression) };
283
+ } else {
284
+ return { root: mkOtherVariableRoot(expression) };
285
+ }
286
+ }
287
+ resolvePropertyAccess(expression) {
288
+ return this.resolvePropertyAccessChain(expression);
289
+ }
290
+ resolveIdentifier(expression) {
291
+ let variableName = expression.text;
292
+ let nameResolver = this;
293
+ let resolved;
294
+ while ((resolved = nameResolver.getVariable(variableName)) === UNKNOWN_VARIABLE && nameResolver.parentNameResolver)
295
+ nameResolver = nameResolver.parentNameResolver;
296
+ if (resolved === UNKNOWN_VARIABLE)
297
+ return { root: mkGlobalVariableRoot(variableName) };
298
+ return resolved;
299
+ }
300
+ addImportDeclaration(node) {
301
+ if (node.importClause?.name) {
302
+ let root = mkImportModuleVariableRoot(
303
+ node.moduleSpecifier,
304
+ 0
305
+ /* defaultImport */
306
+ );
307
+ let variable = mkVariable({
308
+ name: node.importClause.name.text,
309
+ definingStatement: node,
310
+ root
311
+ });
312
+ this.variables.set(node.importClause.name.text, variable);
313
+ }
314
+ if (node.importClause?.namedBindings) {
315
+ let root = mkImportModuleVariableRoot(
316
+ node.moduleSpecifier,
317
+ 1
318
+ /* namedImport */
319
+ );
320
+ let namedBindings = node.importClause.namedBindings;
321
+ if (isNamespaceImport(namedBindings)) {
322
+ let variable = mkVariable({
323
+ name: namedBindings.name.text,
324
+ definingStatement: node,
325
+ root
326
+ });
327
+ this.variables.set(namedBindings.name.text, variable);
328
+ } else if (isNamedImports$1(namedBindings)) {
329
+ namedBindings.elements.forEach((importSpecifier) => {
330
+ if (!importSpecifier.isTypeOnly) {
331
+ let variable = mkVariable({
332
+ name: importSpecifier.name.text,
333
+ accessedByProperty: (importSpecifier.propertyName || importSpecifier.name).text,
334
+ accessedFrom: {
335
+ definingStatement: node,
336
+ root
337
+ },
338
+ definingStatement: node
339
+ });
340
+ this.variables.set(importSpecifier.name.text, variable);
341
+ }
342
+ });
343
+ }
344
+ }
345
+ }
346
+ }
347
+ function flattenVariable(variable, path2 = []) {
348
+ if (variable.assignedFrom)
349
+ return flattenVariable(variable.assignedFrom, path2);
350
+ else if (variable.accessedFrom) {
351
+ return flattenVariable(variable.accessedFrom, [variable.accessedByProperty, ...path2]);
352
+ } else if (variable.properties && !!variable.properties.find((_) => _.name === path2[0])) {
353
+ return flattenVariable(
354
+ variable.properties.find((_) => _.name === path2[0]),
355
+ path2.slice(1)
356
+ );
357
+ } else
358
+ return { path: path2, root: variable.root };
359
+ }
360
+ function byAnd() {
361
+ return (agg, value) => agg && value;
362
+ }
363
+ const {
364
+ isIdentifier: isIdentifier$6,
365
+ SyntaxKind: SyntaxKind$4,
366
+ isStringLiteral: isStringLiteral$7,
367
+ visitNode: visitNode$4,
368
+ isFunctionDeclaration: isFunctionDeclaration$1$1,
369
+ isVariableStatement: isVariableStatement$2,
370
+ isImportDeclaration: isImportDeclaration$3,
371
+ isBlock: isBlock$2,
372
+ isForStatement: isForStatement$1,
373
+ isVariableDeclarationList,
374
+ isForInStatement: isForInStatement$1,
375
+ isForOfStatement: isForOfStatement$1,
376
+ isTypeReferenceNode: isTypeReferenceNode$1,
377
+ isArrayTypeNode,
378
+ isFunctionTypeNode,
379
+ isUnionTypeNode
380
+ } = c;
381
+ const BUILT_IN_TYPES = ["RegExp", "Date"];
382
+ function builtInType(text) {
383
+ return BUILT_IN_TYPES.findIndex((_) => _ === text) > -1;
384
+ }
385
+ class BuiltInResolvedType {
386
+ constructor(name) {
387
+ this.name = name;
388
+ }
389
+ canBeAssignedFrom(rightSide) {
390
+ return rightSide instanceof BuiltInResolvedType && this.name === rightSide.name;
391
+ }
392
+ }
393
+ class ImportFromModuleResolvedType {
394
+ constructor(module, path2) {
395
+ this.module = module;
396
+ this.path = path2;
397
+ }
398
+ canBeAssignedFrom(rightSide) {
399
+ if (rightSide instanceof ImportFromModuleResolvedType) {
400
+ let pathEqual = this.path.length === rightSide.path.length;
401
+ if (pathEqual) {
402
+ pathEqual = this.path.map((value, index) => value === rightSide.path[index]).reduce(byAnd(), true);
403
+ }
404
+ return pathEqual && this.module === rightSide.module;
405
+ }
406
+ return false;
407
+ }
408
+ }
409
+ class ArrayResolvedType {
410
+ constructor(itemType) {
411
+ this.itemType = itemType;
412
+ }
413
+ canBeAssignedFrom(rightSide) {
414
+ return rightSide instanceof ArrayResolvedType && this.itemType === rightSide.itemType;
415
+ }
416
+ }
417
+ class FunctionResolvedType {
418
+ constructor(params, returns) {
419
+ this.params = params;
420
+ this.returns = returns;
421
+ }
422
+ canBeAssignedFrom(rightSide) {
423
+ return rightSide instanceof FunctionResolvedType && this.returns.canBeAssignedFrom(rightSide.returns);
424
+ }
425
+ }
426
+ class UnionResolvedType {
427
+ constructor(types) {
428
+ this.types = types;
429
+ }
430
+ canBeAssignedFrom(rightSide) {
431
+ if (rightSide instanceof UnionResolvedType) {
432
+ for (const item1 of this.types)
433
+ for (const item2 of rightSide.types)
434
+ if (item1.canBeAssignedFrom(item2))
435
+ return true;
436
+ } else
437
+ for (const item1 of this.types)
438
+ if (item1.canBeAssignedFrom(rightSide))
439
+ return true;
440
+ return false;
441
+ }
442
+ }
443
+ class GlobalResolvedType {
444
+ constructor(name) {
445
+ this.name = name;
446
+ }
447
+ canBeAssignedFrom(rightSide) {
448
+ return rightSide instanceof GlobalResolvedType && this.name === rightSide.name;
449
+ }
450
+ }
451
+ class SourceFileBindingResolver {
452
+ constructor(sourceFile) {
453
+ __publicField2(this, "nameBindingResolvers", /* @__PURE__ */ new Map());
454
+ this.nameBindingResolvers.set(sourceFile, new NameBindingResolver());
455
+ const nbResolversQueue = [
456
+ this.nameBindingResolvers.get(sourceFile)
457
+ ];
458
+ const doWithChildBindingResolver = (node, callback) => {
459
+ nbResolversQueue.unshift(new NameBindingResolver(nbResolversQueue[0]));
460
+ this.nameBindingResolvers.set(node, nbResolversQueue[0]);
461
+ callback();
462
+ node.getChildren().forEach((child) => visitNode$4(child, visitor));
463
+ nbResolversQueue.shift();
464
+ return node;
465
+ };
466
+ const visitor = (node) => {
467
+ if (isFunctionDeclaration$1$1(node))
468
+ nbResolversQueue[0].addFunctionDeclaration(node);
469
+ if (isVariableStatement$2(node))
470
+ nbResolversQueue[0].addVariableStatement(node);
471
+ else if (isImportDeclaration$3(node))
472
+ nbResolversQueue[0].addImportDeclaration(node);
473
+ else if (isBlock$2(node))
474
+ return doWithChildBindingResolver(node, () => {
475
+ });
476
+ else if (isFunctionLikeDeclarationBase(node)) {
477
+ return doWithChildBindingResolver(
478
+ node,
479
+ () => nbResolversQueue[0].addFunctionParams(node)
480
+ );
481
+ } else if (isForStatement$1(node) && isVariableDeclarationList(node.initializer)) {
482
+ return doWithChildBindingResolver(
483
+ node,
484
+ () => nbResolversQueue[0].addVariableDeclarationList(
485
+ node.initializer
486
+ )
487
+ );
488
+ } else if ((isForInStatement$1(node) || isForOfStatement$1(node)) && isVariableDeclarationList(node.initializer) && node.initializer.declarations.length === 1 && isIdentifier$6(node.initializer.declarations[0].name)) {
489
+ return doWithChildBindingResolver(node, () => {
490
+ let name = node.initializer.declarations[0].name.text;
491
+ nbResolversQueue[0].addVariable(
492
+ name,
493
+ mkVariable({
494
+ name,
495
+ root: mkOtherVariableRoot(node),
496
+ definingStatement: node
497
+ })
498
+ );
499
+ });
500
+ }
501
+ node.getChildren().forEach((child) => visitNode$4(child, visitor));
502
+ return node;
503
+ };
504
+ visitNode$4(sourceFile, visitor);
505
+ }
506
+ findBindingResolver(node) {
507
+ let found;
508
+ while (!(found = this.nameBindingResolvers.get(node)) && node.parent)
509
+ node = node.parent;
510
+ return found;
511
+ }
512
+ explain(identifier) {
513
+ return this.findBindingResolver(identifier).resolvePropertyAccessChain(identifier);
514
+ }
515
+ explainFlattenedVariableType(flattened) {
516
+ if (!!flattened.root) {
517
+ if (isImportModuleVariableRoot(flattened.root) && isStringLiteral$7(flattened.root.module)) {
518
+ return new ImportFromModuleResolvedType(flattened.root.module.text, flattened.path);
519
+ }
520
+ } else
521
+ return void 0;
522
+ }
523
+ explainType(type) {
524
+ if (type) {
525
+ if (isTypeReferenceNode$1(type)) {
526
+ let typeName = type.typeName;
527
+ if (isIdentifier$6(typeName)) {
528
+ let resolved = this.findBindingResolver(typeName).resolveIdentifier(typeName);
529
+ let flattened = flattenVariable(resolved);
530
+ let typeFromFlattened = this.explainFlattenedVariableType(flattened);
531
+ if (typeFromFlattened)
532
+ return typeFromFlattened;
533
+ if (builtInType(typeName.text))
534
+ return new BuiltInResolvedType(typeName.text);
535
+ }
536
+ } else if (type.kind === SyntaxKind$4.StringKeyword)
537
+ return new BuiltInResolvedType("string");
538
+ else if (type.kind === SyntaxKind$4.NumberKeyword)
539
+ return new BuiltInResolvedType("number");
540
+ else if (type.kind === SyntaxKind$4.BooleanKeyword)
541
+ return new BuiltInResolvedType("boolean");
542
+ else if (type.kind === SyntaxKind$4.AnyKeyword)
543
+ return new BuiltInResolvedType("any");
544
+ else if (type.kind === SyntaxKind$4.VoidKeyword)
545
+ return new BuiltInResolvedType("void");
546
+ else if (isArrayTypeNode(type))
547
+ return new ArrayResolvedType(this.explainType(type.elementType));
548
+ else if (isFunctionTypeNode(type)) {
549
+ const params = type.parameters.map((param) => this.explainType(param.type));
550
+ const ret = this.explainType(type.type);
551
+ return new FunctionResolvedType(params, ret);
552
+ } else if (isUnionTypeNode(type))
553
+ return new UnionResolvedType(type.types.map((aType) => this.explainType(aType)));
554
+ }
555
+ return new BuiltInResolvedType("void");
556
+ }
557
+ globalType(globalVariableRoot) {
558
+ return new GlobalResolvedType(globalVariableRoot.name);
559
+ }
560
+ }
561
+ const { isStringLiteral: isStringLiteral$1 } = c;
562
+ function areFlattenedAccessChainsEqual(chain1, chain2) {
563
+ if (chain1.path.length !== chain2.path.length) {
564
+ return false;
565
+ }
566
+ const pathsEqual = chain1.path.every((value, index) => value === chain2.path[index]);
567
+ if (!pathsEqual) {
568
+ return false;
569
+ }
570
+ return areVariableRootsEqual(chain1.root, chain2.root);
571
+ }
572
+ function areVariableRootsEqual(root1, root2) {
573
+ if (!root1 && !root2) {
574
+ return true;
575
+ }
576
+ if (!root1 || !root2) {
577
+ return false;
578
+ }
579
+ if (root1.kind !== root2.kind) {
580
+ return false;
581
+ }
582
+ switch (root1.kind) {
583
+ case VariableRootType.ImportModule:
584
+ if (isImportModuleVariableRoot(root1) && isImportModuleVariableRoot(root2)) {
585
+ const module1 = isStringLiteral$1(root1.module) ? root1.module.text : String(root1.module);
586
+ const module2 = isStringLiteral$1(root2.module) ? root2.module.text : String(root2.module);
587
+ return module1 === module2 && root1.importType === root2.importType;
588
+ }
589
+ return false;
590
+ case VariableRootType.FunctionParameter:
591
+ case VariableRootType.FunctionDefinition:
592
+ case VariableRootType.Literal:
593
+ case VariableRootType.FunctionCall:
594
+ return true;
595
+ case VariableRootType.Global:
596
+ case VariableRootType.Other:
597
+ return true;
598
+ default:
599
+ return false;
600
+ }
601
+ }
602
+ const SERVER_METHODS = /* @__PURE__ */ new Set([
603
+ "withServices",
604
+ "withLoadParams",
605
+ "withSlowlyRender",
606
+ "withFastRender"
607
+ ]);
608
+ const CLIENT_METHODS = /* @__PURE__ */ new Set([
609
+ "withInteractive",
610
+ "withContexts"
611
+ ]);
612
+ function shouldRemoveMethod(methodName, environment) {
613
+ return environment === "client" && SERVER_METHODS.has(methodName) || environment === "server" && CLIENT_METHODS.has(methodName);
614
+ }
615
+ const { isCallExpression: isCallExpression$1, isPropertyAccessExpression: isPropertyAccessExpression$1, isIdentifier: isIdentifier$2, isStringLiteral } = c$1;
616
+ function findBuilderMethodsToRemove(sourceFile, bindingResolver, environment) {
617
+ const callsToRemove = [];
618
+ const removedVariables = /* @__PURE__ */ new Set();
619
+ const visit = (node) => {
620
+ if (isCallExpression$1(node) && isPropertyAccessExpression$1(node.expression) && isPartOfJayStackChain(node, bindingResolver)) {
621
+ const methodName = node.expression.name.text;
622
+ if (shouldRemoveMethod(methodName, environment)) {
623
+ const variable = bindingResolver.explain(node.expression);
624
+ const flattened = flattenVariable(variable);
625
+ callsToRemove.push(flattened);
626
+ collectVariablesFromArguments(node.arguments, bindingResolver, removedVariables);
627
+ }
628
+ }
629
+ node.forEachChild(visit);
630
+ };
631
+ sourceFile.forEachChild(visit);
632
+ return { callsToRemove, removedVariables };
633
+ }
634
+ function isPartOfJayStackChain(callExpr, bindingResolver) {
635
+ let current = callExpr.expression;
636
+ while (true) {
637
+ if (isPropertyAccessExpression$1(current)) {
638
+ current = current.expression;
639
+ } else if (isCallExpression$1(current)) {
640
+ if (isIdentifier$2(current.expression)) {
641
+ const variable = bindingResolver.explain(current.expression);
642
+ const flattened = flattenVariable(variable);
643
+ if (flattened.path.length === 1 && flattened.path[0] === "makeJayStackComponent" && isImportModuleVariableRoot(flattened.root) && isStringLiteral(flattened.root.module) && flattened.root.module.text === "@jay-framework/fullstack-component")
644
+ return true;
645
+ }
646
+ if (isPropertyAccessExpression$1(current.expression)) {
647
+ current = current.expression.expression;
648
+ continue;
649
+ } else {
650
+ break;
651
+ }
652
+ } else {
653
+ break;
654
+ }
655
+ }
656
+ return false;
657
+ }
658
+ function collectVariablesFromArguments(args, bindingResolver, variables) {
659
+ const visitor = (node) => {
660
+ if (isIdentifier$2(node)) {
661
+ const variable = bindingResolver.explain(node);
662
+ if (variable && (variable.name || variable.root)) {
663
+ variables.add(variable);
664
+ }
665
+ }
666
+ node.forEachChild(visitor);
667
+ };
668
+ args.forEach((arg) => visitor(arg));
669
+ }
670
+ const {
671
+ isIdentifier: isIdentifier$1,
672
+ isImportDeclaration: isImportDeclaration$1,
673
+ isFunctionDeclaration: isFunctionDeclaration$1,
674
+ isVariableStatement: isVariableStatement$1,
675
+ isInterfaceDeclaration: isInterfaceDeclaration$1,
676
+ isTypeAliasDeclaration: isTypeAliasDeclaration$1,
677
+ isClassDeclaration,
678
+ isEnumDeclaration,
679
+ SyntaxKind
680
+ } = c$1;
681
+ function analyzeUnusedStatements(sourceFile, bindingResolver) {
682
+ const statementsToRemove = /* @__PURE__ */ new Set();
683
+ const collectUsedIdentifiers = () => {
684
+ const used = /* @__PURE__ */ new Set();
685
+ for (const statement of sourceFile.statements) {
686
+ if (isImportDeclaration$1(statement) || statementsToRemove.has(statement)) {
687
+ continue;
688
+ }
689
+ const definedName = getStatementDefinedName(statement);
690
+ const visitor = (node, parent) => {
691
+ if (isIdentifier$1(node)) {
692
+ if (node.text !== definedName) {
693
+ used.add(node.text);
694
+ }
695
+ }
696
+ node.forEachChild((child) => visitor(child));
697
+ };
698
+ statement.forEachChild((child) => visitor(child));
699
+ }
700
+ return used;
701
+ };
702
+ let changed = true;
703
+ while (changed) {
704
+ changed = false;
705
+ const stillUsedIdentifiers = collectUsedIdentifiers();
706
+ for (const statement of sourceFile.statements) {
707
+ if (statementsToRemove.has(statement) || isImportDeclaration$1(statement) || isExportStatement(statement)) {
708
+ continue;
709
+ }
710
+ const definedName = getStatementDefinedName(statement);
711
+ if (definedName && !stillUsedIdentifiers.has(definedName)) {
712
+ statementsToRemove.add(statement);
713
+ changed = true;
714
+ }
715
+ }
716
+ }
717
+ const finalUsedIdentifiers = collectUsedIdentifiers();
718
+ const unusedImports = /* @__PURE__ */ new Set();
719
+ for (const statement of sourceFile.statements) {
720
+ if (isImportDeclaration$1(statement) && statement.importClause?.namedBindings) {
721
+ const namedBindings = statement.importClause.namedBindings;
722
+ if ("elements" in namedBindings) {
723
+ for (const element of namedBindings.elements) {
724
+ const importName = element.name.text;
725
+ if (!finalUsedIdentifiers.has(importName)) {
726
+ unusedImports.add(importName);
727
+ }
728
+ }
729
+ }
730
+ }
731
+ }
732
+ return { statementsToRemove, unusedImports };
733
+ }
734
+ function isExportStatement(statement) {
735
+ const modifiers = "modifiers" in statement ? statement.modifiers : void 0;
736
+ if (modifiers) {
737
+ return modifiers.some((mod) => mod.kind === SyntaxKind.ExportKeyword);
738
+ }
739
+ return false;
740
+ }
741
+ function getStatementDefinedName(statement) {
742
+ if (isFunctionDeclaration$1(statement) && statement.name) {
743
+ return statement.name.text;
744
+ }
745
+ if (isVariableStatement$1(statement)) {
746
+ const firstDecl = statement.declarationList.declarations[0];
747
+ if (firstDecl && isIdentifier$1(firstDecl.name)) {
748
+ return firstDecl.name.text;
749
+ }
750
+ }
751
+ if (isInterfaceDeclaration$1(statement) && statement.name) {
752
+ return statement.name.text;
753
+ }
754
+ if (isTypeAliasDeclaration$1(statement) && statement.name) {
755
+ return statement.name.text;
756
+ }
757
+ if (isClassDeclaration(statement) && statement.name) {
758
+ return statement.name.text;
759
+ }
760
+ if (isEnumDeclaration(statement) && statement.name) {
761
+ return statement.name.text;
762
+ }
763
+ return void 0;
764
+ }
765
+ const {
766
+ createPrinter,
767
+ createSourceFile,
768
+ ScriptTarget,
769
+ visitEachChild,
770
+ isCallExpression,
771
+ isPropertyAccessExpression,
772
+ isImportDeclaration,
773
+ isNamedImports,
774
+ isIdentifier,
775
+ isFunctionDeclaration,
776
+ isVariableStatement,
777
+ isInterfaceDeclaration,
778
+ isTypeAliasDeclaration
779
+ } = c$1;
780
+ function transformJayStackBuilder(code, filePath, environment) {
781
+ const sourceFile = createSourceFile(
782
+ filePath,
783
+ code,
784
+ ScriptTarget.Latest,
785
+ true
786
+ );
787
+ const transformers = [mkTransformer(mkJayStackCodeSplitTransformer, { environment })];
788
+ const printer = createPrinter();
789
+ const result = c$1.transform(sourceFile, transformers);
790
+ const transformedFile = result.transformed[0];
791
+ const transformedCode = printer.printFile(transformedFile);
792
+ result.dispose();
793
+ return {
794
+ code: transformedCode
795
+ };
796
+ }
797
+ function isCallToRemove(flattened, callsToRemove) {
798
+ return callsToRemove.some((call) => areFlattenedAccessChainsEqual(flattened, call));
799
+ }
800
+ function mkJayStackCodeSplitTransformer({
801
+ factory,
802
+ sourceFile,
803
+ context,
804
+ environment
805
+ }) {
806
+ const bindingResolver = new SourceFileBindingResolver(sourceFile);
807
+ const { callsToRemove, removedVariables } = findBuilderMethodsToRemove(
808
+ sourceFile,
809
+ bindingResolver,
810
+ environment
811
+ );
812
+ const transformVisitor = (node) => {
813
+ if (isCallExpression(node) && isPropertyAccessExpression(node.expression)) {
814
+ const variable = bindingResolver.explain(node.expression);
815
+ const flattened = flattenVariable(variable);
816
+ if (isCallToRemove(flattened, callsToRemove)) {
817
+ const receiver = node.expression.expression;
818
+ return transformVisitor(receiver);
819
+ }
820
+ }
821
+ return visitEachChild(node, transformVisitor, context);
822
+ };
823
+ let transformedSourceFile = visitEachChild(sourceFile, transformVisitor, context);
824
+ new SourceFileBindingResolver(transformedSourceFile);
825
+ const { statementsToRemove, unusedImports } = analyzeUnusedStatements(
826
+ transformedSourceFile
827
+ );
828
+ const transformedStatements = transformedSourceFile.statements.map((statement) => {
829
+ if (statementsToRemove.has(statement)) {
830
+ return void 0;
831
+ }
832
+ if (isImportDeclaration(statement)) {
833
+ return filterImportDeclaration(statement, unusedImports, factory);
834
+ }
835
+ return statement;
836
+ }).filter((s2) => s2 !== void 0);
837
+ return factory.updateSourceFile(transformedSourceFile, transformedStatements);
838
+ }
839
+ function filterImportDeclaration(statement, unusedImports, factory) {
840
+ const importClause = statement.importClause;
841
+ if (!importClause?.namedBindings || !isNamedImports(importClause.namedBindings)) {
842
+ return statement;
843
+ }
844
+ const usedElements = importClause.namedBindings.elements.filter(
845
+ (element) => !unusedImports.has(element.name.text)
846
+ );
847
+ if (usedElements.length === 0) {
848
+ return void 0;
849
+ }
850
+ return factory.updateImportDeclaration(
851
+ statement,
852
+ statement.modifiers,
853
+ factory.updateImportClause(
854
+ importClause,
855
+ importClause.isTypeOnly,
856
+ importClause.name,
857
+ factory.updateNamedImports(
858
+ importClause.namedBindings,
859
+ usedElements
860
+ )
861
+ ),
862
+ statement.moduleSpecifier,
863
+ statement.assertClause
864
+ );
865
+ }
866
+ function jayStackCompiler(jayOptions = {}) {
867
+ return [
868
+ // First: Jay Stack code splitting transformation
869
+ {
870
+ name: "jay-stack:code-split",
871
+ enforce: "pre",
872
+ // Run before jay:runtime
873
+ transform(code, id) {
874
+ const isClientBuild = id.includes("?jay-client");
875
+ const isServerBuild = id.includes("?jay-server");
876
+ if (!isClientBuild && !isServerBuild) {
877
+ return null;
878
+ }
879
+ const environment = isClientBuild ? "client" : "server";
880
+ if (!id.endsWith(".ts") && !id.includes(".ts?")) {
881
+ return null;
882
+ }
883
+ try {
884
+ return transformJayStackBuilder(code, id, environment);
885
+ } catch (error) {
886
+ console.error(`[jay-stack:code-split] Error transforming ${id}:`, error);
887
+ return null;
888
+ }
889
+ }
890
+ },
891
+ // Second: Jay runtime compilation (existing plugin)
892
+ jayRuntime(jayOptions)
893
+ ];
894
+ }
15
895
  class ServiceLifecycleManager {
16
896
  constructor(projectRoot, sourceBase = "src") {
17
897
  __publicField(this, "initFilePath", null);
@@ -142,7 +1022,10 @@ async function initRoutes(pagesBaseFolder) {
142
1022
  function defaults(options) {
143
1023
  const publicBaseUrlPath = options.publicBaseUrlPath || process.env.BASE || "/";
144
1024
  const projectRootFolder = options.projectRootFolder || ".";
145
- const pagesRootFolder = path__default.resolve(projectRootFolder, options.pagesRootFolder || "./src/pages");
1025
+ const pagesRootFolder = path__default.resolve(
1026
+ projectRootFolder,
1027
+ options.pagesRootFolder || "./src/pages"
1028
+ );
146
1029
  const tsConfigFilePath = options.jayRollupConfig.tsConfigFilePath || path__default.resolve(projectRootFolder, "./tsconfig.json");
147
1030
  return {
148
1031
  publicBaseUrlPath,
@@ -217,21 +1100,27 @@ function mkRoute(route, vite, slowlyPhase, options) {
217
1100
  console.log(pageParts.validations.join("\n"));
218
1101
  res.status(500).end(pageParts.validations.join("\n"));
219
1102
  }
220
- } catch (e) {
221
- vite?.ssrFixStacktrace(e);
222
- console.log(e.stack);
223
- res.status(500).end(e.stack);
1103
+ } catch (e2) {
1104
+ vite?.ssrFixStacktrace(e2);
1105
+ console.log(e2.stack);
1106
+ res.status(500).end(e2.stack);
224
1107
  }
225
1108
  };
226
1109
  return { path: path2, handler, fsRoute: route };
227
1110
  }
228
1111
  async function mkDevServer(options) {
229
- const { publicBaseUrlPath, pagesRootFolder, projectRootFolder, jayRollupConfig, dontCacheSlowly } = defaults(options);
1112
+ const {
1113
+ publicBaseUrlPath,
1114
+ pagesRootFolder,
1115
+ projectRootFolder,
1116
+ jayRollupConfig,
1117
+ dontCacheSlowly
1118
+ } = defaults(options);
230
1119
  const lifecycleManager = new ServiceLifecycleManager(projectRootFolder);
231
1120
  setupGracefulShutdown(lifecycleManager);
232
1121
  const vite = await createServer({
233
1122
  server: { middlewareMode: true },
234
- plugins: [jayRuntime(jayRollupConfig)],
1123
+ plugins: [...jayStackCompiler(jayRollupConfig)],
235
1124
  appType: "custom",
236
1125
  base: publicBaseUrlPath,
237
1126
  root: pagesRootFolder,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jay-framework/dev-server",
3
- "version": "0.8.0",
3
+ "version": "0.9.0",
4
4
  "type": "module",
5
5
  "license": "Apache-2.0",
6
6
  "main": "dist/index.js",
@@ -22,20 +22,20 @@
22
22
  "test:watch": "vitest"
23
23
  },
24
24
  "dependencies": {
25
- "@jay-framework/compiler-shared": "^0.8.0",
26
- "@jay-framework/component": "^0.8.0",
27
- "@jay-framework/fullstack-component": "^0.8.0",
28
- "@jay-framework/runtime": "^0.8.0",
29
- "@jay-framework/stack-client-runtime": "^0.8.0",
30
- "@jay-framework/stack-route-scanner": "^0.8.0",
31
- "@jay-framework/stack-server-runtime": "^0.8.0",
32
- "@jay-framework/vite-plugin": "^0.8.0",
25
+ "@jay-framework/compiler-jay-stack": "^0.9.0",
26
+ "@jay-framework/compiler-shared": "^0.9.0",
27
+ "@jay-framework/component": "^0.9.0",
28
+ "@jay-framework/fullstack-component": "^0.9.0",
29
+ "@jay-framework/runtime": "^0.9.0",
30
+ "@jay-framework/stack-client-runtime": "^0.9.0",
31
+ "@jay-framework/stack-route-scanner": "^0.9.0",
32
+ "@jay-framework/stack-server-runtime": "^0.9.0",
33
33
  "vite": "^5.0.11"
34
34
  },
35
35
  "devDependencies": {
36
- "@jay-framework/dev-environment": "^0.8.0",
37
- "@jay-framework/jay-cli": "^0.8.0",
38
- "@jay-framework/stack-client-runtime": "^0.8.0",
36
+ "@jay-framework/dev-environment": "^0.9.0",
37
+ "@jay-framework/jay-cli": "^0.9.0",
38
+ "@jay-framework/stack-client-runtime": "^0.9.0",
39
39
  "@types/express": "^5.0.2",
40
40
  "@types/node": "^22.15.21",
41
41
  "nodemon": "^3.0.3",