fix(plugin-react): check for import React statement in .js files#6320
fix(plugin-react): check for import React statement in .js files#6320patak-cat merged 4 commits intovitejs:mainfrom
Conversation
|
i compare the webpack version(create-react-app) and vite: vite/packages/plugin-react/src/index.ts Line 121 in d8502e2 the util.js transformed result by webpack/vite (simplified version): // util.js with 'react' keyword
export function Test() {
console.log("Capital fn");
}
export function normal() {
console.log("normal"); // react words here
}// transform by webpack
function Test() {
console.log("Capital fn");
}
_c = Test;
function normal() {
console.log("normal"); // react words here
}
var _c;
// 1. regist
__webpack_require__.$Refresh$.register(_c, "Test");
// 2. react refresh
$ReactRefreshModuleRuntime$($ReactRefreshCurrentExports$);// transform by vite
if (import.meta.hot) {
window.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform;
}
export function Test() {
console.log("Capital fn");
}
_c = Test;
export function normal() {
console.log("normal"); // react words here
}
var _c;
// 1. regist
$RefreshReg$(_c, "Test");
if (import.meta.hot) {
// 2. react refresh
RefreshRuntime.performReactRefresh();
}but if there are no 'react' in util.js, vite wouldn't transform the file. let useFastRefresh = false
if (!skipFastRefresh && !ssr && !isNodeModules) {
// Modules with .js or .ts extension must import React.
// const isReactModule = isJSX || code.includes('react')
// if (isReactModule && filter(id)) {
// ---Replace isReactModule with isWorker condition here---
if (!isWorker && filter(id)) {
useFastRefresh = true
plugins.push([
await loadPlugin('react-refresh/babel.js'),
{ skipEnvCheck: true }
])
}
}@aleclarson what do you think ? 😅 |
|
Vite requires Could you update the vite/packages/plugin-react/src/index.ts Line 65 in 2825779 edit: Nevermind, I've got it |
…vitejs#6148)" This reverts commit 136883d.
…instead of using naïve `code.includes("react")` check
…ejs#6320) * fix: reactPlugin skip inject react hmr code to workerFile (fix vitejs#6148) * Revert "fix: reactPlugin skip inject react hmr code to workerFile (fix vitejs#6148)" This reverts commit 136883d. * fix(plugin-react): check for import React statement in .js files …instead of using naïve `code.includes("react")` check Co-authored-by: Alec Larson <1925840+aleclarson@users.noreply.github.com>
Description
Additional context
What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123).in vite-react project, when import a worker which contains 'react' word and export function name's FirstLetter is capital, the react/plugin will inject the hmr code to the worker.
reproduction : issue #6148 's repo.
in packages\plugin-react\src\index.ts file
before


after add the isWorker check