-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(plugin-react): check for import React statement in .js files #6320
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer if we improved the code.includes("react")
check so it doesn't have false positives
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