@vitejs/plugin-react-swc 3.0.1 → 3.1.0

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
@@ -24,7 +24,7 @@ export default defineConfig({
24
24
 
25
25
  ## Caveats
26
26
 
27
- This plugin is only used in development and aims to be kept simple to enable good performances and be transpiler agnostic. Here is the list of non-configurable options that impact runtime behaviour:
27
+ This plugin has limited options to enable good performances and be transpiler agnostic. Here is the list of non-configurable options that impact runtime behaviour:
28
28
 
29
29
  - [useDefineForClassFields](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#the-usedefineforclassfields-flag-and-the-declare-property-modifier) is always activated, as this matches the current ECMAScript spec
30
30
  - `jsx runtime` is always `automatic`
@@ -34,17 +34,30 @@ This plugin is only used in development and aims to be kept simple to enable goo
34
34
  - JS files are not transformed
35
35
  - tsconfig is not resolved, so properties other than the ones listed above behaves like TS defaults
36
36
 
37
- ## Changing the JSX import source
37
+ ## Options
38
38
 
39
- You can use the jsxImportSource option like this:
39
+ ### jsxImportSource
40
+
41
+ Control where the JSX factory is imported from.
40
42
 
41
43
  ```ts
42
- import { defineConfig } from "vite";
43
- import react from "@vitejs/plugin-react-swc";
44
+ react({ jsxImportSource: "@emotion/react" });
45
+ ```
44
46
 
45
- export default defineConfig({
46
- plugins: [react({ jsxImportSource: "@emotion/react" })],
47
- });
47
+ ### tsDecorators
48
+
49
+ Enable TypeScript decorators. Requires `experimentalDecorators` in tsconfig.
50
+
51
+ ```ts
52
+ react({ tsDecorators: true });
53
+ ```
54
+
55
+ ## plugins
56
+
57
+ Use SWC plugins. Enable SWC at build time.
58
+
59
+ ```ts
60
+ react({ plugins: [["@swc/plugin-styled-components", {}]] });
48
61
  ```
49
62
 
50
63
  ## Consistent components exports
package/index.cjs CHANGED
@@ -3,6 +3,7 @@ var import_fs = require("fs");
3
3
  var import_path = require("path");
4
4
  var import_url = require("url");
5
5
  var import_core = require("@swc/core");
6
+ var import_module = require("module");
6
7
  var import_meta = {};
7
8
  var runtimePublicPath = "/@react-refresh";
8
9
  var preambleCode = `import { injectIntoGlobalHook } from "__PATH__";
@@ -10,72 +11,51 @@ injectIntoGlobalHook(window);
10
11
  window.$RefreshReg$ = () => {};
11
12
  window.$RefreshSig$ = () => (type) => type;`;
12
13
  var _dirname = typeof __dirname !== "undefined" ? __dirname : (0, import_path.dirname)((0, import_url.fileURLToPath)(import_meta.url));
14
+ var resolve = (0, import_module.createRequire)(
15
+ typeof __filename !== "undefined" ? __filename : import_meta.url
16
+ ).resolve;
13
17
  var refreshContentRE = /\$Refresh(?:Reg|Sig)\$\(/;
14
- var react = (options) => [
15
- {
16
- name: "vite:react-swc",
17
- apply: "serve",
18
- config: () => ({
19
- esbuild: false,
20
- optimizeDeps: { include: ["react/jsx-dev-runtime"] }
21
- }),
22
- resolveId: (id) => id === runtimePublicPath ? id : void 0,
23
- load: (id) => id === runtimePublicPath ? (0, import_fs.readFileSync)((0, import_path.join)(_dirname, "refresh-runtime.js"), "utf-8") : void 0,
24
- transformIndexHtml: (_, config) => [
25
- {
26
- tag: "script",
27
- attrs: { type: "module" },
28
- children: preambleCode.replace(
29
- "__PATH__",
30
- config.server.config.base + runtimePublicPath.slice(1)
31
- )
32
- }
33
- ],
34
- async transform(code, _id, transformOptions) {
35
- const id = _id.split("?")[0];
36
- if (id.includes("node_modules"))
37
- return;
38
- const parser = id.endsWith(".tsx") ? { syntax: "typescript", tsx: true } : id.endsWith(".ts") ? { syntax: "typescript", tsx: false } : id.endsWith(".jsx") ? { syntax: "ecmascript", jsx: true } : void 0;
39
- if (!parser)
40
- return;
41
- let result;
42
- try {
43
- result = await (0, import_core.transform)(code, {
44
- filename: id,
45
- swcrc: false,
46
- configFile: false,
47
- sourceMaps: true,
48
- jsc: {
49
- target: "es2020",
50
- parser,
51
- transform: {
52
- useDefineForClassFields: true,
53
- react: {
54
- refresh: !(transformOptions == null ? void 0 : transformOptions.ssr),
55
- development: true,
56
- useBuiltins: true,
57
- runtime: "automatic",
58
- importSource: options == null ? void 0 : options.jsxImportSource
59
- }
60
- }
61
- }
18
+ var react = (_options) => {
19
+ const options = {
20
+ jsxImportSource: _options == null ? void 0 : _options.jsxImportSource,
21
+ tsDecorators: _options == null ? void 0 : _options.tsDecorators,
22
+ plugins: (_options == null ? void 0 : _options.plugins) ? _options == null ? void 0 : _options.plugins.map((el) => [resolve(el[0]), el[1]]) : void 0
23
+ };
24
+ return [
25
+ {
26
+ name: "vite:react-swc",
27
+ apply: "serve",
28
+ config: () => ({
29
+ esbuild: false,
30
+ optimizeDeps: { include: ["react/jsx-dev-runtime"] }
31
+ }),
32
+ resolveId: (id) => id === runtimePublicPath ? id : void 0,
33
+ load: (id) => id === runtimePublicPath ? (0, import_fs.readFileSync)((0, import_path.join)(_dirname, "refresh-runtime.js"), "utf-8") : void 0,
34
+ transformIndexHtml: (_, config) => [
35
+ {
36
+ tag: "script",
37
+ attrs: { type: "module" },
38
+ children: preambleCode.replace(
39
+ "__PATH__",
40
+ config.server.config.base + runtimePublicPath.slice(1)
41
+ )
42
+ }
43
+ ],
44
+ async transform(code, _id, transformOptions) {
45
+ const id = _id.split("?")[0];
46
+ const result = await transformWithOptions(id, code, options, {
47
+ refresh: !(transformOptions == null ? void 0 : transformOptions.ssr),
48
+ development: true,
49
+ useBuiltins: true,
50
+ runtime: "automatic",
51
+ importSource: options == null ? void 0 : options.jsxImportSource
62
52
  });
63
- } catch (e) {
64
- const message = e.message;
65
- const fileStartIndex = message.indexOf("\u256D\u2500[");
66
- if (fileStartIndex !== -1) {
67
- const match = message.slice(fileStartIndex).match(/:(\d+):(\d+)]/);
68
- if (match) {
69
- e.line = match[1];
70
- e.column = match[2];
71
- }
53
+ if (!result)
54
+ return;
55
+ if ((transformOptions == null ? void 0 : transformOptions.ssr) || !refreshContentRE.test(result.code)) {
56
+ return result;
72
57
  }
73
- throw e;
74
- }
75
- if ((transformOptions == null ? void 0 : transformOptions.ssr) || !refreshContentRE.test(result.code)) {
76
- return result;
77
- }
78
- result.code = `import * as RefreshRuntime from "${runtimePublicPath}";
58
+ result.code = `import * as RefreshRuntime from "${runtimePublicPath}";
79
59
 
80
60
  if (!window.$RefreshReg$) throw new Error("React refresh preamble was not loaded. Something is wrong.");
81
61
  const prevRefreshReg = window.$RefreshReg$;
@@ -87,31 +67,81 @@ var react = (options) => [
87
67
 
88
68
  window.$RefreshReg$ = prevRefreshReg;
89
69
  window.$RefreshSig$ = prevRefreshSig;
90
- import.meta.hot.accept((nextExports) => {
91
- if (!nextExports) return;
92
- import(/* @vite-ignore */ import.meta.url).then((current) => {
93
- const invalidateMessage = RefreshRuntime.validateRefreshBoundaryAndEnqueueUpdate(current, nextExports);
94
- if (invalidateMessage) import.meta.hot.invalidate(invalidateMessage);
70
+ import(/* @vite-ignore */ import.meta.url).then((currentExports) => {
71
+ RefreshRuntime.registerExportsForReactRefresh("${id}", currentExports);
72
+ import.meta.hot.accept((nextExports) => {
73
+ if (!nextExports) return;
74
+ const invalidateMessage = RefreshRuntime.validateRefreshBoundaryAndEnqueueUpdate(currentExports, nextExports);
75
+ if (invalidateMessage) import.meta.hot.invalidate(invalidateMessage);
76
+ });
95
77
  });
96
- });
97
78
  `;
98
- const sourceMap = JSON.parse(result.map);
99
- sourceMap.mappings = ";;;;;;;;" + sourceMap.mappings;
100
- return { code: result.code, map: sourceMap };
79
+ const sourceMap = JSON.parse(result.map);
80
+ sourceMap.mappings = ";;;;;;;;" + sourceMap.mappings;
81
+ return { code: result.code, map: sourceMap };
82
+ }
83
+ },
84
+ options.plugins ? {
85
+ name: "vite:react-swc",
86
+ apply: "build",
87
+ transform: (code, _id) => transformWithOptions(_id.split("?")[0], code, options, {
88
+ useBuiltins: true,
89
+ runtime: "automatic",
90
+ importSource: options == null ? void 0 : options.jsxImportSource
91
+ })
92
+ } : {
93
+ name: "vite:react-swc",
94
+ apply: "build",
95
+ config: () => ({
96
+ esbuild: {
97
+ jsx: "automatic",
98
+ jsxImportSource: options == null ? void 0 : options.jsxImportSource,
99
+ tsconfigRaw: {
100
+ compilerOptions: { useDefineForClassFields: true }
101
+ }
102
+ }
103
+ })
101
104
  }
102
- },
103
- {
104
- name: "vite:react-swc",
105
- apply: "build",
106
- config: () => ({
107
- esbuild: {
108
- jsx: "automatic",
109
- jsxImportSource: options == null ? void 0 : options.jsxImportSource,
110
- tsconfigRaw: { compilerOptions: { useDefineForClassFields: true } }
105
+ ];
106
+ };
107
+ var transformWithOptions = async (id, code, options, reactConfig) => {
108
+ if (id.includes("node_modules"))
109
+ return;
110
+ const decorators = (options == null ? void 0 : options.tsDecorators) ?? false;
111
+ const parser = id.endsWith(".tsx") ? { syntax: "typescript", tsx: true, decorators } : id.endsWith(".ts") ? { syntax: "typescript", tsx: false, decorators } : id.endsWith(".jsx") ? { syntax: "ecmascript", jsx: true } : void 0;
112
+ if (!parser)
113
+ return;
114
+ let result;
115
+ try {
116
+ result = await (0, import_core.transform)(code, {
117
+ filename: id,
118
+ swcrc: false,
119
+ configFile: false,
120
+ sourceMaps: true,
121
+ jsc: {
122
+ target: "es2020",
123
+ parser,
124
+ experimental: { plugins: options.plugins },
125
+ transform: {
126
+ useDefineForClassFields: true,
127
+ react: reactConfig
128
+ }
129
+ }
130
+ });
131
+ } catch (e) {
132
+ const message = e.message;
133
+ const fileStartIndex = message.indexOf("\u256D\u2500[");
134
+ if (fileStartIndex !== -1) {
135
+ const match = message.slice(fileStartIndex).match(/:(\d+):(\d+)]/);
136
+ if (match) {
137
+ e.line = match[1];
138
+ e.column = match[2];
111
139
  }
112
- })
140
+ }
141
+ throw e;
113
142
  }
114
- ];
143
+ return result;
144
+ };
115
145
  var src_default = react;
116
146
 
117
147
  // <stdin>
package/index.d.ts CHANGED
@@ -5,6 +5,16 @@ type Options = {
5
5
  * @default "react"
6
6
  */
7
7
  jsxImportSource?: string;
8
+ /**
9
+ * Enable TypeScript decorators. Requires experimentalDecorators in tsconfig.
10
+ * @default false
11
+ */
12
+ tsDecorators?: boolean;
13
+ /**
14
+ * Use SWC plugins. Enable SWC at build time.
15
+ * @default undefined
16
+ */
17
+ plugins?: [string, Record<string, any>][];
8
18
  };
9
- declare const react: (options?: Options) => PluginOption[];
19
+ declare const react: (_options?: Options) => PluginOption[];
10
20
  export default react;
package/index.mjs CHANGED
@@ -3,78 +3,58 @@ import { readFileSync } from "fs";
3
3
  import { dirname, join } from "path";
4
4
  import { fileURLToPath } from "url";
5
5
  import { transform } from "@swc/core";
6
+ import { createRequire } from "module";
6
7
  var runtimePublicPath = "/@react-refresh";
7
8
  var preambleCode = `import { injectIntoGlobalHook } from "__PATH__";
8
9
  injectIntoGlobalHook(window);
9
10
  window.$RefreshReg$ = () => {};
10
11
  window.$RefreshSig$ = () => (type) => type;`;
11
12
  var _dirname = typeof __dirname !== "undefined" ? __dirname : dirname(fileURLToPath(import.meta.url));
13
+ var resolve = createRequire(
14
+ typeof __filename !== "undefined" ? __filename : import.meta.url
15
+ ).resolve;
12
16
  var refreshContentRE = /\$Refresh(?:Reg|Sig)\$\(/;
13
- var react = (options) => [
14
- {
15
- name: "vite:react-swc",
16
- apply: "serve",
17
- config: () => ({
18
- esbuild: false,
19
- optimizeDeps: { include: ["react/jsx-dev-runtime"] }
20
- }),
21
- resolveId: (id) => id === runtimePublicPath ? id : void 0,
22
- load: (id) => id === runtimePublicPath ? readFileSync(join(_dirname, "refresh-runtime.js"), "utf-8") : void 0,
23
- transformIndexHtml: (_, config) => [
24
- {
25
- tag: "script",
26
- attrs: { type: "module" },
27
- children: preambleCode.replace(
28
- "__PATH__",
29
- config.server.config.base + runtimePublicPath.slice(1)
30
- )
31
- }
32
- ],
33
- async transform(code, _id, transformOptions) {
34
- const id = _id.split("?")[0];
35
- if (id.includes("node_modules"))
36
- return;
37
- const parser = id.endsWith(".tsx") ? { syntax: "typescript", tsx: true } : id.endsWith(".ts") ? { syntax: "typescript", tsx: false } : id.endsWith(".jsx") ? { syntax: "ecmascript", jsx: true } : void 0;
38
- if (!parser)
39
- return;
40
- let result;
41
- try {
42
- result = await transform(code, {
43
- filename: id,
44
- swcrc: false,
45
- configFile: false,
46
- sourceMaps: true,
47
- jsc: {
48
- target: "es2020",
49
- parser,
50
- transform: {
51
- useDefineForClassFields: true,
52
- react: {
53
- refresh: !(transformOptions == null ? void 0 : transformOptions.ssr),
54
- development: true,
55
- useBuiltins: true,
56
- runtime: "automatic",
57
- importSource: options == null ? void 0 : options.jsxImportSource
58
- }
59
- }
60
- }
17
+ var react = (_options) => {
18
+ const options = {
19
+ jsxImportSource: _options == null ? void 0 : _options.jsxImportSource,
20
+ tsDecorators: _options == null ? void 0 : _options.tsDecorators,
21
+ plugins: (_options == null ? void 0 : _options.plugins) ? _options == null ? void 0 : _options.plugins.map((el) => [resolve(el[0]), el[1]]) : void 0
22
+ };
23
+ return [
24
+ {
25
+ name: "vite:react-swc",
26
+ apply: "serve",
27
+ config: () => ({
28
+ esbuild: false,
29
+ optimizeDeps: { include: ["react/jsx-dev-runtime"] }
30
+ }),
31
+ resolveId: (id) => id === runtimePublicPath ? id : void 0,
32
+ load: (id) => id === runtimePublicPath ? readFileSync(join(_dirname, "refresh-runtime.js"), "utf-8") : void 0,
33
+ transformIndexHtml: (_, config) => [
34
+ {
35
+ tag: "script",
36
+ attrs: { type: "module" },
37
+ children: preambleCode.replace(
38
+ "__PATH__",
39
+ config.server.config.base + runtimePublicPath.slice(1)
40
+ )
41
+ }
42
+ ],
43
+ async transform(code, _id, transformOptions) {
44
+ const id = _id.split("?")[0];
45
+ const result = await transformWithOptions(id, code, options, {
46
+ refresh: !(transformOptions == null ? void 0 : transformOptions.ssr),
47
+ development: true,
48
+ useBuiltins: true,
49
+ runtime: "automatic",
50
+ importSource: options == null ? void 0 : options.jsxImportSource
61
51
  });
62
- } catch (e) {
63
- const message = e.message;
64
- const fileStartIndex = message.indexOf("\u256D\u2500[");
65
- if (fileStartIndex !== -1) {
66
- const match = message.slice(fileStartIndex).match(/:(\d+):(\d+)]/);
67
- if (match) {
68
- e.line = match[1];
69
- e.column = match[2];
70
- }
52
+ if (!result)
53
+ return;
54
+ if ((transformOptions == null ? void 0 : transformOptions.ssr) || !refreshContentRE.test(result.code)) {
55
+ return result;
71
56
  }
72
- throw e;
73
- }
74
- if ((transformOptions == null ? void 0 : transformOptions.ssr) || !refreshContentRE.test(result.code)) {
75
- return result;
76
- }
77
- result.code = `import * as RefreshRuntime from "${runtimePublicPath}";
57
+ result.code = `import * as RefreshRuntime from "${runtimePublicPath}";
78
58
 
79
59
  if (!window.$RefreshReg$) throw new Error("React refresh preamble was not loaded. Something is wrong.");
80
60
  const prevRefreshReg = window.$RefreshReg$;
@@ -86,31 +66,81 @@ var react = (options) => [
86
66
 
87
67
  window.$RefreshReg$ = prevRefreshReg;
88
68
  window.$RefreshSig$ = prevRefreshSig;
89
- import.meta.hot.accept((nextExports) => {
90
- if (!nextExports) return;
91
- import(/* @vite-ignore */ import.meta.url).then((current) => {
92
- const invalidateMessage = RefreshRuntime.validateRefreshBoundaryAndEnqueueUpdate(current, nextExports);
93
- if (invalidateMessage) import.meta.hot.invalidate(invalidateMessage);
69
+ import(/* @vite-ignore */ import.meta.url).then((currentExports) => {
70
+ RefreshRuntime.registerExportsForReactRefresh("${id}", currentExports);
71
+ import.meta.hot.accept((nextExports) => {
72
+ if (!nextExports) return;
73
+ const invalidateMessage = RefreshRuntime.validateRefreshBoundaryAndEnqueueUpdate(currentExports, nextExports);
74
+ if (invalidateMessage) import.meta.hot.invalidate(invalidateMessage);
75
+ });
94
76
  });
95
- });
96
77
  `;
97
- const sourceMap = JSON.parse(result.map);
98
- sourceMap.mappings = ";;;;;;;;" + sourceMap.mappings;
99
- return { code: result.code, map: sourceMap };
78
+ const sourceMap = JSON.parse(result.map);
79
+ sourceMap.mappings = ";;;;;;;;" + sourceMap.mappings;
80
+ return { code: result.code, map: sourceMap };
81
+ }
82
+ },
83
+ options.plugins ? {
84
+ name: "vite:react-swc",
85
+ apply: "build",
86
+ transform: (code, _id) => transformWithOptions(_id.split("?")[0], code, options, {
87
+ useBuiltins: true,
88
+ runtime: "automatic",
89
+ importSource: options == null ? void 0 : options.jsxImportSource
90
+ })
91
+ } : {
92
+ name: "vite:react-swc",
93
+ apply: "build",
94
+ config: () => ({
95
+ esbuild: {
96
+ jsx: "automatic",
97
+ jsxImportSource: options == null ? void 0 : options.jsxImportSource,
98
+ tsconfigRaw: {
99
+ compilerOptions: { useDefineForClassFields: true }
100
+ }
101
+ }
102
+ })
100
103
  }
101
- },
102
- {
103
- name: "vite:react-swc",
104
- apply: "build",
105
- config: () => ({
106
- esbuild: {
107
- jsx: "automatic",
108
- jsxImportSource: options == null ? void 0 : options.jsxImportSource,
109
- tsconfigRaw: { compilerOptions: { useDefineForClassFields: true } }
104
+ ];
105
+ };
106
+ var transformWithOptions = async (id, code, options, reactConfig) => {
107
+ if (id.includes("node_modules"))
108
+ return;
109
+ const decorators = (options == null ? void 0 : options.tsDecorators) ?? false;
110
+ const parser = id.endsWith(".tsx") ? { syntax: "typescript", tsx: true, decorators } : id.endsWith(".ts") ? { syntax: "typescript", tsx: false, decorators } : id.endsWith(".jsx") ? { syntax: "ecmascript", jsx: true } : void 0;
111
+ if (!parser)
112
+ return;
113
+ let result;
114
+ try {
115
+ result = await transform(code, {
116
+ filename: id,
117
+ swcrc: false,
118
+ configFile: false,
119
+ sourceMaps: true,
120
+ jsc: {
121
+ target: "es2020",
122
+ parser,
123
+ experimental: { plugins: options.plugins },
124
+ transform: {
125
+ useDefineForClassFields: true,
126
+ react: reactConfig
127
+ }
110
128
  }
111
- })
129
+ });
130
+ } catch (e) {
131
+ const message = e.message;
132
+ const fileStartIndex = message.indexOf("\u256D\u2500[");
133
+ if (fileStartIndex !== -1) {
134
+ const match = message.slice(fileStartIndex).match(/:(\d+):(\d+)]/);
135
+ if (match) {
136
+ e.line = match[1];
137
+ e.column = match[2];
138
+ }
139
+ }
140
+ throw e;
112
141
  }
113
- ];
142
+ return result;
143
+ };
114
144
  var src_default = react;
115
145
  export {
116
146
  src_default as default
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vitejs/plugin-react-swc",
3
3
  "description": "Speed up your Vite dev server with SWC",
4
- "version": "3.0.1",
4
+ "version": "3.1.0",
5
5
  "author": "Arnaud Barré (https://github.com/ArnaudBarre)",
6
6
  "license": "MIT",
7
7
  "repository": "github:vitejs/vite-plugin-react-swc",
@@ -10,8 +10,8 @@
10
10
  "module": "index.mjs",
11
11
  "exports": {
12
12
  ".": {
13
- "require": "./index.cjs",
14
13
  "types": "./index.d.ts",
14
+ "require": "./index.cjs",
15
15
  "import": "./index.mjs"
16
16
  }
17
17
  },
@@ -27,6 +27,6 @@
27
27
  "vite": "^4"
28
28
  },
29
29
  "dependencies": {
30
- "@swc/core": "^1.3.22"
30
+ "@swc/core": "^1.3.30"
31
31
  }
32
32
  }
@@ -4,12 +4,12 @@ const REACT_MEMO_TYPE = Symbol.for("react.memo");
4
4
  let allFamiliesByID = /* @__PURE__ */ new Map();
5
5
  let allFamiliesByType = /* @__PURE__ */ new WeakMap();
6
6
  let allSignaturesByType = /* @__PURE__ */ new WeakMap();
7
- let updatedFamiliesByType = /* @__PURE__ */ new WeakMap();
7
+ const updatedFamiliesByType = /* @__PURE__ */ new WeakMap();
8
8
  let pendingUpdates = [];
9
- let helpersByRendererID = /* @__PURE__ */ new Map();
10
- let helpersByRoot = /* @__PURE__ */ new Map();
11
- let mountedRoots = /* @__PURE__ */ new Set();
12
- let failedRoots = /* @__PURE__ */ new Set();
9
+ const helpersByRendererID = /* @__PURE__ */ new Map();
10
+ const helpersByRoot = /* @__PURE__ */ new Map();
11
+ const mountedRoots = /* @__PURE__ */ new Set();
12
+ const failedRoots = /* @__PURE__ */ new Set();
13
13
  let rootElements = /* @__PURE__ */ new WeakMap();
14
14
  let isPerformingRefresh = false;
15
15
  function computeFullKey(signature) {
@@ -26,17 +26,17 @@ function computeFullKey(signature) {
26
26
  return fullKey;
27
27
  }
28
28
  for (let i = 0; i < hooks.length; i++) {
29
- let hook = hooks[i];
29
+ const hook = hooks[i];
30
30
  if (typeof hook !== "function") {
31
31
  signature.forceReset = true;
32
32
  signature.fullKey = fullKey;
33
33
  return fullKey;
34
34
  }
35
- let nestedHookSignature = allSignaturesByType.get(hook);
35
+ const nestedHookSignature = allSignaturesByType.get(hook);
36
36
  if (nestedHookSignature === void 0) {
37
37
  continue;
38
38
  }
39
- let nestedHookKey = computeFullKey(nestedHookSignature);
39
+ const nestedHookKey = computeFullKey(nestedHookSignature);
40
40
  if (nestedHookSignature.forceReset) {
41
41
  signature.forceReset = true;
42
42
  }
@@ -46,8 +46,8 @@ function computeFullKey(signature) {
46
46
  return fullKey;
47
47
  }
48
48
  function haveEqualSignatures(prevType, nextType) {
49
- let prevSignature = allSignaturesByType.get(prevType);
50
- let nextSignature = allSignaturesByType.get(nextType);
49
+ const prevSignature = allSignaturesByType.get(prevType);
50
+ const nextSignature = allSignaturesByType.get(nextType);
51
51
  if (prevSignature === void 0 && nextSignature === void 0) {
52
52
  return true;
53
53
  }
@@ -93,13 +93,12 @@ function performReactRefresh() {
93
93
  }
94
94
  isPerformingRefresh = true;
95
95
  try {
96
- let staleFamilies = /* @__PURE__ */ new Set();
97
- let updatedFamilies = /* @__PURE__ */ new Set();
98
- let updates = pendingUpdates;
96
+ const staleFamilies = /* @__PURE__ */ new Set();
97
+ const updatedFamilies = /* @__PURE__ */ new Set();
98
+ const updates = pendingUpdates;
99
99
  pendingUpdates = [];
100
- updates.forEach(function(_ref) {
101
- let family = _ref[0], nextType = _ref[1];
102
- let prevType = family.current;
100
+ updates.forEach(([family, nextType]) => {
101
+ const prevType = family.current;
103
102
  updatedFamiliesByType.set(prevType, family);
104
103
  updatedFamiliesByType.set(nextType, family);
105
104
  family.current = nextType;
@@ -109,20 +108,22 @@ function performReactRefresh() {
109
108
  staleFamilies.add(family);
110
109
  }
111
110
  });
112
- let update = {
111
+ const update = {
113
112
  updatedFamilies,
113
+ // Families that will re-render preserving state
114
114
  staleFamilies
115
+ // Families that will be remounted
115
116
  };
116
- helpersByRendererID.forEach(function(helpers) {
117
+ helpersByRendererID.forEach((helpers) => {
117
118
  helpers.setRefreshHandler(resolveFamily);
118
119
  });
119
120
  let didError = false;
120
121
  let firstError = null;
121
- let failedRootsSnapshot = new Set(failedRoots);
122
- let mountedRootsSnapshot = new Set(mountedRoots);
123
- let helpersByRootSnapshot = new Map(helpersByRoot);
124
- failedRootsSnapshot.forEach(function(root) {
125
- let helpers = helpersByRootSnapshot.get(root);
122
+ const failedRootsSnapshot = new Set(failedRoots);
123
+ const mountedRootsSnapshot = new Set(mountedRoots);
124
+ const helpersByRootSnapshot = new Map(helpersByRoot);
125
+ failedRootsSnapshot.forEach((root) => {
126
+ const helpers = helpersByRootSnapshot.get(root);
126
127
  if (helpers === void 0) {
127
128
  throw new Error(
128
129
  "Could not find helpers for a root. This is a bug in React Refresh."
@@ -130,10 +131,13 @@ function performReactRefresh() {
130
131
  }
131
132
  if (!failedRoots.has(root)) {
132
133
  }
134
+ if (rootElements === null) {
135
+ return;
136
+ }
133
137
  if (!rootElements.has(root)) {
134
138
  return;
135
139
  }
136
- let element = rootElements.get(root);
140
+ const element = rootElements.get(root);
137
141
  try {
138
142
  helpers.scheduleRoot(root, element);
139
143
  } catch (err) {
@@ -143,8 +147,8 @@ function performReactRefresh() {
143
147
  }
144
148
  }
145
149
  });
146
- mountedRootsSnapshot.forEach(function(root) {
147
- let helpers = helpersByRootSnapshot.get(root);
150
+ mountedRootsSnapshot.forEach((root) => {
151
+ const helpers = helpersByRootSnapshot.get(root);
148
152
  if (helpers === void 0) {
149
153
  throw new Error(
150
154
  "Could not find helpers for a root. This is a bug in React Refresh."
@@ -169,14 +173,6 @@ function performReactRefresh() {
169
173
  isPerformingRefresh = false;
170
174
  }
171
175
  }
172
- function debounce(fn, delay) {
173
- let handle;
174
- return () => {
175
- clearTimeout(handle);
176
- handle = setTimeout(fn, delay);
177
- };
178
- }
179
- const enqueueUpdate = debounce(performReactRefresh, 16);
180
176
  function register(type, id) {
181
177
  if (type === null) {
182
178
  return;
@@ -189,9 +185,7 @@ function register(type, id) {
189
185
  }
190
186
  let family = allFamiliesByID.get(id);
191
187
  if (family === void 0) {
192
- family = {
193
- current: type
194
- };
188
+ family = { current: type };
195
189
  allFamiliesByID.set(id, family);
196
190
  } else {
197
191
  pendingUpdates.push([family, type]);
@@ -208,18 +202,13 @@ function register(type, id) {
208
202
  }
209
203
  }
210
204
  }
211
- function getRefreshReg(filename) {
212
- return (type, id) => register(type, filename + " " + id);
213
- }
214
- function setSignature(type, key, forceReset = false, getCustomHooks) {
205
+ function setSignature(type, key, forceReset, getCustomHooks) {
215
206
  if (!allSignaturesByType.has(type)) {
216
207
  allSignaturesByType.set(type, {
217
208
  forceReset,
218
209
  ownKey: key,
219
210
  fullKey: null,
220
- getCustomHooks: getCustomHooks || function() {
221
- return [];
222
- }
211
+ getCustomHooks: getCustomHooks || (() => [])
223
212
  });
224
213
  }
225
214
  if (typeof type === "object" && type !== null) {
@@ -234,7 +223,7 @@ function setSignature(type, key, forceReset = false, getCustomHooks) {
234
223
  }
235
224
  }
236
225
  function collectCustomHooksForSignature(type) {
237
- let signature = allSignaturesByType.get(type);
226
+ const signature = allSignaturesByType.get(type);
238
227
  if (signature !== void 0) {
239
228
  computeFullKey(signature);
240
229
  }
@@ -246,14 +235,12 @@ function injectIntoGlobalHook(globalObject) {
246
235
  globalObject.__REACT_DEVTOOLS_GLOBAL_HOOK__ = hook = {
247
236
  renderers: /* @__PURE__ */ new Map(),
248
237
  supportsFiber: true,
249
- inject: function(injected) {
250
- return nextID++;
251
- },
252
- onScheduleFiberRoot: function(id, root, children) {
238
+ inject: (injected) => nextID++,
239
+ onScheduleFiberRoot: (id, root, children) => {
253
240
  },
254
- onCommitFiberRoot: function(id, root, maybePriorityLevel, didError) {
241
+ onCommitFiberRoot: (id, root, maybePriorityLevel, didError) => {
255
242
  },
256
- onCommitFiberUnmount: function() {
243
+ onCommitFiberUnmount() {
257
244
  }
258
245
  };
259
246
  }
@@ -263,38 +250,40 @@ function injectIntoGlobalHook(globalObject) {
263
250
  );
264
251
  return;
265
252
  }
266
- let oldInject = hook.inject;
253
+ const oldInject = hook.inject;
267
254
  hook.inject = function(injected) {
268
- let id = oldInject.apply(this, arguments);
255
+ const id = oldInject.apply(this, arguments);
269
256
  if (typeof injected.scheduleRefresh === "function" && typeof injected.setRefreshHandler === "function") {
270
257
  helpersByRendererID.set(id, injected);
271
258
  }
272
259
  return id;
273
260
  };
274
- hook.renderers.forEach(function(injected, id) {
261
+ hook.renderers.forEach((injected, id) => {
275
262
  if (typeof injected.scheduleRefresh === "function" && typeof injected.setRefreshHandler === "function") {
276
263
  helpersByRendererID.set(id, injected);
277
264
  }
278
265
  });
279
- let oldOnCommitFiberRoot = hook.onCommitFiberRoot;
280
- let oldOnScheduleFiberRoot = hook.onScheduleFiberRoot || function() {
281
- };
266
+ const oldOnCommitFiberRoot = hook.onCommitFiberRoot;
267
+ const oldOnScheduleFiberRoot = hook.onScheduleFiberRoot || (() => {
268
+ });
282
269
  hook.onScheduleFiberRoot = function(id, root, children) {
283
270
  if (!isPerformingRefresh) {
284
271
  failedRoots.delete(root);
285
- rootElements.set(root, children);
272
+ if (rootElements !== null) {
273
+ rootElements.set(root, children);
274
+ }
286
275
  }
287
276
  return oldOnScheduleFiberRoot.apply(this, arguments);
288
277
  };
289
278
  hook.onCommitFiberRoot = function(id, root, maybePriorityLevel, didError) {
290
- let helpers = helpersByRendererID.get(id);
279
+ const helpers = helpersByRendererID.get(id);
291
280
  if (helpers !== void 0) {
292
281
  helpersByRoot.set(root, helpers);
293
- let current = root.current;
294
- let alternate = current.alternate;
282
+ const current = root.current;
283
+ const alternate = current.alternate;
295
284
  if (alternate !== null) {
296
- let wasMounted = alternate.memoizedState != null && alternate.memoizedState.element != null;
297
- let isMounted = current.memoizedState != null && current.memoizedState.element != null;
285
+ const wasMounted = alternate.memoizedState != null && alternate.memoizedState.element != null && mountedRoots.has(root);
286
+ const isMounted = current.memoizedState != null && current.memoizedState.element != null;
298
287
  if (!wasMounted && isMounted) {
299
288
  mountedRoots.add(root);
300
289
  failedRoots.delete(root);
@@ -375,6 +364,27 @@ function isLikelyComponentType(type) {
375
364
  }
376
365
  }
377
366
  }
367
+ function getRefreshReg(filename) {
368
+ return (type, id) => register(type, filename + " " + id);
369
+ }
370
+ function registerExportsForReactRefresh(filename, moduleExports) {
371
+ for (const key in moduleExports) {
372
+ if (key === "__esModule")
373
+ continue;
374
+ const exportValue = moduleExports[key];
375
+ if (isLikelyComponentType(exportValue)) {
376
+ register(exportValue, filename + " " + key);
377
+ }
378
+ }
379
+ }
380
+ function debounce(fn, delay) {
381
+ let handle;
382
+ return () => {
383
+ clearTimeout(handle);
384
+ handle = setTimeout(fn, delay);
385
+ };
386
+ }
387
+ const enqueueUpdate = debounce(performReactRefresh, 16);
378
388
  function validateRefreshBoundaryAndEnqueueUpdate(prevExports, nextExports) {
379
389
  if (!predicateOnExport(prevExports, (key) => !!nextExports[key])) {
380
390
  return "Could not Fast Refresh (export removed)";
@@ -409,16 +419,12 @@ function predicateOnExport(moduleExports, predicate) {
409
419
  }
410
420
  return true;
411
421
  }
412
- var refresh_runtime_default = {
413
- getRefreshReg,
414
- injectIntoGlobalHook,
415
- createSignatureFunctionForTransform,
416
- validateRefreshBoundaryAndEnqueueUpdate
417
- };
422
+ var refresh_runtime_default = { injectIntoGlobalHook };
418
423
  export {
419
424
  createSignatureFunctionForTransform,
420
425
  refresh_runtime_default as default,
421
426
  getRefreshReg,
422
427
  injectIntoGlobalHook,
428
+ registerExportsForReactRefresh,
423
429
  validateRefreshBoundaryAndEnqueueUpdate
424
430
  };