@vitejs/plugin-react 2.1.0 → 2.2.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.
@@ -135,4 +135,4 @@ function babelRestoreJsx({ types: t }, { reactAlias = "React" }) {
135
135
  };
136
136
  }
137
137
 
138
- exports["default"] = babelRestoreJsx;
138
+ exports.default = babelRestoreJsx;
package/dist/index.cjs CHANGED
@@ -7,37 +7,31 @@ const MagicString = require('magic-string');
7
7
  const fs = require('node:fs');
8
8
  const node_module = require('node:module');
9
9
 
10
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e["default"] : e; }
11
-
12
- function _interopNamespace(e) {
13
- if (e && e.__esModule) return e;
10
+ function _interopNamespaceDefault(e) {
14
11
  const n = Object.create(null);
15
12
  if (e) {
16
13
  for (const k in e) {
17
14
  n[k] = e[k];
18
15
  }
19
16
  }
20
- n["default"] = e;
17
+ n.default = e;
21
18
  return n;
22
19
  }
23
20
 
24
- const path__default = /*#__PURE__*/_interopDefaultLegacy(path);
25
- const babel__namespace = /*#__PURE__*/_interopNamespace(babel);
26
- const MagicString__default = /*#__PURE__*/_interopDefaultLegacy(MagicString);
27
- const fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
21
+ const babel__namespace = /*#__PURE__*/_interopNamespaceDefault(babel);
28
22
 
29
23
  const runtimePublicPath = "/@react-refresh";
30
24
  const _require = node_module.createRequire((typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('index.cjs', document.baseURI).href)));
