@jay-framework/dev-server 0.7.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.
- package/dist/index.d.ts +50 -3
- package/dist/index.js +1082 -19
- package/package.json +12 -12
package/dist/index.d.ts
CHANGED
|
@@ -1,15 +1,61 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ViteDevServer, Connect } from 'vite';
|
|
2
2
|
import { JayRoute } from '@jay-framework/stack-route-scanner';
|
|
3
3
|
import { RequestHandler } from 'express-serve-static-core';
|
|
4
4
|
import { JayRollupConfig } from '@jay-framework/rollup-plugin';
|
|
5
5
|
|
|
6
6
|
interface DevServerOptions {
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
publicBaseUrlPath?: string;
|
|
8
|
+
projectRootFolder?: string;
|
|
9
|
+
pagesRootFolder?: string;
|
|
9
10
|
jayRollupConfig: JayRollupConfig;
|
|
10
11
|
dontCacheSlowly: boolean;
|
|
11
12
|
}
|
|
12
13
|
|
|
14
|
+
/**
|
|
15
|
+
* Service lifecycle management for the Jay Stack dev-server.
|
|
16
|
+
*
|
|
17
|
+
* Handles loading jay.init.ts, running init/shutdown callbacks,
|
|
18
|
+
* hot reloading services, and graceful shutdown.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
declare class ServiceLifecycleManager {
|
|
22
|
+
private projectRoot;
|
|
23
|
+
private sourceBase;
|
|
24
|
+
private initFilePath;
|
|
25
|
+
private isInitialized;
|
|
26
|
+
private viteServer;
|
|
27
|
+
constructor(projectRoot: string, sourceBase?: string);
|
|
28
|
+
/**
|
|
29
|
+
* Set the Vite server instance for SSR module loading
|
|
30
|
+
*/
|
|
31
|
+
setViteServer(viteServer: ViteDevServer): void;
|
|
32
|
+
/**
|
|
33
|
+
* Finds the jay.init.ts (or .js) file in the source directory.
|
|
34
|
+
* Looks in: {projectRoot}/{sourceBase}/jay.init.{ts,js,mts,mjs}
|
|
35
|
+
*/
|
|
36
|
+
private findInitFile;
|
|
37
|
+
/**
|
|
38
|
+
* Initializes services by loading and executing jay.init.ts
|
|
39
|
+
*/
|
|
40
|
+
initialize(): Promise<void>;
|
|
41
|
+
/**
|
|
42
|
+
* Shuts down services gracefully with timeout
|
|
43
|
+
*/
|
|
44
|
+
shutdown(timeoutMs?: number): Promise<void>;
|
|
45
|
+
/**
|
|
46
|
+
* Hot reload: shutdown, clear caches, re-import, and re-initialize
|
|
47
|
+
*/
|
|
48
|
+
reload(): Promise<void>;
|
|
49
|
+
/**
|
|
50
|
+
* Returns the path to the init file if found
|
|
51
|
+
*/
|
|
52
|
+
getInitFilePath(): string | null;
|
|
53
|
+
/**
|
|
54
|
+
* Checks if services are initialized
|
|
55
|
+
*/
|
|
56
|
+
isReady(): boolean;
|
|
57
|
+
}
|
|
58
|
+
|
|
13
59
|
interface DevServerRoute {
|
|
14
60
|
path: string;
|
|
15
61
|
handler: RequestHandler;
|
|
@@ -19,6 +65,7 @@ interface DevServer {
|
|
|
19
65
|
server: Connect.Server;
|
|
20
66
|
viteServer: ViteDevServer;
|
|
21
67
|
routes: DevServerRoute[];
|
|
68
|
+
lifecycleManager: ServiceLifecycleManager;
|
|
22
69
|
}
|
|
23
70
|
declare function mkDevServer(options: DevServerOptions): Promise<DevServer>;
|
|
24
71
|
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,1018 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
+
var __publicField = (obj, key, value) => {
|
|
4
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
5
|
+
return value;
|
|
6
|
+
};
|
|
1
7
|
import { createServer } from "vite";
|
|
2
8
|
import { scanRoutes, routeToExpressRoute } from "@jay-framework/stack-route-scanner";
|
|
3
|
-
import { DevSlowlyChangingPhase, loadPageParts, renderFastChangingData, generateClientScript } from "@jay-framework/stack-server-runtime";
|
|
9
|
+
import { runInitCallbacks, runShutdownCallbacks, clearLifecycleCallbacks, clearServiceRegistry, DevSlowlyChangingPhase, loadPageParts, renderFastChangingData, generateClientScript } from "@jay-framework/stack-server-runtime";
|
|
4
10
|
import { jayRuntime } from "@jay-framework/vite-plugin";
|
|
5
|
-
import
|
|
11
|
+
import { createRequire } from "module";
|
|
12
|
+
import "@jay-framework/compiler-shared";
|
|
13
|
+
import * as path from "node:path";
|
|
14
|
+
import path__default from "node:path";
|
|
15
|
+
import "@jay-framework/compiler-jay-html";
|
|
16
|
+
import * as fs from "node:fs";
|
|
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
|
+
}
|
|
895
|
+
class ServiceLifecycleManager {
|
|
896
|
+
constructor(projectRoot, sourceBase = "src") {
|
|
897
|
+
__publicField(this, "initFilePath", null);
|
|
898
|
+
__publicField(this, "isInitialized", false);
|
|
899
|
+
__publicField(this, "viteServer", null);
|
|
900
|
+
this.projectRoot = projectRoot;
|
|
901
|
+
this.sourceBase = sourceBase;
|
|
902
|
+
}
|
|
903
|
+
/**
|
|
904
|
+
* Set the Vite server instance for SSR module loading
|
|
905
|
+
*/
|
|
906
|
+
setViteServer(viteServer) {
|
|
907
|
+
this.viteServer = viteServer;
|
|
908
|
+
}
|
|
909
|
+
/**
|
|
910
|
+
* Finds the jay.init.ts (or .js) file in the source directory.
|
|
911
|
+
* Looks in: {projectRoot}/{sourceBase}/jay.init.{ts,js,mts,mjs}
|
|
912
|
+
*/
|
|
913
|
+
findInitFile() {
|
|
914
|
+
const extensions = [".ts", ".js", ".mts", ".mjs"];
|
|
915
|
+
const baseFilename = "jay.init";
|
|
916
|
+
for (const ext of extensions) {
|
|
917
|
+
const filePath = path.join(this.projectRoot, this.sourceBase, baseFilename + ext);
|
|
918
|
+
if (fs.existsSync(filePath)) {
|
|
919
|
+
return filePath;
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
return null;
|
|
923
|
+
}
|
|
924
|
+
/**
|
|
925
|
+
* Initializes services by loading and executing jay.init.ts
|
|
926
|
+
*/
|
|
927
|
+
async initialize() {
|
|
928
|
+
if (this.isInitialized) {
|
|
929
|
+
console.warn("[Services] Already initialized, skipping...");
|
|
930
|
+
return;
|
|
931
|
+
}
|
|
932
|
+
this.initFilePath = this.findInitFile();
|
|
933
|
+
if (!this.initFilePath) {
|
|
934
|
+
console.log("[Services] No jay.init.ts found in src/, skipping service initialization");
|
|
935
|
+
return;
|
|
936
|
+
}
|
|
937
|
+
console.log(`[Services] Loading initialization file: ${this.initFilePath}`);
|
|
938
|
+
try {
|
|
939
|
+
if (this.viteServer) {
|
|
940
|
+
await this.viteServer.ssrLoadModule(this.initFilePath);
|
|
941
|
+
} else {
|
|
942
|
+
const fileUrl = pathToFileURL(this.initFilePath).href;
|
|
943
|
+
await import(fileUrl);
|
|
944
|
+
}
|
|
945
|
+
await runInitCallbacks();
|
|
946
|
+
this.isInitialized = true;
|
|
947
|
+
console.log("[Services] Initialization complete");
|
|
948
|
+
} catch (error) {
|
|
949
|
+
console.error("[Services] Failed to initialize:", error);
|
|
950
|
+
throw error;
|
|
951
|
+
}
|
|
952
|
+
}
|
|
953
|
+
/**
|
|
954
|
+
* Shuts down services gracefully with timeout
|
|
955
|
+
*/
|
|
956
|
+
async shutdown(timeoutMs = 5e3) {
|
|
957
|
+
if (!this.isInitialized) {
|
|
958
|
+
return;
|
|
959
|
+
}
|
|
960
|
+
console.log("[Services] Shutting down...");
|
|
961
|
+
try {
|
|
962
|
+
await Promise.race([
|
|
963
|
+
runShutdownCallbacks(),
|
|
964
|
+
new Promise(
|
|
965
|
+
(_, reject) => setTimeout(() => reject(new Error("Shutdown timeout")), timeoutMs)
|
|
966
|
+
)
|
|
967
|
+
]);
|
|
968
|
+
console.log("[Services] Shutdown complete");
|
|
969
|
+
} catch (error) {
|
|
970
|
+
if (error.message === "Shutdown timeout") {
|
|
971
|
+
console.warn("[Services] Shutdown timed out after", timeoutMs, "ms");
|
|
972
|
+
} else {
|
|
973
|
+
console.error("[Services] Shutdown error:", error);
|
|
974
|
+
}
|
|
975
|
+
} finally {
|
|
976
|
+
this.isInitialized = false;
|
|
977
|
+
}
|
|
978
|
+
}
|
|
979
|
+
/**
|
|
980
|
+
* Hot reload: shutdown, clear caches, re-import, and re-initialize
|
|
981
|
+
*/
|
|
982
|
+
async reload() {
|
|
983
|
+
if (!this.initFilePath) {
|
|
984
|
+
console.log("[Services] No init file to reload");
|
|
985
|
+
return;
|
|
986
|
+
}
|
|
987
|
+
console.log("[Services] Reloading services...");
|
|
988
|
+
await this.shutdown();
|
|
989
|
+
clearLifecycleCallbacks();
|
|
990
|
+
clearServiceRegistry();
|
|
991
|
+
if (this.viteServer) {
|
|
992
|
+
const moduleNode = this.viteServer.moduleGraph.getModuleById(this.initFilePath);
|
|
993
|
+
if (moduleNode) {
|
|
994
|
+
await this.viteServer.moduleGraph.invalidateModule(moduleNode);
|
|
995
|
+
}
|
|
996
|
+
} else {
|
|
997
|
+
delete require.cache[require.resolve(this.initFilePath)];
|
|
998
|
+
}
|
|
999
|
+
this.isInitialized = false;
|
|
1000
|
+
await this.initialize();
|
|
1001
|
+
console.log("[Services] Reload complete");
|
|
1002
|
+
}
|
|
1003
|
+
/**
|
|
1004
|
+
* Returns the path to the init file if found
|
|
1005
|
+
*/
|
|
1006
|
+
getInitFilePath() {
|
|
1007
|
+
return this.initFilePath;
|
|
1008
|
+
}
|
|
1009
|
+
/**
|
|
1010
|
+
* Checks if services are initialized
|
|
1011
|
+
*/
|
|
1012
|
+
isReady() {
|
|
1013
|
+
return this.isInitialized;
|
|
1014
|
+
}
|
|
1015
|
+
}
|
|
6
1016
|
async function initRoutes(pagesBaseFolder) {
|
|
7
1017
|
return await scanRoutes(pagesBaseFolder, {
|
|
8
1018
|
jayHtmlFilename: "page.jay-html",
|
|
@@ -10,12 +1020,17 @@ async function initRoutes(pagesBaseFolder) {
|
|
|
10
1020
|
});
|
|
11
1021
|
}
|
|
12
1022
|
function defaults(options) {
|
|
13
|
-
const
|
|
14
|
-
const
|
|
15
|
-
const
|
|
1023
|
+
const publicBaseUrlPath = options.publicBaseUrlPath || process.env.BASE || "/";
|
|
1024
|
+
const projectRootFolder = options.projectRootFolder || ".";
|
|
1025
|
+
const pagesRootFolder = path__default.resolve(
|
|
1026
|
+
projectRootFolder,
|
|
1027
|
+
options.pagesRootFolder || "./src/pages"
|
|
1028
|
+
);
|
|
1029
|
+
const tsConfigFilePath = options.jayRollupConfig.tsConfigFilePath || path__default.resolve(projectRootFolder, "./tsconfig.json");
|
|
16
1030
|
return {
|
|
17
|
-
|
|
18
|
-
|
|
1031
|
+
publicBaseUrlPath,
|
|
1032
|
+
pagesRootFolder,
|
|
1033
|
+
projectRootFolder,
|
|
19
1034
|
dontCacheSlowly: options.dontCacheSlowly,
|
|
20
1035
|
jayRollupConfig: {
|
|
21
1036
|
...options.jayRollupConfig || {},
|
|
@@ -35,7 +1050,7 @@ function mkRoute(route, vite, slowlyPhase, options) {
|
|
|
35
1050
|
const path2 = routeToExpressRoute(route);
|
|
36
1051
|
const handler = async (req, res) => {
|
|
37
1052
|
try {
|
|
38
|
-
const url = req.originalUrl.replace(options.
|
|
1053
|
+
const url = req.originalUrl.replace(options.publicBaseUrlPath, "");
|
|
39
1054
|
const pageParams = req.params;
|
|
40
1055
|
const pageProps = {
|
|
41
1056
|
language: "en",
|
|
@@ -45,7 +1060,7 @@ function mkRoute(route, vite, slowlyPhase, options) {
|
|
|
45
1060
|
const pageParts = await loadPageParts(
|
|
46
1061
|
vite,
|
|
47
1062
|
route,
|
|
48
|
-
options.
|
|
1063
|
+
options.pagesRootFolder,
|
|
49
1064
|
options.jayRollupConfig
|
|
50
1065
|
);
|
|
51
1066
|
if (pageParts.val) {
|
|
@@ -85,24 +1100,40 @@ function mkRoute(route, vite, slowlyPhase, options) {
|
|
|
85
1100
|
console.log(pageParts.validations.join("\n"));
|
|
86
1101
|
res.status(500).end(pageParts.validations.join("\n"));
|
|
87
1102
|
}
|
|
88
|
-
} catch (
|
|
89
|
-
vite?.ssrFixStacktrace(
|
|
90
|
-
console.log(
|
|
91
|
-
res.status(500).end(
|
|
1103
|
+
} catch (e2) {
|
|
1104
|
+
vite?.ssrFixStacktrace(e2);
|
|
1105
|
+
console.log(e2.stack);
|
|
1106
|
+
res.status(500).end(e2.stack);
|
|
92
1107
|
}
|
|
93
1108
|
};
|
|
94
1109
|
return { path: path2, handler, fsRoute: route };
|
|
95
1110
|
}
|
|
96
1111
|
async function mkDevServer(options) {
|
|
97
|
-
const {
|
|
1112
|
+
const {
|
|
1113
|
+
publicBaseUrlPath,
|
|
1114
|
+
pagesRootFolder,
|
|
1115
|
+
projectRootFolder,
|
|
1116
|
+
jayRollupConfig,
|
|
1117
|
+
dontCacheSlowly
|
|
1118
|
+
} = defaults(options);
|
|
1119
|
+
const lifecycleManager = new ServiceLifecycleManager(projectRootFolder);
|
|
1120
|
+
setupGracefulShutdown(lifecycleManager);
|
|
98
1121
|
const vite = await createServer({
|
|
99
1122
|
server: { middlewareMode: true },
|
|
100
|
-
plugins: [
|
|
1123
|
+
plugins: [...jayStackCompiler(jayRollupConfig)],
|
|
101
1124
|
appType: "custom",
|
|
102
|
-
base:
|
|
103
|
-
root:
|
|
1125
|
+
base: publicBaseUrlPath,
|
|
1126
|
+
root: pagesRootFolder,
|
|
1127
|
+
ssr: {
|
|
1128
|
+
// Mark stack-server-runtime as external so Vite uses Node's require
|
|
1129
|
+
// This ensures jay.init.ts and dev-server share the same module instance
|
|
1130
|
+
external: ["@jay-framework/stack-server-runtime"]
|
|
1131
|
+
}
|
|
104
1132
|
});
|
|
105
|
-
|
|
1133
|
+
lifecycleManager.setViteServer(vite);
|
|
1134
|
+
await lifecycleManager.initialize();
|
|
1135
|
+
setupServiceHotReload(vite, lifecycleManager);
|
|
1136
|
+
const routes = await initRoutes(pagesRootFolder);
|
|
106
1137
|
const slowlyPhase = new DevSlowlyChangingPhase(dontCacheSlowly);
|
|
107
1138
|
const devServerRoutes = routes.map(
|
|
108
1139
|
(route) => mkRoute(route, vite, slowlyPhase, options)
|
|
@@ -110,8 +1141,40 @@ async function mkDevServer(options) {
|
|
|
110
1141
|
return {
|
|
111
1142
|
server: vite.middlewares,
|
|
112
1143
|
viteServer: vite,
|
|
113
|
-
routes: devServerRoutes
|
|
1144
|
+
routes: devServerRoutes,
|
|
1145
|
+
lifecycleManager
|
|
1146
|
+
};
|
|
1147
|
+
}
|
|
1148
|
+
function setupGracefulShutdown(lifecycleManager) {
|
|
1149
|
+
const shutdown = async (signal) => {
|
|
1150
|
+
console.log(`
|
|
1151
|
+
${signal} received, shutting down gracefully...`);
|
|
1152
|
+
await lifecycleManager.shutdown();
|
|
1153
|
+
process.exit(0);
|
|
114
1154
|
};
|
|
1155
|
+
process.on("SIGTERM", () => shutdown("SIGTERM"));
|
|
1156
|
+
process.on("SIGINT", () => shutdown("SIGINT"));
|
|
1157
|
+
}
|
|
1158
|
+
function setupServiceHotReload(vite, lifecycleManager) {
|
|
1159
|
+
const initFilePath = lifecycleManager.getInitFilePath();
|
|
1160
|
+
if (!initFilePath) {
|
|
1161
|
+
return;
|
|
1162
|
+
}
|
|
1163
|
+
vite.watcher.add(initFilePath);
|
|
1164
|
+
vite.watcher.on("change", async (changedPath) => {
|
|
1165
|
+
if (changedPath === initFilePath) {
|
|
1166
|
+
console.log("[Services] jay.init.ts changed, reloading services...");
|
|
1167
|
+
try {
|
|
1168
|
+
await lifecycleManager.reload();
|
|
1169
|
+
vite.ws.send({
|
|
1170
|
+
type: "full-reload",
|
|
1171
|
+
path: "*"
|
|
1172
|
+
});
|
|
1173
|
+
} catch (error) {
|
|
1174
|
+
console.error("[Services] Failed to reload services:", error);
|
|
1175
|
+
}
|
|
1176
|
+
}
|
|
1177
|
+
});
|
|
115
1178
|
}
|
|
116
1179
|
export {
|
|
117
1180
|
mkDevServer
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jay-framework/dev-server",
|
|
3
|
-
"version": "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-
|
|
26
|
-
"@jay-framework/
|
|
27
|
-
"@jay-framework/
|
|
28
|
-
"@jay-framework/
|
|
29
|
-
"@jay-framework/
|
|
30
|
-
"@jay-framework/stack-
|
|
31
|
-
"@jay-framework/stack-
|
|
32
|
-
"@jay-framework/
|
|
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.
|
|
37
|
-
"@jay-framework/jay-cli": "^0.
|
|
38
|
-
"@jay-framework/stack-client-runtime": "^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",
|