Skip to content

Commit

Permalink
fix: make vike-cloudflare a real Vike extension (#37)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: update vike to `>=0.4.219`
  • Loading branch information
brillout committed Jan 24, 2025
1 parent 51c276a commit f38b4c9
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 40 deletions.
7 changes: 6 additions & 1 deletion examples/hattip-app/pages/+config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import vikeSolid from "vike-solid/config";
import vikeCloudflare from "vike-cloudflare/config";
import type { Config } from "vike/types";
import Head from "../layouts/HeadDefault.js";
import Layout from "../layouts/LayoutDefault.js";
Expand All @@ -9,5 +10,9 @@ export default {
Head,
title: "My Vike App",
stream: "web",
extends: vikeSolid,
server: {
kind: "hattip",
entry: "hattip-entry.ts",
},
extends: [vikeSolid, vikeCloudflare],
} satisfies Config;
13 changes: 1 addition & 12 deletions examples/hattip-app/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { hattip } from "@hattip/vite";
import { pages } from "vike-cloudflare";
import vikeSolid from "vike-solid/vite";
import vike from "vike/plugin";
import { defineConfig } from "vite";
Expand All @@ -8,15 +7,5 @@ export default defineConfig({
build: {
target: "es2022",
},
plugins: [
hattip(),
vike({}),
vikeSolid(),
pages({
server: {
kind: "hattip",
entry: "hattip-entry.ts",
},
}),
],
plugins: [hattip(), vike(), vikeSolid()],
});
7 changes: 6 additions & 1 deletion examples/hono-app/pages/+config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import vikeSolid from "vike-solid/config";
import vikeCloudflare from "vike-cloudflare/config";
import type { Config } from "vike/types";
import Head from "../layouts/HeadDefault.js";
import Layout from "../layouts/LayoutDefault.js";
Expand All @@ -9,5 +10,9 @@ export default {
Head,
// <title>
title: "My Vike App",
extends: vikeSolid,
server: {
kind: "hono",
entry: "hono-entry.ts",
},
extends: [vikeSolid, vikeCloudflare],
} satisfies Config;
9 changes: 1 addition & 8 deletions examples/hono-app/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import devServer from "@hono/vite-dev-server";
import adapter from "@hono/vite-dev-server/cloudflare";
import { pages } from "vike-cloudflare";
import vikeSolid from "vike-solid/vite";
import vike from "vike/plugin";
import { defineConfig } from "vite";
Expand All @@ -26,13 +25,7 @@ export default defineConfig({

injectClientScript: false,
}),
vike({}),
vike(),
vikeSolid(),
pages({
server: {
kind: "hono",
entry: "hono-entry.ts",
},
}),
],
});
3 changes: 2 additions & 1 deletion examples/vike-app/pages/+config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import vikeSolid from "vike-solid/config";
import vikeCloudflare from "vike-cloudflare/config";
import type { Config } from "vike/types";
import Head from "../layouts/HeadDefault.js";
import Layout from "../layouts/LayoutDefault.js";
Expand All @@ -9,5 +10,5 @@ export default {
Head,
// <title>
title: "My Vike App",
extends: vikeSolid,
extends: [vikeSolid, vikeCloudflare],
} satisfies Config;
3 changes: 1 addition & 2 deletions examples/vike-app/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { pages } from "vike-cloudflare";
import vikeSolid from "vike-solid/vite";
import vike from "vike/plugin";
import { defineConfig } from "vite";
Expand All @@ -7,5 +6,5 @@ export default defineConfig({
build: {
target: "es2022",
},
plugins: [vike({}), vikeSolid(), pages()],
plugins: [vike(), vikeSolid()],
});
1 change: 1 addition & 0 deletions packages/vike-cloudflare/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"./config": "./dist/config.js",
"./types": {
"types": "./vike.d.ts"
}
Expand Down
31 changes: 31 additions & 0 deletions packages/vike-cloudflare/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export { config as default };

import { pages as plugin, type VikeCloudflarePagesOptions } from "./index";
import type { Config } from "vike/types";

const config = {
name: "vike-cloudflare",
require: {
vike: ">=0.4.219",
},
vite: {
plugins: [plugin()],
},
/* TODO/now
prerender: {
value: null,
disableAutoFullBuild: true
},
*/
meta: {
server: { env: { config: true }, global: true },
},
} satisfies Config;

declare global {
namespace Vike {
interface Config {
server?: VikeCloudflarePagesOptions["server"];
}
}
}
33 changes: 19 additions & 14 deletions packages/vike-cloudflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@ import { normalizePath, type Plugin, type ResolvedConfig } from "vite";
import hattipAsset from "../assets/hattip.js?raw";
import honoAsset from "../assets/hono.js?raw";
import vikeAsset from "../assets/vike.js?raw";
import type { Config } from "vike/types";

declare module "vite" {
interface UserConfig {
// TODO/now:
// - Re-use declaration merging done by Vike
// - Extend ResolvedConfig ?
vike?: {
global: {
config: Config;
};
};
}
}

const NAME = "vike-cloudflare";
const WORKER_JS_NAME = "_worker.js";
Expand Down Expand Up @@ -36,25 +50,17 @@ function getAsset(kind: SupportedServers | undefined) {
}
}

export const pages = (options?: VikeCloudflarePagesOptions): Plugin[] => {
export const pages = (): any => {
const virtualEntryId = "virtual:vike-cloudflare-entry";
const virtualServerId = "virtual:vike-cloudflare-server";
const resolvedVirtualServerId = `\0${virtualServerId}`;
let resolvedConfig: ResolvedConfig;
let shouldPrerender = false;
let options: VikeCloudflarePagesOptions;

return [
{
name: `${NAME}:disableAutoFullBuild`,
// @ts-ignore
config() {
return {
// TODO/next-major-release: remove this and require >[email protected]
vitePluginSsr: {
disableAutoFullBuild: "prerender",
},
};
},
},
{
name: NAME,
Expand Down Expand Up @@ -101,9 +107,8 @@ export const pages = (options?: VikeCloudflarePagesOptions): Plugin[] => {
},
configResolved: async (config) => {
resolvedConfig = config;
// TODO/next-major-release: remove this and require >[email protected]
// biome-ignore lint/suspicious/noExplicitAny:
shouldPrerender = !!(await (config as any).configVikePromise).prerender;
options = { server: config.vike!.global.config.server };
shouldPrerender = !!config.vike!.global.config.prerender;
},
options(inputOptions) {
assert(
Expand Down Expand Up @@ -196,7 +201,7 @@ export default handler;
},
},
},
];
] satisfies Plugin[];
};

async function symlinkOrCopy(target: string, path: string) {
Expand Down
2 changes: 1 addition & 1 deletion packages/vike-cloudflare/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default defineConfig({
target: "node18",
format: "esm",
dts: true,
entry: ["src/index.ts"],
entry: ["src/index.ts", "src/config.ts"],
clean: true,
esbuildPlugins: [RawPlugin()],
});

0 comments on commit f38b4c9

Please sign in to comment.