Skip to content
Open
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
5 changes: 4 additions & 1 deletion packages/vite-plugin-cloudflare/playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
"@cloudflare/vite-plugin": "workspace:*",
"@cloudflare/workers-tsconfig": "workspace:*",
"playwright-chromium": "catalog:default",
"rolldown-vite": "^7.1.16",
"ts-dedent": "^2.2.0",
"typescript": "catalog:default"
"typescript": "catalog:default",
"vite-6": "npm:vite@^6.3.6",
"vite-7": "npm:vite@^7.1.9"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import type {
createBuilder,
createServer,
loadConfigFromFile,
mergeConfig,
preview,
version as ViteVersion,
} from "vite";

/**
* Returns the correct Vite module to test based on the `VITE_VERSION_TO_TEST` environment variable
*
* @returns The vite module to test
*/
export async function getViteModuleToTest(): Promise<{
packageName: "vite" | "rolldown-vite";
version: typeof ViteVersion;
loadConfigFromFile: typeof loadConfigFromFile;
mergeConfig: typeof mergeConfig;
createServer: typeof createServer;
createBuilder: typeof createBuilder;
preview: typeof preview;
}> {
if (process.env.VITE_VERSION_TO_TEST === undefined) {
return {
packageName: "vite",
// Note: If VITE_VERSION_TO_TEST is not set we we want to fallback to a generic version of vite
// (instead of falling back to one of the options below) because we want to make sure that
// the vite version can be correctly overridden in the vite-ecosystem-ci runs
// (ref: https://github.com/vitejs/vite-ecosystem-ci/blob/a0fab/tests/vite-plugin-cloudflare.ts)
...(await import("vite")),
};
}

if (process.env.VITE_VERSION_TO_TEST === "6") {
return {
packageName: "vite",
...(await import("vite-6")),
} as any;
}

if (process.env.VITE_VERSION_TO_TEST === "7") {
return {
packageName: "vite",
...(await import("vite-7")),
} as any;
}

if (process.env.VITE_VERSION_TO_TEST === "rolldown-vite") {
return {
packageName: "rolldown-vite",
...(await import("rolldown-vite")),
} as any;
}

throw new Error(
`Invalid value provided for "VITE_VERSION_TO_TEST" (${JSON.stringify(process.env.VITE_VERSION_TO_TEST)}). The available options are: "6", "7" and "rolldown-vite"`
);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { chromium } from "playwright-chromium";
import { getViteModuleToTest } from "./vite-module-to-test";
import type { BrowserServer } from "playwright-chromium";
import type { GlobalSetupContext } from "vitest/node";

Expand All @@ -17,6 +18,14 @@ export async function setup({ provide }: GlobalSetupContext): Promise<void> {
});

provide("wsEndpoint", browserServer.wsEndpoint());

const viteModule = await getViteModuleToTest();
const viteInUseMessage = `Running playground tests against ${viteModule.packageName}@${viteModule.version}`;
const lineLength = viteInUseMessage.length + 8;
console.log(`┌${"─".repeat(lineLength)}┐`);
console.log(`│ ${viteInUseMessage} │`);
console.log(`└${"─".repeat(lineLength)}┘`);
console.log("");
}

export async function teardown(): Promise<void> {
Expand Down
26 changes: 11 additions & 15 deletions packages/vite-plugin-cloudflare/playground/vitest-setup.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
import fs from "node:fs";
import path from "node:path";
import { chromium } from "playwright-chromium";
import {
createBuilder,
createServer,
loadConfigFromFile,
mergeConfig,
preview,
Rollup,
} from "vite";
import { beforeAll, inject } from "vitest";
import { getViteModuleToTest } from "./vite-module-to-test";
import type * as http from "node:http";
import type { Browser, Page } from "playwright-chromium";
import type {
Expand All @@ -19,6 +12,7 @@ import type {
PluginOption,
PreviewServer,
ResolvedConfig,
Rollup,
UserConfig,
ViteDevServer,
} from "vite";
Expand Down Expand Up @@ -72,6 +66,8 @@ export let browser: Browser = undefined!;
export let viteTestUrl: string = "";
export let watcher: Rollup.RollupWatcher | undefined = undefined;

const vite = await getViteModuleToTest();

export function setViteUrl(url: string): void {
viteTestUrl = url;
}
Expand Down Expand Up @@ -198,7 +194,7 @@ export async function loadConfig(configEnv: ConfigEnv) {
`vite.config.${variantName}.${extension}`
);
if (fs.existsSync(configVariantPath)) {
const res = await loadConfigFromFile(configEnv, configVariantPath);
const res = await vite.loadConfigFromFile(configEnv, configVariantPath);
if (res) {
config = res.config;
break;
Expand All @@ -208,7 +204,7 @@ export async function loadConfig(configEnv: ConfigEnv) {
}
// config file from test root dir
if (!config) {
const res = await loadConfigFromFile(configEnv, undefined, rootDir);
const res = await vite.loadConfigFromFile(configEnv, undefined, rootDir);
if (res) {
config = res.config;
}
Expand Down Expand Up @@ -243,7 +239,7 @@ export async function loadConfig(configEnv: ConfigEnv) {
serverLogs.errors
),
};
return mergeConfig(options, config || {});
return vite.mergeConfig(options, config || {});
}

export async function startDefaultServe(): Promise<
Expand All @@ -254,7 +250,7 @@ export async function startDefaultServe(): Promise<
if (!isBuild) {
process.env.VITE_INLINE = "inline-serve";
const config = await loadConfig({ command: "serve", mode: "development" });
viteServer = await (await createServer(config)).listen();
viteServer = await (await vite.createServer(config)).listen();
viteTestUrl = viteServer.resolvedUrls!.local[0]!;
if (viteServer.config.base === "/") {
viteTestUrl = viteTestUrl.replace(/\/$/, "");
Expand All @@ -270,7 +266,7 @@ export async function startDefaultServe(): Promise<
resolvedConfig = config;
},
});
const buildConfig = mergeConfig(
const buildConfig = vite.mergeConfig(
await loadConfig({
command: "build",
mode: "production",
Expand All @@ -279,7 +275,7 @@ export async function startDefaultServe(): Promise<
plugins: [resolvedPlugin()],
}
);
const builder = await createBuilder(buildConfig);
const builder = await vite.createBuilder(buildConfig);
await builder.buildApp();

const previewConfig = await loadConfig({
Expand All @@ -291,7 +287,7 @@ export async function startDefaultServe(): Promise<
// Make sure we are running from within the playground.
// Otherwise workerd will error with messages about not being allowed to escape the starting directory with `..`.
process.chdir(previewConfig.root);
const previewServer = await preview(previewConfig);
const previewServer = await vite.preview(previewConfig);
// prevent preview change NODE_ENV
process.env.NODE_ENV = _nodeEnv;
viteTestUrl = previewServer!.resolvedUrls!.local[0]!;
Expand Down
Loading
Loading