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
1 change: 1 addition & 0 deletions packages/vinext/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"dependencies": {
"@unpic/react": "^1.0.2",
"@vercel/og": "^0.8.6",
"@vitejs/plugin-react": "^5.1.4",
"@vitejs/plugin-rsc": "^0.5.19",
"magic-string": "^0.30.21",
"react-server-dom-webpack": "^19.2.4",
Expand Down
34 changes: 24 additions & 10 deletions packages/vinext/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Plugin, UserConfig, ViteDevServer } from "vite";
import type { Plugin, PluginOption, UserConfig, ViteDevServer } from "vite";
import { loadEnv, parseAst } from "vite";
import { pagesRouter, apiRouter, invalidateRouteCache, matchRoute, patternToNextFormat as pagesPatternToNextFormat, type Route } from "./routing/pages-router.js";
import { appRouter, invalidateAppRouteCache } from "./routing/app-router.js";
Expand Down Expand Up @@ -37,6 +37,7 @@ import { scanMetadataFiles } from "./server/metadata-routes.js";
import { staticExportPages } from "./build/static-export.js";
import { detectPackageManager } from "./utils/project.js";
import tsconfigPaths from "vite-tsconfig-paths";
import react, { Options as VitePluginReactOptions } from "@vitejs/plugin-react";
import MagicString from "magic-string";
import path from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
Expand Down Expand Up @@ -572,9 +573,17 @@ export interface VinextOptions {
* @default true
*/
rsc?: boolean;
/**
* Options passed to @vitejs/plugin-react (React Fast Refresh + JSX transform).
* Enabled by default. Set to `false` to disable (e.g. if you already have
* @vitejs/plugin-react in your vite.config.ts), or pass an options object
* to customize the Babel transform.
* @default true
*/
react?: VitePluginReactOptions | boolean;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The @default undefined is misleading — when options.react is undefined (the default), the plugin IS enabled with default options (line 1692 falls through to react(undefined)). The effective default behavior is "enabled with defaults", not "undefined".

Consider @default true or rewording the description to say "Enabled by default. Set to false to disable."

}

export default function vinext(options: VinextOptions = {}): Plugin[] {
export default function vinext(options: VinextOptions = {}): PluginOption[] {
let root: string;
let pagesDir: string;
let appDir: string;
Expand Down Expand Up @@ -1680,10 +1689,16 @@ hydrate();
// files are detected and @mdx-js/rollup is installed.
let mdxDelegate: Plugin | null = null;

const plugins: (Plugin | Promise<Plugin[]>)[] = [
const reactPlugin = options.react === false
? false
: react(options.react === true ? undefined : options.react);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth considering: if a user already has @vitejs/plugin-react in their vite.config.ts (e.g., they configured it before vinext supported it, or they want custom Babel plugins), they'll get two instances of the React plugin — duplicate Babel transforms and double React Refresh injection.

The options.react === false escape hatch exists, but users won't know they need it unless they hit a confusing error. A couple of options:

  1. Detect and skip — in the vinext:config hook, check if the resolved config already includes a plugin named vite:react-babel and skip the auto-injected one.
  2. Document it — at minimum, the JSDoc should mention that users who already have @vitejs/plugin-react in their config should set react: false.

Not blocking since no current examples or fixtures do this, but it will come up.


const plugins: PluginOption[] = [
// Resolve tsconfig paths/baseUrl aliases so real-world Next.js repos
// that use @/*, #/*, or baseUrl imports work out of the box.
tsconfigPaths(),
// React Fast Refresh + JSX transform for client components.
reactPlugin,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: when options.react === false, reactPlugin is false which becomes a falsy PluginOption element. Vite handles this fine (it filters falsy plugins), so this works correctly. Just noting for clarity.

// Transform CJS require()/module.exports to ESM before other plugins
// analyze imports (RSC directive scanning, shim resolution, etc.)
commonjs(),
Expand Down Expand Up @@ -2333,12 +2348,11 @@ hydrate();
name: "vinext:pages-router",

// HMR: trigger full-reload for Pages Router page changes.
// Without @vitejs/plugin-react (React Fast Refresh), component edits
// can't be hot-updated. In theory Vite's default propagation should
// reach the root and trigger a full-reload, but the Pages Router
// injects hydration via inline <script type="module"> which may not
// be tracked in the module graph. Explicitly sending full-reload
// ensures changes are always reflected in the browser.
// Even with @vitejs/plugin-react providing React Fast Refresh,
// the Pages Router injects hydration via inline <script type="module">
// which may not be tracked in Vite's module graph. Explicitly
// sending full-reload ensures changes are always reflected in
// the browser.
hotUpdate(options: { file: string; server: ViteDevServer; modules: any[] }) {
if (!hasPagesDir || hasAppDir) return;
const ext = /\.(tsx?|jsx?|mdx)$/;
Expand Down Expand Up @@ -3425,7 +3439,7 @@ hydrate();
plugins.push(rscPluginPromise);
}

return plugins as Plugin[];
return plugins;
}

/**
Expand Down
Loading
Loading