@vitejs/plugin-react-swc 4.1.0 → 4.2.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
@@ -125,6 +125,38 @@ If set, disables the recommendation to use `@vitejs/plugin-react-oxc` (which is
125
125
  react({ disableOxcRecommendation: true })
126
126
  ```
127
127
 
128
+ ## `@vitejs/plugin-react-swc/preamble`
129
+
130
+ The package provides `@vitejs/plugin-react-swc/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:
131
+
132
+ ```js
133
+ // [entry.client.js]
134
+ import '@vitejs/plugin-react-swc/preamble'
135
+ ```
136
+
137
+ Alternatively, you can manually call `transformIndexHtml` during SSR, which sets up equivalent initialization code. Here's an example for an Express server:
138
+
139
+ ```js
140
+ app.get('/', async (req, res, next) => {
141
+ try {
142
+ let html = fs.readFileSync(path.resolve(root, 'index.html'), 'utf-8')
143
+
144
+ // Transform HTML using Vite plugins.
145
+ html = await viteServer.transformIndexHtml(req.url, html)
146
+
147
+ res.send(html)
148
+ } catch (e) {
149
+ return next(e)
150
+ }
151
+ })
152
+ ```
153
+
154
+ Otherwise, you'll get the following error:
155
+
156
+ ```
157
+ Uncaught Error: @vitejs/plugin-react-swc can't detect preamble. Something is wrong.
158
+ ```
159
+
128
160
  ## Consistent components exports
129
161
 
130
162
  For React refresh to work correctly, your file should only export React components. The best explanation I've read is the one from the [Gatsby docs](https://www.gatsbyjs.com/docs/reference/local-development/fast-refresh/#how-it-works).
package/index.js CHANGED
@@ -1,10 +1,9 @@
1
1
  import { createRequire } from "node:module";
2
2
  import { readFileSync } from "node:fs";
3
- import { dirname, join } from "node:path";
4
- import { fileURLToPath } from "node:url";
3
+ import { join } from "node:path";
5
4
  import { transform } from "@swc/core";
6
- import * as vite from "vite";
7
5
  import { exactRegex } from "@rolldown/pluginutils";
6
+ import * as vite from "vite";
8
7
 
9
8
  //#region ../common/refresh-utils.ts
10
9
  const runtimePublicPath = "/@react-refresh";
@@ -46,6 +45,27 @@ function $RefreshSig$() { return RefreshRuntime.createSignatureFunctionForTransf
46
45
  `;
47
46
  return newCode;
48
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
+ }
49
69
 
50
70
  //#endregion
51
71
  //#region ../common/warning.ts
@@ -58,10 +78,9 @@ const silenceUseClientWarning = (userConfig) => ({ rollupOptions: { onwarn(warni
58
78
 
59
79
  //#endregion
60
80
  //#region src/index.ts
61
- const _dirname = typeof __dirname !== "undefined" ? __dirname : dirname(fileURLToPath(import.meta.url));
62
- const resolve = createRequire(typeof __filename !== "undefined" ? __filename : import.meta.url).resolve;
81
+ const resolve = createRequire(import.meta.url).resolve;
63
82
  const react = (_options) => {
64
- let hmrDisabled = false;
83
+ let hmrDisabled = true;
65
84
  let viteCacheRoot;
66
85
  const options = {
67
86
  jsxImportSource: _options?.jsxImportSource ?? "react",
@@ -84,7 +103,7 @@ const react = (_options) => {
84
103
  },
85
104
  load: {
86
105
  filter: { id: exactRegex(runtimePublicPath) },
87
- handler: (id) => id === runtimePublicPath ? readFileSync(join(_dirname, "refresh-runtime.js"), "utf-8").replace(/__README_URL__/g, "https://github.com/vitejs/vite-plugin-react/tree/main/packages/plugin-react-swc") : void 0
106
+ handler: (id) => id === runtimePublicPath ? readFileSync(join(import.meta.dirname, "refresh-runtime.js"), "utf-8").replace(/__README_URL__/g, "https://github.com/vitejs/vite-plugin-react/tree/main/packages/plugin-react-swc") : void 0
88
107
  }
89
108
  },
90
109
  {
@@ -100,7 +119,7 @@ const react = (_options) => {
100
119
  }),
101
120
  configResolved(config) {
102
121
  viteCacheRoot = config.cacheDir;
103
- if (config.server.hmr === false) hmrDisabled = true;
122
+ hmrDisabled = config.server.hmr === false;
104
123
  const mdxIndex = config.plugins.findIndex((p) => p.name === "@mdx-js/rollup");
105
124
  if (mdxIndex !== -1 && mdxIndex > config.plugins.findIndex((p) => p.name === "vite:react-swc")) throw new Error("[vite:react-swc] The MDX plugin should be placed before this plugin");
106
125
  if ("rolldownVersion" in vite && !options.plugins && !options.useAtYourOwnRisk_mutateSwcOptions && !options.disableOxcRecommendation) config.logger.warn("[vite:react-swc] We recommend switching to `@vitejs/plugin-react` for improved performance as no swc plugins are used. More information at https://vite.dev/rolldown");
@@ -129,7 +148,7 @@ const react = (_options) => {
129
148
  };
130
149
  }
131
150
  },
132
- options.plugins ? {
151
+ options.plugins || options.useAtYourOwnRisk_mutateSwcOptions ? {
133
152
  name: "vite:react-swc",
134
153
  apply: "build",
135
154
  enforce: "pre",
@@ -155,7 +174,11 @@ const react = (_options) => {
155
174
  configResolved(config) {
156
175
  viteCacheRoot = config.cacheDir;
157
176
  }
158
- }
177
+ },
178
+ virtualPreamblePlugin({
179
+ name: "@vitejs/plugin-react-swc/preamble",
180
+ isEnabled: () => !hmrDisabled
181
+ })
159
182
  ];
160
183
  };
161
184
  const transformWithOptions = async (id, code, target, options, viteCacheRoot, reactConfig) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vitejs/plugin-react-swc",
3
- "version": "4.1.0",
3
+ "version": "4.2.1",
4
4
  "license": "MIT",
5
5
  "author": "Arnaud Barré (https://github.com/ArnaudBarre)",
6
6
  "description": "Speed up your Vite dev server with SWC",
@@ -26,11 +26,14 @@
26
26
  },
27
27
  "homepage": "https://github.com/vitejs/vite-plugin-react/tree/main/packages/plugin-react-swc#readme",
28
28
  "dependencies": {
29
- "@rolldown/pluginutils": "1.0.0-beta.35",
29
+ "@rolldown/pluginutils": "1.0.0-beta.46",
30
30
  "@swc/core": "^1.13.5"
31
31
  },
32
32
  "peerDependencies": {
33
33
  "vite": "^4 || ^5 || ^6 || ^7"
34
34
  },
35
- "exports": "./index.js"
35
+ "exports": {
36
+ ".": "./index.js",
37
+ "./preamble": "./types/preamble.d.ts"
38
+ }
36
39
  }
@@ -0,0 +1 @@
1
+ export {}