@vitejs/plugin-react 5.0.4 → 5.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -102,9 +102,16 @@ react({ reactRefreshHost: 'http://localhost:3000' })
102
102
 
103
103
  Under the hood, this simply updates the React Fash Refresh runtime URL from `/@react-refresh` to `http://localhost:3000/@react-refresh` to ensure there is only one Refresh runtime across the whole application. Note that if you define `base` option in the host application, you need to include it in the option, like: `http://localhost:3000/{base}`.
104
104
 
105
- ## Middleware mode
105
+ ## `@vitejs/plugin-react/preamble`
106
106
 
107
- In [middleware mode](https://vite.dev/config/server-options.html#server-middlewaremode), you should make sure your entry `index.html` file is transformed by Vite. Here's an example for an Express server:
107
+ The package provides `@vitejs/plugin-react/preamble` to initialize HMR runtime from client entrypoint for SSR applications which don't use [`transformIndexHtml` API](https://vite.dev/guide/api-javascript.html#vitedevserver). For example:
108
+
109
+ ```js
110
+ // [entry.client.js]
111
+ import '@vitejs/plugin-react/preamble'
112
+ ```
113
+
114
+ Alternatively, you can manually call `transformIndexHtml` during SSR, which sets up equivalent initialization code. Here's an example for an Express server:
108
115
 
109
116
  ```js
110
117
  app.get('/', async (req, res, next) => {
@@ -121,7 +128,7 @@ app.get('/', async (req, res, next) => {
121
128
  })
122
129
  ```
123
130
 
124
- Otherwise, you'll probably get this error:
131
+ Otherwise, you'll get the following error:
125
132
 
126
133
  ```
127
134
  Uncaught Error: @vitejs/plugin-react can't detect preamble. Something is wrong.
package/dist/index.js CHANGED
@@ -45,6 +45,27 @@ function $RefreshSig$() { return RefreshRuntime.createSignatureFunctionForTransf
45
45
  `;
46
46
  return newCode;
47
47
  }
48
+ function virtualPreamblePlugin({ name, isEnabled }) {
49
+ return {
50
+ name: "vite:react-virtual-preamble",
51
+ resolveId: {
52
+ order: "pre",
53
+ filter: { id: exactRegex(name) },
54
+ handler(source) {
55
+ if (source === name) return "\0" + source;
56
+ }
57
+ },
58
+ load: {
59
+ filter: { id: exactRegex("\0" + name) },
60
+ handler(id) {
61
+ if (id === "\0" + name) {
62
+ if (isEnabled()) return preambleCode.replace("__BASE__", "/");
63
+ return "";
64
+ }
65
+ }
66
+ }
67
+ };
68
+ }
48
69
 
49
70
  //#endregion
50
71
  //#region ../common/warning.ts
