@mastra/deployer 0.0.0-storage-20250225005900
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/.turbo/turbo-build.log +30 -0
- package/CHANGELOG.md +855 -0
- package/LICENSE +44 -0
- package/README.md +159 -0
- package/dist/_tsup-dts-rollup.d.ts +360 -0
- package/dist/build/analyze.d.ts +1 -0
- package/dist/build/analyze.js +2 -0
- package/dist/build/bundler.d.ts +2 -0
- package/dist/build/bundler.js +2 -0
- package/dist/build/index.d.ts +10 -0
- package/dist/build/index.js +5 -0
- package/dist/bundler/index.d.ts +1 -0
- package/dist/bundler/index.js +5 -0
- package/dist/chunk-AXS5WSIK.js +290 -0
- package/dist/chunk-BTUQ6UBB.js +254 -0
- package/dist/chunk-IHPQAB7O.js +113 -0
- package/dist/chunk-KXS4XYEJ.js +251 -0
- package/dist/chunk-SESHBX76.js +267 -0
- package/dist/chunk-YNXJO2XU.js +69 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +131 -0
- package/dist/server/index.d.ts +2 -0
- package/dist/server/index.js +4907 -0
- package/dist/templates/instrumentation-template.js +86 -0
- package/eslint.config.js +6 -0
- package/global.d.ts +0 -0
- package/package.json +84 -0
- package/public/templates/instrumentation-template.js +86 -0
- package/src/build/analyze.ts +271 -0
- package/src/build/babel/fix-libsql.ts +41 -0
- package/src/build/babel/get-deployer.ts +54 -0
- package/src/build/babel/get-telemetry-config.ts +62 -0
- package/src/build/babel/remove-deployer.ts +43 -0
- package/src/build/bundle.ts +139 -0
- package/src/build/bundler.ts +126 -0
- package/src/build/deployer.ts +67 -0
- package/src/build/deps.ts +149 -0
- package/src/build/env.ts +76 -0
- package/src/build/fs.ts +66 -0
- package/src/build/index.ts +7 -0
- package/src/build/isNodeBuiltin.ts +7 -0
- package/src/build/plugins/fix-libsql.ts +69 -0
- package/src/build/plugins/hono-alias.ts +17 -0
- package/src/build/plugins/pino.ts +93 -0
- package/src/build/plugins/remove-deployer.ts +37 -0
- package/src/build/plugins/telemetry-fix.ts +54 -0
- package/src/build/telemetry.ts +76 -0
- package/src/build/utils.ts +12 -0
- package/src/build/watcher.ts +43 -0
- package/src/bundler/index.ts +145 -0
- package/src/deploy/base.ts +30 -0
- package/src/deploy/index.ts +2 -0
- package/src/deploy/log.ts +61 -0
- package/src/index.ts +3 -0
- package/src/server/handlers/agents.ts +210 -0
- package/src/server/handlers/client.ts +36 -0
- package/src/server/handlers/error.ts +29 -0
- package/src/server/handlers/logs.ts +53 -0
- package/src/server/handlers/memory.ts +196 -0
- package/src/server/handlers/prompt.ts +128 -0
- package/src/server/handlers/root.ts +6 -0
- package/src/server/handlers/telemetry.ts +48 -0
- package/src/server/handlers/tools.ts +107 -0
- package/src/server/handlers/utils.ts +15 -0
- package/src/server/handlers/vector.ts +149 -0
- package/src/server/handlers/workflows.ts +104 -0
- package/src/server/index.ts +1355 -0
- package/src/server/openapi.json +434 -0
- package/src/server/openapi.script.js +22 -0
- package/src/server/types.ts +4 -0
- package/src/server/welcome.ts +105 -0
- package/tsconfig.json +5 -0
- package/vitest.config.ts +8 -0
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
import { removeDeployer } from './chunk-YNXJO2XU.js';
|
|
2
|
+
import alias from '@rollup/plugin-alias';
|
|
3
|
+
import json from '@rollup/plugin-json';
|
|
4
|
+
import nodeResolve from '@rollup/plugin-node-resolve';
|
|
5
|
+
import { fileURLToPath } from 'node:url';
|
|
6
|
+
import { rollup } from 'rollup';
|
|
7
|
+
import esbuild from 'rollup-plugin-esbuild';
|
|
8
|
+
import * as babel from '@babel/core';
|
|
9
|
+
import babel__default from '@babel/core';
|
|
10
|
+
import { currentTarget } from '@neon-rs/load';
|
|
11
|
+
import { familySync, GLIBC } from 'detect-libc';
|
|
12
|
+
import { dirname, join, resolve } from 'path';
|
|
13
|
+
import fsExtra from 'fs-extra/esm';
|
|
14
|
+
import { addNamed } from '@babel/helper-module-imports';
|
|
15
|
+
import { platform } from 'process';
|
|
16
|
+
|
|
17
|
+
function rewriteLibsqlImport() {
|
|
18
|
+
const t = babel__default.types;
|
|
19
|
+
let hasReplaced = false;
|
|
20
|
+
return {
|
|
21
|
+
name: "rewrite-libsql-import",
|
|
22
|
+
visitor: {
|
|
23
|
+
FunctionDeclaration(path) {
|
|
24
|
+
if (path.node.id?.name === "requireNative" && !hasReplaced) {
|
|
25
|
+
hasReplaced = true;
|
|
26
|
+
const createRequire = addNamed(path, "createRequire", "module");
|
|
27
|
+
const requireIdentifier = t.identifier("require");
|
|
28
|
+
path.replaceWith(
|
|
29
|
+
t.functionDeclaration(
|
|
30
|
+
t.identifier("requireNative"),
|
|
31
|
+
[],
|
|
32
|
+
t.blockStatement([
|
|
33
|
+
t.variableDeclaration("const", [
|
|
34
|
+
t.variableDeclarator(
|
|
35
|
+
requireIdentifier,
|
|
36
|
+
t.callExpression(createRequire, [
|
|
37
|
+
t.memberExpression(
|
|
38
|
+
t.metaProperty(t.identifier("import"), t.identifier("meta")),
|
|
39
|
+
t.identifier("url")
|
|
40
|
+
)
|
|
41
|
+
])
|
|
42
|
+
)
|
|
43
|
+
]),
|
|
44
|
+
t.returnStatement(t.callExpression(requireIdentifier, [t.stringLiteral("./libsql.node")]))
|
|
45
|
+
])
|
|
46
|
+
)
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// src/build/plugins/fix-libsql.ts
|
|
55
|
+
function libSqlFix() {
|
|
56
|
+
return {
|
|
57
|
+
name: "libSqlFix",
|
|
58
|
+
transform(code, id) {
|
|
59
|
+
if (!id.includes("\\libsql\\index.js") && !id.includes("/libsql/index.js")) {
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
return new Promise((resolve2, reject) => {
|
|
63
|
+
babel.transform(
|
|
64
|
+
code,
|
|
65
|
+
{
|
|
66
|
+
babelrc: false,
|
|
67
|
+
configFile: false,
|
|
68
|
+
filename: id,
|
|
69
|
+
plugins: [rewriteLibsqlImport]
|
|
70
|
+
},
|
|
71
|
+
(err, result) => {
|
|
72
|
+
if (err) {
|
|
73
|
+
return reject(err);
|
|
74
|
+
}
|
|
75
|
+
resolve2({
|
|
76
|
+
code: result.code,
|
|
77
|
+
map: result.map
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
);
|
|
81
|
+
});
|
|
82
|
+
},
|
|
83
|
+
async generateBundle({ file, dir }) {
|
|
84
|
+
if (!file && !dir) {
|
|
85
|
+
throw new Error("No output options were given.");
|
|
86
|
+
}
|
|
87
|
+
const outputDirectory = dir || dirname(file);
|
|
88
|
+
let target = currentTarget();
|
|
89
|
+
if (familySync() == GLIBC) {
|
|
90
|
+
switch (target) {
|
|
91
|
+
case "linux-x64-musl":
|
|
92
|
+
target = "linux-x64-gnu";
|
|
93
|
+
break;
|
|
94
|
+
case "linux-arm64-musl":
|
|
95
|
+
target = "linux-arm64-gnu";
|
|
96
|
+
break;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
const fileToCopy = await this.resolve(`@libsql/${target}/index.node`);
|
|
100
|
+
if (fileToCopy) {
|
|
101
|
+
await fsExtra.copy(fileToCopy.id, join(outputDirectory, "libsql.node"));
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
function getTelemetryMachineFile() {
|
|
107
|
+
switch (platform) {
|
|
108
|
+
case "darwin":
|
|
109
|
+
return "getMachineId-darwin";
|
|
110
|
+
case "linux":
|
|
111
|
+
return "getMachineId-linux";
|
|
112
|
+
case "freebsd":
|
|
113
|
+
return "getMachineId-bsd";
|
|
114
|
+
case "win32":
|
|
115
|
+
return "getMachineId-win";
|
|
116
|
+
default:
|
|
117
|
+
return "getMachineId-unsupported";
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
function telemetryFix() {
|
|
121
|
+
return {
|
|
122
|
+
name: "telemetry-fix",
|
|
123
|
+
transform(code, id) {
|
|
124
|
+
if (id.includes("require-in-the-middle")) {
|
|
125
|
+
return code.replace(
|
|
126
|
+
`const path = require('path')`,
|
|
127
|
+
`const path = require('path');
|
|
128
|
+
const { createRequire } = require('module');
|
|
129
|
+
const realRequire = createRequire(import.meta.url)`
|
|
130
|
+
).replaceAll(`require.resolve`, `realRequire.resolve`);
|
|
131
|
+
}
|
|
132
|
+
},
|
|
133
|
+
resolveId(id, importer) {
|
|
134
|
+
if (id === "./machine-id/getMachineId" && importer) {
|
|
135
|
+
return { id: resolve(dirname(importer), `./machine-id/${getTelemetryMachineFile()}.js`) };
|
|
136
|
+
}
|
|
137
|
+
if (id === "formdata-node") {
|
|
138
|
+
return { id: "formdata-node", external: false };
|
|
139
|
+
}
|
|
140
|
+
},
|
|
141
|
+
load(id) {
|
|
142
|
+
if (id.startsWith("formdata-node")) {
|
|
143
|
+
return "export default {};";
|
|
144
|
+
}
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// src/build/bundler.ts
|
|
151
|
+
async function getInputOptions(entryFile, analyzedBundleInfo, platform2) {
|
|
152
|
+
let nodeResolvePlugin = platform2 === "node" ? nodeResolve({
|
|
153
|
+
preferBuiltins: true,
|
|
154
|
+
exportConditions: ["node", "import", "require"],
|
|
155
|
+
mainFields: ["module", "main"]
|
|
156
|
+
}) : nodeResolve({
|
|
157
|
+
preferBuiltins: false,
|
|
158
|
+
exportConditions: ["browser", "import", "require"],
|
|
159
|
+
mainFields: ["module", "main"],
|
|
160
|
+
browser: true
|
|
161
|
+
});
|
|
162
|
+
return {
|
|
163
|
+
logLevel: process.env.MASTRA_BUNDLER_DEBUG === "true" ? "debug" : "silent",
|
|
164
|
+
treeshake: true,
|
|
165
|
+
preserveSymlinks: true,
|
|
166
|
+
external: Array.from(analyzedBundleInfo.externalDependencies).concat(["@mastra/core/hooks"]),
|
|
167
|
+
plugins: [
|
|
168
|
+
telemetryFix(),
|
|
169
|
+
libSqlFix(),
|
|
170
|
+
{
|
|
171
|
+
name: "alias-optimized-deps",
|
|
172
|
+
// @ts-ignore
|
|
173
|
+
resolveId(id) {
|
|
174
|
+
if (!analyzedBundleInfo.dependencies.has(id)) {
|
|
175
|
+
return null;
|
|
176
|
+
}
|
|
177
|
+
const isInvalidChunk = analyzedBundleInfo.invalidChunks.has(analyzedBundleInfo.dependencies.get(id));
|
|
178
|
+
if (isInvalidChunk) {
|
|
179
|
+
return {
|
|
180
|
+
id,
|
|
181
|
+
external: true
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
return {
|
|
185
|
+
id: ".mastra/.build/" + analyzedBundleInfo.dependencies.get(id),
|
|
186
|
+
external: false
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
},
|
|
190
|
+
alias({
|
|
191
|
+
entries: [
|
|
192
|
+
{
|
|
193
|
+
find: /^\#server$/,
|
|
194
|
+
replacement: fileURLToPath(import.meta.resolve("@mastra/deployer/server")).replaceAll("\\", "/")
|
|
195
|
+
},
|
|
196
|
+
{ find: /^\#mastra$/, replacement: entryFile.replaceAll("\\", "/") }
|
|
197
|
+
]
|
|
198
|
+
}),
|
|
199
|
+
nodeResolvePlugin,
|
|
200
|
+
// for debugging
|
|
201
|
+
// {
|
|
202
|
+
// name: 'logger',
|
|
203
|
+
// //@ts-ignore
|
|
204
|
+
// resolveId(id, ...args) {
|
|
205
|
+
// console.log({ id, args });
|
|
206
|
+
// },
|
|
207
|
+
// // @ts-ignore
|
|
208
|
+
// transform(code, id) {
|
|
209
|
+
// if (code.includes('class Duplexify ')) {
|
|
210
|
+
// console.log({ duplex: id });
|
|
211
|
+
// }
|
|
212
|
+
// },
|
|
213
|
+
// },
|
|
214
|
+
json(),
|
|
215
|
+
esbuild({
|
|
216
|
+
target: "node20",
|
|
217
|
+
platform: platform2,
|
|
218
|
+
minify: false,
|
|
219
|
+
define: {
|
|
220
|
+
"process.env.NODE_ENV": JSON.stringify("production")
|
|
221
|
+
}
|
|
222
|
+
}),
|
|
223
|
+
removeDeployer(entryFile),
|
|
224
|
+
// treeshake unused imports
|
|
225
|
+
esbuild({
|
|
226
|
+
include: entryFile,
|
|
227
|
+
target: "node20",
|
|
228
|
+
platform: platform2,
|
|
229
|
+
minify: false
|
|
230
|
+
})
|
|
231
|
+
].filter(Boolean)
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
async function createBundler(inputOptions, outputOptions) {
|
|
235
|
+
const bundler = await rollup(inputOptions);
|
|
236
|
+
return {
|
|
237
|
+
write: () => {
|
|
238
|
+
return bundler.write({
|
|
239
|
+
...outputOptions,
|
|
240
|
+
format: "esm",
|
|
241
|
+
entryFileNames: "[name].mjs",
|
|
242
|
+
chunkFileNames: "[name].mjs"
|
|
243
|
+
});
|
|
244
|
+
},
|
|
245
|
+
close: () => {
|
|
246
|
+
return bundler.close();
|
|
247
|
+
}
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
export { createBundler, getInputOptions, libSqlFix, telemetryFix };
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import { removeDeployer } from './chunk-YNXJO2XU.js';
|
|
2
|
+
import commonjs 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 'node:fs';
|
|
7
|
+
import { fileURLToPath } from 'node:url';
|
|
8
|
+
import { rollup } from 'rollup';
|
|
9
|
+
import esbuild from 'rollup-plugin-esbuild';
|
|
10
|
+
import { builtinModules } from 'node:module';
|
|
11
|
+
|
|
12
|
+
function isNodeBuiltin(dep) {
|
|
13
|
+
const [pkg] = dep.split("/");
|
|
14
|
+
return dep.startsWith("node:") || builtinModules.includes(dep) || builtinModules.includes(pkg);
|
|
15
|
+
}
|
|
16
|
+
function aliasHono() {
|
|
17
|
+
return {
|
|
18
|
+
name: "hono-alias",
|
|
19
|
+
resolveId(id) {
|
|
20
|
+
if (!id.startsWith("@hono/") && !id.startsWith("hono/") && id !== "hono" && id !== "hono-openapi") {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
const path = import.meta.resolve(id);
|
|
24
|
+
return fileURLToPath(path);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// src/build/plugins/pino.ts
|
|
30
|
+
function pino() {
|
|
31
|
+
const workerFiles = [
|
|
32
|
+
{
|
|
33
|
+
id: "thread-stream-worker",
|
|
34
|
+
file: "pino-thread-stream-worker"
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
id: "pino-worker",
|
|
38
|
+
file: "pino-worker"
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
id: "pino/file",
|
|
42
|
+
file: "pino-file"
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
id: "pino-pretty",
|
|
46
|
+
file: "pino-pretty"
|
|
47
|
+
}
|
|
48
|
+
];
|
|
49
|
+
const fileReferences = /* @__PURE__ */ new Map();
|
|
50
|
+
return {
|
|
51
|
+
name: "rollup-plugin-pino",
|
|
52
|
+
async resolveId(id, importee) {
|
|
53
|
+
if (id === "pino") {
|
|
54
|
+
const resolvedPino = await this.resolve(id, importee);
|
|
55
|
+
if (resolvedPino) {
|
|
56
|
+
await Promise.all(
|
|
57
|
+
workerFiles.map(async (file) => {
|
|
58
|
+
const resolvedEntry = await this.resolve(file.id, resolvedPino.id);
|
|
59
|
+
if (!resolvedEntry) {
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
const reference = this.emitFile({
|
|
63
|
+
type: "chunk",
|
|
64
|
+
id: resolvedEntry.id,
|
|
65
|
+
name: `${file.file}`
|
|
66
|
+
});
|
|
67
|
+
fileReferences.set(file.id, reference);
|
|
68
|
+
})
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
renderChunk(code, chunk) {
|
|
74
|
+
if (chunk.type === "chunk" && chunk.isEntry && fileReferences.size && chunk.name === "index") {
|
|
75
|
+
const importRegex = /^(?:import(?:["'\s]*[\w*${}\n\r\t, ]+from\s*)?["'\s].+[;"'\s]*)$/gm;
|
|
76
|
+
const codeToInject = `globalThis.__bundlerPathsOverrides = {
|
|
77
|
+
${Array.from(fileReferences.entries()).map(([key, file]) => {
|
|
78
|
+
return '"' + key + '": import.meta.ROLLUP_FILE_URL_' + file;
|
|
79
|
+
}).join(",\n")}
|
|
80
|
+
};`;
|
|
81
|
+
const matches = Array.from(code.matchAll(importRegex));
|
|
82
|
+
if (matches.length > 0) {
|
|
83
|
+
const lastImport = matches[matches.length - 1];
|
|
84
|
+
const lastImportEnd = lastImport.index + lastImport[0].length;
|
|
85
|
+
const newCode = code.slice(0, lastImportEnd) + "\n\n" + codeToInject + "\n\n" + code.slice(lastImportEnd);
|
|
86
|
+
return {
|
|
87
|
+
code: newCode,
|
|
88
|
+
map: null
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
return {
|
|
92
|
+
code: `${codeToInject}
|
|
93
|
+
|
|
94
|
+
${code}`,
|
|
95
|
+
map: null
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// src/build/analyze.ts
|
|
103
|
+
async function analyze(entry, mastraEntry, isVirtualFile, platform, logger) {
|
|
104
|
+
logger.info("Analyzing dependencies...");
|
|
105
|
+
let virtualPlugin = null;
|
|
106
|
+
if (isVirtualFile) {
|
|
107
|
+
virtualPlugin = virtual({
|
|
108
|
+
"#entry": entry
|
|
109
|
+
});
|
|
110
|
+
entry = "#entry";
|
|
111
|
+
}
|
|
112
|
+
const optimizerBundler = await rollup({
|
|
113
|
+
logLevel: process.env.MASTRA_BUNDLER_DEBUG === "true" ? "debug" : "silent",
|
|
114
|
+
input: isVirtualFile ? "#entry" : entry,
|
|
115
|
+
treeshake: true,
|
|
116
|
+
preserveSymlinks: true,
|
|
117
|
+
plugins: [
|
|
118
|
+
virtualPlugin,
|
|
119
|
+
{
|
|
120
|
+
name: "custom-alias-resolver",
|
|
121
|
+
resolveId(id) {
|
|
122
|
+
if (id === "#server") {
|
|
123
|
+
return fileURLToPath(import.meta.resolve("@mastra/deployer/server")).replaceAll("\\", "/");
|
|
124
|
+
}
|
|
125
|
+
if (id === "#mastra") {
|
|
126
|
+
return mastraEntry;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
},
|
|
130
|
+
json(),
|
|
131
|
+
esbuild({
|
|
132
|
+
target: "node20",
|
|
133
|
+
platform,
|
|
134
|
+
minify: false
|
|
135
|
+
}),
|
|
136
|
+
removeDeployer(mastraEntry),
|
|
137
|
+
esbuild({
|
|
138
|
+
target: "node20",
|
|
139
|
+
platform,
|
|
140
|
+
minify: false
|
|
141
|
+
})
|
|
142
|
+
].filter(Boolean)
|
|
143
|
+
});
|
|
144
|
+
const { output } = await optimizerBundler.generate({
|
|
145
|
+
format: "esm",
|
|
146
|
+
inlineDynamicImports: true
|
|
147
|
+
});
|
|
148
|
+
await optimizerBundler.close();
|
|
149
|
+
const depsToOptimize = new Map(Object.entries(output[0].importedBindings));
|
|
150
|
+
for (const dep of depsToOptimize.keys()) {
|
|
151
|
+
if (isNodeBuiltin(dep)) {
|
|
152
|
+
depsToOptimize.delete(dep);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return depsToOptimize;
|
|
156
|
+
}
|
|
157
|
+
async function bundleExternals(depsToOptimize, outputDir, logger) {
|
|
158
|
+
logger.info("Optimizing dependencies...");
|
|
159
|
+
logger.debug(
|
|
160
|
+
`${Array.from(depsToOptimize.keys()).map((key) => `- ${key}`).join("\n")}`
|
|
161
|
+
);
|
|
162
|
+
const reverseVirtualReferenceMap = /* @__PURE__ */ new Map();
|
|
163
|
+
const virtualDependencies = /* @__PURE__ */ new Map();
|
|
164
|
+
for (const [dep, exports] of depsToOptimize.entries()) {
|
|
165
|
+
const name = dep.replaceAll("/", "-");
|
|
166
|
+
reverseVirtualReferenceMap.set(name, dep);
|
|
167
|
+
const virtualFile = [];
|
|
168
|
+
let exportStringBuilder = [];
|
|
169
|
+
for (const local of exports) {
|
|
170
|
+
if (local === "*") {
|
|
171
|
+
virtualFile.push(`export * from '${dep}';`);
|
|
172
|
+
} else {
|
|
173
|
+
exportStringBuilder.push(local);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
if (exportStringBuilder.length > 0) {
|
|
177
|
+
virtualFile.push(`export { ${exportStringBuilder.join(", ")} } from '${dep}';`);
|
|
178
|
+
}
|
|
179
|
+
virtualDependencies.set(dep, {
|
|
180
|
+
name,
|
|
181
|
+
virtual: virtualFile.join("\n")
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
const bundler = await rollup({
|
|
185
|
+
logLevel: process.env.MASTRA_BUNDLER_DEBUG === "true" ? "debug" : "silent",
|
|
186
|
+
input: Array.from(virtualDependencies.entries()).reduce(
|
|
187
|
+
(acc, [dep, virtualDep]) => {
|
|
188
|
+
acc[virtualDep.name] = `#virtual-${dep}`;
|
|
189
|
+
return acc;
|
|
190
|
+
},
|
|
191
|
+
{}
|
|
192
|
+
),
|
|
193
|
+
// this dependency breaks the build, so we need to exclude it
|
|
194
|
+
// TODO actually fix this so we don't need to exclude it
|
|
195
|
+
external: ["jsdom"],
|
|
196
|
+
treeshake: "smallest",
|
|
197
|
+
preserveSymlinks: true,
|
|
198
|
+
plugins: [
|
|
199
|
+
virtual(
|
|
200
|
+
Array.from(virtualDependencies.entries()).reduce(
|
|
201
|
+
(acc, [dep, virtualDep]) => {
|
|
202
|
+
acc[`#virtual-${dep}`] = virtualDep.virtual;
|
|
203
|
+
return acc;
|
|
204
|
+
},
|
|
205
|
+
{}
|
|
206
|
+
)
|
|
207
|
+
),
|
|
208
|
+
pino(),
|
|
209
|
+
commonjs({
|
|
210
|
+
strictRequires: "strict",
|
|
211
|
+
transformMixedEsModules: true,
|
|
212
|
+
ignoreTryCatch: false
|
|
213
|
+
}),
|
|
214
|
+
nodeResolve({
|
|
215
|
+
preferBuiltins: true,
|
|
216
|
+
exportConditions: ["node", "import", "require"],
|
|
217
|
+
mainFields: ["module", "main"]
|
|
218
|
+
}),
|
|
219
|
+
// hono is imported from deployer, so we need to resolve from here instead of the project root
|
|
220
|
+
aliasHono(),
|
|
221
|
+
json()
|
|
222
|
+
].filter(Boolean)
|
|
223
|
+
});
|
|
224
|
+
const { output } = await bundler.write({
|
|
225
|
+
format: "esm",
|
|
226
|
+
dir: outputDir,
|
|
227
|
+
entryFileNames: "[name].mjs",
|
|
228
|
+
chunkFileNames: "[name].mjs"
|
|
229
|
+
});
|
|
230
|
+
await bundler.close();
|
|
231
|
+
return { output, reverseVirtualReferenceMap };
|
|
232
|
+
}
|
|
233
|
+
async function validateOutput(output, reverseVirtualReferenceMap, outputDir, logger) {
|
|
234
|
+
const result = {
|
|
235
|
+
invalidChunks: /* @__PURE__ */ new Set(),
|
|
236
|
+
dependencies: /* @__PURE__ */ new Map(),
|
|
237
|
+
externalDependencies: /* @__PURE__ */ new Set()
|
|
238
|
+
};
|
|
239
|
+
for (const file of output) {
|
|
240
|
+
if (file.type === "asset") {
|
|
241
|
+
continue;
|
|
242
|
+
}
|
|
243
|
+
try {
|
|
244
|
+
logger.debug(`Validating if ${file.fileName} is a valid module.`);
|
|
245
|
+
if (file.isEntry && reverseVirtualReferenceMap.has(file.name)) {
|
|
246
|
+
result.dependencies.set(reverseVirtualReferenceMap.get(file.name), file.fileName);
|
|
247
|
+
}
|
|
248
|
+
await import(`file:${outputDir}/${file.fileName}`);
|
|
249
|
+
} catch (err) {
|
|
250
|
+
result.invalidChunks.add(file.fileName);
|
|
251
|
+
if (file.isEntry && reverseVirtualReferenceMap.has(file.name)) {
|
|
252
|
+
result.externalDependencies.add(reverseVirtualReferenceMap.get(file.name));
|
|
253
|
+
}
|
|
254
|
+
console.log(file.fileName, file.isEntry, file.isDynamicEntry, err);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
return result;
|
|
258
|
+
}
|
|
259
|
+
async function analyzeBundle(entry, mastraEntry, outputDir, platform, logger) {
|
|
260
|
+
const isVirtualFile = entry.includes("\n") || !existsSync(entry);
|
|
261
|
+
const depsToOptimize = await analyze(entry, mastraEntry, isVirtualFile, platform, logger);
|
|
262
|
+
const { output, reverseVirtualReferenceMap } = await bundleExternals(depsToOptimize, outputDir, logger);
|
|
263
|
+
const result = await validateOutput(output, reverseVirtualReferenceMap, outputDir, logger);
|
|
264
|
+
return result;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
export { aliasHono, analyzeBundle };
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import * as babel from '@babel/core';
|
|
2
|
+
import babel__default from '@babel/core';
|
|
3
|
+
|
|
4
|
+
// src/build/plugins/remove-deployer.ts
|
|
5
|
+
function removeDeployer() {
|
|
6
|
+
const t = babel__default.types;
|
|
7
|
+
let mastraClass = null;
|
|
8
|
+
return {
|
|
9
|
+
name: "remove-deployer",
|
|
10
|
+
visitor: {
|
|
11
|
+
ImportDeclaration(path) {
|
|
12
|
+
if ((path.node.source.value === "@mastra/core" || path.node.source.value === "@mastra/core/mastra") && path.node.specifiers) {
|
|
13
|
+
const mastraObj = path.node.specifiers.find(
|
|
14
|
+
(p) => t.isImportSpecifier(p) && t.isIdentifier(p.imported) && p.imported.name === "Mastra"
|
|
15
|
+
);
|
|
16
|
+
if (mastraObj?.local?.name) {
|
|
17
|
+
mastraClass = mastraObj.local.name;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
NewExpression(path, state) {
|
|
22
|
+
if (mastraClass && t.isIdentifier(path.node.callee) && path.node.callee.name === mastraClass && !state.hasReplaced) {
|
|
23
|
+
state.hasReplaced = true;
|
|
24
|
+
const newMastraObj = t.cloneNode(path.node);
|
|
25
|
+
if (t.isObjectExpression(newMastraObj.arguments[0]) && newMastraObj.arguments[0].properties?.[0]) {
|
|
26
|
+
newMastraObj.arguments[0].properties = newMastraObj.arguments[0].properties.filter(
|
|
27
|
+
(prop) => t.isObjectProperty(prop) && t.isIdentifier(prop.key) && prop.key.name !== "deployer"
|
|
28
|
+
);
|
|
29
|
+
path.replaceWith(newMastraObj);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// src/build/plugins/remove-deployer.ts
|
|
38
|
+
function removeDeployer2(mastraEntry) {
|
|
39
|
+
return {
|
|
40
|
+
name: "remove-deployer",
|
|
41
|
+
transform(code, id) {
|
|
42
|
+
if (id !== mastraEntry) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
return new Promise((resolve, reject) => {
|
|
46
|
+
babel.transform(
|
|
47
|
+
code,
|
|
48
|
+
{
|
|
49
|
+
babelrc: false,
|
|
50
|
+
configFile: false,
|
|
51
|
+
filename: id,
|
|
52
|
+
plugins: [removeDeployer]
|
|
53
|
+
},
|
|
54
|
+
(err, result) => {
|
|
55
|
+
if (err) {
|
|
56
|
+
return reject(err);
|
|
57
|
+
}
|
|
58
|
+
resolve({
|
|
59
|
+
code: result.code,
|
|
60
|
+
map: result.map
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
);
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export { removeDeployer2 as removeDeployer };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { Deps } from './_tsup-dts-rollup.js';
|
|
2
|
+
export { FileService } from './_tsup-dts-rollup.js';
|
|
3
|
+
export { getDeployer } from './_tsup-dts-rollup.js';
|
|
4
|
+
export { Deployer } from './_tsup-dts-rollup.js';
|
|
5
|
+
export { createChildProcessLogger } from './_tsup-dts-rollup.js';
|
|
6
|
+
export { createPinoStream } from './_tsup-dts-rollup.js';
|