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
feat(babel-loader): provide migration help message for babel config #52565
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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 |
---|---|---|
|
@@ -159,6 +159,95 @@ function getCustomBabelConfig(configFilePath: string) { | |
) | ||
} | ||
|
||
let babelConfigWarned = false | ||
/** | ||
* Check if custom babel configuration from user only contains options that | ||
* can be migrated into latest Next.js features supported by SWC. | ||
* | ||
* This raises soft warning messages only, not making any errors yet. | ||
*/ | ||
function checkCustomBabelConfigDeprecation( | ||
config: Record<string, any> | undefined | ||
) { | ||
if (!config || Object.keys(config).length === 0) { | ||
return | ||
} | ||
|
||
const { plugins, presets, ...otherOptions } = config | ||
if (Object.keys(otherOptions ?? {}).length > 0) { | ||
return | ||
} | ||
|
||
if (babelConfigWarned) { | ||
return | ||
} | ||
|
||
babelConfigWarned = true | ||
Comment on lines
+181
to
+185
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. Nit: let's move this up to the top of the function. |
||
|
||
const isPresetReadyToDeprecate = | ||
!presets || | ||
presets.length === 0 || | ||
(presets.length === 1 && presets[0] === 'next/babel') | ||
const pluginReasons = [] | ||
const unsupportedPlugins = [] | ||
|
||
if (Array.isArray(plugins)) { | ||
for (const plugin of plugins) { | ||
const pluginName = Array.isArray(plugin) ? plugin[0] : plugin | ||
|
||
// [NOTE]: We cannot detect if the user uses babel-plugin-macro based transform plugins, | ||
// such as `styled-components/macro` in here. | ||
switch (pluginName) { | ||
case 'styled-components': | ||
case 'babel-plugin-styled-components': | ||
pluginReasons.push( | ||
`\t- 'styled-components' can be enabled via 'compiler.styledComponents' in 'next.config.js'` | ||
) | ||
break | ||
case '@emotion/babel-plugin': | ||
pluginReasons.push( | ||
`\t- '@emotion/babel-plugin' can be enabled via 'compiler.emotion' in 'next.config.js'` | ||
) | ||
break | ||
case 'babel-plugin-relay': | ||
pluginReasons.push( | ||
`\t- 'babel-plugin-relay' can be enabled via 'compiler.relay' in 'next.config.js'` | ||
) | ||
break | ||
case 'react-remove-properties': | ||
pluginReasons.push( | ||
`\t- 'react-remove-properties' can be enabled via 'compiler.reactRemoveProperties' in 'next.config.js'` | ||
) | ||
break | ||
case 'transform-remove-console': | ||
pluginReasons.push( | ||
`\t- 'transform-remove-console' can be enabled via 'compiler.removeConsole' in 'next.config.js'` | ||
) | ||
break | ||
default: | ||
unsupportedPlugins.push(pluginName) | ||
break | ||
} | ||
} | ||
} | ||
|
||
if (isPresetReadyToDeprecate && unsupportedPlugins.length === 0) { | ||
Log.warn( | ||
`It looks like there is a custom Babel configuration can be removed ${ | ||
pluginReasons.length > 0 ? ':' : '.' | ||
}` | ||
) | ||
|
||
if (pluginReasons.length > 0) { | ||
Log.warn(`Next.js supports the following features natively: `) | ||
Log.warn(pluginReasons.join('')) | ||
Log.warn( | ||
`For more details configuration options, please refer https://nextjs.org/docs/architecture/nextjs-compiler#supported-features` | ||
) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Generate a new, flat Babel config, ready to be handed to Babel-traverse. | ||
* This config should have no unresolved overrides, presets, etc. | ||
|
@@ -178,6 +267,8 @@ function getFreshConfig( | |
? getCustomBabelConfig(configFile) | ||
: undefined | ||
|
||
checkCustomBabelConfigDeprecation(customConfig) | ||
|
||
let options = { | ||
babelrc: false, | ||
cloneInputAst: false, | ||
|
Oops, something went wrong.
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.
otherOptions
is guaranteed to be an object.