Skip to content
Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { test } from "vitest";
import { fetchJson, page, viteTestUrl } from "../../../__test-utils__";

test("returns the correct home page", async ({ expect }) => {
const content = await page.textContent("h1");
expect(content).toBe("Vite + TypeScript");
});

test("returns the correct response from the API", async ({ expect }) => {
const result = await fetchJson("/api/");
expect(result).toEqual({ name: "Cloudflare" });
});

test("returns the correct response from the experimental plugin", async ({ expect }) => {
const response = await fetch(`${viteTestUrl}/__experimental-plugin-test`);
expect(response.status).toBe(200);
const result = await response.json();
expect(result).toEqual({ hasMiniflare: true, type: "workers" });
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { cloudflare, createPlugin } from "@cloudflare/vite-plugin";
import { defineConfig } from "vite";

export default defineConfig({
plugins: [
cloudflare({
configPath: "./wrangler.with-api.jsonc",
inspectorPort: false,
persistState: false,
experimental: {
additionalPlugins: [
createPlugin("test-plugin", (ctx) => ({
configureServer(server) {
server.middlewares.use((req, res, next) => {
if (req.url === "/__experimental-plugin-test") {
const config = ctx.resolvedPluginConfig;
res.setHeader("Content-Type", "application/json");
res.end(
JSON.stringify({
type: config.type,
hasMiniflare: ctx.miniflare !== undefined,
})
);
return;
}
next();
});
},
})),
],
},
}),
],
});
7 changes: 7 additions & 0 deletions packages/vite-plugin-cloudflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ import type { PluginConfig } from "./plugin-config";
import type * as vite from "vite";

export { getLocalWorkerdCompatibilityDate } from "@cloudflare/workers-utils";
export { createPlugin } from "./utils";

export type { PluginConfig } from "./plugin-config";
export type { WorkerConfig } from "./workers-configs";
export type { PluginContext } from "./context";

const sharedContext: SharedContext = {
hasShownWorkerConfigWarnings: false,
Expand All @@ -47,6 +49,10 @@ await assertWranglerVersion();
export function cloudflare(pluginConfig: PluginConfig = {}): vite.Plugin[] {
const ctx = new PluginContext(sharedContext);

const additionalPlugins = (
pluginConfig.experimental?.additionalPlugins ?? []
).map((pluginFactory) => pluginFactory(ctx));

return [
{
name: "vite-plugin-cloudflare",
Expand Down Expand Up @@ -90,5 +96,6 @@ export function cloudflare(pluginConfig: PluginConfig = {}): vite.Plugin[] {
nodeJsAlsPlugin(ctx),
nodeJsCompatPlugin(ctx),
nodeJsCompatWarningsPlugin(ctx),
...additionalPlugins,
];
}
3 changes: 3 additions & 0 deletions packages/vite-plugin-cloudflare/src/plugin-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
readWorkerConfigFromFile,
resolveWorkerType,
} from "./workers-configs";
import type { PluginContext } from "./context";
import type { Defined } from "./utils";
import type {
AssetsOnlyWorkerResolvedConfig,
Expand Down Expand Up @@ -51,6 +52,8 @@ interface Experimental {
headersAndRedirectsDevModeSupport?: boolean;
/** Experimental support for a dedicated prerender Worker */
prerenderWorker?: AuxiliaryWorkerConfig;
/** Experimental support for extending the cloudflare plugin with additional Vite plugins. */
additionalPlugins?: ((ctx: PluginContext) => vite.Plugin)[];
}

type FilteredEntryWorkerConfig = Omit<
Expand Down
Loading