@vitejs/plugin-react 3.0.0-alpha.2 → 3.0.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
@@ -14,7 +14,7 @@ import { defineConfig } from 'vite'
14
14
  import react from '@vitejs/plugin-react'
15
15
 
16
16
  export default defineConfig({
17
- plugins: [react()]
17
+ plugins: [react()],
18
18
  })
19
19
  ```
20
20
 
@@ -29,7 +29,7 @@ react({
29
29
  // Exclude storybook stories
30
30
  exclude: /\.stories\.(t|j)sx?$/,
31
31
  // Only .tsx files
32
- include: '**/*.tsx'
32
+ include: '**/*.tsx',
33
33
  })
34
34
  ```
35
35
 
@@ -39,7 +39,7 @@ By default, the plugin uses the [automatic JSX runtime](https://github.com/alloc
39
39
 
40
40
  ```js
41
41
  react({
42
- jsxRuntime: 'classic'
42
+ jsxRuntime: 'classic',
43
43
  })
44
44
  ```
45
45
 
@@ -69,9 +69,9 @@ If you are using ES syntax that are still in proposal status (e.g. class propert
69
69
  react({
70
70
  babel: {
71
71
  parserOpts: {
72
- plugins: ['decorators-legacy']
73
- }
74
- }
72
+ plugins: ['decorators-legacy'],
73
+ },
74
+ },
75
75
  })
76
76
  ```
77
77
 
package/dist/index.cjs CHANGED
@@ -78,13 +78,6 @@ const timeout = `
78
78
  }, 30);
79
79
  }
80
80
  `;
81
- const footer = `
82
- if (import.meta.hot) {
83
- window.$RefreshReg$ = prevRefreshReg;
84
- window.$RefreshSig$ = prevRefreshSig;
85
-
86
- __ACCEPT__
87
- }`;
88
81
  const checkAndAccept = `
