Skip to content

fix: add metadata to jsx when using @vite/plugin-react-refresh #1933

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

Merged
merged 1 commit into from
Feb 8, 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
21 changes: 21 additions & 0 deletions packages/playground/react/__tests__/react.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,24 @@ test('should hmr', async () => {
// preserve state
expect(await page.textContent('button')).toMatch('count is: 1')
})

test('should have annotated jsx with file location metadata', async () => {
// we're not annotating in prod,
// so we skip this test when isBuild is true
if (isBuild) return

const meta = await page.evaluate(() => {
const button = document.querySelector('button')
const key = Object.keys(button).find(
(key) => key.indexOf('__reactFiber') === 0
)
return button[key]._debugSource
})
// If the evaluate call doesn't crash, and the returned metadata has
// the expected fields, we're good.
expect(Object.keys(meta).sort()).toEqual([
'columnNumber',
'fileName',
'lineNumber'
])
})
21 changes: 19 additions & 2 deletions packages/plugin-react-refresh/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @ts-check
const fs = require('fs')
const { transformSync } = require('@babel/core')
const { transformSync, ParserOptions } = require('@babel/core')

const runtimePublicPath = '/@react-refresh'
const runtimeFilePath = require.resolve(
Expand Down Expand Up @@ -41,6 +41,8 @@ function reactRefreshPlugin(opts) {
return {
name: 'react-refresh',

enforce: 'pre',

configResolved(config) {
shouldSkip = config.command === 'build' || config.isProduction
base = config.base
Expand Down Expand Up @@ -73,15 +75,30 @@ function reactRefreshPlugin(opts) {
return
}

/**
* @type ParserOptions["plugins"]
*/
const parserPlugins = ['jsx']
if (/\.tsx?$/.test(id)) {
// it's a typescript file
parserPlugins.push('typescript')
}
if (opts && opts.parserPlugins) {
parserPlugins.push(...opts.parserPlugins)
}

const isReasonReact = id.endsWith('.bs.js')
const result = transformSync(code, {
configFile: false,
filename: id,
parserOpts: {
sourceType: 'module',
allowAwaitOutsideFunction: true,
plugins: opts && opts.parserPlugins
plugins: parserPlugins
},
plugins: [
require('@babel/plugin-transform-react-jsx-self'),
require('@babel/plugin-transform-react-jsx-source'),
require('@babel/plugin-syntax-import-meta'),
[require('react-refresh/babel'), { skipEnvCheck: true }]
],
Expand Down
2 changes: 2 additions & 0 deletions packages/plugin-react-refresh/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
"dependencies": {
"@babel/core": "^7.12.10",
"@babel/plugin-syntax-import-meta": "^7.10.4",
"@babel/plugin-transform-react-jsx-self": "^7.12.10",
"@babel/plugin-transform-react-jsx-source": "^7.12.10",
"react-refresh": "^0.9.0"
}
}
14 changes: 14 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,20 @@
dependencies:
"@babel/helper-plugin-utils" "^7.12.13"

"@babel/plugin-transform-react-jsx-self@^7.12.10":
version "7.12.13"
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-react-jsx-self/-/plugin-transform-react-jsx-self-7.12.13.tgz#422d99d122d592acab9c35ea22a6cfd9bf189f60"
integrity sha512-FXYw98TTJ125GVCCkFLZXlZ1qGcsYqNQhVBQcZjyrwf8FEUtVfKIoidnO8S0q+KBQpDYNTmiGo1gn67Vti04lQ==
dependencies:
"@babel/helper-plugin-utils" "^7.12.13"

"@babel/plugin-transform-react-jsx-source@^7.12.10":
version "7.12.13"
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-react-jsx-source/-/plugin-transform-react-jsx-source-7.12.13.tgz#051d76126bee5c9a6aa3ba37be2f6c1698856bcb"
integrity sha512-O5JJi6fyfih0WfDgIJXksSPhGP/G0fQpfxYy87sDc+1sFmsCS6wr3aAn+whbzkhbjtq4VMqLRaSzR6IsshIC0Q==
dependencies:
"@babel/helper-plugin-utils" "^7.12.13"

"@babel/plugin-transform-typescript@^7.12.1":
version "7.12.13"
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-typescript/-/plugin-transform-typescript-7.12.13.tgz#8bcb5dd79cb8bba690d6920e19992d9228dfed48"
Expand Down