@vitejs/plugin-react-swc 3.0.0-beta.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/LICENSE +21 -0
- package/README.md +43 -0
- package/index.cjs +129 -0
- package/index.d.ts +3 -0
- package/index.mjs +105 -0
- package/package.json +32 -0
- package/refresh-runtime.js +417 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Arnaud Barré (https://github.com/ArnaudBarre)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# @vitejs/plugin-react-swc [](https://www.npmjs.com/package/@vitejs/plugin-react-swc)
|
|
2
|
+
|
|
3
|
+
Use the versatility of [SWC](https://swc.rs/) for development and the maturity of [esbuild](https://esbuild.github.io/) for production.
|
|
4
|
+
|
|
5
|
+
- ✅ A fast Fast Refresh (~20x faster than Babel)
|
|
6
|
+
- ✅ Enable [automatic JSX runtime](https://reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html)
|
|
7
|
+
|
|
8
|
+
> ⚠️ This is an alpha release for Vite 4, for the v2 named `vite-plugin-swc-react-refresh`, see the [v2 branch](https://github.com/vitejs/vite-plugin-react-swc/tree/v2).
|
|
9
|
+
|
|
10
|
+
## Installation
|
|
11
|
+
|
|
12
|
+
```sh
|
|
13
|
+
npm i -D @vitejs/plugin-react-swc
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Usage
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { defineConfig } from "vite";
|
|
20
|
+
import react from "@vitejs/plugin-react-swc";
|
|
21
|
+
|
|
22
|
+
export default defineConfig({
|
|
23
|
+
plugins: [react()],
|
|
24
|
+
});
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Caveats
|
|
28
|
+
|
|
29
|
+
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:
|
|
30
|
+
|
|
31
|
+
- [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
|
|
32
|
+
- `jsx runtime` is always `automatic`
|
|
33
|
+
- In development:
|
|
34
|
+
- esbuild is disabled, so the [esbuild configuration](https://vitejs.dev/config/shared-options.html#esbuild) has no effect
|
|
35
|
+
- `target` is `es2020`
|
|
36
|
+
- JS files are not transformed
|
|
37
|
+
- tsconfig is not resolved, so properties other than the ones listed above behaves like TS defaults
|
|
38
|
+
|
|
39
|
+
## Consistent components exports
|
|
40
|
+
|
|
41
|
+
For React refresh to work, 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).
|
|
42
|
+
|
|
43
|
+
You can catch mistakes and get more detailed warning with this [eslint rule](https://github.com/ArnaudBarre/eslint-plugin-react-refresh).
|
package/index.cjs
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
default: () => src_default
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(src_exports);
|
|
26
|
+
var import_fs = require("fs");
|
|
27
|
+
var import_path = require("path");
|
|
28
|
+
var import_url = require("url");
|
|
29
|
+
var import_core = require("@swc/core");
|
|
30
|
+
var import_meta = {};
|
|
31
|
+
var runtimePublicPath = "/@react-refresh";
|
|
32
|
+
var preambleCode = `import { injectIntoGlobalHook } from "${runtimePublicPath}";
|
|
33
|
+
injectIntoGlobalHook(window);
|
|
34
|
+
window.$RefreshReg$ = () => {};
|
|
35
|
+
window.$RefreshSig$ = () => (type) => type;`;
|
|
36
|
+
var _dirname = typeof __dirname !== "undefined" ? __dirname : (0, import_path.dirname)((0, import_url.fileURLToPath)(import_meta.url));
|
|
37
|
+
var react = () => [
|
|
38
|
+
{
|
|
39
|
+
name: "@vitejs/plugin-react-swc",
|
|
40
|
+
apply: "serve",
|
|
41
|
+
config: () => ({
|
|
42
|
+
esbuild: false,
|
|
43
|
+
optimizeDeps: { include: ["react/jsx-dev-runtime"] }
|
|
44
|
+
}),
|
|
45
|
+
resolveId: (id) => id === runtimePublicPath ? id : void 0,
|
|
46
|
+
load: (id) => id === runtimePublicPath ? (0, import_fs.readFileSync)((0, import_path.join)(_dirname, "refresh-runtime.js"), "utf-8") : void 0,
|
|
47
|
+
transformIndexHtml: () => [
|
|
48
|
+
{ tag: "script", attrs: { type: "module" }, children: preambleCode }
|
|
49
|
+
],
|
|
50
|
+
async transform(code, id, transformOptions) {
|
|
51
|
+
if (id.includes("node_modules"))
|
|
52
|
+
return;
|
|
53
|
+
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;
|
|
54
|
+
if (!parser)
|
|
55
|
+
return;
|
|
56
|
+
let result;
|
|
57
|
+
try {
|
|
58
|
+
result = await (0, import_core.transform)(code, {
|
|
59
|
+
filename: id,
|
|
60
|
+
swcrc: false,
|
|
61
|
+
configFile: false,
|
|
62
|
+
sourceMaps: true,
|
|
63
|
+
jsc: {
|
|
64
|
+
target: "es2020",
|
|
65
|
+
parser,
|
|
66
|
+
transform: {
|
|
67
|
+
useDefineForClassFields: true,
|
|
68
|
+
react: {
|
|
69
|
+
refresh: !(transformOptions == null ? void 0 : transformOptions.ssr),
|
|
70
|
+
development: true,
|
|
71
|
+
useBuiltins: true,
|
|
72
|
+
runtime: "automatic"
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
} catch (e) {
|
|
78
|
+
const message = e.message;
|
|
79
|
+
const fileStartIndex = message.indexOf("\u256D\u2500[");
|
|
80
|
+
if (fileStartIndex !== -1) {
|
|
81
|
+
const match = message.slice(fileStartIndex).match(/:(\d+):(\d+)]/);
|
|
82
|
+
if (match) {
|
|
83
|
+
e.line = match[1];
|
|
84
|
+
e.column = match[2];
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
throw e;
|
|
88
|
+
}
|
|
89
|
+
if (!result.code.includes("$RefreshReg$"))
|
|
90
|
+
return result;
|
|
91
|
+
result.code = `import * as RefreshRuntime from "${runtimePublicPath}";
|
|
92
|
+
|
|
93
|
+
if (!window.$RefreshReg$) throw new Error("React refresh preamble was not loaded. Something is wrong.");
|
|
94
|
+
const prevRefreshReg = window.$RefreshReg$;
|
|
95
|
+
const prevRefreshSig = window.$RefreshSig$;
|
|
96
|
+
window.$RefreshReg$ = RefreshRuntime.getRefreshReg("${id}");
|
|
97
|
+
window.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform;
|
|
98
|
+
|
|
99
|
+
${result.code}
|
|
100
|
+
|
|
101
|
+
window.$RefreshReg$ = prevRefreshReg;
|
|
102
|
+
window.$RefreshSig$ = prevRefreshSig;
|
|
103
|
+
import.meta.hot.accept((nextExports) => {
|
|
104
|
+
if (!nextExports) return;
|
|
105
|
+
import(/* @vite-ignore */ import.meta.url).then((current) => {
|
|
106
|
+
const invalidateMessage = RefreshRuntime.validateRefreshBoundaryAndEnqueueUpdate(current, nextExports);
|
|
107
|
+
if (invalidateMessage) import.meta.hot.invalidate(invalidateMessage);
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
`;
|
|
111
|
+
const sourceMap = JSON.parse(result.map);
|
|
112
|
+
sourceMap.mappings = ";;;;;;;;" + sourceMap.mappings;
|
|
113
|
+
return { code: result.code, map: sourceMap };
|
|
114
|
+
}
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
name: "@vitejs/plugin-react-swc",
|
|
118
|
+
apply: "build",
|
|
119
|
+
config: () => ({
|
|
120
|
+
esbuild: {
|
|
121
|
+
jsx: "automatic",
|
|
122
|
+
tsconfigRaw: { compilerOptions: { useDefineForClassFields: true } }
|
|
123
|
+
}
|
|
124
|
+
})
|
|
125
|
+
}
|
|
126
|
+
];
|
|
127
|
+
var src_default = react;
|
|
128
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
129
|
+
0 && (module.exports = {});
|
package/index.d.ts
ADDED
package/index.mjs
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { readFileSync } from "fs";
|
|
3
|
+
import { dirname, join } from "path";
|
|
4
|
+
import { fileURLToPath } from "url";
|
|
5
|
+
import { transform } from "@swc/core";
|
|
6
|
+
var runtimePublicPath = "/@react-refresh";
|
|
7
|
+
var preambleCode = `import { injectIntoGlobalHook } from "${runtimePublicPath}";
|
|
8
|
+
injectIntoGlobalHook(window);
|
|
9
|
+
window.$RefreshReg$ = () => {};
|
|
10
|
+
window.$RefreshSig$ = () => (type) => type;`;
|
|
11
|
+
var _dirname = typeof __dirname !== "undefined" ? __dirname : dirname(fileURLToPath(import.meta.url));
|
|
12
|
+
var react = () => [
|
|
13
|
+
{
|
|
14
|
+
name: "@vitejs/plugin-react-swc",
|
|
15
|
+
apply: "serve",
|
|
16
|
+
config: () => ({
|
|
17
|
+
esbuild: false,
|
|
18
|
+
optimizeDeps: { include: ["react/jsx-dev-runtime"] }
|
|
19
|
+
}),
|
|
20
|
+
resolveId: (id) => id === runtimePublicPath ? id : void 0,
|
|
21
|
+
load: (id) => id === runtimePublicPath ? readFileSync(join(_dirname, "refresh-runtime.js"), "utf-8") : void 0,
|
|
22
|
+
transformIndexHtml: () => [
|
|
23
|
+
{ tag: "script", attrs: { type: "module" }, children: preambleCode }
|
|
24
|
+
],
|
|
25
|
+
async transform(code, id, transformOptions) {
|
|
26
|
+
if (id.includes("node_modules"))
|
|
27
|
+
return;
|
|
28
|
+
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;
|
|
29
|
+
if (!parser)
|
|
30
|
+
return;
|
|
31
|
+
let result;
|
|
32
|
+
try {
|
|
33
|
+
result = await transform(code, {
|
|
34
|
+
filename: id,
|
|
35
|
+
swcrc: false,
|
|
36
|
+
configFile: false,
|
|
37
|
+
sourceMaps: true,
|
|
38
|
+
jsc: {
|
|
39
|
+
target: "es2020",
|
|
40
|
+
parser,
|
|
41
|
+
transform: {
|
|
42
|
+
useDefineForClassFields: true,
|
|
43
|
+
react: {
|
|
44
|
+
refresh: !(transformOptions == null ? void 0 : transformOptions.ssr),
|
|
45
|
+
development: true,
|
|
46
|
+
useBuiltins: true,
|
|
47
|
+
runtime: "automatic"
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
} catch (e) {
|
|
53
|
+
const message = e.message;
|
|
54
|
+
const fileStartIndex = message.indexOf("\u256D\u2500[");
|
|
55
|
+
if (fileStartIndex !== -1) {
|
|
56
|
+
const match = message.slice(fileStartIndex).match(/:(\d+):(\d+)]/);
|
|
57
|
+
if (match) {
|
|
58
|
+
e.line = match[1];
|
|
59
|
+
e.column = match[2];
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
throw e;
|
|
63
|
+
}
|
|
64
|
+
if (!result.code.includes("$RefreshReg$"))
|
|
65
|
+
return result;
|
|
66
|
+
result.code = `import * as RefreshRuntime from "${runtimePublicPath}";
|
|
67
|
+
|
|
68
|
+
if (!window.$RefreshReg$) throw new Error("React refresh preamble was not loaded. Something is wrong.");
|
|
69
|
+
const prevRefreshReg = window.$RefreshReg$;
|
|
70
|
+
const prevRefreshSig = window.$RefreshSig$;
|
|
71
|
+
window.$RefreshReg$ = RefreshRuntime.getRefreshReg("${id}");
|
|
72
|
+
window.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform;
|
|
73
|
+
|
|
74
|
+
${result.code}
|
|
75
|
+
|
|
76
|
+
window.$RefreshReg$ = prevRefreshReg;
|
|
77
|
+
window.$RefreshSig$ = prevRefreshSig;
|
|
78
|
+
import.meta.hot.accept((nextExports) => {
|
|
79
|
+
if (!nextExports) return;
|
|
80
|
+
import(/* @vite-ignore */ import.meta.url).then((current) => {
|
|
81
|
+
const invalidateMessage = RefreshRuntime.validateRefreshBoundaryAndEnqueueUpdate(current, nextExports);
|
|
82
|
+
if (invalidateMessage) import.meta.hot.invalidate(invalidateMessage);
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
`;
|
|
86
|
+
const sourceMap = JSON.parse(result.map);
|
|
87
|
+
sourceMap.mappings = ";;;;;;;;" + sourceMap.mappings;
|
|
88
|
+
return { code: result.code, map: sourceMap };
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
name: "@vitejs/plugin-react-swc",
|
|
93
|
+
apply: "build",
|
|
94
|
+
config: () => ({
|
|
95
|
+
esbuild: {
|
|
96
|
+
jsx: "automatic",
|
|
97
|
+
tsconfigRaw: { compilerOptions: { useDefineForClassFields: true } }
|
|
98
|
+
}
|
|
99
|
+
})
|
|
100
|
+
}
|
|
101
|
+
];
|
|
102
|
+
var src_default = react;
|
|
103
|
+
export {
|
|
104
|
+
src_default as default
|
|
105
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vitejs/plugin-react-swc",
|
|
3
|
+
"description": "Use the versatility of SWC for development and the maturity of esbuild for production",
|
|
4
|
+
"version": "3.0.0-beta.0",
|
|
5
|
+
"author": "Arnaud Barré (https://github.com/ArnaudBarre)",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": "github:vitejs/vite-plugin-react-swc",
|
|
8
|
+
"main": "index.cjs",
|
|
9
|
+
"types": "index.d.ts",
|
|
10
|
+
"module": "index.mjs",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"require": "index.cjs",
|
|
14
|
+
"types": "index.d.ts",
|
|
15
|
+
"import": "index.mjs"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"vite",
|
|
20
|
+
"vite-plugin",
|
|
21
|
+
"react",
|
|
22
|
+
"swc",
|
|
23
|
+
"react-refresh",
|
|
24
|
+
"fast refresh"
|
|
25
|
+
],
|
|
26
|
+
"peerDependencies": {
|
|
27
|
+
"vite": "^4.0.0-alpha.5"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@swc/core": "^1.3.21"
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,417 @@
|
|
|
1
|
+
/*! Copyright (c) Meta Platforms, Inc. and affiliates. **/
|
|
2
|
+
const REACT_FORWARD_REF_TYPE = Symbol.for("react.forward_ref");
|
|
3
|
+
const REACT_MEMO_TYPE = Symbol.for("react.memo");
|
|
4
|
+
let allFamiliesByID = /* @__PURE__ */ new Map();
|
|
5
|
+
let allFamiliesByType = /* @__PURE__ */ new WeakMap();
|
|
6
|
+
let allSignaturesByType = /* @__PURE__ */ new WeakMap();
|
|
7
|
+
let updatedFamiliesByType = /* @__PURE__ */ new WeakMap();
|
|
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();
|
|
13
|
+
let rootElements = /* @__PURE__ */ new WeakMap();
|
|
14
|
+
let isPerformingRefresh = false;
|
|
15
|
+
function computeFullKey(signature) {
|
|
16
|
+
if (signature.fullKey !== null) {
|
|
17
|
+
return signature.fullKey;
|
|
18
|
+
}
|
|
19
|
+
let fullKey = signature.ownKey;
|
|
20
|
+
let hooks;
|
|
21
|
+
try {
|
|
22
|
+
hooks = signature.getCustomHooks();
|
|
23
|
+
} catch (err) {
|
|
24
|
+
signature.forceReset = true;
|
|
25
|
+
signature.fullKey = fullKey;
|
|
26
|
+
return fullKey;
|
|
27
|
+
}
|
|
28
|
+
for (let i = 0; i < hooks.length; i++) {
|
|
29
|
+
let hook = hooks[i];
|
|
30
|
+
if (typeof hook !== "function") {
|
|
31
|
+
signature.forceReset = true;
|
|
32
|
+
signature.fullKey = fullKey;
|
|
33
|
+
return fullKey;
|
|
34
|
+
}
|
|
35
|
+
let nestedHookSignature = allSignaturesByType.get(hook);
|
|
36
|
+
if (nestedHookSignature === void 0) {
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
let nestedHookKey = computeFullKey(nestedHookSignature);
|
|
40
|
+
if (nestedHookSignature.forceReset) {
|
|
41
|
+
signature.forceReset = true;
|
|
42
|
+
}
|
|
43
|
+
fullKey += "\n---\n" + nestedHookKey;
|
|
44
|
+
}
|
|
45
|
+
signature.fullKey = fullKey;
|
|
46
|
+
return fullKey;
|
|
47
|
+
}
|
|
48
|
+
function haveEqualSignatures(prevType, nextType) {
|
|
49
|
+
let prevSignature = allSignaturesByType.get(prevType);
|
|
50
|
+
let nextSignature = allSignaturesByType.get(nextType);
|
|
51
|
+
if (prevSignature === void 0 && nextSignature === void 0) {
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
if (prevSignature === void 0 || nextSignature === void 0) {
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
if (computeFullKey(prevSignature) !== computeFullKey(nextSignature)) {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
if (nextSignature.forceReset) {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
65
|
+
function isReactClass(type) {
|
|
66
|
+
return type.prototype && type.prototype.isReactComponent;
|
|
67
|
+
}
|
|
68
|
+
function canPreserveStateBetween(prevType, nextType) {
|
|
69
|
+
if (isReactClass(prevType) || isReactClass(nextType)) {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
if (haveEqualSignatures(prevType, nextType)) {
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
function resolveFamily(type) {
|
|
78
|
+
return updatedFamiliesByType.get(type);
|
|
79
|
+
}
|
|
80
|
+
function getProperty(object, property) {
|
|
81
|
+
try {
|
|
82
|
+
return object[property];
|
|
83
|
+
} catch (err) {
|
|
84
|
+
return void 0;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
function performReactRefresh() {
|
|
88
|
+
if (pendingUpdates.length === 0) {
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
if (isPerformingRefresh) {
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
isPerformingRefresh = true;
|
|
95
|
+
try {
|
|
96
|
+
let staleFamilies = /* @__PURE__ */ new Set();
|
|
97
|
+
let updatedFamilies = /* @__PURE__ */ new Set();
|
|
98
|
+
let updates = pendingUpdates;
|
|
99
|
+
pendingUpdates = [];
|
|
100
|
+
updates.forEach(function(_ref) {
|
|
101
|
+
let family = _ref[0], nextType = _ref[1];
|
|
102
|
+
let prevType = family.current;
|
|
103
|
+
updatedFamiliesByType.set(prevType, family);
|
|
104
|
+
updatedFamiliesByType.set(nextType, family);
|
|
105
|
+
family.current = nextType;
|
|
106
|
+
if (canPreserveStateBetween(prevType, nextType)) {
|
|
107
|
+
updatedFamilies.add(family);
|
|
108
|
+
} else {
|
|
109
|
+
staleFamilies.add(family);
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
let update = {
|
|
113
|
+
updatedFamilies,
|
|
114
|
+
staleFamilies
|
|
115
|
+
};
|
|
116
|
+
helpersByRendererID.forEach(function(helpers) {
|
|
117
|
+
helpers.setRefreshHandler(resolveFamily);
|
|
118
|
+
});
|
|
119
|
+
let didError = false;
|
|
120
|
+
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);
|
|
126
|
+
if (helpers === void 0) {
|
|
127
|
+
throw new Error(
|
|
128
|
+
"Could not find helpers for a root. This is a bug in React Refresh."
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
if (!failedRoots.has(root)) {
|
|
132
|
+
}
|
|
133
|
+
if (!rootElements.has(root)) {
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
let element = rootElements.get(root);
|
|
137
|
+
try {
|
|
138
|
+
helpers.scheduleRoot(root, element);
|
|
139
|
+
} catch (err) {
|
|
140
|
+
if (!didError) {
|
|
141
|
+
didError = true;
|
|
142
|
+
firstError = err;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
mountedRootsSnapshot.forEach(function(root) {
|
|
147
|
+
let helpers = helpersByRootSnapshot.get(root);
|
|
148
|
+
if (helpers === void 0) {
|
|
149
|
+
throw new Error(
|
|
150
|
+
"Could not find helpers for a root. This is a bug in React Refresh."
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
if (!mountedRoots.has(root)) {
|
|
154
|
+
}
|
|
155
|
+
try {
|
|
156
|
+
helpers.scheduleRefresh(root, update);
|
|
157
|
+
} catch (err) {
|
|
158
|
+
if (!didError) {
|
|
159
|
+
didError = true;
|
|
160
|
+
firstError = err;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
if (didError) {
|
|
165
|
+
throw firstError;
|
|
166
|
+
}
|
|
167
|
+
return update;
|
|
168
|
+
} finally {
|
|
169
|
+
isPerformingRefresh = false;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
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
|
+
function register(type, id) {
|
|
181
|
+
if (type === null) {
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
if (typeof type !== "function" && typeof type !== "object") {
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
if (allFamiliesByType.has(type)) {
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
let family = allFamiliesByID.get(id);
|
|
191
|
+
if (family === void 0) {
|
|
192
|
+
family = {
|
|
193
|
+
current: type
|
|
194
|
+
};
|
|
195
|
+
allFamiliesByID.set(id, family);
|
|
196
|
+
} else {
|
|
197
|
+
pendingUpdates.push([family, type]);
|
|
198
|
+
}
|
|
199
|
+
allFamiliesByType.set(type, family);
|
|
200
|
+
if (typeof type === "object" && type !== null) {
|
|
201
|
+
switch (getProperty(type, "$$typeof")) {
|
|
202
|
+
case REACT_FORWARD_REF_TYPE:
|
|
203
|
+
register(type.render, id + "$render");
|
|
204
|
+
break;
|
|
205
|
+
case REACT_MEMO_TYPE:
|
|
206
|
+
register(type.type, id + "$type");
|
|
207
|
+
break;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
function getRefreshReg(filename) {
|
|
212
|
+
return (type, id) => register(type, filename + " " + id);
|
|
213
|
+
}
|
|
214
|
+
function setSignature(type, key, forceReset = false, getCustomHooks) {
|
|
215
|
+
if (!allSignaturesByType.has(type)) {
|
|
216
|
+
allSignaturesByType.set(type, {
|
|
217
|
+
forceReset,
|
|
218
|
+
ownKey: key,
|
|
219
|
+
fullKey: null,
|
|
220
|
+
getCustomHooks: getCustomHooks || function() {
|
|
221
|
+
return [];
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
if (typeof type === "object" && type !== null) {
|
|
226
|
+
switch (getProperty(type, "$$typeof")) {
|
|
227
|
+
case REACT_FORWARD_REF_TYPE:
|
|
228
|
+
setSignature(type.render, key, forceReset, getCustomHooks);
|
|
229
|
+
break;
|
|
230
|
+
case REACT_MEMO_TYPE:
|
|
231
|
+
setSignature(type.type, key, forceReset, getCustomHooks);
|
|
232
|
+
break;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
function collectCustomHooksForSignature(type) {
|
|
237
|
+
let signature = allSignaturesByType.get(type);
|
|
238
|
+
if (signature !== void 0) {
|
|
239
|
+
computeFullKey(signature);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
function injectIntoGlobalHook(globalObject) {
|
|
243
|
+
let hook = globalObject.__REACT_DEVTOOLS_GLOBAL_HOOK__;
|
|
244
|
+
if (hook === void 0) {
|
|
245
|
+
let nextID = 0;
|
|
246
|
+
globalObject.__REACT_DEVTOOLS_GLOBAL_HOOK__ = hook = {
|
|
247
|
+
renderers: /* @__PURE__ */ new Map(),
|
|
248
|
+
supportsFiber: true,
|
|
249
|
+
inject: function(injected) {
|
|
250
|
+
return nextID++;
|
|
251
|
+
},
|
|
252
|
+
onScheduleFiberRoot: function(id, root, children) {
|
|
253
|
+
},
|
|
254
|
+
onCommitFiberRoot: function(id, root, maybePriorityLevel, didError) {
|
|
255
|
+
},
|
|
256
|
+
onCommitFiberUnmount: function() {
|
|
257
|
+
}
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
if (hook.isDisabled) {
|
|
261
|
+
console["warn"](
|
|
262
|
+
"Something has shimmed the React DevTools global hook (__REACT_DEVTOOLS_GLOBAL_HOOK__). Fast Refresh is not compatible with this shim and will be disabled."
|
|
263
|
+
);
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
let oldInject = hook.inject;
|
|
267
|
+
hook.inject = function(injected) {
|
|
268
|
+
let id = oldInject.apply(this, arguments);
|
|
269
|
+
if (typeof injected.scheduleRefresh === "function" && typeof injected.setRefreshHandler === "function") {
|
|
270
|
+
helpersByRendererID.set(id, injected);
|
|
271
|
+
}
|
|
272
|
+
return id;
|
|
273
|
+
};
|
|
274
|
+
hook.renderers.forEach(function(injected, id) {
|
|
275
|
+
if (typeof injected.scheduleRefresh === "function" && typeof injected.setRefreshHandler === "function") {
|
|
276
|
+
helpersByRendererID.set(id, injected);
|
|
277
|
+
}
|
|
278
|
+
});
|
|
279
|
+
let oldOnCommitFiberRoot = hook.onCommitFiberRoot;
|
|
280
|
+
let oldOnScheduleFiberRoot = hook.onScheduleFiberRoot || function() {
|
|
281
|
+
};
|
|
282
|
+
hook.onScheduleFiberRoot = function(id, root, children) {
|
|
283
|
+
if (!isPerformingRefresh) {
|
|
284
|
+
failedRoots.delete(root);
|
|
285
|
+
rootElements.set(root, children);
|
|
286
|
+
}
|
|
287
|
+
return oldOnScheduleFiberRoot.apply(this, arguments);
|
|
288
|
+
};
|
|
289
|
+
hook.onCommitFiberRoot = function(id, root, maybePriorityLevel, didError) {
|
|
290
|
+
let helpers = helpersByRendererID.get(id);
|
|
291
|
+
if (helpers !== void 0) {
|
|
292
|
+
helpersByRoot.set(root, helpers);
|
|
293
|
+
let current = root.current;
|
|
294
|
+
let alternate = current.alternate;
|
|
295
|
+
if (alternate !== null) {
|
|
296
|
+
let wasMounted = alternate.memoizedState != null && alternate.memoizedState.element != null;
|
|
297
|
+
let isMounted = current.memoizedState != null && current.memoizedState.element != null;
|
|
298
|
+
if (!wasMounted && isMounted) {
|
|
299
|
+
mountedRoots.add(root);
|
|
300
|
+
failedRoots.delete(root);
|
|
301
|
+
} else if (wasMounted && isMounted) {
|
|
302
|
+
} else if (wasMounted && !isMounted) {
|
|
303
|
+
mountedRoots.delete(root);
|
|
304
|
+
if (didError) {
|
|
305
|
+
failedRoots.add(root);
|
|
306
|
+
} else {
|
|
307
|
+
helpersByRoot.delete(root);
|
|
308
|
+
}
|
|
309
|
+
} else if (!wasMounted && !isMounted) {
|
|
310
|
+
if (didError) {
|
|
311
|
+
failedRoots.add(root);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
} else {
|
|
315
|
+
mountedRoots.add(root);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
return oldOnCommitFiberRoot.apply(this, arguments);
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
function createSignatureFunctionForTransform() {
|
|
322
|
+
let savedType;
|
|
323
|
+
let hasCustomHooks;
|
|
324
|
+
let didCollectHooks = false;
|
|
325
|
+
return function(type, key, forceReset, getCustomHooks) {
|
|
326
|
+
if (typeof key === "string") {
|
|
327
|
+
if (!savedType) {
|
|
328
|
+
savedType = type;
|
|
329
|
+
hasCustomHooks = typeof getCustomHooks === "function";
|
|
330
|
+
}
|
|
331
|
+
if (type != null && (typeof type === "function" || typeof type === "object")) {
|
|
332
|
+
setSignature(type, key, forceReset, getCustomHooks);
|
|
333
|
+
}
|
|
334
|
+
return type;
|
|
335
|
+
} else {
|
|
336
|
+
if (!didCollectHooks && hasCustomHooks) {
|
|
337
|
+
didCollectHooks = true;
|
|
338
|
+
collectCustomHooksForSignature(savedType);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
function isLikelyComponentType(type) {
|
|
344
|
+
switch (typeof type) {
|
|
345
|
+
case "function": {
|
|
346
|
+
if (type.prototype != null) {
|
|
347
|
+
if (type.prototype.isReactComponent) {
|
|
348
|
+
return true;
|
|
349
|
+
}
|
|
350
|
+
const ownNames = Object.getOwnPropertyNames(type.prototype);
|
|
351
|
+
if (ownNames.length > 1 || ownNames[0] !== "constructor") {
|
|
352
|
+
return false;
|
|
353
|
+
}
|
|
354
|
+
if (type.prototype.__proto__ !== Object.prototype) {
|
|
355
|
+
return false;
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
const name = type.name || type.displayName;
|
|
359
|
+
return typeof name === "string" && /^[A-Z]/.test(name);
|
|
360
|
+
}
|
|
361
|
+
case "object": {
|
|
362
|
+
if (type != null) {
|
|
363
|
+
switch (getProperty(type, "$$typeof")) {
|
|
364
|
+
case REACT_FORWARD_REF_TYPE:
|
|
365
|
+
case REACT_MEMO_TYPE:
|
|
366
|
+
return true;
|
|
367
|
+
default:
|
|
368
|
+
return false;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
return false;
|
|
372
|
+
}
|
|
373
|
+
default: {
|
|
374
|
+
return false;
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
function validateRefreshBoundaryAndEnqueueUpdate(prevExports, nextExports) {
|
|
379
|
+
if (!predicateOnExport(prevExports, (key) => !!nextExports[key])) {
|
|
380
|
+
return "Could not Fast Refresh (export removed)";
|
|
381
|
+
}
|
|
382
|
+
let hasExports = false;
|
|
383
|
+
const allExportsAreComponentsOrUnchanged = predicateOnExport(
|
|
384
|
+
nextExports,
|
|
385
|
+
(key, value) => {
|
|
386
|
+
hasExports = true;
|
|
387
|
+
if (isLikelyComponentType(value))
|
|
388
|
+
return true;
|
|
389
|
+
if (!prevExports[key])
|
|
390
|
+
return false;
|
|
391
|
+
return prevExports[key] === nextExports[key];
|
|
392
|
+
}
|
|
393
|
+
);
|
|
394
|
+
if (hasExports && allExportsAreComponentsOrUnchanged) {
|
|
395
|
+
enqueueUpdate();
|
|
396
|
+
} else {
|
|
397
|
+
return "Could not Fast Refresh. Learn more at https://github.com/vitejs/vite-plugin-react-swc#consistent-components-exports";
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
function predicateOnExport(moduleExports, predicate) {
|
|
401
|
+
for (const key in moduleExports) {
|
|
402
|
+
if (key === "__esModule")
|
|
403
|
+
continue;
|
|
404
|
+
const desc = Object.getOwnPropertyDescriptor(moduleExports, key);
|
|
405
|
+
if (desc && desc.get)
|
|
406
|
+
return false;
|
|
407
|
+
if (!predicate(key, moduleExports[key]))
|
|
408
|
+
return false;
|
|
409
|
+
}
|
|
410
|
+
return true;
|
|
411
|
+
}
|
|
412
|
+
export {
|
|
413
|
+
createSignatureFunctionForTransform,
|
|
414
|
+
getRefreshReg,
|
|
415
|
+
injectIntoGlobalHook,
|
|
416
|
+
validateRefreshBoundaryAndEnqueueUpdate
|
|
417
|
+
};
|