Skip to content

Commit

Permalink
fix: remove unnecessary react/jsx-runtime loader
Browse files Browse the repository at this point in the history
  • Loading branch information
pengx17 committed Jul 26, 2021
1 parent 6b07c31 commit f9006ba
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 42 deletions.
5 changes: 2 additions & 3 deletions packages/plugin-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"scripts": {
"dev": "tsc -p . -w --incremental",
"build": "rimraf dist && run-s build-bundle build-types",
"build-bundle": "esbuild src/index.ts --bundle --platform=node --target=node12 --external:@babel/* --external:@rollup/* --external:resolve --external:react-refresh/* --outfile=dist/index.js",
"build-bundle": "esbuild src/index.ts --bundle --platform=node --target=node12 --external:@babel/* --external:@rollup/* --external:react-refresh/* --outfile=dist/index.js",
"build-types": "tsc -p . --emitDeclarationOnly --outDir temp && api-extractor run && rimraf temp",
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s --commit-path . --lerna-package plugin-react",
"release": "node ../../scripts/release.js --skipBuild"
Expand All @@ -38,8 +38,7 @@
"@babel/plugin-transform-react-jsx-self": "^7.14.5",
"@babel/plugin-transform-react-jsx-source": "^7.14.5",
"@rollup/pluginutils": "^4.1.0",
"react-refresh": "^0.10.0",
"resolve": "^1.20.0"
"react-refresh": "^0.10.0"
},
"devDependencies": {
"vite": "link:../vite"
Expand Down
41 changes: 2 additions & 39 deletions packages/plugin-react/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { ParserOptions, TransformOptions, types as t } from '@babel/core'
import * as babel from '@babel/core'
import { createFilter } from '@rollup/pluginutils'
import resolve from 'resolve'
import type { Logger, Plugin, PluginOption } from 'vite'
import {
addRefreshWrapper,
Expand Down Expand Up @@ -217,7 +216,7 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
config.plugins.forEach(
(plugin) =>
(plugin.name === 'react-refresh' ||
(plugin !== viteReactJsx && plugin.name === 'vite:react-jsx')) &&
plugin.name === 'vite:react-jsx') &&
config.logger.warn(
`[@vitejs/plugin-react] You should stop using "${plugin.name}" ` +
`since this plugin conflicts with it.`
Expand Down Expand Up @@ -246,43 +245,7 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
}
}

const runtimeId = 'react/jsx-runtime'
// Adapted from https://github.com/alloc/vite-react-jsx
const viteReactJsx: Plugin = {
name: 'vite:react-jsx',
enforce: 'pre',
config: () => ({
// react/jsx-runtime is in CJS format
// we want to explicitly cast it to ESM
optimizeDeps: {
include: ['react/jsx-runtime']
}
}),
resolveId(id: string) {
return id === runtimeId ? id : null
},
load(id: string) {
if (id === runtimeId) {
const runtimePath = resolve.sync(runtimeId, {
basedir: projectRoot
})
const exports = ['jsx', 'jsxs', 'Fragment']
return [
`import * as jsxRuntime from '${runtimePath}'`,
// We can't use `export * from` or else any callsite that uses
// this module will be compiled to `jsxRuntime.exports.jsx`
// instead of the more concise `jsx` alias.
...exports.map((name) => `export const ${name} = jsxRuntime.${name}`)
].join('\n')
}
}
}

return [
viteBabel,
viteReactRefresh,
opts.jsxRuntime === 'automatic' && viteReactJsx
]
return [viteBabel, viteReactRefresh]
}

viteReact.preambleCode = preambleCode
Expand Down

0 comments on commit f9006ba

Please sign in to comment.