@vitejs/plugin-react-swc 3.1.0-beta.2 → 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.
Files changed (2) hide show
  1. package/package.json +3 -3
  2. package/refresh-runtime.js +61 -63
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.1.0-beta.2",
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.26"
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,22 +108,22 @@ function performReactRefresh() {
109
108
  staleFamilies.add(family);
110
109
  }
111
110
  });
112
- let update = {
111
+ const update = {
113
112
  updatedFamilies,
114
113
  // Families that will re-render preserving state
115
114
  staleFamilies
116
115
  // Families that will be remounted
117
116
  };
118
- helpersByRendererID.forEach(function(helpers) {
117
+ helpersByRendererID.forEach((helpers) => {
119
118
  helpers.setRefreshHandler(resolveFamily);
120
119
  });
121
120
  let didError = false;
122
121
  let firstError = null;
123
- let failedRootsSnapshot = new Set(failedRoots);
124
- let mountedRootsSnapshot = new Set(mountedRoots);
125
- let helpersByRootSnapshot = new Map(helpersByRoot);
126
- failedRootsSnapshot.forEach(function(root) {
127
- 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);
128
127
  if (helpers === void 0) {
129
128
  throw new Error(
130
129
  "Could not find helpers for a root. This is a bug in React Refresh."
@@ -132,10 +131,13 @@ function performReactRefresh() {
132
131
  }
133
132
  if (!failedRoots.has(root)) {
134
133
  }
134
+ if (rootElements === null) {
135
+ return;
136
+ }
135
137
  if (!rootElements.has(root)) {
136
138
  return;
137
139
  }
138
- let element = rootElements.get(root);
140
+ const element = rootElements.get(root);
139
141
  try {
140
142
  helpers.scheduleRoot(root, element);
141
143
  } catch (err) {
@@ -145,8 +147,8 @@ function performReactRefresh() {
145
147
  }
146
148
  }
147
149
  });
148
- mountedRootsSnapshot.forEach(function(root) {
149
- let helpers = helpersByRootSnapshot.get(root);
150
+ mountedRootsSnapshot.forEach((root) => {
151
+ const helpers = helpersByRootSnapshot.get(root);
150
152
  if (helpers === void 0) {
151
153
  throw new Error(
152
154
  "Could not find helpers for a root. This is a bug in React Refresh."
@@ -171,14 +173,6 @@ function performReactRefresh() {
171
173
  isPerformingRefresh = false;
172
174
  }
173
175
  }
174
- function debounce(fn, delay) {
175
- let handle;
176
- return () => {
177
- clearTimeout(handle);
178
- handle = setTimeout(fn, delay);
179
- };
180
- }
181
- const enqueueUpdate = debounce(performReactRefresh, 16);
182
176
  function register(type, id) {
183
177
  if (type === null) {
184
178
  return;
@@ -191,9 +185,7 @@ function register(type, id) {
191
185
  }
192
186
  let family = allFamiliesByID.get(id);
193
187
  if (family === void 0) {
194
- family = {
195
- current: type
196
- };
188
+ family = { current: type };
197
189
  allFamiliesByID.set(id, family);
198
190
  } else {
199
191
  pendingUpdates.push([family, type]);
@@ -210,18 +202,13 @@ function register(type, id) {
210
202
  }
211
203
  }
212
204
  }
213
- function getRefreshReg(filename) {
214
- return (type, id) => register(type, filename + " " + id);
215
- }
216
- function setSignature(type, key, forceReset = false, getCustomHooks) {
205
+ function setSignature(type, key, forceReset, getCustomHooks) {
217
206
  if (!allSignaturesByType.has(type)) {
218
207
  allSignaturesByType.set(type, {
219
208
  forceReset,
220
209
  ownKey: key,
221
210
  fullKey: null,
222
- getCustomHooks: getCustomHooks || function() {
223
- return [];
224
- }
211
+ getCustomHooks: getCustomHooks || (() => [])
225
212
  });
226
213
  }
227
214
  if (typeof type === "object" && type !== null) {
@@ -236,7 +223,7 @@ function setSignature(type, key, forceReset = false, getCustomHooks) {
236
223
  }
237
224
  }
238
225
  function collectCustomHooksForSignature(type) {
239
- let signature = allSignaturesByType.get(type);
226
+ const signature = allSignaturesByType.get(type);
240
227
  if (signature !== void 0) {
241
228
  computeFullKey(signature);
242
229
  }
@@ -248,14 +235,12 @@ function injectIntoGlobalHook(globalObject) {
248
235
  globalObject.__REACT_DEVTOOLS_GLOBAL_HOOK__ = hook = {
249
236
  renderers: /* @__PURE__ */ new Map(),
250
237
  supportsFiber: true,
251
- inject: function(injected) {
252
- return nextID++;
238
+ inject: (injected) => nextID++,
239
+ onScheduleFiberRoot: (id, root, children) => {
253
240
  },
254
- onScheduleFiberRoot: function(id, root, children) {
241
+ onCommitFiberRoot: (id, root, maybePriorityLevel, didError) => {
255
242
  },
256
- onCommitFiberRoot: function(id, root, maybePriorityLevel, didError) {
257
- },
258
- onCommitFiberUnmount: function() {
243
+ onCommitFiberUnmount() {
259
244
  }
260
245
  };
261
246
  }
@@ -265,38 +250,40 @@ function injectIntoGlobalHook(globalObject) {
265
250
  );
266
251
  return;
267
252
  }
268
- let oldInject = hook.inject;
253
+ const oldInject = hook.inject;
269
254
  hook.inject = function(injected) {
270
- let id = oldInject.apply(this, arguments);
255
+ const id = oldInject.apply(this, arguments);
271
256
  if (typeof injected.scheduleRefresh === "function" && typeof injected.setRefreshHandler === "function") {
272
257
  helpersByRendererID.set(id, injected);
273
258
  }
274
259
  return id;
275
260
  };
276
- hook.renderers.forEach(function(injected, id) {
261
+ hook.renderers.forEach((injected, id) => {
277
262
  if (typeof injected.scheduleRefresh === "function" && typeof injected.setRefreshHandler === "function") {
278
263
  helpersByRendererID.set(id, injected);
279
264
  }
280
265
  });
281
- let oldOnCommitFiberRoot = hook.onCommitFiberRoot;
282
- let oldOnScheduleFiberRoot = hook.onScheduleFiberRoot || function() {
283
- };
266
+ const oldOnCommitFiberRoot = hook.onCommitFiberRoot;
267
+ const oldOnScheduleFiberRoot = hook.onScheduleFiberRoot || (() => {
268
+ });
284
269
  hook.onScheduleFiberRoot = function(id, root, children) {
285
270
  if (!isPerformingRefresh) {
286
271
  failedRoots.delete(root);
287
- rootElements.set(root, children);
272
+ if (rootElements !== null) {
273
+ rootElements.set(root, children);
274
+ }
288
275
  }
289
276
  return oldOnScheduleFiberRoot.apply(this, arguments);
290
277
  };
291
278
  hook.onCommitFiberRoot = function(id, root, maybePriorityLevel, didError) {
292
- let helpers = helpersByRendererID.get(id);
279
+ const helpers = helpersByRendererID.get(id);
293
280
  if (helpers !== void 0) {
294
281
  helpersByRoot.set(root, helpers);
295
- let current = root.current;
296
- let alternate = current.alternate;
282
+ const current = root.current;
283
+ const alternate = current.alternate;
297
284
  if (alternate !== null) {
298
- let wasMounted = alternate.memoizedState != null && alternate.memoizedState.element != null;
299
- 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;
300
287
  if (!wasMounted && isMounted) {
301
288
  mountedRoots.add(root);
302
289
  failedRoots.delete(root);
@@ -377,6 +364,9 @@ function isLikelyComponentType(type) {
377
364
  }
378
365
  }
379
366
  }
367
+ function getRefreshReg(filename) {
368
+ return (type, id) => register(type, filename + " " + id);
369
+ }
380
370
  function registerExportsForReactRefresh(filename, moduleExports) {
381
371
  for (const key in moduleExports) {
382
372
  if (key === "__esModule")
@@ -387,6 +377,14 @@ function registerExportsForReactRefresh(filename, moduleExports) {
387
377
  }
388
378
  }
389
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);
390
388
  function validateRefreshBoundaryAndEnqueueUpdate(prevExports, nextExports) {
391
389
  if (!predicateOnExport(prevExports, (key) => !!nextExports[key])) {
392
390
  return "Could not Fast Refresh (export removed)";