-
-
Notifications
You must be signed in to change notification settings - Fork 6.1k
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): apply babel.plugins
to project files only
#5255
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
df1cbf8
fix(plugin-react): apply `babel.plugins` to project files only
aleclarson e62341f
fix(plugin-react): treat virtual modules as "inside the project root"
aleclarson 363685a
refactor: add isJSX variable
aleclarson bb7563c
chore(plugin-react): skip `code.replace` call if no React alias exists
aleclarson e31cfac
fix: avoid checking optimized react-dom for compiled JSX
aleclarson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,36 +108,17 @@ export default function viteReact(opts: Options = {}): PluginOption[] { | |
[] | ||
|
||
if (/\.(mjs|[tj]sx?)$/.test(extension)) { | ||
const plugins = [...userPlugins] | ||
const isJSX = extension.endsWith('x') | ||
const isNodeModules = id.includes('/node_modules/') | ||
const isProjectFile = | ||
!isNodeModules && (id[0] === '\0' || id.startsWith(projectRoot + '/')) | ||
|
||
const parserPlugins: typeof userParserPlugins = [ | ||
...userParserPlugins, | ||
'importMeta', | ||
// This plugin is applied before esbuild transforms the code, | ||
// so we need to enable some stage 3 syntax that is supported in | ||
// TypeScript and some environments already. | ||
'topLevelAwait', | ||
'classProperties', | ||
'classPrivateProperties', | ||
'classPrivateMethods' | ||
] | ||
|
||
if (!extension.endsWith('.ts')) { | ||
parserPlugins.push('jsx') | ||
} | ||
|
||
const isTypeScript = /\.tsx?$/.test(extension) | ||
if (isTypeScript) { | ||
parserPlugins.push('typescript') | ||
} | ||
|
||
const isNodeModules = id.includes('node_modules') | ||
let plugins = isProjectFile ? [...userPlugins] : [] | ||
|
||
let useFastRefresh = false | ||
if (!skipFastRefresh && !ssr && !isNodeModules) { | ||
// Modules with .js or .ts extension must import React. | ||
const isReactModule = | ||
extension.endsWith('x') || code.includes('react') | ||
const isReactModule = isJSX || code.includes('react') | ||
if (isReactModule && filter(id)) { | ||
useFastRefresh = true | ||
plugins.push([ | ||
|
@@ -148,16 +129,16 @@ export default function viteReact(opts: Options = {}): PluginOption[] { | |
} | ||
|
||
let ast: t.File | null | undefined | ||
if (isNodeModules || extension.endsWith('x')) { | ||
if (!isProjectFile || isJSX) { | ||
if (useAutomaticRuntime) { | ||
// By reverse-compiling "React.createElement" calls into JSX, | ||
// React elements provided by dependencies will also use the | ||
// automatic runtime! | ||
const [restoredAst, isCommonJS] = isNodeModules | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A slight difference in behavior here:
|
||
const [restoredAst, isCommonJS] = !isProjectFile | ||
? await restoreJSX(babel, code, id) | ||
: [null, false] | ||
|
||
if (!isNodeModules || (ast = restoredAst)) { | ||
if (isProjectFile || (ast = restoredAst)) { | ||
plugins.push([ | ||
await loadPlugin( | ||
'@babel/plugin-transform-react-jsx' + | ||
|
@@ -174,7 +155,7 @@ export default function viteReact(opts: Options = {}): PluginOption[] { | |
plugins.push(babelImportToRequire) | ||
} | ||
} | ||
} else if (!isNodeModules) { | ||
} else if (isProjectFile) { | ||
// These plugins are only needed for the classic runtime. | ||
if (!isProduction) { | ||
plugins.push( | ||
|
@@ -191,7 +172,40 @@ export default function viteReact(opts: Options = {}): PluginOption[] { | |
} | ||
} | ||
|
||
const isReasonReact = extension.endsWith('.bs.js') | ||
// Plugins defined through this Vite plugin are only applied | ||
// to modules within the project root, but "babel.config.js" | ||
// files can define plugins that need to be applied to every | ||
// module, including node_modules and linked packages. | ||
const shouldSkip = | ||
!plugins.length && | ||
!opts.babel?.configFile && | ||
!(isProjectFile && opts.babel?.babelrc) | ||
|
||
if (shouldSkip) { | ||
return // Avoid parsing if no plugins exist. | ||
} | ||
|
||
const parserPlugins: typeof userParserPlugins = [ | ||
...userParserPlugins, | ||
'importMeta', | ||
// This plugin is applied before esbuild transforms the code, | ||
// so we need to enable some stage 3 syntax that is supported in | ||
// TypeScript and some environments already. | ||
'topLevelAwait', | ||
'classProperties', | ||
'classPrivateProperties', | ||
'classPrivateMethods' | ||
] | ||
|
||
if (!id.endsWith('.ts')) { | ||
parserPlugins.push('jsx') | ||
} | ||
|
||
if (/\.tsx?$/.test(id)) { | ||
parserPlugins.push('typescript') | ||
} | ||
|
||
const isReasonReact = id.endsWith('.bs.js') | ||
|
||
const babelOpts: TransformOptions = { | ||
babelrc: false, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If a virtual module wants to avoid being transformed by Babel, it can include
/node_modules/
in its module ID.