Skip to content
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

Merged
merged 4 commits into from
Jan 4, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/plugin-react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ export default function viteReact(opts: Options = {}): PluginOption[] {

if (/\.(mjs|[tj]sx?)$/.test(extension)) {
const isJSX = extension.endsWith('x')
// as worker, querystring could be
// import MyWorker from './worker?worker' => worker
// import MyWorker from './worker?worker&inline' => worker&inline
// transformed by webWorkerPlugin :'/worker?worker_file' => worker_file
const isWorker = querystring.includes('worker')
const isNodeModules = id.includes('/node_modules/')
const isProjectFile =
!isNodeModules && (id[0] === '\0' || id.startsWith(projectRoot + '/'))
Expand All @@ -118,7 +123,7 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
let useFastRefresh = false
if (!skipFastRefresh && !ssr && !isNodeModules) {
// Modules with .js or .ts extension must import React.
const isReactModule = isJSX || code.includes('react')
const isReactModule = isJSX || (code.includes('react') && !isWorker)
if (isReactModule && filter(id)) {
useFastRefresh = true
plugins.push([
Expand Down