89
82
  function isReactRefreshBoundary(mod) {
90
83
  if (mod == null || typeof mod !== 'object') {
@@ -118,37 +111,15 @@ import.meta.hot.accept(mod => {
118
111
  }
119
112
  });
120
113
  `;
121
- function addRefreshWrapper(code, id, accept) {
122
- return header.replace("__SOURCE__", JSON.stringify(id)) + code + footer.replace("__ACCEPT__", accept ? checkAndAccept : timeout);
123
- }
124
- function isRefreshBoundary(ast) {
125
- return ast.program.body.every((node) => {
126
- if (node.type !== "ExportNamedDeclaration") {
127
- return true;
128
- }
129
- const { declaration, specifiers } = node;
130
- if (declaration) {
131
- if (declaration.type === "ClassDeclaration")
132
- return false;
133
- if (declaration.type === "VariableDeclaration") {
134
- return declaration.declarations.every(
135
- (variable) => isComponentLikeIdentifier(variable.id)
136
- );
137
- }
138
- if (declaration.type === "FunctionDeclaration") {
139
- return !!declaration.id && isComponentLikeIdentifier(declaration.id);
140
- }
141
- }
142
- return specifiers.every((spec) => {
143
- return isComponentLikeIdentifier(spec.exported);
144
- });
145
- });
146
- }
147
- function isComponentLikeIdentifier(node) {
148
- return node.type === "Identifier" && isComponentLikeName(node.name);
149
- }
150
- function isComponentLikeName(name) {
151
- return typeof name === "string" && name[0] >= "A" && name[0] <= "Z";
114
+ const footer = `
115
+ if (import.meta.hot) {
116
+ window.$RefreshReg$ = prevRefreshReg;
117
+ window.$RefreshSig$ = prevRefreshSig;
118
+
119
+ ${checkAndAccept}
120
+ }`;
121
+ function addRefreshWrapper(code, id) {
122
+ return header.replace("__SOURCE__", JSON.stringify(id)) + code + footer;
152
123
  }
153
124
 
154
125
  const prependReactImportCode = "import React from 'react'; ";
@@ -309,11 +280,8 @@ function viteReact(opts = {}) {
309
280
  if (/\.tsx?$/.test(extension)) {
310
281
  parserPlugins.push("typescript");
311
282
  }
312
- const transformAsync = babel__namespace.transformAsync.bind(babel__namespace, code);
313
- const isReasonReact = extension.endsWith(".bs.js");
314
- const result = await transformAsync({
283
+ const result = await babel__namespace.transformAsync(code, {
315
284
  ...babelOptions,
316
- ast: !isReasonReact,
317
285
  root: projectRoot,
318
286
  filename: id,
319
287
  sourceFileName: filepath,
@@ -334,8 +302,7 @@ function viteReact(opts = {}) {
334
302
  if (result) {
335
303
  let code2 = result.code;
336
304
  if (useFastRefresh && /\$RefreshReg\$\(/.test(code2)) {
337
- const accept = isReasonReact || isRefreshBoundary(result.ast);
338
- code2 = addRefreshWrapper(code2, id, accept);
305
+ code2 = addRefreshWrapper(code2, id);
339
306
  }
340
307
  return {
341
308
  code: code2,
package/dist/index.d.ts CHANGED
@@ -33,7 +33,7 @@ interface Options {
33
33
  ssr?: boolean;
34
34
  }) => BabelOptions);
35
35
  }
36
- declare type BabelOptions = Omit<TransformOptions, 'ast' | 'filename' | 'root' | 'sourceFileName' | 'sourceMaps' | 'inputSourceMap'>;
36
+ type BabelOptions = Omit<TransformOptions, 'ast' | 'filename' | 'root' | 'sourceFileName' | 'sourceMaps' | 'inputSourceMap'>;
37
37
  /**
38
38
  * The object type used by the `options` passed to plugins with
39
39
  * an `api.reactBabel` method.
@@ -46,8 +46,8 @@ interface ReactBabelOptions extends BabelOptions {
46
46
  plugins: Extract<ParserOptions['plugins'], any[]>;
47
47
  };
48
48
  }
49
- declare type ReactBabelHook = (babelConfig: ReactBabelOptions, context: ReactBabelHookContext, config: ResolvedConfig) => void;
50
- declare type ReactBabelHookContext = {
49
+ type ReactBabelHook = (babelConfig: ReactBabelOptions, context: ReactBabelHookContext, config: ResolvedConfig) => void;
50
+ type ReactBabelHookContext = {
51
51
  ssr: boolean;
52
52
  id: string;
53
53
  };
package/dist/index.mjs CHANGED
@@ -63,13 +63,6 @@ const timeout = `
63
63
  }, 30);
64
64
  }
65
65
  `;
66
- const footer = `
67
- if (import.meta.hot) {
68
- window.$RefreshReg$ = prevRefreshReg;
69
- window.$RefreshSig$ = prevRefreshSig;
70
-
71
- __ACCEPT__
72
- }`;
73
66
  const checkAndAccept = `
