Skip to content

feat: Allow project-owned vite plugin dep #537

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jun 23, 2025
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
9 changes: 8 additions & 1 deletion docs/src/content/docs/guides/frontend/tailwind.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,19 @@ Since the RedwoodSDK is based on React and Vite, we can work through the ["Using
import { defineConfig } from "vite";
import tailwindcss from "@tailwindcss/vite";
import { redwood } from "rwsdk/vite";
import { cloudflare } from "@cloudflare/vite-plugin";

export default defineConfig({
environments: {
ssr: {},
},
plugins: [redwood(), tailwindcss()],
plugins: [
cloudflare({
viteEnvironment: { name: "worker" },
}),
redwood(),
tailwindcss()
],
});
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,16 @@ Since the RedwoodSDK is based on React and Vite, we can work through the ["Using
import { defineConfig } from "vite";
import tailwindcss from '@tailwindcss/vite'
import { redwood } from "rwsdk/vite";
import { cloudflare } from "@cloudflare/vite-plugin";

export default defineConfig({
environments: {
ssr: {},
},
plugins: [
cloudflare({
viteEnvironment: { name: "worker" },
}),
redwood(),
tailwindcss(),
],
Expand Down
105 changes: 24 additions & 81 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rwsdk",
"version": "0.1.0-alpha.19",
"version": "0.1.0-alpha.19-test.20250623033339",
"description": "Build fast, server-driven webapps on Cloudflare with SSR, RSC, and realtime",
"type": "module",
"bin": {
Expand Down
1 change: 1 addition & 0 deletions sdk/src/scripts/worker-run.mts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export const runWorkerScript = async (relativeScriptPath: string) => {
plugins: [
redwood({
configPath: tmpWorkerPath.path,
includeCloudflarePlugin: true,
entry: {
worker: scriptPath,
},
Expand Down
20 changes: 20 additions & 0 deletions sdk/src/vite/hasOwnCloudflareVitePlugin.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { readFile } from "fs/promises";
import path from "path";

export async function hasOwnCloudflareVitePlugin({
rootProjectDir,
}: {
rootProjectDir: string;
}) {
const packageJsonPath = path.join(rootProjectDir, "package.json");
try {
const packageJson = JSON.parse(await readFile(packageJsonPath, "utf-8"));
return !!(
packageJson.dependencies?.["@cloudflare/vite-plugin"] ||
packageJson.devDependencies?.["@cloudflare/vite-plugin"]
);
} catch (error) {
console.error("Error reading package.json:", error);
return false;
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import { HotUpdateOptions, Plugin } from "vite";
import { cloudflare } from "@cloudflare/vite-plugin";
import { resolve } from "node:path";
import colors from "picocolors";
import { readFile } from "node:fs/promises";

import { getShortName } from "../lib/getShortName.mjs";
import { pathExists } from "fs-extra";

type BasePluginOptions = Parameters<typeof cloudflare>[0];

type MiniflarePluginOptions = BasePluginOptions & {};

const hasEntryAsAncestor = (
module: any,
entryFile: string,
Expand Down Expand Up @@ -89,17 +84,15 @@ const isUseClientModule = async (
}
};

export const miniflarePlugin = (
givenOptions: MiniflarePluginOptions & {
rootDir: string;
workerEntryPathname: string;
},
): (Plugin | Plugin[])[] => [
cloudflare(givenOptions),
export const miniflareHMRPlugin = (givenOptions: {
rootDir: string;
viteEnvironment: { name: string };
workerEntryPathname: string;
}): (Plugin | Plugin[])[] => [
{
name: "rwsdk:miniflare-hmr",
async hotUpdate(ctx) {
const environment = givenOptions.viteEnvironment?.name ?? "worker";
const environment = givenOptions.viteEnvironment.name;
const entry = givenOptions.workerEntryPathname;

if (!["client", environment].includes(this.environment.name)) {
Expand Down
22 changes: 18 additions & 4 deletions sdk/src/vite/redwoodPlugin.mts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { resolve } from "node:path";
import { InlineConfig } from "vite";
import { cloudflare } from "@cloudflare/vite-plugin";
import { hasOwnCloudflareVitePlugin } from "./hasOwnCloudflareVitePlugin.mjs";

import reactPlugin from "@vitejs/plugin-react";
import tsconfigPaths from "vite-tsconfig-paths";
Expand All @@ -8,7 +10,7 @@ import { transformJsxScriptTagsPlugin } from "./transformJsxScriptTagsPlugin.mjs
import { directivesPlugin } from "./directivesPlugin.mjs";
import { useClientLookupPlugin } from "./useClientLookupPlugin.mjs";
import { useServerLookupPlugin } from "./useServerLookupPlugin.mjs";
import { miniflarePlugin } from "./miniflarePlugin.mjs";
import { miniflareHMRPlugin } from "./miniflareHMRPlugin.mjs";
import { moveStaticAssetsPlugin } from "./moveStaticAssetsPlugin.mjs";
import { configPlugin } from "./configPlugin.mjs";
import { $ } from "../lib/$.mjs";
Expand All @@ -26,6 +28,7 @@ export type RedwoodPluginOptions = {
silent?: boolean;
rootDir?: string;
mode?: "development" | "production";
includeCloudflarePlugin?: boolean;
configPath?: string;
entry?: {
client?: string;
Expand All @@ -37,6 +40,7 @@ export const redwoodPlugin = async (
options: RedwoodPluginOptions = {},
): Promise<InlineConfig["plugins"]> => {
const projectRootDir = process.cwd();

const mode =
options.mode ??
(process.env.NODE_ENV === "development" ? "development" : "production");
Expand All @@ -52,6 +56,10 @@ export const redwoodPlugin = async (
const clientFiles = new Set<string>();
const serverFiles = new Set<string>();

const shouldIncludeCloudflarePlugin =
options.includeCloudflarePlugin ??
!(await hasOwnCloudflareVitePlugin({ rootProjectDir: projectRootDir }));

// context(justinvdm, 31 Mar 2025): We assume that if there is no .wrangler directory,
// then this is fresh install, and we run `npm run dev:init` here.
if (
Expand All @@ -69,6 +77,7 @@ export const redwoodPlugin = async (
stdio: ["ignore", "inherit", "inherit"],
})`npm run dev:init`;
}

return [
devServerTimingPlugin(),
configPlugin({
Expand All @@ -85,12 +94,17 @@ export const redwoodPlugin = async (
}),
reactConditionsResolverPlugin(),
tsconfigPaths({ root: projectRootDir }),
miniflarePlugin({
shouldIncludeCloudflarePlugin
? cloudflare({
viteEnvironment: { name: "worker" },
configPath:
options.configPath ?? (await findWranglerConfig(projectRootDir)),
})
: [],
miniflareHMRPlugin({
rootDir: projectRootDir,
viteEnvironment: { name: "worker" },
workerEntryPathname,
configPath:
options.configPath ?? (await findWranglerConfig(projectRootDir)),
}),
reactPlugin(),
directivesPlugin({
Expand Down
6 changes: 4 additions & 2 deletions starters/minimal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@
"types": "tsc"
},
"dependencies": {
"rwsdk": "0.1.0-alpha.18-test.20250623021216"
"rwsdk": "0.1.0-alpha.19-test.20250623033339"
},
"devDependencies": {
"@cloudflare/vite-plugin": "1.7.4",
"@cloudflare/workers-types": "^4.20250407.0",
"@types/node": "^22.14.0",
"@types/react": "^19.1.2",
"@types/react-dom": "^19.1.2",
"typescript": "^5.8.3",
"vite": "^6.2.6",
"wrangler": "^4.20.3"
"wrangler": "^4.20.5"
},
"pnpm": {
"onlyBuiltDependencies": [
Expand Down
Loading