Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 16 additions & 15 deletions packages/alchemy/src/Bundle/Vite.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { NonEmptyArray } from "effect/Array";
import * as Cause from "effect/Cause";
import * as Deferred from "effect/Deferred";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Path from "effect/Path";
Expand Down Expand Up @@ -45,11 +44,18 @@ export const viteBuildOutputPlugin = Effect.fn(function* ({
string,
Effect.Effect<BundleFile, BundleError>
>();
const deferred = yield* Deferred.make<ViteBuildOutput, BundleError>();

const plugin: vite.Plugin = {
name: "alchemy:build-output",
sharedDuringBuild: true,
// Collect the client output directory and server chunks as each
// environment writes its bundle. We deliberately do NOT resolve the build
// result from a `buildApp` hook: on Vite 8, when a project declares no
// `builder.buildApp` (e.g. a plain client-only SPA), `builder.buildApp()`
// runs post-order `buildApp` hooks *before* the default environment builds,
// so a hook fires while the output is still empty (issue #792). Instead,
// `viteBuild` reads `output` *after* `builder.buildApp()` resolves — by
// which point every environment that actually built has run `writeBundle`.
async writeBundle(_, bundle) {
if (this.environment.name === "client") {
clientDirectory = path.resolve(
Expand Down Expand Up @@ -107,18 +113,6 @@ export const viteBuildOutputPlugin = Effect.fn(function* ({
}),
);
},
buildApp: {
order: "post",
async handler() {
Deferred.doneUnsafe(
deferred,
Effect.succeed({
clientDirectory,
serverBundle: makeServerBundle(),
}),
);
},
},
};

// The server bundle may include chunks from more than one Vite environment, so we need to prefix them with the environment-specific output directory.
Expand Down Expand Up @@ -199,6 +193,13 @@ export const viteBuildOutputPlugin = Effect.fn(function* ({

return {
plugin,
output: Deferred.await(deferred),
// Read lazily so callers observe the state collected during the build.
// Only safe to await *after* `builder.buildApp()` has resolved.
output: Effect.sync(
(): ViteBuildOutput => ({
clientDirectory,
serverBundle: makeServerBundle(),
}),
),
};
});
55 changes: 55 additions & 0 deletions packages/alchemy/test/Cloudflare/Website/Vite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const logLevel = Effect.provideService(
);

const fixtureDir = pathe.resolve(import.meta.dirname, "vite-fixture");
const spaFixtureDir = pathe.resolve(import.meta.dirname, "vite-spa-fixture");
const doFixtureDir = pathe.resolve(import.meta.dirname, "vite-do-fixture");
const reactRouterRscFixtureDir = pathe.resolve(
import.meta.dirname,
Expand Down Expand Up @@ -123,6 +124,60 @@ test.provider(
{ timeout: 360_000 },
);

// Regression test for https://github.com/alchemy-run/alchemy-effect/issues/792.
//
// A pure client-only Vite project (no vite.config.ts, no plugins, no worker
// entry) resolves as `appType: "spa"`, so the Cloudflare Vite plugin declares
// no `builder.buildApp`. On Vite 8, `builder.buildApp()` then runs post-order
// `buildApp` hooks *before* the default environment builds — which used to make
// our build-output plugin resolve while the client output was still undefined,
// so the deploy died with "Vite build produced neither assets nor server
// output". Detecting completion in `writeBundle` (per environment) instead of a
// post-order `buildApp` hook fixes it; this test proves the SPA path deploys.
test.provider(
"Vite: client-only SPA (no config, no plugins) builds and serves assets",
(stack) =>
Effect.gen(function* () {
const { accountId } = yield* yield* CloudflareEnvironment;
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;

yield* stack.destroy();

const rootDir = yield* cloneFixture(spaFixtureDir, {
prefix: "alchemy-vite-spa-",
tempRoot,
entries: ["index.html", "package.json", "src"],
});
const indexPath = path.join(rootDir, "index.html");
const memoInclude = ["index.html", "src/**", "package.json"];

const marker = `vite-spa-${Date.now()}`;
yield* fs.writeFileString(indexPath, htmlPage(marker));

const site = yield* stack.deploy(
Effect.gen(function* () {
return yield* Cloudflare.Website.Vite(
"FixViteSpa",
viteProps(rootDir, memoInclude),
);
}),
);

expect(site.url).toBeDefined();
expect(site.hash?.input).toBeDefined();
yield* expectWorkerExists(site.workerName, accountId);
yield* expectUrlContains(`${site.url!}/`, marker, {
timeout: "120 seconds",
label: "spa marker",
});

yield* stack.destroy();
yield* waitForWorkerToBeDeleted(site.workerName, accountId);
}).pipe(logLevel),
{ timeout: 360_000 },
);

test.provider(
"Vite: class form deploys and serves the built assets",
(stack) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist
node_modules
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Vite SPA fixture</title>
</head>
<body>
<div id="app">Vite SPA fixture</div>
<script type="module" src="/src/main.ts"></script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "alchemy-vite-spa-fixture",
"version": "0.0.0",
"private": true,
"type": "module"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// The entire fixture: a single client module referenced by index.html.
// There is no vite.config.ts and no plugins, so the Cloudflare Vite plugin
// resolves the project as `appType: "spa"` with no worker entry — which means
// it declares no `builder.buildApp`. On Vite 8 that is the code path where a
// post-order `buildApp` hook fires before the client environment builds, so
// this fixture is the minimal reproduction for issue #792.
const el = document.getElementById("app");
if (el) {
el.textContent = `${el.textContent} (hydrated)`;
}
Loading