74
67
  function isReactRefreshBoundary(mod) {
75
68
  if (mod == null || typeof mod !== 'object') {
@@ -103,37 +96,15 @@ import.meta.hot.accept(mod => {
103
96
  }
104
97
  });
105
98
  `;
106
- function addRefreshWrapper(code, id, accept) {
107
- return header.replace("__SOURCE__", JSON.stringify(id)) + code + footer.replace("__ACCEPT__", accept ? checkAndAccept : timeout);
108
- }
109
- function isRefreshBoundary(ast) {
110
- return ast.program.body.every((node) => {
111
- if (node.type !== "ExportNamedDeclaration") {
112
- return true;
113
- }
114
- const { declaration, specifiers } = node;
115
- if (declaration) {
116
- if (declaration.type === "ClassDeclaration")
117
- return false;
118
- if (declaration.type === "VariableDeclaration") {
119
- return declaration.declarations.every(
120
- (variable) => isComponentLikeIdentifier(variable.id)
121
- );
122
- }
123
- if (declaration.type === "FunctionDeclaration") {
124
- return !!declaration.id && isComponentLikeIdentifier(declaration.id);
125
- }
126
- }
127
- return specifiers.every((spec) => {
128
- return isComponentLikeIdentifier(spec.exported);
129
- });
130
- });
131
- }
132
- function isComponentLikeIdentifier(node) {
133
- return node.type === "Identifier" && isComponentLikeName(node.name);
134
- }
135
- function isComponentLikeName(name) {
136
- return typeof name === "string" && name[0] >= "A" && name[0] <= "Z";
99
+ const footer = `
100
+ if (import.meta.hot) {
101
+ window.$RefreshReg$ = prevRefreshReg;
102
+ window.$RefreshSig$ = prevRefreshSig;
103
+
104
+ ${checkAndAccept}
105
+ }`;
106
+ function addRefreshWrapper(code, id) {
107
+ return header.replace("__SOURCE__", JSON.stringify(id)) + code + footer;
137
108
  }
138
109
 
139
110
  const prependReactImportCode = "import React from 'react'; ";
@@ -294,11 +265,8 @@ function viteReact(opts = {}) {
294
265
  if (/\.tsx?$/.test(extension)) {
295
266
  parserPlugins.push("typescript");
296
267
  }
297
- const transformAsync = babel.transformAsync.bind(babel, code);
298
- const isReasonReact = extension.endsWith(".bs.js");
299
- const result = await transformAsync({
268
+ const result = await babel.transformAsync(code, {
300
269
  ...babelOptions,
301
- ast: !isReasonReact,
302
270
  root: projectRoot,
303
271
  filename: id,
304
272
  sourceFileName: filepath,
@@ -319,8 +287,7 @@ function viteReact(opts = {}) {
319
287
  if (result) {
320
288
  let code2 = result.code;
321
289
  if (useFastRefresh && /\$RefreshReg\$\(/.test(code2)) {
322
- const accept = isReasonReact || isRefreshBoundary(result.ast);
323
- code2 = addRefreshWrapper(code2, id, accept);
290
+ code2 = addRefreshWrapper(code2, id);
324
291
  }
325
292
  return {
326
293
  code: code2,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vitejs/plugin-react",
3
- "version": "3.0.0-alpha.2",
3
+ "version": "3.0.0",
4
4
  "license": "MIT",
5
5
  "author": "Evan You",
6
6
  "contributors": [
@@ -30,26 +30,21 @@
30
30
  },
31
31
  "repository": {
32
32
  "type": "git",
33
- "url": "git+https://github.com/vitejs/vite.git",
33
+ "url": "git+https://github.com/vitejs/vite-plugin-react.git",
34
34
  "directory": "packages/plugin-react"
35
35
  },
36
36
  "bugs": {
37
- "url": "https://github.com/vitejs/vite/issues"
37
+ "url": "https://github.com/vitejs/vite-plugin-react/issues"
38
38
  },
39
- "homepage": "https://github.com/vitejs/vite/tree/main/packages/plugin-react#readme",
39
+ "homepage": "https://github.com/vitejs/vite-plugin-react/tree/main/packages/plugin-react#readme",
40
40
  "dependencies": {
41
41
  "@babel/core": "^7.20.5",
42
- "@babel/plugin-transform-react-jsx": "^7.19.0",
43
- "@babel/plugin-transform-react-jsx-development": "^7.18.6",
44
42
  "@babel/plugin-transform-react-jsx-self": "^7.18.6",
45
43
  "@babel/plugin-transform-react-jsx-source": "^7.19.6",
46
- "magic-string": "^0.26.7",
44
+ "magic-string": "^0.27.0",
47
45
  "react-refresh": "^0.14.0"
48
46
  },
49
47
  "peerDependencies": {
50
- "vite": "^3.0.0"
51
- },
52
- "devDependencies": {
53
- "vite": "workspace:*"
48
+ "vite": "^4.0.0"
54
49
  }
55
50
  }