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): account for querystring in transform hook #5333

Merged
merged 3 commits into from
Oct 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
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