@vite-hub/sandbox 0.0.0 → 0.0.1
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/README.md +56 -1
- package/dist/_shared-CJ2_pS4E.js +53 -0
- package/dist/cloudflare-retry-Cfra7MvS.js +21 -0
- package/dist/common-CS4FcWrh.d.ts +369 -0
- package/dist/index-B4j3Mvzr.d.ts +146 -0
- package/dist/index.d.ts +74 -0
- package/dist/index.js +785 -0
- package/dist/integration-wkNE8jMO.js +2032 -0
- package/dist/module-types-BomzaqnM.js +7 -0
- package/dist/module-types-CUcY8iuI.d.ts +88 -0
- package/dist/nitro.d.ts +19 -0
- package/dist/nitro.js +488 -0
- package/dist/provider-detection-IbezCeoR.js +25 -0
- package/dist/runtime/empty-registry.d.ts +6 -0
- package/dist/runtime/empty-registry.js +20 -0
- package/dist/runtime/nitro-plugin.d.ts +4 -0
- package/dist/runtime/nitro-plugin.js +8 -0
- package/dist/runtime/provider-loader.d.ts +17 -0
- package/dist/runtime/provider-loader.js +21 -0
- package/dist/runtime/providers/cloudflare.d.ts +35 -0
- package/dist/runtime/providers/cloudflare.js +22 -0
- package/dist/runtime/providers/vercel.d.ts +21 -0
- package/dist/runtime/providers/vercel.js +65 -0
- package/dist/runtime/state.d.ts +17 -0
- package/dist/runtime/state.js +21 -0
- package/dist/sandbox/providers/cloudflare.d.ts +7 -0
- package/dist/sandbox/providers/cloudflare.js +339 -0
- package/dist/sandbox/providers/vercel.d.ts +17 -0
- package/dist/sandbox/providers/vercel.js +538 -0
- package/dist/shared-pqTHYWg1.d.ts +8 -0
- package/dist/utils-V7ODsDW_.js +44 -0
- package/dist/vercel-runtime-BPzvh_6V.js +11 -0
- package/dist/vite.d.ts +40 -0
- package/dist/vite.js +441 -0
- package/package.json +103 -5
package/dist/vite.js
ADDED
|
@@ -0,0 +1,441 @@
|
|
|
1
|
+
import { C as resolveFeatureRuntimePath, E as createDiscoveredDefinitionCompiler, O as dedupeServerImports, c as createFeatureNitroBridge, n as sandboxFeatureEngine, o as buildFeatureViteContext, r as getViteHubFeatureServerImports, s as createFeatureEngine } from "./integration-wkNE8jMO.js";
|
|
2
|
+
import { resolve } from "pathe";
|
|
3
|
+
import { existsSync } from "node:fs";
|
|
4
|
+
import { cp, mkdir } from "node:fs/promises";
|
|
5
|
+
import { Readable } from "node:stream";
|
|
6
|
+
//#region ../internal/src/build/vite.ts
|
|
7
|
+
function createNoExternalMerger(packageName) {
|
|
8
|
+
return (current) => {
|
|
9
|
+
if (current === true) return true;
|
|
10
|
+
if (!current) return [packageName];
|
|
11
|
+
const values = Array.isArray(current) ? current : [current];
|
|
12
|
+
return values.includes(packageName) ? values : [...values, packageName];
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
function isServerEnvironment(name, config) {
|
|
16
|
+
return name === "ssr" || config.consumer === "server";
|
|
17
|
+
}
|
|
18
|
+
const VITEHUB_MODES = {
|
|
19
|
+
e2e: "e2e",
|
|
20
|
+
blob: "blob",
|
|
21
|
+
chat: "chat",
|
|
22
|
+
db: "db",
|
|
23
|
+
env: "env",
|
|
24
|
+
kv: "kv",
|
|
25
|
+
queue: "queue",
|
|
26
|
+
schedule: "schedule",
|
|
27
|
+
sandbox: "sandbox",
|
|
28
|
+
workspace: "workspace",
|
|
29
|
+
workflow: "workflow"
|
|
30
|
+
};
|
|
31
|
+
function isViteCli(argv) {
|
|
32
|
+
return argv.some((arg) => /(?:^|[/\\])vite(?:\.[cm]?js)?$/.test(arg) || arg === "vite");
|
|
33
|
+
}
|
|
34
|
+
function getViteCliMode(argv = process.argv) {
|
|
35
|
+
if (!isViteCli(argv)) return;
|
|
36
|
+
const modes = new Set(Object.values(VITEHUB_MODES));
|
|
37
|
+
for (let index = 0; index < argv.length; index += 1) {
|
|
38
|
+
const arg = argv[index];
|
|
39
|
+
const mode = arg === "--mode" || arg === "-m" ? argv[index + 1] : arg.startsWith("--mode=") ? arg.slice(7) : void 0;
|
|
40
|
+
if (mode && modes.has(mode)) return mode;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
function getViteMode(env = process.env) {
|
|
44
|
+
return env["VITEHUB_VITE_MODE"] ?? getViteCliMode();
|
|
45
|
+
}
|
|
46
|
+
//#endregion
|
|
47
|
+
//#region src/internal/shared/vite/config-merge.ts
|
|
48
|
+
function mergeUserConfigs(base, extra) {
|
|
49
|
+
if (!extra) return base;
|
|
50
|
+
return {
|
|
51
|
+
...extra,
|
|
52
|
+
...base,
|
|
53
|
+
define: {
|
|
54
|
+
...extra.define,
|
|
55
|
+
...base.define
|
|
56
|
+
},
|
|
57
|
+
resolve: {
|
|
58
|
+
...extra.resolve,
|
|
59
|
+
...base.resolve,
|
|
60
|
+
alias: {
|
|
61
|
+
...typeof extra.resolve?.alias === "object" && !Array.isArray(extra.resolve.alias) ? extra.resolve.alias : {},
|
|
62
|
+
...typeof base.resolve?.alias === "object" && !Array.isArray(base.resolve.alias) ? base.resolve.alias : {}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
//#endregion
|
|
68
|
+
//#region src/internal/shared/vite/nitro-integration.ts
|
|
69
|
+
let nitroBuilderPromise;
|
|
70
|
+
async function loadNitroBuilder() {
|
|
71
|
+
nitroBuilderPromise ||= import("nitro/builder").catch((error) => {
|
|
72
|
+
throw new Error("[vitehub] Vite projects with `nitro.handlers` require `nitro` to be installed.", { cause: error });
|
|
73
|
+
});
|
|
74
|
+
return await nitroBuilderPromise;
|
|
75
|
+
}
|
|
76
|
+
function mergeRuntimeConfig(stateRuntimeConfig, nitroRuntimeConfig, configKey) {
|
|
77
|
+
const stateFeatureConfig = stateRuntimeConfig[configKey];
|
|
78
|
+
const nitroFeatureConfig = nitroRuntimeConfig?.[configKey];
|
|
79
|
+
return {
|
|
80
|
+
...stateRuntimeConfig,
|
|
81
|
+
...nitroRuntimeConfig,
|
|
82
|
+
...stateFeatureConfig && typeof stateFeatureConfig === "object" && !Array.isArray(stateFeatureConfig) && nitroFeatureConfig && typeof nitroFeatureConfig === "object" && !Array.isArray(nitroFeatureConfig) ? { [configKey]: {
|
|
83
|
+
...stateFeatureConfig,
|
|
84
|
+
...nitroFeatureConfig
|
|
85
|
+
} } : {}
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
function resolveNitroOptions(rootDir, configKey, state, nitro, featureNitroModule, dev) {
|
|
89
|
+
const distDir = resolve(rootDir, typeof state.extraConfig?.build?.outDir === "string" ? state.extraConfig.build.outDir : "dist");
|
|
90
|
+
const srcDir = existsSync(resolve(rootDir, "src")) ? "src" : void 0;
|
|
91
|
+
const handlers = Array.isArray(nitro?.handlers) ? nitro.handlers : [];
|
|
92
|
+
const modules = [...featureNitroModule ? [featureNitroModule] : [], ...(nitro?.modules ?? []).filter(Boolean)];
|
|
93
|
+
return {
|
|
94
|
+
[configKey]: state.config,
|
|
95
|
+
alias: nitro?.alias,
|
|
96
|
+
compatibilityDate: nitro?.compatibilityDate,
|
|
97
|
+
dev,
|
|
98
|
+
handlers,
|
|
99
|
+
imports: nitro?.imports,
|
|
100
|
+
modules,
|
|
101
|
+
plugins: nitro?.plugins,
|
|
102
|
+
preset: process.env.NITRO_PRESET || nitro?.preset || "node-server",
|
|
103
|
+
publicAssets: [{
|
|
104
|
+
baseURL: "/",
|
|
105
|
+
dir: distDir,
|
|
106
|
+
fallthrough: true,
|
|
107
|
+
maxAge: 0
|
|
108
|
+
}, ...(nitro?.publicAssets ?? []).map((asset) => ({
|
|
109
|
+
...asset,
|
|
110
|
+
maxAge: asset.maxAge ?? 0
|
|
111
|
+
}))],
|
|
112
|
+
rootDir,
|
|
113
|
+
runtimeConfig: mergeRuntimeConfig(state.runtimeConfig, nitro?.runtimeConfig, configKey),
|
|
114
|
+
srcDir
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
function isFeatureDefinitionChange(feature, file) {
|
|
118
|
+
if (feature === "sandbox") return /\.sandbox\.[cm]?[jt]s$/i.test(file);
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
function createNitroRouteMatcher(handlers) {
|
|
122
|
+
const entries = handlers.map((handler) => {
|
|
123
|
+
const source = handler.route.replace(/[.*+?^${}()|[\]\\]/g, "\\$&").replace(/\\\[\.{3}[^/\\]+\]/g, ".+").replace(/\\\[[^/\\]+\]/g, "[^/]+").replace(/:([^/]+)/g, "[^/]+");
|
|
124
|
+
return {
|
|
125
|
+
method: handler.method?.toUpperCase(),
|
|
126
|
+
pattern: new RegExp(`^${source}/?$`)
|
|
127
|
+
};
|
|
128
|
+
});
|
|
129
|
+
return (method, url) => {
|
|
130
|
+
const pathname = new URL(url, "http://vitehub.local").pathname;
|
|
131
|
+
return entries.some((entry) => (!entry.method || entry.method === method) && entry.pattern.test(pathname));
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
async function copyViteBuildOutputToNitroPublicDir(nitro, rootDir, outDir) {
|
|
135
|
+
const distDir = resolve(rootDir, outDir || "dist");
|
|
136
|
+
if (!existsSync(distDir)) return;
|
|
137
|
+
await mkdir(nitro.options.output.publicDir, { recursive: true });
|
|
138
|
+
await cp(distDir, nitro.options.output.publicDir, {
|
|
139
|
+
force: true,
|
|
140
|
+
recursive: true
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
async function sendFetchResponse(res, response) {
|
|
144
|
+
res.statusCode = response.status;
|
|
145
|
+
res.statusMessage = response.statusText;
|
|
146
|
+
for (const [name, value] of response.headers) res.setHeader(name, value);
|
|
147
|
+
if (!response.body) {
|
|
148
|
+
res.end();
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
await new Promise((resolve, reject) => {
|
|
152
|
+
Readable.fromWeb(response.body).on("error", reject).pipe(res).on("finish", resolve).on("error", reject);
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
//#endregion
|
|
156
|
+
//#region src/internal/shared/vite/virtual-modules.ts
|
|
157
|
+
function createStateModuleId(feature) {
|
|
158
|
+
return `#vitehub/${feature}`;
|
|
159
|
+
}
|
|
160
|
+
function createResolvedVirtualModuleId(feature, key) {
|
|
161
|
+
return `\0vitehub:${feature}:${key}`;
|
|
162
|
+
}
|
|
163
|
+
function createStateModuleContents(feature, configKey, state) {
|
|
164
|
+
const payload = {
|
|
165
|
+
feature,
|
|
166
|
+
configKey,
|
|
167
|
+
config: state.config,
|
|
168
|
+
runtimeConfig: state.runtimeConfig,
|
|
169
|
+
hosting: state.hosting,
|
|
170
|
+
rootDir: state.rootDir,
|
|
171
|
+
mode: state.mode,
|
|
172
|
+
command: state.command
|
|
173
|
+
};
|
|
174
|
+
return [
|
|
175
|
+
`const state = ${JSON.stringify(payload, null, 2)}`,
|
|
176
|
+
"export const feature = state.feature",
|
|
177
|
+
"export const configKey = state.configKey",
|
|
178
|
+
"export const config = state.config",
|
|
179
|
+
"export const runtimeConfig = state.runtimeConfig",
|
|
180
|
+
"export const hosting = state.hosting",
|
|
181
|
+
"export const rootDir = state.rootDir",
|
|
182
|
+
"export const mode = state.mode",
|
|
183
|
+
"export const command = state.command",
|
|
184
|
+
"export default state",
|
|
185
|
+
""
|
|
186
|
+
].join("\n");
|
|
187
|
+
}
|
|
188
|
+
async function resolveFeatureViteState(engine, userConfig, env) {
|
|
189
|
+
const context = await buildFeatureViteContext(engine, userConfig, env);
|
|
190
|
+
if (!context) return void 0;
|
|
191
|
+
const setupResult = await engine.setupVite?.(context);
|
|
192
|
+
const modules = /* @__PURE__ */ new Map();
|
|
193
|
+
const resolvedIds = /* @__PURE__ */ new Map();
|
|
194
|
+
const stateModuleId = createStateModuleId(engine.feature);
|
|
195
|
+
modules.set(stateModuleId, createStateModuleContents(engine.feature, engine.configKey, context));
|
|
196
|
+
const resolvedStateModuleId = createResolvedVirtualModuleId(engine.feature, "state");
|
|
197
|
+
modules.set(resolvedStateModuleId, modules.get(stateModuleId));
|
|
198
|
+
resolvedIds.set(stateModuleId, resolvedStateModuleId);
|
|
199
|
+
return {
|
|
200
|
+
...context,
|
|
201
|
+
virtualModules: modules,
|
|
202
|
+
resolvedIds,
|
|
203
|
+
extraConfig: setupResult && "config" in setupResult ? setupResult.config : void 0
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
//#endregion
|
|
207
|
+
//#region src/internal/shared/vite.ts
|
|
208
|
+
function normalizeViteEngine(options) {
|
|
209
|
+
if ("readPublicOptions" in options) return options;
|
|
210
|
+
return createFeatureEngine({
|
|
211
|
+
name: options.name,
|
|
212
|
+
feature: options.feature,
|
|
213
|
+
configKey: options.configKey,
|
|
214
|
+
defaultOptions: options.defaultOptions,
|
|
215
|
+
loadDeps: options.loadDeps,
|
|
216
|
+
normalizeOptions: options.normalizeOptions,
|
|
217
|
+
resolveConfig: options.resolveConfig,
|
|
218
|
+
assignRuntimeConfig: options.assignRuntimeConfig,
|
|
219
|
+
readPublicOptions(source) {
|
|
220
|
+
if (source.kind !== "vite") throw new TypeError(`[vitehub] ${options.name} Vite engine cannot read non-Vite options.`);
|
|
221
|
+
return options.readOptions(source.userConfig, source.env);
|
|
222
|
+
},
|
|
223
|
+
setupVite: options.setup,
|
|
224
|
+
setupNitro: options.setupNitro
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
function createFeatureBridgeBundle(options) {
|
|
228
|
+
const engine = normalizeViteEngine(options);
|
|
229
|
+
let state;
|
|
230
|
+
let rawConfig = {};
|
|
231
|
+
let rawEnv = {
|
|
232
|
+
command: "serve",
|
|
233
|
+
mode: "development"
|
|
234
|
+
};
|
|
235
|
+
let nitroDevServer;
|
|
236
|
+
let nitroInstance;
|
|
237
|
+
async function refreshState() {
|
|
238
|
+
state = await resolveFeatureViteState(engine, rawConfig, rawEnv);
|
|
239
|
+
return state;
|
|
240
|
+
}
|
|
241
|
+
const plugin = {
|
|
242
|
+
name: `${engine.name}/vite`,
|
|
243
|
+
enforce: "pre",
|
|
244
|
+
async config(config, env) {
|
|
245
|
+
rawConfig = config;
|
|
246
|
+
rawEnv = env;
|
|
247
|
+
const nextState = await refreshState();
|
|
248
|
+
if (!nextState) return;
|
|
249
|
+
return mergeUserConfigs({}, nextState.extraConfig);
|
|
250
|
+
},
|
|
251
|
+
configEnvironment(name, environment) {
|
|
252
|
+
if (environment.consumer !== "server") return;
|
|
253
|
+
return { define: { [`__VITEHUB_ENVIRONMENT_${engine.feature.toUpperCase()}__`]: JSON.stringify(name) } };
|
|
254
|
+
},
|
|
255
|
+
resolveId(id) {
|
|
256
|
+
if (!state) return;
|
|
257
|
+
if (id.startsWith("\0") && state.virtualModules.has(id)) return id;
|
|
258
|
+
return state.resolvedIds.get(id);
|
|
259
|
+
},
|
|
260
|
+
load(id) {
|
|
261
|
+
if (!state) return;
|
|
262
|
+
if (state.virtualModules.has(id)) return state.virtualModules.get(id);
|
|
263
|
+
},
|
|
264
|
+
async configureServer(server) {
|
|
265
|
+
const nitroConfig = rawConfig.nitro;
|
|
266
|
+
if (!state || !nitroConfig?.handlers?.length) return;
|
|
267
|
+
const nitroBuilder = await loadNitroBuilder();
|
|
268
|
+
let rebuildPromise;
|
|
269
|
+
async function closeNitroDevServer() {
|
|
270
|
+
await nitroDevServer?.close();
|
|
271
|
+
await nitroInstance?.close();
|
|
272
|
+
nitroDevServer = void 0;
|
|
273
|
+
nitroInstance = void 0;
|
|
274
|
+
}
|
|
275
|
+
async function rebuildNitroDevServer() {
|
|
276
|
+
if (!state) return;
|
|
277
|
+
await closeNitroDevServer();
|
|
278
|
+
nitroInstance = await nitroBuilder.createNitro(resolveNitroOptions(state.rootDir, engine.configKey, state, nitroConfig, plugin.nitro, true));
|
|
279
|
+
await nitroBuilder.prepare(nitroInstance);
|
|
280
|
+
nitroDevServer = nitroBuilder.createDevServer(nitroInstance);
|
|
281
|
+
await nitroBuilder.build(nitroInstance);
|
|
282
|
+
}
|
|
283
|
+
function scheduleNitroRebuild(file) {
|
|
284
|
+
if (!isFeatureDefinitionChange(engine.feature, file)) return;
|
|
285
|
+
rebuildPromise ||= rebuildNitroDevServer().catch((error) => server.config.logger.error(error)).finally(() => {
|
|
286
|
+
rebuildPromise = void 0;
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
await rebuildNitroDevServer();
|
|
290
|
+
const matchesNitroRoute = createNitroRouteMatcher(nitroConfig.handlers);
|
|
291
|
+
server.middlewares.use(async (req, res, next) => {
|
|
292
|
+
const method = req.method?.toUpperCase() || "GET";
|
|
293
|
+
const url = req.url || "/";
|
|
294
|
+
if (!matchesNitroRoute(method, url)) {
|
|
295
|
+
next();
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
try {
|
|
299
|
+
if (rebuildPromise) await rebuildPromise;
|
|
300
|
+
if (!nitroDevServer) throw new Error("[vitehub] Nitro dev server is not ready.");
|
|
301
|
+
const request = new Request(new URL(url, "http://vitehub.local"), {
|
|
302
|
+
method,
|
|
303
|
+
headers: new Headers(req.headers),
|
|
304
|
+
body: method === "GET" || method === "HEAD" ? void 0 : req,
|
|
305
|
+
duplex: method === "GET" || method === "HEAD" ? void 0 : "half"
|
|
306
|
+
});
|
|
307
|
+
await sendFetchResponse(res, await nitroDevServer.fetch(request));
|
|
308
|
+
} catch (error) {
|
|
309
|
+
next(error);
|
|
310
|
+
}
|
|
311
|
+
});
|
|
312
|
+
server.watcher.on("add", scheduleNitroRebuild);
|
|
313
|
+
server.watcher.on("change", scheduleNitroRebuild);
|
|
314
|
+
server.watcher.on("unlink", scheduleNitroRebuild);
|
|
315
|
+
server.httpServer?.once("close", () => {
|
|
316
|
+
server.watcher.off("add", scheduleNitroRebuild);
|
|
317
|
+
server.watcher.off("change", scheduleNitroRebuild);
|
|
318
|
+
server.watcher.off("unlink", scheduleNitroRebuild);
|
|
319
|
+
closeNitroDevServer();
|
|
320
|
+
});
|
|
321
|
+
},
|
|
322
|
+
async closeBundle() {
|
|
323
|
+
const nitroConfig = rawConfig.nitro;
|
|
324
|
+
if (!state || !nitroConfig?.handlers?.length || rawEnv.command !== "build" || getViteMode() === VITEHUB_MODES.e2e) return;
|
|
325
|
+
const nitroBuilder = await loadNitroBuilder();
|
|
326
|
+
const nitro = await nitroBuilder.createNitro(resolveNitroOptions(state.rootDir, engine.configKey, state, nitroConfig, plugin.nitro, false));
|
|
327
|
+
try {
|
|
328
|
+
await nitroBuilder.prepare(nitro);
|
|
329
|
+
await nitroBuilder.copyPublicAssets(nitro);
|
|
330
|
+
await nitroBuilder.prerender(nitro);
|
|
331
|
+
await nitroBuilder.build(nitro);
|
|
332
|
+
await copyViteBuildOutputToNitroPublicDir(nitro, state.rootDir, typeof state.extraConfig?.build?.outDir === "string" ? state.extraConfig.build.outDir : void 0);
|
|
333
|
+
} finally {
|
|
334
|
+
await nitro.close();
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
};
|
|
338
|
+
if (engine.setupNitro) plugin.nitro = createFeatureNitroBridge(engine);
|
|
339
|
+
return {
|
|
340
|
+
vite: plugin,
|
|
341
|
+
nitro: plugin.nitro
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
function createFeatureVitePlugin(options) {
|
|
345
|
+
return createFeatureBridgeBundle(options).vite;
|
|
346
|
+
}
|
|
347
|
+
//#endregion
|
|
348
|
+
//#region src/internal/shared/vitehub-auto-imports.ts
|
|
349
|
+
const defaultFeatures = [
|
|
350
|
+
"browser",
|
|
351
|
+
"queue",
|
|
352
|
+
"sandbox",
|
|
353
|
+
"workflow"
|
|
354
|
+
];
|
|
355
|
+
const definitionPathPattern = /[/\\]server[/\\](?:browsers|queues|sandboxes|workflows)[/\\].+\.[cm]?[jt]s$/;
|
|
356
|
+
function resolveFeatureImports(features) {
|
|
357
|
+
return dedupeServerImports(features.flatMap((feature) => getViteHubFeatureServerImports(feature)));
|
|
358
|
+
}
|
|
359
|
+
function createViteHubDefinitionAutoImportsPlugin(options = {}) {
|
|
360
|
+
const rootDir = resolve(options.rootDir || process.cwd());
|
|
361
|
+
const scanRoots = (options.scanRoots?.length ? options.scanRoots : [rootDir]).map((root) => resolve(root));
|
|
362
|
+
const include = options.include || definitionPathPattern;
|
|
363
|
+
const featureImports = resolveFeatureImports(options.features?.length ? options.features : defaultFeatures);
|
|
364
|
+
let compilerPromise;
|
|
365
|
+
return {
|
|
366
|
+
name: "vitehub-definition-auto-imports",
|
|
367
|
+
async transform(code, id) {
|
|
368
|
+
if (!include.test(id)) return;
|
|
369
|
+
compilerPromise ||= createDiscoveredDefinitionCompiler({
|
|
370
|
+
rootDir,
|
|
371
|
+
scanRoots,
|
|
372
|
+
nitroImports: options.nitroImports ?? { presets: [] },
|
|
373
|
+
featureImports
|
|
374
|
+
});
|
|
375
|
+
const transformed = await (await compilerPromise).injectSource(code, id);
|
|
376
|
+
if (transformed === code) return;
|
|
377
|
+
return transformed;
|
|
378
|
+
}
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
//#endregion
|
|
382
|
+
//#region src/vite.ts
|
|
383
|
+
const SANDBOX_PROVIDER_LOADER_ID = "vitehub-sandbox-provider-loader";
|
|
384
|
+
function sandboxProviderLoaderFallback() {
|
|
385
|
+
return resolveFeatureRuntimePath(import.meta.url, "@vite-hub/sandbox", "./runtime/provider-loader", "runtime/provider-loader.js");
|
|
386
|
+
}
|
|
387
|
+
function mergeProviderLoaderAlias(alias) {
|
|
388
|
+
const replacement = sandboxProviderLoaderFallback();
|
|
389
|
+
if (Array.isArray(alias)) return [{
|
|
390
|
+
find: SANDBOX_PROVIDER_LOADER_ID,
|
|
391
|
+
replacement
|
|
392
|
+
}, ...alias];
|
|
393
|
+
return {
|
|
394
|
+
...alias && typeof alias === "object" ? alias : {},
|
|
395
|
+
[SANDBOX_PROVIDER_LOADER_ID]: replacement
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
function readResolveOptions(config) {
|
|
399
|
+
if (!config || typeof config !== "object" || !("resolve" in config)) return {};
|
|
400
|
+
const resolve = config.resolve;
|
|
401
|
+
return resolve && typeof resolve === "object" ? resolve : {};
|
|
402
|
+
}
|
|
403
|
+
function hubSandbox(options) {
|
|
404
|
+
const plugin = createFeatureVitePlugin({
|
|
405
|
+
...sandboxFeatureEngine,
|
|
406
|
+
readPublicOptions(source) {
|
|
407
|
+
const configOptions = sandboxFeatureEngine.readPublicOptions(source);
|
|
408
|
+
return source.kind === "vite" && typeof configOptions === "undefined" ? options : configOptions;
|
|
409
|
+
}
|
|
410
|
+
});
|
|
411
|
+
const configHook = plugin.config;
|
|
412
|
+
const configEnvironment = plugin.configEnvironment;
|
|
413
|
+
const mergeNoExternal = createNoExternalMerger("@vite-hub/sandbox");
|
|
414
|
+
return {
|
|
415
|
+
...plugin,
|
|
416
|
+
async config(config, env) {
|
|
417
|
+
const result = typeof configHook === "function" ? await configHook.call(this, config, env) : void 0;
|
|
418
|
+
return {
|
|
419
|
+
...result && typeof result === "object" ? result : {},
|
|
420
|
+
resolve: {
|
|
421
|
+
...readResolveOptions(result),
|
|
422
|
+
alias: mergeProviderLoaderAlias(readResolveOptions(result).alias)
|
|
423
|
+
}
|
|
424
|
+
};
|
|
425
|
+
},
|
|
426
|
+
configEnvironment(name, config) {
|
|
427
|
+
const result = typeof configEnvironment === "function" ? configEnvironment.call(this, name, config, void 0) : void 0;
|
|
428
|
+
if (!isServerEnvironment(name, config)) return result;
|
|
429
|
+
return {
|
|
430
|
+
...result,
|
|
431
|
+
resolve: {
|
|
432
|
+
...readResolveOptions(result),
|
|
433
|
+
alias: mergeProviderLoaderAlias(readResolveOptions(result).alias),
|
|
434
|
+
noExternal: mergeNoExternal(config.resolve?.noExternal)
|
|
435
|
+
}
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
};
|
|
439
|
+
}
|
|
440
|
+
//#endregion
|
|
441
|
+
export { createViteHubDefinitionAutoImportsPlugin, hubSandbox };
|
package/package.json
CHANGED
|
@@ -1,9 +1,107 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vite-hub/sandbox",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Sandbox execution primitives and Vite integration for ViteHub.",
|
|
5
|
+
"author": "onmax <maximogarciamtnez@gmail.com>",
|
|
5
6
|
"license": "Apache-2.0",
|
|
6
|
-
"
|
|
7
|
-
|
|
7
|
+
"homepage": "https://github.com/vite-hub/vitehub#readme",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/vite-hub/vitehub.git",
|
|
11
|
+
"directory": "packages/sandbox"
|
|
12
|
+
},
|
|
13
|
+
"bugs": "https://github.com/vite-hub/vitehub/issues",
|
|
14
|
+
"keywords": [
|
|
15
|
+
"vitehub",
|
|
16
|
+
"vite",
|
|
17
|
+
"nitro",
|
|
18
|
+
"sandbox",
|
|
19
|
+
"cloudflare",
|
|
20
|
+
"vercel"
|
|
21
|
+
],
|
|
22
|
+
"sideEffects": false,
|
|
23
|
+
"type": "module",
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"imports": {
|
|
26
|
+
"#vitehub-sandbox-provider-loader": {
|
|
27
|
+
"types": "./dist/runtime/provider-loader.d.ts",
|
|
28
|
+
"default": "./dist/runtime/provider-loader.js"
|
|
29
|
+
},
|
|
30
|
+
"#vitehub-sandbox-registry": {
|
|
31
|
+
"types": "./dist/runtime/empty-registry.d.ts",
|
|
32
|
+
"default": "./dist/runtime/empty-registry.js"
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"exports": {
|
|
36
|
+
".": "./dist/index.js",
|
|
37
|
+
"./nitro": "./dist/nitro.js",
|
|
38
|
+
"./runtime/empty-registry": "./dist/runtime/empty-registry.js",
|
|
39
|
+
"./runtime/nitro-plugin": "./dist/runtime/nitro-plugin.js",
|
|
40
|
+
"./runtime/provider-loader": "./dist/runtime/provider-loader.js",
|
|
41
|
+
"./runtime/providers/cloudflare": "./dist/runtime/providers/cloudflare.js",
|
|
42
|
+
"./runtime/providers/vercel": "./dist/runtime/providers/vercel.js",
|
|
43
|
+
"./runtime/state": "./dist/runtime/state.js",
|
|
44
|
+
"./sandbox/providers/cloudflare": "./dist/sandbox/providers/cloudflare.js",
|
|
45
|
+
"./sandbox/providers/vercel": "./dist/sandbox/providers/vercel.js",
|
|
46
|
+
"./vite": "./dist/vite.js",
|
|
47
|
+
"./package.json": "./package.json"
|
|
48
|
+
},
|
|
49
|
+
"files": [
|
|
50
|
+
"dist",
|
|
51
|
+
"package.json"
|
|
52
|
+
],
|
|
53
|
+
"dependencies": {
|
|
54
|
+
"esbuild": "^0.28.0",
|
|
55
|
+
"exsolve": "^1.0.8",
|
|
56
|
+
"h3": "2.0.1-rc.20",
|
|
57
|
+
"local-pkg": "^1.1.2",
|
|
58
|
+
"mlly": "^1.8.1",
|
|
59
|
+
"pathe": "^2.0.3",
|
|
60
|
+
"pkg-types": "^2.3.0",
|
|
61
|
+
"rollup": "^4.53.3",
|
|
62
|
+
"scule": "^1.3.0",
|
|
63
|
+
"typescript": "^6.0.2",
|
|
64
|
+
"ufo": "^1.6.1",
|
|
65
|
+
"unctx": "^2.4.1",
|
|
66
|
+
"unimport": "^6.0.2"
|
|
67
|
+
},
|
|
68
|
+
"peerDependencies": {
|
|
69
|
+
"@cloudflare/sandbox": "^0.8.11",
|
|
70
|
+
"@vercel/sandbox": "^1.10.0",
|
|
71
|
+
"nitro": "^3.0.0-0",
|
|
72
|
+
"vite": ">=8.0.0"
|
|
73
|
+
},
|
|
74
|
+
"peerDependenciesMeta": {
|
|
75
|
+
"@cloudflare/sandbox": {
|
|
76
|
+
"optional": true
|
|
77
|
+
},
|
|
78
|
+
"@vercel/sandbox": {
|
|
79
|
+
"optional": true
|
|
80
|
+
},
|
|
81
|
+
"nitro": {
|
|
82
|
+
"optional": true
|
|
83
|
+
},
|
|
84
|
+
"vite": {
|
|
85
|
+
"optional": true
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
"devDependencies": {
|
|
89
|
+
"@cloudflare/sandbox": "^0.8.11",
|
|
90
|
+
"@vercel/sandbox": "^1.10.0",
|
|
91
|
+
"nitro": "3.0.260311-beta",
|
|
92
|
+
"ofetch": "^1.5.1",
|
|
93
|
+
"publint": "^0.3.15",
|
|
94
|
+
"tsdown": "^0.21.8",
|
|
95
|
+
"tsx": "^4.21.0",
|
|
96
|
+
"vite": "^8.0.8",
|
|
97
|
+
"vitest": "^4.1.4",
|
|
98
|
+
"@vite-hub/internal": "0.0.1"
|
|
99
|
+
},
|
|
100
|
+
"scripts": {
|
|
101
|
+
"build": "tsdown --tsconfig tsconfig.build.json",
|
|
102
|
+
"dev": "tsdown --watch",
|
|
103
|
+
"typecheck": "tsc --noEmit -p ./tsconfig.json",
|
|
104
|
+
"test": "pnpm --filter @vite-hub/agent... build && pnpm --dir ../blob build && pnpm --dir ../kv build && pnpm --dir ../queue build && pnpm --dir . build && vitest run",
|
|
105
|
+
"test:e2e": "tsx ./test/e2e.ts"
|
|
8
106
|
}
|
|
9
|
-
}
|
|
107
|
+
}
|