@mastra/deployer 0.10.12 → 0.10.13
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/package.json +4 -4
- package/dist/_tsup-dts-rollup.d.cts +0 -836
- package/dist/_tsup-dts-rollup.d.ts +0 -836
- package/dist/build/analyze.cjs +0 -10
- package/dist/build/analyze.d.cts +0 -1
- package/dist/build/analyze.d.ts +0 -1
- package/dist/build/analyze.js +0 -1
- package/dist/build/bundler.cjs +0 -14
- package/dist/build/bundler.d.cts +0 -2
- package/dist/build/bundler.d.ts +0 -2
- package/dist/build/bundler.js +0 -1
- package/dist/build/index.cjs +0 -50
- package/dist/build/index.d.cts +0 -10
- package/dist/build/index.d.ts +0 -10
- package/dist/build/index.js +0 -5
- package/dist/bundler/index.cjs +0 -10
- package/dist/bundler/index.d.cts +0 -1
- package/dist/bundler/index.d.ts +0 -1
- package/dist/bundler/index.js +0 -1
- package/dist/chunk-2C7ENC5E.js +0 -474
- package/dist/chunk-54KOF3NB.cjs +0 -137
- package/dist/chunk-6QMONK4A.cjs +0 -2
- package/dist/chunk-C4JT7CIH.js +0 -1
- package/dist/chunk-D2DCFCLH.cjs +0 -157
- package/dist/chunk-D6736SJL.js +0 -209
- package/dist/chunk-ENT5RDOI.js +0 -99
- package/dist/chunk-FK2WUSEN.cjs +0 -238
- package/dist/chunk-GPD54HBC.js +0 -380
- package/dist/chunk-KCP5ITLV.cjs +0 -412
- package/dist/chunk-M2VZQFTW.cjs +0 -125
- package/dist/chunk-MKB7NDY3.cjs +0 -486
- package/dist/chunk-TIC2KT3M.js +0 -146
- package/dist/chunk-UYQZMNZL.js +0 -572
- package/dist/chunk-XKH6F4NE.cjs +0 -604
- package/dist/chunk-Z544XXXK.js +0 -111
- package/dist/index.cjs +0 -194
- package/dist/index.d.cts +0 -8
- package/dist/index.d.ts +0 -8
- package/dist/index.js +0 -153
- package/dist/server/index.cjs +0 -8326
- package/dist/server/index.d.cts +0 -2
- package/dist/server/index.d.ts +0 -2
- package/dist/server/index.js +0 -8318
- package/dist/services/index.cjs +0 -18
- package/dist/services/index.d.cts +0 -3
- package/dist/services/index.d.ts +0 -3
- package/dist/services/index.js +0 -1
- package/dist/templates/instrumentation-template.js +0 -137
- package/dist/validator/custom-resolver.cjs +0 -52
- package/dist/validator/custom-resolver.d.cts +0 -1
- package/dist/validator/custom-resolver.d.ts +0 -1
- package/dist/validator/custom-resolver.js +0 -50
- package/dist/validator/loader.cjs +0 -7
- package/dist/validator/loader.d.cts +0 -1
- package/dist/validator/loader.d.ts +0 -1
- package/dist/validator/loader.js +0 -4
package/dist/chunk-UYQZMNZL.js
DELETED
|
@@ -1,572 +0,0 @@
|
|
|
1
|
-
import { tsConfigPaths, removeDeployer } from './chunk-Z544XXXK.js';
|
|
2
|
-
import commonjs2 from '@rollup/plugin-commonjs';
|
|
3
|
-
import json from '@rollup/plugin-json';
|
|
4
|
-
import nodeResolve from '@rollup/plugin-node-resolve';
|
|
5
|
-
import virtual from '@rollup/plugin-virtual';
|
|
6
|
-
import { existsSync } from 'fs';
|
|
7
|
-
import { fileURLToPath } from 'url';
|
|
8
|
-
import { rollup } from 'rollup';
|
|
9
|
-
import esbuild from 'rollup-plugin-esbuild';
|
|
10
|
-
import { builtinModules } from 'module';
|
|
11
|
-
import { join, dirname } from 'path';
|
|
12
|
-
import { spawn as spawn$1 } from 'child_process';
|
|
13
|
-
import { writeFile } from 'fs/promises';
|
|
14
|
-
import * as babel from '@babel/core';
|
|
15
|
-
import babel__default from '@babel/core';
|
|
16
|
-
|
|
17
|
-
function isNodeBuiltin(dep) {
|
|
18
|
-
const [pkg] = dep.split("/");
|
|
19
|
-
return dep.startsWith("node:") || builtinModules.includes(dep) || builtinModules.includes(pkg);
|
|
20
|
-
}
|
|
21
|
-
function aliasHono() {
|
|
22
|
-
return {
|
|
23
|
-
name: "hono-alias",
|
|
24
|
-
resolveId(id) {
|
|
25
|
-
if (!id.startsWith("@hono/") && !id.startsWith("hono/") && id !== "hono" && id !== "hono-openapi") {
|
|
26
|
-
return;
|
|
27
|
-
}
|
|
28
|
-
const path = import.meta.resolve(id);
|
|
29
|
-
return fileURLToPath(path);
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
function spawn(command, args = [], options = {}) {
|
|
34
|
-
return new Promise((resolve, reject) => {
|
|
35
|
-
const childProcess = spawn$1(command, args, {
|
|
36
|
-
// stdio: 'inherit',
|
|
37
|
-
...options
|
|
38
|
-
});
|
|
39
|
-
childProcess.on("error", (error) => {
|
|
40
|
-
reject(error);
|
|
41
|
-
});
|
|
42
|
-
let stderr = "";
|
|
43
|
-
childProcess.stderr?.on("data", (message) => {
|
|
44
|
-
stderr += message;
|
|
45
|
-
});
|
|
46
|
-
childProcess.on("close", (code) => {
|
|
47
|
-
if (code === 0) {
|
|
48
|
-
resolve();
|
|
49
|
-
} else {
|
|
50
|
-
reject(new Error(stderr));
|
|
51
|
-
}
|
|
52
|
-
});
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
function validate(file) {
|
|
56
|
-
return spawn(
|
|
57
|
-
"node",
|
|
58
|
-
[
|
|
59
|
-
"--import",
|
|
60
|
-
import.meta.resolve("@mastra/deployer/loader"),
|
|
61
|
-
"--input-type=module",
|
|
62
|
-
"-e",
|
|
63
|
-
`import('file://${file.replaceAll("\\", "/")}')`
|
|
64
|
-
],
|
|
65
|
-
{
|
|
66
|
-
cwd: dirname(file)
|
|
67
|
-
}
|
|
68
|
-
);
|
|
69
|
-
}
|
|
70
|
-
function removeAllOptionsFromMastraExcept(result, option) {
|
|
71
|
-
const t = babel__default.types;
|
|
72
|
-
return {
|
|
73
|
-
name: "remove-all-except-" + option + "-config",
|
|
74
|
-
visitor: {
|
|
75
|
-
ExportNamedDeclaration: {
|
|
76
|
-
// remove all exports
|
|
77
|
-
exit(path) {
|
|
78
|
-
path.remove();
|
|
79
|
-
}
|
|
80
|
-
},
|
|
81
|
-
NewExpression(path, state) {
|
|
82
|
-
const varDeclaratorPath = path.findParent((path2) => t.isVariableDeclarator(path2.node));
|
|
83
|
-
if (!varDeclaratorPath) {
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
86
|
-
const parentNode = path.parentPath.node;
|
|
87
|
-
if (!t.isVariableDeclarator(parentNode) || !t.isIdentifier(parentNode.id) || parentNode.id.name !== "mastra") {
|
|
88
|
-
return;
|
|
89
|
-
}
|
|
90
|
-
let mastraArgs = t.objectExpression([]);
|
|
91
|
-
if (t.isObjectExpression(path.node.arguments[0])) {
|
|
92
|
-
mastraArgs = path.node.arguments[0];
|
|
93
|
-
}
|
|
94
|
-
let telemetry = mastraArgs.properties.find(
|
|
95
|
-
// @ts-ignore
|
|
96
|
-
(prop) => prop.key.name === option
|
|
97
|
-
);
|
|
98
|
-
let telemetryValue = t.objectExpression([]);
|
|
99
|
-
const programPath = path.scope.getProgramParent().path;
|
|
100
|
-
if (!programPath) {
|
|
101
|
-
return;
|
|
102
|
-
}
|
|
103
|
-
if (telemetry && t.isObjectProperty(telemetry) && t.isExpression(telemetry.value)) {
|
|
104
|
-
result.hasCustomConfig = true;
|
|
105
|
-
telemetryValue = telemetry.value;
|
|
106
|
-
if (t.isIdentifier(telemetry.value) && telemetry.value.name === option) {
|
|
107
|
-
const telemetryBinding = state.file.scope.getBinding(option);
|
|
108
|
-
if (telemetryBinding && t.isVariableDeclarator(telemetryBinding.path.node)) {
|
|
109
|
-
const id = path.scope.generateUidIdentifier(option);
|
|
110
|
-
telemetryBinding.path.replaceWith(t.variableDeclarator(id, telemetryBinding.path.node.init));
|
|
111
|
-
telemetryValue = id;
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
const exportDeclaration = t.exportNamedDeclaration(
|
|
116
|
-
t.variableDeclaration("const", [t.variableDeclarator(t.identifier(option), telemetryValue)]),
|
|
117
|
-
[]
|
|
118
|
-
);
|
|
119
|
-
programPath.node.body.push(exportDeclaration);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
};
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
// src/build/babel/remove-all-options-bundler.ts
|
|
126
|
-
function removeAllOptionsExceptBundler(result) {
|
|
127
|
-
return removeAllOptionsFromMastraExcept(result, "bundler");
|
|
128
|
-
}
|
|
129
|
-
function removeNonReferencedNodes() {
|
|
130
|
-
const t = babel__default.types;
|
|
131
|
-
return {
|
|
132
|
-
name: "remove-non-referenced-nodes",
|
|
133
|
-
visitor: {
|
|
134
|
-
Program(path) {
|
|
135
|
-
const scope = path.scope;
|
|
136
|
-
const currentBody = path.get("body");
|
|
137
|
-
const filteredBody = currentBody.filter((childPath) => {
|
|
138
|
-
if (childPath.isExportDeclaration()) {
|
|
139
|
-
return true;
|
|
140
|
-
}
|
|
141
|
-
if (childPath.isVariableDeclaration()) {
|
|
142
|
-
return childPath.node.declarations.some((decl) => {
|
|
143
|
-
if (!t.isIdentifier(decl.id)) {
|
|
144
|
-
return false;
|
|
145
|
-
}
|
|
146
|
-
const name = decl.id.name;
|
|
147
|
-
const binding = scope.getBinding(name);
|
|
148
|
-
return binding && (binding.referenced || binding.referencePaths.length > 0);
|
|
149
|
-
});
|
|
150
|
-
}
|
|
151
|
-
if (childPath.isFunctionDeclaration() || childPath.isClassDeclaration()) {
|
|
152
|
-
if (!t.isIdentifier(childPath.node.id)) {
|
|
153
|
-
return false;
|
|
154
|
-
}
|
|
155
|
-
const name = childPath.node.id.name;
|
|
156
|
-
const binding = scope.getBinding(name);
|
|
157
|
-
return binding && (binding.referenced || binding.referencePaths.length > 0);
|
|
158
|
-
}
|
|
159
|
-
if (childPath.isImportDeclaration()) {
|
|
160
|
-
return childPath.node.specifiers.some((specifier) => {
|
|
161
|
-
const importedName = specifier.local.name;
|
|
162
|
-
const binding = scope.getBinding(importedName);
|
|
163
|
-
return binding && (binding.referenced || binding.referencePaths.length > 0);
|
|
164
|
-
});
|
|
165
|
-
}
|
|
166
|
-
return false;
|
|
167
|
-
});
|
|
168
|
-
path.set(
|
|
169
|
-
"body",
|
|
170
|
-
filteredBody.map((p) => p.node)
|
|
171
|
-
);
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
};
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
// src/build/plugins/remove-unused-references.ts
|
|
178
|
-
function recursiveRemoveNonReferencedNodes(code) {
|
|
179
|
-
return new Promise(async (resolve, reject) => {
|
|
180
|
-
babel.transform(
|
|
181
|
-
code,
|
|
182
|
-
{
|
|
183
|
-
babelrc: false,
|
|
184
|
-
configFile: false,
|
|
185
|
-
plugins: [removeNonReferencedNodes()]
|
|
186
|
-
},
|
|
187
|
-
(err, result) => {
|
|
188
|
-
if (err) {
|
|
189
|
-
return reject(err);
|
|
190
|
-
}
|
|
191
|
-
if (result && result.code !== code) {
|
|
192
|
-
return recursiveRemoveNonReferencedNodes(result.code).then(resolve, reject);
|
|
193
|
-
}
|
|
194
|
-
resolve({
|
|
195
|
-
code: result.code,
|
|
196
|
-
map: result.map
|
|
197
|
-
});
|
|
198
|
-
}
|
|
199
|
-
);
|
|
200
|
-
});
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
// src/build/bundlerOptions.ts
|
|
204
|
-
function getBundlerOptionsBundler(entryFile, result) {
|
|
205
|
-
return rollup({
|
|
206
|
-
logLevel: "silent",
|
|
207
|
-
input: {
|
|
208
|
-
"bundler-config": entryFile
|
|
209
|
-
},
|
|
210
|
-
treeshake: "smallest",
|
|
211
|
-
plugins: [
|
|
212
|
-
tsConfigPaths(),
|
|
213
|
-
// transpile typescript to something we understand
|
|
214
|
-
esbuild({
|
|
215
|
-
target: "node20",
|
|
216
|
-
platform: "node",
|
|
217
|
-
minify: false
|
|
218
|
-
}),
|
|
219
|
-
commonjs2({
|
|
220
|
-
extensions: [".js", ".ts"],
|
|
221
|
-
strictRequires: "strict",
|
|
222
|
-
transformMixedEsModules: true,
|
|
223
|
-
ignoreTryCatch: false
|
|
224
|
-
}),
|
|
225
|
-
{
|
|
226
|
-
name: "get-bundler-config",
|
|
227
|
-
transform(code, id) {
|
|
228
|
-
if (id !== entryFile) {
|
|
229
|
-
return;
|
|
230
|
-
}
|
|
231
|
-
return new Promise((resolve, reject) => {
|
|
232
|
-
babel.transform(
|
|
233
|
-
code,
|
|
234
|
-
{
|
|
235
|
-
babelrc: false,
|
|
236
|
-
configFile: false,
|
|
237
|
-
filename: id,
|
|
238
|
-
plugins: [removeAllOptionsExceptBundler(result)]
|
|
239
|
-
},
|
|
240
|
-
(err, result2) => {
|
|
241
|
-
if (err) {
|
|
242
|
-
return reject(err);
|
|
243
|
-
}
|
|
244
|
-
resolve({
|
|
245
|
-
code: result2.code,
|
|
246
|
-
map: result2.map
|
|
247
|
-
});
|
|
248
|
-
}
|
|
249
|
-
);
|
|
250
|
-
});
|
|
251
|
-
}
|
|
252
|
-
},
|
|
253
|
-
// let esbuild remove all unused imports
|
|
254
|
-
esbuild({
|
|
255
|
-
target: "node20",
|
|
256
|
-
platform: "node",
|
|
257
|
-
minify: false
|
|
258
|
-
}),
|
|
259
|
-
{
|
|
260
|
-
name: "cleanup",
|
|
261
|
-
transform(code, id) {
|
|
262
|
-
if (id !== entryFile) {
|
|
263
|
-
return;
|
|
264
|
-
}
|
|
265
|
-
return recursiveRemoveNonReferencedNodes(code);
|
|
266
|
-
}
|
|
267
|
-
},
|
|
268
|
-
// let esbuild remove all unused imports
|
|
269
|
-
esbuild({
|
|
270
|
-
target: "node20",
|
|
271
|
-
platform: "node",
|
|
272
|
-
minify: false
|
|
273
|
-
})
|
|
274
|
-
]
|
|
275
|
-
});
|
|
276
|
-
}
|
|
277
|
-
async function getBundlerOptions(entryFile, outputDir) {
|
|
278
|
-
const result = {
|
|
279
|
-
hasCustomConfig: false
|
|
280
|
-
};
|
|
281
|
-
const bundle = await getBundlerOptionsBundler(entryFile, result);
|
|
282
|
-
await bundle.write({
|
|
283
|
-
dir: outputDir,
|
|
284
|
-
format: "es",
|
|
285
|
-
entryFileNames: "[name].mjs"
|
|
286
|
-
});
|
|
287
|
-
if (result.hasCustomConfig) {
|
|
288
|
-
return (await import(`file:${outputDir}/bundler-config.mjs`)).bundler;
|
|
289
|
-
}
|
|
290
|
-
return null;
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
// src/build/analyze.ts
|
|
294
|
-
var globalExternals = [
|
|
295
|
-
"pino",
|
|
296
|
-
"pino-pretty",
|
|
297
|
-
"@libsql/client",
|
|
298
|
-
"pg",
|
|
299
|
-
"libsql",
|
|
300
|
-
"jsdom",
|
|
301
|
-
"sqlite3",
|
|
302
|
-
"fastembed",
|
|
303
|
-
"nodemailer"
|
|
304
|
-
];
|
|
305
|
-
function findExternalImporter(module, external, allOutputs) {
|
|
306
|
-
const capturedFiles = /* @__PURE__ */ new Set();
|
|
307
|
-
for (const id of module.imports) {
|
|
308
|
-
if (id === external) {
|
|
309
|
-
return module;
|
|
310
|
-
} else {
|
|
311
|
-
if (id.endsWith(".mjs")) {
|
|
312
|
-
capturedFiles.add(id);
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
for (const file of capturedFiles) {
|
|
317
|
-
const nextModule = allOutputs.find((o) => o.fileName === file);
|
|
318
|
-
if (nextModule) {
|
|
319
|
-
const importer = findExternalImporter(nextModule, external, allOutputs);
|
|
320
|
-
if (importer) {
|
|
321
|
-
return importer;
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
return null;
|
|
326
|
-
}
|
|
327
|
-
async function analyze(entry, mastraEntry, isVirtualFile, platform, logger) {
|
|
328
|
-
logger.info("Analyzing dependencies...");
|
|
329
|
-
let virtualPlugin = null;
|
|
330
|
-
if (isVirtualFile) {
|
|
331
|
-
virtualPlugin = virtual({
|
|
332
|
-
"#entry": entry
|
|
333
|
-
});
|
|
334
|
-
entry = "#entry";
|
|
335
|
-
}
|
|
336
|
-
const normalizedMastraEntry = mastraEntry.replaceAll("\\", "/");
|
|
337
|
-
const optimizerBundler = await rollup({
|
|
338
|
-
logLevel: process.env.MASTRA_BUNDLER_DEBUG === "true" ? "debug" : "silent",
|
|
339
|
-
input: isVirtualFile ? "#entry" : entry,
|
|
340
|
-
treeshake: "smallest",
|
|
341
|
-
preserveSymlinks: true,
|
|
342
|
-
plugins: [
|
|
343
|
-
virtualPlugin,
|
|
344
|
-
tsConfigPaths(),
|
|
345
|
-
{
|
|
346
|
-
name: "custom-alias-resolver",
|
|
347
|
-
resolveId(id) {
|
|
348
|
-
if (id === "#server") {
|
|
349
|
-
return fileURLToPath(import.meta.resolve("@mastra/deployer/server")).replaceAll("\\", "/");
|
|
350
|
-
}
|
|
351
|
-
if (id === "#mastra") {
|
|
352
|
-
return normalizedMastraEntry;
|
|
353
|
-
}
|
|
354
|
-
if (id.startsWith("@mastra/server")) {
|
|
355
|
-
return fileURLToPath(import.meta.resolve(id));
|
|
356
|
-
}
|
|
357
|
-
if (id === "#tools") {
|
|
358
|
-
return {
|
|
359
|
-
id: "#tools",
|
|
360
|
-
external: true
|
|
361
|
-
};
|
|
362
|
-
}
|
|
363
|
-
}
|
|
364
|
-
},
|
|
365
|
-
json(),
|
|
366
|
-
esbuild({
|
|
367
|
-
target: "node20",
|
|
368
|
-
platform,
|
|
369
|
-
minify: false
|
|
370
|
-
}),
|
|
371
|
-
commonjs2({
|
|
372
|
-
strictRequires: "debug",
|
|
373
|
-
ignoreTryCatch: false,
|
|
374
|
-
transformMixedEsModules: true,
|
|
375
|
-
extensions: [".js", ".ts"]
|
|
376
|
-
}),
|
|
377
|
-
removeDeployer(normalizedMastraEntry),
|
|
378
|
-
esbuild({
|
|
379
|
-
target: "node20",
|
|
380
|
-
platform,
|
|
381
|
-
minify: false
|
|
382
|
-
})
|
|
383
|
-
].filter(Boolean)
|
|
384
|
-
});
|
|
385
|
-
const { output } = await optimizerBundler.generate({
|
|
386
|
-
format: "esm",
|
|
387
|
-
inlineDynamicImports: true
|
|
388
|
-
});
|
|
389
|
-
await optimizerBundler.close();
|
|
390
|
-
const depsToOptimize = new Map(Object.entries(output[0].importedBindings));
|
|
391
|
-
for (const dep of depsToOptimize.keys()) {
|
|
392
|
-
if (isNodeBuiltin(dep)) {
|
|
393
|
-
depsToOptimize.delete(dep);
|
|
394
|
-
}
|
|
395
|
-
}
|
|
396
|
-
for (const o of output) {
|
|
397
|
-
if (o.type !== "chunk") {
|
|
398
|
-
continue;
|
|
399
|
-
}
|
|
400
|
-
const dynamicImports = o.dynamicImports.filter((d) => d !== "#tools");
|
|
401
|
-
if (!dynamicImports.length) {
|
|
402
|
-
continue;
|
|
403
|
-
}
|
|
404
|
-
console.log(dynamicImports);
|
|
405
|
-
for (const dynamicImport of dynamicImports) {
|
|
406
|
-
if (!depsToOptimize.has(dynamicImport) && !isNodeBuiltin(dynamicImport)) {
|
|
407
|
-
depsToOptimize.set(dynamicImport, ["*"]);
|
|
408
|
-
}
|
|
409
|
-
}
|
|
410
|
-
}
|
|
411
|
-
return depsToOptimize;
|
|
412
|
-
}
|
|
413
|
-
async function bundleExternals(depsToOptimize, outputDir, logger, customExternals) {
|
|
414
|
-
logger.info("Optimizing dependencies...");
|
|
415
|
-
logger.debug(
|
|
416
|
-
`${Array.from(depsToOptimize.keys()).map((key) => `- ${key}`).join("\n")}`
|
|
417
|
-
);
|
|
418
|
-
const allExternals = [...globalExternals, ...customExternals || []];
|
|
419
|
-
const reverseVirtualReferenceMap = /* @__PURE__ */ new Map();
|
|
420
|
-
const virtualDependencies = /* @__PURE__ */ new Map();
|
|
421
|
-
for (const [dep, exports] of depsToOptimize.entries()) {
|
|
422
|
-
const name = dep.replaceAll("/", "-");
|
|
423
|
-
reverseVirtualReferenceMap.set(name, dep);
|
|
424
|
-
const virtualFile = [];
|
|
425
|
-
let exportStringBuilder = [];
|
|
426
|
-
for (const local of exports) {
|
|
427
|
-
if (local === "*") {
|
|
428
|
-
virtualFile.push(`export * from '${dep}';`);
|
|
429
|
-
} else if (local === "default") {
|
|
430
|
-
virtualFile.push(`export { default } from '${dep}';`);
|
|
431
|
-
} else {
|
|
432
|
-
exportStringBuilder.push(local);
|
|
433
|
-
}
|
|
434
|
-
}
|
|
435
|
-
if (exportStringBuilder.length > 0) {
|
|
436
|
-
virtualFile.push(`export { ${exportStringBuilder.join(", ")} } from '${dep}';`);
|
|
437
|
-
}
|
|
438
|
-
virtualDependencies.set(dep, {
|
|
439
|
-
name,
|
|
440
|
-
virtual: virtualFile.join("\n")
|
|
441
|
-
});
|
|
442
|
-
}
|
|
443
|
-
const bundler = await rollup({
|
|
444
|
-
logLevel: process.env.MASTRA_BUNDLER_DEBUG === "true" ? "debug" : "silent",
|
|
445
|
-
input: Array.from(virtualDependencies.entries()).reduce(
|
|
446
|
-
(acc, [dep, virtualDep]) => {
|
|
447
|
-
acc[virtualDep.name] = `#virtual-${dep}`;
|
|
448
|
-
return acc;
|
|
449
|
-
},
|
|
450
|
-
{}
|
|
451
|
-
),
|
|
452
|
-
// this dependency breaks the build, so we need to exclude it
|
|
453
|
-
// TODO actually fix this so we don't need to exclude it
|
|
454
|
-
external: allExternals,
|
|
455
|
-
treeshake: "smallest",
|
|
456
|
-
plugins: [
|
|
457
|
-
virtual(
|
|
458
|
-
Array.from(virtualDependencies.entries()).reduce(
|
|
459
|
-
(acc, [dep, virtualDep]) => {
|
|
460
|
-
acc[`#virtual-${dep}`] = virtualDep.virtual;
|
|
461
|
-
return acc;
|
|
462
|
-
},
|
|
463
|
-
{}
|
|
464
|
-
)
|
|
465
|
-
),
|
|
466
|
-
commonjs2({
|
|
467
|
-
strictRequires: "strict",
|
|
468
|
-
transformMixedEsModules: true,
|
|
469
|
-
ignoreTryCatch: false
|
|
470
|
-
}),
|
|
471
|
-
nodeResolve({
|
|
472
|
-
preferBuiltins: true,
|
|
473
|
-
exportConditions: ["node", "import", "require"],
|
|
474
|
-
mainFields: ["module", "main"]
|
|
475
|
-
}),
|
|
476
|
-
// hono is imported from deployer, so we need to resolve from here instead of the project root
|
|
477
|
-
aliasHono(),
|
|
478
|
-
json()
|
|
479
|
-
].filter(Boolean)
|
|
480
|
-
});
|
|
481
|
-
const { output } = await bundler.write({
|
|
482
|
-
format: "esm",
|
|
483
|
-
dir: outputDir,
|
|
484
|
-
entryFileNames: "[name].mjs",
|
|
485
|
-
chunkFileNames: "[name].mjs",
|
|
486
|
-
hoistTransitiveImports: false
|
|
487
|
-
});
|
|
488
|
-
const moduleResolveMap = {};
|
|
489
|
-
const filteredChunks = output.filter((o) => o.type === "chunk");
|
|
490
|
-
for (const o of filteredChunks.filter((o2) => o2.isEntry || o2.isDynamicEntry)) {
|
|
491
|
-
for (const external of allExternals) {
|
|
492
|
-
const importer = findExternalImporter(o, external, filteredChunks);
|
|
493
|
-
if (importer) {
|
|
494
|
-
const fullPath = join(outputDir, importer.fileName);
|
|
495
|
-
moduleResolveMap[fullPath] = moduleResolveMap[fullPath] || {};
|
|
496
|
-
if (importer.moduleIds.length) {
|
|
497
|
-
moduleResolveMap[fullPath][external] = importer.moduleIds[importer.moduleIds.length - 1]?.startsWith(
|
|
498
|
-
"\0virtual:#virtual"
|
|
499
|
-
) ? importer.moduleIds[importer.moduleIds.length - 2] : importer.moduleIds[importer.moduleIds.length - 1];
|
|
500
|
-
}
|
|
501
|
-
}
|
|
502
|
-
}
|
|
503
|
-
}
|
|
504
|
-
await writeFile(join(outputDir, "module-resolve-map.json"), JSON.stringify(moduleResolveMap, null, 2));
|
|
505
|
-
await bundler.close();
|
|
506
|
-
return { output, reverseVirtualReferenceMap, usedExternals: moduleResolveMap };
|
|
507
|
-
}
|
|
508
|
-
async function validateOutput({
|
|
509
|
-
output,
|
|
510
|
-
reverseVirtualReferenceMap,
|
|
511
|
-
usedExternals,
|
|
512
|
-
outputDir
|
|
513
|
-
}, logger) {
|
|
514
|
-
const result = {
|
|
515
|
-
invalidChunks: /* @__PURE__ */ new Set(),
|
|
516
|
-
dependencies: /* @__PURE__ */ new Map(),
|
|
517
|
-
externalDependencies: /* @__PURE__ */ new Set()
|
|
518
|
-
};
|
|
519
|
-
for (const deps of Object.values(usedExternals)) {
|
|
520
|
-
for (const dep of Object.keys(deps)) {
|
|
521
|
-
result.externalDependencies.add(dep);
|
|
522
|
-
}
|
|
523
|
-
}
|
|
524
|
-
for (const file of output) {
|
|
525
|
-
if (file.type === "asset") {
|
|
526
|
-
continue;
|
|
527
|
-
}
|
|
528
|
-
try {
|
|
529
|
-
logger.debug(`Validating if ${file.fileName} is a valid module.`);
|
|
530
|
-
if (file.isEntry && reverseVirtualReferenceMap.has(file.name)) {
|
|
531
|
-
result.dependencies.set(reverseVirtualReferenceMap.get(file.name), file.fileName);
|
|
532
|
-
}
|
|
533
|
-
if (!file.isDynamicEntry && file.isEntry) {
|
|
534
|
-
await validate(join(outputDir, file.fileName));
|
|
535
|
-
}
|
|
536
|
-
} catch (err) {
|
|
537
|
-
result.invalidChunks.add(file.fileName);
|
|
538
|
-
if (file.isEntry && reverseVirtualReferenceMap.has(file.name)) {
|
|
539
|
-
const reference = reverseVirtualReferenceMap.get(file.name);
|
|
540
|
-
const dep = reference.startsWith("@") ? reference.split("/").slice(0, 2).join("/") : reference.split("/")[0];
|
|
541
|
-
result.externalDependencies.add(dep);
|
|
542
|
-
}
|
|
543
|
-
}
|
|
544
|
-
}
|
|
545
|
-
return result;
|
|
546
|
-
}
|
|
547
|
-
async function analyzeBundle(entries, mastraEntry, outputDir, platform, logger) {
|
|
548
|
-
const depsToOptimize = /* @__PURE__ */ new Map();
|
|
549
|
-
for (const entry of entries) {
|
|
550
|
-
const isVirtualFile = entry.includes("\n") || !existsSync(entry);
|
|
551
|
-
const analyzeResult = await analyze(entry, mastraEntry, isVirtualFile, platform, logger);
|
|
552
|
-
for (const [dep, exports] of analyzeResult.entries()) {
|
|
553
|
-
if (depsToOptimize.has(dep)) {
|
|
554
|
-
const existingExports = depsToOptimize.get(dep);
|
|
555
|
-
depsToOptimize.set(dep, [.../* @__PURE__ */ new Set([...existingExports, ...exports])]);
|
|
556
|
-
} else {
|
|
557
|
-
depsToOptimize.set(dep, exports);
|
|
558
|
-
}
|
|
559
|
-
}
|
|
560
|
-
}
|
|
561
|
-
const customExternals = (await getBundlerOptions(mastraEntry, outputDir))?.externals;
|
|
562
|
-
const { output, reverseVirtualReferenceMap, usedExternals } = await bundleExternals(
|
|
563
|
-
depsToOptimize,
|
|
564
|
-
outputDir,
|
|
565
|
-
logger,
|
|
566
|
-
customExternals
|
|
567
|
-
);
|
|
568
|
-
const result = await validateOutput({ output, reverseVirtualReferenceMap, usedExternals, outputDir }, logger);
|
|
569
|
-
return result;
|
|
570
|
-
}
|
|
571
|
-
|
|
572
|
-
export { aliasHono, analyzeBundle, getBundlerOptions, recursiveRemoveNonReferencedNodes, removeAllOptionsFromMastraExcept };
|