Skip to content

Commit

Permalink
fix(plugin-react): account for querystring in transform hook (#5333)
Browse files Browse the repository at this point in the history
  • Loading branch information
frandiox authored Oct 19, 2021
1 parent 96591bf commit 13c3813
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
3 changes: 3 additions & 0 deletions packages/playground/react/App.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useState } from 'react'
import Dummy from './components/Dummy?qs-should-not-break-plugin-react'

function App() {
const [count, setCount] = useState(0)
Expand All @@ -23,6 +24,8 @@ function App() {
Learn React
</a>
</header>

<Dummy />
</div>
)
}
Expand Down
3 changes: 3 additions & 0 deletions packages/playground/react/components/Dummy.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Dummy() {
return <></>
}
23 changes: 17 additions & 6 deletions packages/plugin-react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
// - import React, {useEffect} from 'react';
const importReactRE = /(^|\n)import\s+(\*\s+as\s+)?React(,|\s+)/

// Any extension, including compound ones like '.bs.js'
const fileExtensionRE = /\.[^\/\s\?]+$/

const viteBabel: Plugin = {
name: 'vite:react-babel',
enforce: 'pre',
Expand Down Expand Up @@ -96,7 +99,14 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
)
},
async transform(code, id, ssr) {
if (/\.(mjs|[tj]sx?)$/.test(id)) {
// File extension could be mocked/overriden in querystring.
const [filepath, querystring = ''] = id.split('?')
const [extension = ''] =
querystring.match(fileExtensionRE) ||
filepath.match(fileExtensionRE) ||
[]

if (/\.(mjs|[tj]sx?)$/.test(extension)) {
const plugins = [...userPlugins]

const parserPlugins: typeof userParserPlugins = [
Expand All @@ -111,11 +121,11 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
'classPrivateMethods'
]

if (!id.endsWith('.ts')) {
if (!extension.endsWith('.ts')) {
parserPlugins.push('jsx')
}

const isTypeScript = /\.tsx?$/.test(id)
const isTypeScript = /\.tsx?$/.test(extension)
if (isTypeScript) {
parserPlugins.push('typescript')
}
Expand All @@ -125,7 +135,8 @@ 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 = id.endsWith('x') || code.includes('react')
const isReactModule =
extension.endsWith('x') || code.includes('react')
if (isReactModule && filter(id)) {
useFastRefresh = true
plugins.push([
Expand All @@ -136,7 +147,7 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
}

let ast: t.File | null | undefined
if (isNodeModules || id.endsWith('x')) {
if (isNodeModules || extension.endsWith('x')) {
if (useAutomaticRuntime) {
// By reverse-compiling "React.createElement" calls into JSX,
// React elements provided by dependencies will also use the
Expand Down Expand Up @@ -179,7 +190,7 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
}
}

const isReasonReact = id.endsWith('.bs.js')
const isReasonReact = extension.endsWith('.bs.js')

const babelOpts: TransformOptions = {
babelrc: false,
Expand Down

0 comments on commit 13c3813

Please sign in to comment.