31
- const reactRefreshDir = path__default.dirname(
25
+ const reactRefreshDir = path.dirname(
32
26
  _require.resolve("react-refresh/package.json")
33
27
  );
34
- const runtimeFilePath = path__default.join(
28
+ const runtimeFilePath = path.join(
35
29
  reactRefreshDir,
36
30
  "cjs/react-refresh-runtime.development.js"
37
31
  );
38
32
  const runtimeCode = `
39
33
  const exports = {}
40
- ${fs__default.readFileSync(runtimeFilePath, "utf-8")}
34
+ ${fs.readFileSync(runtimeFilePath, "utf-8")}
41
35
  function debounce(fn, delay) {
42
36
  let handle
43
37
  return () => {
@@ -76,21 +70,56 @@ if (import.meta.hot) {
76
70
  };
77
71
  window.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform;
78
72
  }`.replace(/[\n]+/gm, "");
79
- const footer = `
80
- if (import.meta.hot) {
81
- window.$RefreshReg$ = prevRefreshReg;
82
- window.$RefreshSig$ = prevRefreshSig;
83
-
84
- __ACCEPT__
73
+ const timeout = `
85
74
  if (!window.__vite_plugin_react_timeout) {
86
75
  window.__vite_plugin_react_timeout = setTimeout(() => {
87
76
  window.__vite_plugin_react_timeout = 0;
88
77
  RefreshRuntime.performReactRefresh();
89
78
  }, 30);
90
79
  }
80
+ `;
81
+ const footer = `
82
+ if (import.meta.hot) {
83
+ window.$RefreshReg$ = prevRefreshReg;
84
+ window.$RefreshSig$ = prevRefreshSig;
85
+
86
+ __ACCEPT__
91
87
  }`;
88
+ const checkAndAccept = `
89
+ function isReactRefreshBoundary(mod) {
90
+ if (mod == null || typeof mod !== 'object') {
91
+ return false;
92
+ }
93
+ let hasExports = false;
94
+ let areAllExportsComponents = true;
95
+ for (const exportName in mod) {
96
+ hasExports = true;
97
+ if (exportName === '__esModule') {
98
+ continue;
99
+ }
100
+ const desc = Object.getOwnPropertyDescriptor(mod, exportName);
101
+ if (desc && desc.get) {
102
+ // Don't invoke getters as they may have side effects.
103
+ return false;
104
+ }
105
+ const exportValue = mod[exportName];
106
+ if (!RefreshRuntime.isLikelyComponentType(exportValue)) {
107
+ areAllExportsComponents = false;
108
+ }
109
+ }
110
+ return hasExports && areAllExportsComponents;
111
+ }
112
+
113
+ import.meta.hot.accept(mod => {
114
+ if (isReactRefreshBoundary(mod)) {
115
+ ${timeout}
116
+ } else {
117
+ import.meta.hot.invalidate();
118
+ }
119
+ });
120
+ `;
92
121
  function addRefreshWrapper(code, id, accept) {
93
- return header.replace("__SOURCE__", JSON.stringify(id)) + code + footer.replace("__ACCEPT__", accept ? "import.meta.hot.accept();" : "");
122
+ return header.replace("__SOURCE__", JSON.stringify(id)) + code + footer.replace("__ACCEPT__", accept ? checkAndAccept : timeout);
94
123
  }
95
124
  function isRefreshBoundary(ast) {
96
125
  return ast.program.body.every((node) => {
@@ -227,7 +256,7 @@ function viteReact(opts = {}) {
227
256
  configResolved(config) {
228
257
  devBase = config.base;
229
258
  projectRoot = config.root;
230
- resolvedCacheDir = vite.normalizePath(path__default.resolve(config.cacheDir));
259
+ resolvedCacheDir = vite.normalizePath(path.resolve(config.cacheDir));
231
260
  filter = vite.createFilter(opts.include, opts.exclude, {
232
261
  resolve: projectRoot
233
262
  });
@@ -305,7 +334,8 @@ function viteReact(opts = {}) {
305
334
  {
306
335
  runtime: "automatic",
307
336
  importSource: opts.jsxImportSource,
308
- pure: opts.jsxPure !== false
337
+ pure: opts.jsxPure !== false,
338
+ throwIfNamespace: opts.jsxThrowIfNamespace
309
339
  }
310
340
  ]);
311
341
  if (isCommonJS) {
@@ -327,7 +357,7 @@ function viteReact(opts = {}) {
327
357
  let inputMap;
328
358
  if (prependReactImport) {
329
359
  if (needHiresSourcemap) {
330
- const s = new MagicString__default(code);
360
+ const s = new MagicString(code);
331
361
  s.prepend(prependReactImportCode);
332
362
  code = s.toString();
333
363
  inputMap = s.generateMap({ hires: true, source: id });
@@ -483,4 +513,4 @@ function createBabelOptions(rawOptions) {
483
513
  }
484
514
 
485
515
  module.exports = viteReact;
486
- module.exports["default"] = viteReact;
516
+ module.exports.default = viteReact;
package/dist/index.d.ts CHANGED
@@ -26,6 +26,11 @@ interface Options {
26
26
  * @default true
27
27
  */
28
28
  jsxPure?: boolean;
29
+ /**
30
+ * Toggles whether or not to throw an error if an XML namespaced tag name is used.
31
+ * @default true
32
+ */
33
+ jsxThrowIfNamespace?: boolean;
29
34
  /**
30
35
  * Babel configuration applied in both dev and prod.
31
36
  */
package/dist/index.mjs CHANGED
@@ -55,21 +55,56 @@ if (import.meta.hot) {
55
55
  };
56
56
  window.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform;
57
57
  }`.replace(/[\n]+/gm, "");
58
- const footer = `
59
- if (import.meta.hot) {
60
- window.$RefreshReg$ = prevRefreshReg;
61
- window.$RefreshSig$ = prevRefreshSig;
62
-
63
- __ACCEPT__
58
+ const timeout = `
64
59
  if (!window.__vite_plugin_react_timeout) {
65
60
  window.__vite_plugin_react_timeout = setTimeout(() => {
66
61
  window.__vite_plugin_react_timeout = 0;
67
62
  RefreshRuntime.performReactRefresh();
68
63
  }, 30);
69
64
  }
65
+ `;
66
+ const footer = `
67
+ if (import.meta.hot) {
68
+ window.$RefreshReg$ = prevRefreshReg;
69
+ window.$RefreshSig$ = prevRefreshSig;
70
+
71
+ __ACCEPT__
70
72
  }`;
73
+ const checkAndAccept = `
74
+ function isReactRefreshBoundary(mod) {
75
+ if (mod == null || typeof mod !== 'object') {
76
+ return false;
77
+ }
78
+ let hasExports = false;
79
+ let areAllExportsComponents = true;
80
+ for (const exportName in mod) {
81
+ hasExports = true;
82
+ if (exportName === '__esModule') {
83
+ continue;
84
+ }
85
+ const desc = Object.getOwnPropertyDescriptor(mod, exportName);
86
+ if (desc && desc.get) {
87
+ // Don't invoke getters as they may have side effects.
88
+ return false;
89
+ }
90
+ const exportValue = mod[exportName];
91
+ if (!RefreshRuntime.isLikelyComponentType(exportValue)) {
92
+ areAllExportsComponents = false;
93
+ }
94
+ }
95
+ return hasExports && areAllExportsComponents;
96
+ }
97
+
98
+ import.meta.hot.accept(mod => {
99
+ if (isReactRefreshBoundary(mod)) {
100
+ ${timeout}
101
+ } else {
102
+ import.meta.hot.invalidate();
103
+ }
104
+ });
105
+ `;
71
106
  function addRefreshWrapper(code, id, accept) {
72
- return header.replace("__SOURCE__", JSON.stringify(id)) + code + footer.replace("__ACCEPT__", accept ? "import.meta.hot.accept();" : "");
107
+ return header.replace("__SOURCE__", JSON.stringify(id)) + code + footer.replace("__ACCEPT__", accept ? checkAndAccept : timeout);
73
108
  }
74
109
  function isRefreshBoundary(ast) {
75
110
  return ast.program.body.every((node) => {
@@ -284,7 +319,8 @@ function viteReact(opts = {}) {
284
319
  {
285
320
  runtime: "automatic",
286
321
  importSource: opts.jsxImportSource,
287
- pure: opts.jsxPure !== false
322
+ pure: opts.jsxPure !== false,
323
+ throwIfNamespace: opts.jsxThrowIfNamespace
288
324
  }
289
325
  ]);
290
326
  if (isCommonJS) {
package/package.json CHANGED
@@ -1,14 +1,13 @@
1
1
  {
2
2
  "name": "@vitejs/plugin-react",
3
- "version": "2.1.0",
3
+ "version": "2.2.0",
4
4
  "license": "MIT",
5
5
  "author": "Evan You",
6
6
  "contributors": [
7
7
  "Alec Larson"
8
8
  ],
9
9
  "files": [
10
- "dist",
11
- "src"
10
+ "dist"
12
11
  ],
13
12
  "main": "./dist/index.cjs",
14
13
  "module": "./dist/index.mjs",
@@ -39,12 +38,12 @@
39
38
  },
40
39
  "homepage": "https://github.com/vitejs/vite/tree/main/packages/plugin-react#readme",
41
40
  "dependencies": {
42
- "@babel/core": "^7.18.13",
43
- "@babel/plugin-transform-react-jsx": "^7.18.10",
41
+ "@babel/core": "^7.19.6",
42
+ "@babel/plugin-transform-react-jsx": "^7.19.0",
44
43
  "@babel/plugin-transform-react-jsx-development": "^7.18.6",
45
44
  "@babel/plugin-transform-react-jsx-self": "^7.18.6",
46
- "@babel/plugin-transform-react-jsx-source": "^7.18.6",
47
- "magic-string": "^0.26.2",
45
+ "@babel/plugin-transform-react-jsx-source": "^7.19.6",
46
+ "magic-string": "^0.26.7",
48
47
  "react-refresh": "^0.14.0"
49
48
  },
50
49
  "peerDependencies": {
package/src/babel.d.ts DELETED
@@ -1,4 +0,0 @@
1
- declare module '@babel/plugin-transform-react-jsx'
2
- declare module '@babel/plugin-transform-react-jsx-self'
3
- declare module '@babel/plugin-transform-react-jsx-source'
4
- declare module 'react-refresh/babel.js'
@@ -1,117 +0,0 @@
1
- import fs from 'node:fs'
2
- import path from 'node:path'
3
- import { createRequire } from 'node:module'
4
- import type { types as t } from '@babel/core'
5
-
6
- export const runtimePublicPath = '/@react-refresh'
7
-
8
- const _require = createRequire(import.meta.url)
9
- const reactRefreshDir = path.dirname(
10
- _require.resolve('react-refresh/package.json')
11
- )
12
- const runtimeFilePath = path.join(
13
- reactRefreshDir,
14
- 'cjs/react-refresh-runtime.development.js'
15
- )
16
-
17
- export const runtimeCode = `
18
- const exports = {}
19
- ${fs.readFileSync(runtimeFilePath, 'utf-8')}
20
- function debounce(fn, delay) {
21
- let handle
22
- return () => {
23
- clearTimeout(handle)
24
- handle = setTimeout(fn, delay)
25
- }
26
- }
27
- exports.performReactRefresh = debounce(exports.performReactRefresh, 16)
28
- export default exports
29
- `
30
-
31
- export const preambleCode = `
32
- import RefreshRuntime from "__BASE__${runtimePublicPath.slice(1)}"
33
- RefreshRuntime.injectIntoGlobalHook(window)
34
- window.$RefreshReg$ = () => {}
35
- window.$RefreshSig$ = () => (type) => type
36
- window.__vite_plugin_react_preamble_installed__ = true
37
- `
38
-
39
- const header = `
40
- import RefreshRuntime from "${runtimePublicPath}";
41
-
42
- let prevRefreshReg;
43
- let prevRefreshSig;
44
-
45
- if (import.meta.hot) {
46
- if (!window.__vite_plugin_react_preamble_installed__) {
47
- throw new Error(
48
- "@vitejs/plugin-react can't detect preamble. Something is wrong. " +
49
- "See https://github.com/vitejs/vite-plugin-react/pull/11#discussion_r430879201"
50
- );
51
- }
52
-
53
- prevRefreshReg = window.$RefreshReg$;
54
- prevRefreshSig = window.$RefreshSig$;
55
- window.$RefreshReg$ = (type, id) => {
56
- RefreshRuntime.register(type, __SOURCE__ + " " + id)
57
- };
58
- window.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform;
59
- }`.replace(/[\n]+/gm, '')
60
-
61
- const footer = `
62
- if (import.meta.hot) {
63
- window.$RefreshReg$ = prevRefreshReg;
64
- window.$RefreshSig$ = prevRefreshSig;
65
-
66
- __ACCEPT__
67
- if (!window.__vite_plugin_react_timeout) {
68
- window.__vite_plugin_react_timeout = setTimeout(() => {
69
- window.__vite_plugin_react_timeout = 0;
70
- RefreshRuntime.performReactRefresh();
71
- }, 30);
72
- }
73
- }`
74
-
75
- export function addRefreshWrapper(
76
- code: string,
77
- id: string,
78
- accept: boolean
79
- ): string {
80
- return (
81
- header.replace('__SOURCE__', JSON.stringify(id)) +
82
- code +
83
- footer.replace('__ACCEPT__', accept ? 'import.meta.hot.accept();' : '')
84
- )
85
- }
86
-
87
- export function isRefreshBoundary(ast: t.File): boolean {
88
- // Every export must be a React component.
89
- return ast.program.body.every((node) => {
90
- if (node.type !== 'ExportNamedDeclaration') {
91
- return true
92
- }
93
- const { declaration, specifiers } = node
94
- if (declaration) {
95
- if (declaration.type === 'ClassDeclaration') return false
96
- if (declaration.type === 'VariableDeclaration') {
97
- return declaration.declarations.every((variable) =>
98
- isComponentLikeIdentifier(variable.id)
99
- )
100
- }
101
- if (declaration.type === 'FunctionDeclaration') {
102
- return !!declaration.id && isComponentLikeIdentifier(declaration.id)
103
- }
104
- }
105
- return specifiers.every((spec) => {
106
- return isComponentLikeIdentifier(spec.exported)
107
- })
108
- })
109
- }
110
-
111
- function isComponentLikeIdentifier(node: t.Node): boolean {
112
- return node.type === 'Identifier' && isComponentLikeName(node.name)
113
- }
114
-
115
- function isComponentLikeName(name: string): boolean {
116
- return typeof name === 'string' && name[0] >= 'A' && name[0] <= 'Z'
117
- }