@@ -57,8 +78,7 @@ const silenceUseClientWarning = (userConfig) => ({ rollupOptions: { onwarn(warni
57
78
 
58
79
  //#endregion
59
80
  //#region src/index.ts
60
- const _dirname = dirname(fileURLToPath(import.meta.url));
61
- const refreshRuntimePath = join(_dirname, "refresh-runtime.js");
81
+ const refreshRuntimePath = join(dirname(fileURLToPath(import.meta.url)), "refresh-runtime.js");
62
82
  let babel;
63
83
  async function loadBabel() {
64
84
  if (!babel) babel = await import("@babel/core");
@@ -108,7 +128,7 @@ function viteReact(opts = {}) {
108
128
  jsxRefreshInclude: makeIdFiltersToMatchWithQuery(include),
109
129
  jsxRefreshExclude: makeIdFiltersToMatchWithQuery(exclude)
110
130
  },
111
- optimizeDeps: { rollupOptions: { transform: { jsx: { runtime: "automatic" } } } }
131
+ optimizeDeps: { rolldownOptions: { transform: { jsx: { runtime: "automatic" } } } }
112
132
  };
113
133
  if (opts.jsxRuntime === "classic") return { esbuild: { jsx: "transform" } };
114
134
  else return {
@@ -137,8 +157,9 @@ function viteReact(opts = {}) {
137
157
  },
138
158
  options(options) {
139
159
  if (!runningInVite) {
140
- options.jsx = {
141
- mode: opts.jsxRuntime,
160
+ options.transform ??= {};
161
+ options.transform.jsx = {
162
+ runtime: opts.jsxRuntime,
142
163
  importSource: opts.jsxImportSource
143
164
  };
144
165
  return options;
@@ -283,8 +304,7 @@ function viteReact(opts = {}) {
283
304
  jsxImportDevRuntime,
284
305
  jsxImportRuntime
285
306
  ];
286
- const staticBabelPlugins = typeof opts.babel === "object" ? opts.babel?.plugins ?? [] : [];
287
- const reactCompilerPlugin = getReactCompilerPlugin(staticBabelPlugins);
307
+ const reactCompilerPlugin = getReactCompilerPlugin(typeof opts.babel === "object" ? opts.babel?.plugins ?? [] : []);
288
308
  if (reactCompilerPlugin != null) {
289
309
  const reactCompilerRuntimeModule = getReactCompilerRuntimeModule(reactCompilerPlugin);
290
310
  dependencies.push(reactCompilerRuntimeModule);
@@ -323,7 +343,11 @@ function viteReact(opts = {}) {
323
343
  viteConfigPost,
324
344
  viteReactRefreshFullBundleMode
325
345
  ] : [],
326
- viteReactRefresh
346
+ viteReactRefresh,
347
+ virtualPreamblePlugin({
348
+ name: "@vitejs/plugin-react/preamble",
349
+ isEnabled: () => !skipFastRefresh && !isFullBundle
350
+ })
327
351
  ];
328
352
  }
329
353
  viteReact.preambleCode = preambleCode;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vitejs/plugin-react",
3
- "version": "5.0.4",
3
+ "version": "5.1.1",
4
4
  "license": "MIT",
5
5
  "author": "Evan You",
6
6
  "description": "The default Vite plugin for React projects",
@@ -17,10 +17,14 @@
17
17
  "Arnaud Barré"
18
18
  ],
19
19
  "files": [
20
+ "types",
20
21
  "dist"
21
22
  ],
22
23
  "type": "module",
23
- "exports": "./dist/index.js",
24
+ "exports": {
25
+ ".": "./dist/index.js",
26
+ "./preamble": "./types/preamble.d.ts"
27
+ },
24
28
  "scripts": {
25
29
  "dev": "tsdown --watch ./src --watch ../common",
26
30
  "build": "tsdown",
@@ -40,12 +44,12 @@
40
44
  },
41
45
  "homepage": "https://github.com/vitejs/vite-plugin-react/tree/main/packages/plugin-react#readme",
42
46
  "dependencies": {
43
- "@babel/core": "^7.28.4",
47
+ "@babel/core": "^7.28.5",
44
48
  "@babel/plugin-transform-react-jsx-self": "^7.27.1",
45
49
  "@babel/plugin-transform-react-jsx-source": "^7.27.1",
46
- "@rolldown/pluginutils": "1.0.0-beta.38",
50
+ "@rolldown/pluginutils": "1.0.0-beta.47",
47
51
  "@types/babel__core": "^7.20.5",
48
- "react-refresh": "^0.17.0"
52
+ "react-refresh": "^0.18.0"
49
53
  },
50
54
  "peerDependencies": {
51
55
  "vite": "^4.2.0 || ^5.0.0 || ^6.0.0 || ^7.0.0"
@@ -53,10 +57,9 @@
53
57
  "devDependencies": {
54
58
  "@vitejs/react-common": "workspace:*",
55
59
  "babel-plugin-react-compiler": "19.1.0-rc.3",
56
- "react": "^19.1.1",
57
- "react-dom": "^19.1.1",
58
- "rolldown": "1.0.0-beta.38",
59
- "tsdown": "^0.15.4",
60
- "vitest": "^3.2.4"
60
+ "react": "^19.2.0",
61
+ "react-dom": "^19.2.0",
62
+ "rolldown": "1.0.0-beta.47",
63
+ "tsdown": "^0.16.1"
61
64
  }
62
65
  }
@@ -0,0 +1 @@
1
+ export {}