devflare 1.0.0-next.20 → 1.0.0-next.21
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/LLM.md +9 -4
- package/README.md +10 -2
- package/dist/browser.js +3 -3
- package/dist/build-b1z6wqet.js +54 -0
- package/dist/build-x7maz3eb.js +54 -0
- package/dist/cli/commands/config.d.ts.map +1 -1
- package/dist/cli/commands/dev.d.ts +1 -0
- package/dist/cli/commands/dev.d.ts.map +1 -1
- package/dist/cli/commands/doctor.d.ts.map +1 -1
- package/dist/cli/help-pages/pages/core.d.ts.map +1 -1
- package/dist/cli/index.js +1 -1
- package/dist/config-qj5jw8km.js +93 -0
- package/dist/deploy-jf3yczsz.js +1055 -0
- package/dist/deploy-xqm869nf.js +1055 -0
- package/dist/dev-bgpxrwms.js +2551 -0
- package/dist/dev-kzs65xcr.js +2551 -0
- package/dist/dev-server/dev-server-state.d.ts +2 -0
- package/dist/dev-server/dev-server-state.d.ts.map +1 -1
- package/dist/dev-server/miniflare-dev-config.d.ts +4 -0
- package/dist/dev-server/miniflare-dev-config.d.ts.map +1 -1
- package/dist/dev-server/server.d.ts.map +1 -1
- package/dist/dev-zgx7fhe9.js +2553 -0
- package/dist/doctor-0a2brpyz.js +259 -0
- package/dist/index-05pbj4hy.js +1193 -0
- package/dist/index-3edvz3hs.js +124 -0
- package/dist/index-50em8s6c.js +898 -0
- package/dist/index-666tdx14.js +895 -0
- package/dist/index-8p7rxkbs.js +1426 -0
- package/dist/index-aqrwyy57.js +288 -0
- package/dist/index-bj5avaba.js +109 -0
- package/dist/index-dgww0ewn.js +574 -0
- package/dist/index-f1yshy4s.js +412 -0
- package/dist/index-hpwa6vsw.js +239 -0
- package/dist/index-kxc4gtyt.js +574 -0
- package/dist/index-nxkesg55.js +68 -0
- package/dist/index-p7q23nce.js +1031 -0
- package/dist/index-pt49cgjv.js +1426 -0
- package/dist/index-rp0aye39.js +1426 -0
- package/dist/index-tknbyxzn.js +2202 -0
- package/dist/index.js +4 -4
- package/dist/runtime/index.js +4 -4
- package/dist/sveltekit/index.js +2 -2
- package/dist/test/index.js +62 -440
- package/dist/test/resolve-service-bindings.d.ts +63 -3
- package/dist/test/resolve-service-bindings.d.ts.map +1 -1
- package/dist/types-vhvt4hvm.js +693 -0
- package/dist/utils/send-email.d.ts.map +1 -1
- package/dist/utils/send-email.js +1 -1
- package/dist/vite/index.js +4 -3
- package/dist/vite/plugin-context.d.ts +3 -1
- package/dist/vite/plugin-context.d.ts.map +1 -1
- package/dist/vite/plugin-programmatic.d.ts.map +1 -1
- package/dist/vite/plugin-service-bindings.d.ts +13 -0
- package/dist/vite/plugin-service-bindings.d.ts.map +1 -0
- package/dist/vite/plugin.d.ts +4 -2
- package/dist/vite/plugin.d.ts.map +1 -1
- package/dist/worker-entrypoint-3rmzd4c1.js +15 -0
- package/package.json +1 -1
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
// src/transform/worker-entrypoint.ts
|
|
2
|
+
import ts from "typescript";
|
|
3
|
+
import MagicString from "magic-string";
|
|
4
|
+
|
|
5
|
+
// src/worker-entry/extensions.ts
|
|
6
|
+
var SUPPORTED_WORKER_EXTENSIONS = [
|
|
7
|
+
".ts",
|
|
8
|
+
".tsx",
|
|
9
|
+
".mts",
|
|
10
|
+
".mjs",
|
|
11
|
+
".cts",
|
|
12
|
+
".cjs",
|
|
13
|
+
".js",
|
|
14
|
+
".jsx"
|
|
15
|
+
];
|
|
16
|
+
var TS_WORKER_EXTENSIONS = [
|
|
17
|
+
".ts",
|
|
18
|
+
".tsx",
|
|
19
|
+
".mts",
|
|
20
|
+
".cts"
|
|
21
|
+
];
|
|
22
|
+
|
|
23
|
+
// src/transform/worker-entrypoint.ts
|
|
24
|
+
function findExportedFunctions(code) {
|
|
25
|
+
const functions = [];
|
|
26
|
+
const sourceFile = ts.createSourceFile("worker.ts", code, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS);
|
|
27
|
+
function visit(node) {
|
|
28
|
+
if (ts.isFunctionDeclaration(node) && node.name) {
|
|
29
|
+
const modifiers = ts.getModifiers(node);
|
|
30
|
+
const isExported = modifiers?.some((m) => m.kind === ts.SyntaxKind.ExportKeyword);
|
|
31
|
+
const isDefault = modifiers?.some((m) => m.kind === ts.SyntaxKind.DefaultKeyword);
|
|
32
|
+
if (isExported) {
|
|
33
|
+
const isAsync = modifiers?.some((m) => m.kind === ts.SyntaxKind.AsyncKeyword) ?? false;
|
|
34
|
+
const funcName = isDefault ? "fetch" : node.name.text;
|
|
35
|
+
const params = node.parameters.map((p) => code.substring(p.getStart(sourceFile), p.getEnd())).join(", ");
|
|
36
|
+
const returnType = node.type ? code.substring(node.type.getStart(sourceFile), node.type.getEnd()) : undefined;
|
|
37
|
+
functions.push({
|
|
38
|
+
name: funcName,
|
|
39
|
+
isAsync,
|
|
40
|
+
params,
|
|
41
|
+
returnType,
|
|
42
|
+
start: node.getStart(sourceFile),
|
|
43
|
+
end: node.getEnd(),
|
|
44
|
+
isDefault
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
if (ts.isFunctionDeclaration(node) && !node.name) {
|
|
49
|
+
const modifiers = ts.getModifiers(node);
|
|
50
|
+
const isExported = modifiers?.some((m) => m.kind === ts.SyntaxKind.ExportKeyword);
|
|
51
|
+
const isDefault = modifiers?.some((m) => m.kind === ts.SyntaxKind.DefaultKeyword);
|
|
52
|
+
if (isExported && isDefault) {
|
|
53
|
+
const isAsync = modifiers?.some((m) => m.kind === ts.SyntaxKind.AsyncKeyword) ?? false;
|
|
54
|
+
const params = node.parameters.map((p) => code.substring(p.getStart(sourceFile), p.getEnd())).join(", ");
|
|
55
|
+
const returnType = node.type ? code.substring(node.type.getStart(sourceFile), node.type.getEnd()) : undefined;
|
|
56
|
+
functions.push({
|
|
57
|
+
name: "fetch",
|
|
58
|
+
isAsync,
|
|
59
|
+
params,
|
|
60
|
+
returnType,
|
|
61
|
+
start: node.getStart(sourceFile),
|
|
62
|
+
end: node.getEnd(),
|
|
63
|
+
isDefault: true
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (ts.isVariableStatement(node)) {
|
|
68
|
+
const modifiers = ts.getModifiers(node);
|
|
69
|
+
const isExported = modifiers?.some((m) => m.kind === ts.SyntaxKind.ExportKeyword);
|
|
70
|
+
if (isExported) {
|
|
71
|
+
for (const decl of node.declarationList.declarations) {
|
|
72
|
+
if (ts.isIdentifier(decl.name) && decl.initializer) {
|
|
73
|
+
let funcExpr;
|
|
74
|
+
if (ts.isFunctionExpression(decl.initializer)) {
|
|
75
|
+
funcExpr = decl.initializer;
|
|
76
|
+
} else if (ts.isArrowFunction(decl.initializer)) {
|
|
77
|
+
funcExpr = decl.initializer;
|
|
78
|
+
}
|
|
79
|
+
if (funcExpr) {
|
|
80
|
+
const modifiersArr = ts.getModifiers(funcExpr);
|
|
81
|
+
const isAsync = modifiersArr?.some((m) => m.kind === ts.SyntaxKind.AsyncKeyword) ?? (ts.isArrowFunction(funcExpr) && funcExpr.modifiers?.some((m) => m.kind === ts.SyntaxKind.AsyncKeyword)) ?? false;
|
|
82
|
+
const params = funcExpr.parameters.map((p) => code.substring(p.getStart(sourceFile), p.getEnd())).join(", ");
|
|
83
|
+
const returnType = funcExpr.type ? code.substring(funcExpr.type.getStart(sourceFile), funcExpr.type.getEnd()) : undefined;
|
|
84
|
+
functions.push({
|
|
85
|
+
name: decl.name.text,
|
|
86
|
+
isAsync,
|
|
87
|
+
params,
|
|
88
|
+
returnType,
|
|
89
|
+
start: node.getStart(sourceFile),
|
|
90
|
+
end: node.getEnd()
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
ts.forEachChild(node, visit);
|
|
98
|
+
}
|
|
99
|
+
visit(sourceFile);
|
|
100
|
+
return functions;
|
|
101
|
+
}
|
|
102
|
+
function shouldEmitTsSyntax(filename) {
|
|
103
|
+
const lower = filename.toLowerCase();
|
|
104
|
+
return TS_WORKER_EXTENSIONS.some((ext) => lower.endsWith(ext));
|
|
105
|
+
}
|
|
106
|
+
function shouldTransformWorker(code, filePath) {
|
|
107
|
+
const lower = filePath.toLowerCase();
|
|
108
|
+
const isWorkerFile = SUPPORTED_WORKER_EXTENSIONS.some((ext) => lower.endsWith(`worker${ext}`));
|
|
109
|
+
if (!isWorkerFile) {
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
const functions = findExportedFunctions(code);
|
|
113
|
+
return functions.length > 0;
|
|
114
|
+
}
|
|
115
|
+
function transformWorkerEntrypoint(code, id, options = {}) {
|
|
116
|
+
const className = options.className ?? "Worker";
|
|
117
|
+
const injectContext = options.injectContext ?? true;
|
|
118
|
+
const emitTs = shouldEmitTsSyntax(id);
|
|
119
|
+
const functions = findExportedFunctions(code);
|
|
120
|
+
if (functions.length === 0) {
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
const fetchFn = functions.find((f) => f.name === "fetch");
|
|
124
|
+
const rpcMethods = functions.filter((f) => f.name !== "fetch");
|
|
125
|
+
const needsFetchHelpers = Boolean(fetchFn);
|
|
126
|
+
const s = new MagicString(code);
|
|
127
|
+
const importStatement = `import { WorkerEntrypoint } from 'cloudflare:workers'
|
|
128
|
+
`;
|
|
129
|
+
if (needsFetchHelpers) {
|
|
130
|
+
const runtimeImports = ["createFetchEvent", "invokeFetchHandler"];
|
|
131
|
+
if (injectContext) {
|
|
132
|
+
runtimeImports.push("runWithEventContext");
|
|
133
|
+
}
|
|
134
|
+
s.prepend(`import { ${runtimeImports.join(", ")} } from 'devflare/runtime'
|
|
135
|
+
`);
|
|
136
|
+
}
|
|
137
|
+
s.prepend(importStatement);
|
|
138
|
+
const sourceFile = ts.createSourceFile("worker.ts", code, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS);
|
|
139
|
+
const internalNameFor = (name) => name === "fetch" ? "__originalFetch" : `__original_${name}`;
|
|
140
|
+
function visit(node) {
|
|
141
|
+
if (ts.isFunctionDeclaration(node)) {
|
|
142
|
+
const modifiers = ts.getModifiers(node);
|
|
143
|
+
const exportMod = modifiers?.find((m) => m.kind === ts.SyntaxKind.ExportKeyword);
|
|
144
|
+
const defaultMod = modifiers?.find((m) => m.kind === ts.SyntaxKind.DefaultKeyword);
|
|
145
|
+
const asyncMod = modifiers?.find((m) => m.kind === ts.SyntaxKind.AsyncKeyword);
|
|
146
|
+
if (exportMod) {
|
|
147
|
+
const isDefault = Boolean(defaultMod);
|
|
148
|
+
const logicalName = isDefault ? "fetch" : node.name?.text;
|
|
149
|
+
if (logicalName) {
|
|
150
|
+
const internal = internalNameFor(logicalName);
|
|
151
|
+
if (isDefault && !node.name) {
|
|
152
|
+
const funcKeyword = node.getChildren(sourceFile).find((c) => c.kind === ts.SyntaxKind.FunctionKeyword);
|
|
153
|
+
if (funcKeyword) {
|
|
154
|
+
const asyncKw = asyncMod ? "async " : "";
|
|
155
|
+
s.overwrite(node.getStart(sourceFile), funcKeyword.getStart(sourceFile), `const ${internal} = ${asyncKw}`);
|
|
156
|
+
}
|
|
157
|
+
} else if (node.name) {
|
|
158
|
+
s.remove(exportMod.getStart(sourceFile), exportMod.getEnd());
|
|
159
|
+
if (defaultMod) {
|
|
160
|
+
s.remove(defaultMod.getStart(sourceFile), defaultMod.getEnd());
|
|
161
|
+
}
|
|
162
|
+
s.overwrite(node.name.getStart(sourceFile), node.name.getEnd(), internal);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
} else if (ts.isVariableStatement(node)) {
|
|
167
|
+
const modifiers = ts.getModifiers(node);
|
|
168
|
+
const exportMod = modifiers?.find((m) => m.kind === ts.SyntaxKind.ExportKeyword);
|
|
169
|
+
if (exportMod) {
|
|
170
|
+
let rewroteAny = false;
|
|
171
|
+
for (const decl of node.declarationList.declarations) {
|
|
172
|
+
if (ts.isIdentifier(decl.name) && decl.initializer && (ts.isFunctionExpression(decl.initializer) || ts.isArrowFunction(decl.initializer))) {
|
|
173
|
+
const internal = internalNameFor(decl.name.text);
|
|
174
|
+
s.overwrite(decl.name.getStart(sourceFile), decl.name.getEnd(), internal);
|
|
175
|
+
rewroteAny = true;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
if (rewroteAny) {
|
|
179
|
+
s.remove(exportMod.getStart(sourceFile), exportMod.getEnd());
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
ts.forEachChild(node, visit);
|
|
184
|
+
}
|
|
185
|
+
visit(sourceFile);
|
|
186
|
+
let classBody = `
|
|
187
|
+
|
|
188
|
+
// ============ Devflare WorkerEntrypoint ============
|
|
189
|
+
class ${className} extends WorkerEntrypoint {
|
|
190
|
+
`;
|
|
191
|
+
if (fetchFn) {
|
|
192
|
+
const fetchSig = emitTs ? "async fetch(request: Request): Promise<Response>" : "async fetch(request)";
|
|
193
|
+
classBody += ` ${fetchSig} {
|
|
194
|
+
`;
|
|
195
|
+
classBody += ` const __devflareEvent = createFetchEvent(request, this.env, this.ctx)
|
|
196
|
+
`;
|
|
197
|
+
if (injectContext) {
|
|
198
|
+
classBody += ` return runWithEventContext(
|
|
199
|
+
`;
|
|
200
|
+
classBody += ` __devflareEvent,
|
|
201
|
+
`;
|
|
202
|
+
classBody += ` () => invokeFetchHandler(__originalFetch, __devflareEvent)
|
|
203
|
+
`;
|
|
204
|
+
classBody += ` )
|
|
205
|
+
`;
|
|
206
|
+
classBody += ` }
|
|
207
|
+
`;
|
|
208
|
+
} else {
|
|
209
|
+
classBody += ` return invokeFetchHandler(__originalFetch, __devflareEvent)
|
|
210
|
+
`;
|
|
211
|
+
classBody += ` }
|
|
212
|
+
`;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
for (const fn of rpcMethods) {
|
|
216
|
+
const asyncPrefix = fn.isAsync ? "async " : "";
|
|
217
|
+
const paramNames = extractParamNames(fn.params);
|
|
218
|
+
const signatureParams = emitTs ? fn.params : paramNames;
|
|
219
|
+
const returnType = emitTs && fn.returnType ? `: ${fn.returnType}` : "";
|
|
220
|
+
classBody += `
|
|
221
|
+
${asyncPrefix}${fn.name}(${signatureParams})${returnType} {
|
|
222
|
+
`;
|
|
223
|
+
classBody += ` return __original_${fn.name}(${paramNames})
|
|
224
|
+
`;
|
|
225
|
+
classBody += ` }
|
|
226
|
+
`;
|
|
227
|
+
}
|
|
228
|
+
classBody += `}
|
|
229
|
+
|
|
230
|
+
`;
|
|
231
|
+
classBody += `export { ${className} }
|
|
232
|
+
`;
|
|
233
|
+
classBody += `export { ${className} as default }
|
|
234
|
+
`;
|
|
235
|
+
classBody += `// ============ End Devflare WorkerEntrypoint ============
|
|
236
|
+
`;
|
|
237
|
+
s.append(classBody);
|
|
238
|
+
return {
|
|
239
|
+
code: s.toString(),
|
|
240
|
+
map: s.generateMap({
|
|
241
|
+
source: id,
|
|
242
|
+
file: id + ".map",
|
|
243
|
+
includeContent: true
|
|
244
|
+
}),
|
|
245
|
+
rpcMethods: rpcMethods.map((fn) => fn.name),
|
|
246
|
+
className
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
function extractParamNames(params) {
|
|
250
|
+
if (!params.trim())
|
|
251
|
+
return "";
|
|
252
|
+
const tempCode = `function f(${params}) {}`;
|
|
253
|
+
const sourceFile = ts.createSourceFile("temp.ts", tempCode, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS);
|
|
254
|
+
const names = [];
|
|
255
|
+
function visit(node) {
|
|
256
|
+
if (ts.isFunctionDeclaration(node)) {
|
|
257
|
+
for (const param of node.parameters) {
|
|
258
|
+
if (ts.isIdentifier(param.name)) {
|
|
259
|
+
names.push(param.name.text);
|
|
260
|
+
} else if (ts.isObjectBindingPattern(param.name) || ts.isArrayBindingPattern(param.name)) {
|
|
261
|
+
names.push(tempCode.substring(param.name.getStart(sourceFile), param.name.getEnd()));
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
ts.forEachChild(node, visit);
|
|
266
|
+
}
|
|
267
|
+
visit(sourceFile);
|
|
268
|
+
return names.join(", ");
|
|
269
|
+
}
|
|
270
|
+
function generateRpcInterface(functions, interfaceName) {
|
|
271
|
+
const rpcMethods = functions.filter((f) => f.name !== "fetch");
|
|
272
|
+
if (rpcMethods.length === 0) {
|
|
273
|
+
return "";
|
|
274
|
+
}
|
|
275
|
+
let output = `export interface ${interfaceName} {
|
|
276
|
+
`;
|
|
277
|
+
for (const fn of rpcMethods) {
|
|
278
|
+
const returnType = fn.returnType ?? "unknown";
|
|
279
|
+
const promiseReturn = returnType.startsWith("Promise<") ? returnType : `Promise<${returnType}>`;
|
|
280
|
+
output += ` ${fn.name}(${fn.params}): ${promiseReturn}
|
|
281
|
+
`;
|
|
282
|
+
}
|
|
283
|
+
output += `}
|
|
284
|
+
`;
|
|
285
|
+
return output;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
export { SUPPORTED_WORKER_EXTENSIONS, findExportedFunctions, shouldEmitTsSyntax, shouldTransformWorker, transformWorkerEntrypoint, generateRpcInterface };
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import {
|
|
2
|
+
bundleWorkerEntry
|
|
3
|
+
} from "./index-666tdx14.js";
|
|
4
|
+
import {
|
|
5
|
+
DEFAULT_WORKFLOW_PATTERN,
|
|
6
|
+
findFiles
|
|
7
|
+
} from "./index-qwgr4q7s.js";
|
|
8
|
+
import {
|
|
9
|
+
normalizeWorkflowBinding
|
|
10
|
+
} from "./index-syscwrjp.js";
|
|
11
|
+
|
|
12
|
+
// src/workflows/local-workflow-entrypoints.ts
|
|
13
|
+
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
14
|
+
import { join, relative, resolve } from "pathe";
|
|
15
|
+
function findExportedClasses(code) {
|
|
16
|
+
const classes = [];
|
|
17
|
+
const classPattern = /export\s+class\s+(\w+)/g;
|
|
18
|
+
let match;
|
|
19
|
+
while ((match = classPattern.exec(code)) !== null) {
|
|
20
|
+
classes.push(match[1]);
|
|
21
|
+
}
|
|
22
|
+
return classes;
|
|
23
|
+
}
|
|
24
|
+
function toImportSpecifier(fromDir, filePath) {
|
|
25
|
+
const relativePath = relative(fromDir, filePath).replace(/\\/g, "/");
|
|
26
|
+
return relativePath.startsWith(".") ? relativePath : `./${relativePath}`;
|
|
27
|
+
}
|
|
28
|
+
async function discoverWorkflowClasses(config, configDir) {
|
|
29
|
+
const classToFilePath = new Map;
|
|
30
|
+
const workflowPatternConfig = config.files?.workflows;
|
|
31
|
+
const workflowPattern = typeof workflowPatternConfig === "string" ? workflowPatternConfig : DEFAULT_WORKFLOW_PATTERN;
|
|
32
|
+
if (workflowPatternConfig === false) {
|
|
33
|
+
return classToFilePath;
|
|
34
|
+
}
|
|
35
|
+
const files = await findFiles(workflowPattern, { cwd: configDir });
|
|
36
|
+
for (const filePath of files) {
|
|
37
|
+
try {
|
|
38
|
+
const code = await readFile(filePath, "utf-8");
|
|
39
|
+
for (const className of findExportedClasses(code)) {
|
|
40
|
+
classToFilePath.set(className, filePath);
|
|
41
|
+
}
|
|
42
|
+
} catch {}
|
|
43
|
+
}
|
|
44
|
+
return classToFilePath;
|
|
45
|
+
}
|
|
46
|
+
async function resolveLocalWorkflowEntrypoints(config, configDir) {
|
|
47
|
+
const workflows = config.bindings?.workflows;
|
|
48
|
+
if (!workflows || Object.keys(workflows).length === 0) {
|
|
49
|
+
return [];
|
|
50
|
+
}
|
|
51
|
+
const classToFilePath = await discoverWorkflowClasses(config, configDir);
|
|
52
|
+
const entrypoints = [];
|
|
53
|
+
for (const [bindingName, binding] of Object.entries(workflows)) {
|
|
54
|
+
const normalized = normalizeWorkflowBinding(binding);
|
|
55
|
+
if (normalized.scriptName) {
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
const scriptPath = classToFilePath.get(normalized.className);
|
|
59
|
+
if (!scriptPath) {
|
|
60
|
+
throw new Error(`Workflow binding ${bindingName} (className: '${normalized.className}') not found.
|
|
61
|
+
` + `Either set files.workflows to match the workflow source file, or set scriptName when the workflow lives in another worker.`);
|
|
62
|
+
}
|
|
63
|
+
entrypoints.push({
|
|
64
|
+
bindingName,
|
|
65
|
+
className: normalized.className,
|
|
66
|
+
scriptPath
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
return entrypoints;
|
|
70
|
+
}
|
|
71
|
+
function buildWorkflowVirtualEntry(entrypoints, entryDir) {
|
|
72
|
+
const imports = entrypoints.map((entrypoint, index) => {
|
|
73
|
+
const importName = `__DevflareWorkflow${index}`;
|
|
74
|
+
const importPath = toImportSpecifier(entryDir, entrypoint.scriptPath);
|
|
75
|
+
return {
|
|
76
|
+
importName,
|
|
77
|
+
className: entrypoint.className,
|
|
78
|
+
line: `import { ${entrypoint.className} as ${importName} } from '${importPath}'`
|
|
79
|
+
};
|
|
80
|
+
});
|
|
81
|
+
const exports = imports.map((entrypoint) => {
|
|
82
|
+
return `export { ${entrypoint.importName} as ${entrypoint.className} }`;
|
|
83
|
+
});
|
|
84
|
+
return [...imports.map((entrypoint) => entrypoint.line), "", ...exports].join(`
|
|
85
|
+
`);
|
|
86
|
+
}
|
|
87
|
+
async function bundleWorkflowEntrypointScript(config, configDir, options = {}) {
|
|
88
|
+
const entrypoints = await resolveLocalWorkflowEntrypoints(config, configDir);
|
|
89
|
+
if (entrypoints.length === 0) {
|
|
90
|
+
return "";
|
|
91
|
+
}
|
|
92
|
+
const entryDir = resolve(configDir, ".devflare", "workflow-entrypoints");
|
|
93
|
+
const entryPath = join(entryDir, "__entry.ts");
|
|
94
|
+
const outFile = join(entryDir, "index.js");
|
|
95
|
+
await mkdir(entryDir, { recursive: true });
|
|
96
|
+
await writeFile(entryPath, buildWorkflowVirtualEntry(entrypoints, entryDir));
|
|
97
|
+
await bundleWorkerEntry({
|
|
98
|
+
cwd: configDir,
|
|
99
|
+
inputFile: entryPath,
|
|
100
|
+
outFile,
|
|
101
|
+
rolldownOptions: config.rolldown?.options,
|
|
102
|
+
sourcemap: config.rolldown?.sourcemap,
|
|
103
|
+
minify: config.rolldown?.minify,
|
|
104
|
+
logger: options.logger
|
|
105
|
+
});
|
|
106
|
+
return await readFile(outFile, "utf-8");
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export { bundleWorkflowEntrypointScript };
|