| import type { PluginItem } from 'next/dist/compiled/babel/core' |
| import { dirname } from 'path' |
|
|
| const isLoadIntentTest = process.env.NODE_ENV === 'test' |
| const isLoadIntentDevelopment = process.env.NODE_ENV === 'development' |
|
|
| type StyledJsxPlugin = [string, any] | string |
| type StyledJsxBabelOptions = |
| | { |
| plugins?: StyledJsxPlugin[] |
| styleModule?: string |
| 'babel-test'?: boolean |
| } |
| | undefined |
|
|
| |
| function styledJsxOptions(options: StyledJsxBabelOptions) { |
| options = options || {} |
| options.styleModule = 'styled-jsx/style' |
|
|
| if (!Array.isArray(options.plugins)) { |
| return options |
| } |
|
|
| options.plugins = options.plugins.map( |
| (plugin: StyledJsxPlugin): StyledJsxPlugin => { |
| if (Array.isArray(plugin)) { |
| const [name, pluginOptions] = plugin |
| return [require.resolve(name), pluginOptions] |
| } |
|
|
| return require.resolve(plugin) |
| } |
| ) |
|
|
| return options |
| } |
|
|
| type NextBabelPresetOptions = { |
| 'preset-env'?: any |
| 'preset-react'?: any |
| 'class-properties'?: any |
| 'transform-runtime'?: any |
| 'styled-jsx'?: StyledJsxBabelOptions |
| 'preset-typescript'?: any |
| } |
|
|
| type BabelPreset = { |
| presets?: PluginItem[] | null |
| plugins?: PluginItem[] | null |
| sourceType?: 'script' | 'module' | 'unambiguous' |
| overrides?: Array<{ test: RegExp } & Omit<BabelPreset, 'overrides'>> |
| } |
|
|
| |
| function supportsStaticESM(caller: any): boolean { |
| return !!caller?.supportsStaticESM |
| } |
|
|
| export default ( |
| api: any, |
| options: NextBabelPresetOptions = {} |
| ): BabelPreset => { |
| const supportsESM = api.caller(supportsStaticESM) |
| const isServer = api.caller((caller: any) => !!caller && caller.isServer) |
| const isCallerDevelopment = api.caller((caller: any) => caller?.isDev) |
|
|
| |
| const isTest = isCallerDevelopment == null && isLoadIntentTest |
|
|
| |
| const isDevelopment = |
| isCallerDevelopment === true || |
| (isCallerDevelopment == null && isLoadIntentDevelopment) |
|
|
| |
| const isProduction = !(isTest || isDevelopment) |
|
|
| const isBabelLoader = api.caller( |
| (caller: any) => |
| !!caller && |
| (caller.name === 'babel-loader' || |
| caller.name === 'next-babel-turbo-loader') |
| ) |
|
|
| const useJsxRuntime = |
| options['preset-react']?.runtime === 'automatic' || |
| (Boolean(api.caller((caller: any) => !!caller && caller.hasJsxRuntime)) && |
| options['preset-react']?.runtime !== 'classic') |
|
|
| const presetEnvConfig = { |
| |
| |
| modules: 'auto', |
| exclude: ['transform-typeof-symbol'], |
| ...options['preset-env'], |
| } |
|
|
| |
| |
| if ( |
| (isServer || isTest) && |
| (!presetEnvConfig.targets || |
| !( |
| typeof presetEnvConfig.targets === 'object' && |
| 'node' in presetEnvConfig.targets |
| )) |
| ) { |
| presetEnvConfig.targets = { |
| |
| |
| |
| node: process.versions.node, |
| } |
| } |
|
|
| return { |
| sourceType: 'unambiguous', |
| presets: [ |
| [ |
| require('next/dist/compiled/babel/preset-env') as typeof import('next/dist/compiled/babel/preset-env'), |
| presetEnvConfig, |
| ], |
| [ |
| require('next/dist/compiled/babel/preset-react') as typeof import('next/dist/compiled/babel/preset-react'), |
| { |
| |
| |
| development: isDevelopment || isTest, |
| ...(useJsxRuntime ? { runtime: 'automatic' } : { pragma: '__jsx' }), |
| ...options['preset-react'], |
| }, |
| ], |
| [ |
| require('next/dist/compiled/babel/preset-typescript') as typeof import('next/dist/compiled/babel/preset-typescript'), |
| { allowNamespaces: true, ...options['preset-typescript'] }, |
| ], |
| ], |
| plugins: [ |
| !useJsxRuntime && [ |
| require('./plugins/jsx-pragma') as typeof import('./plugins/jsx-pragma'), |
| { |
| |
| |
| |
| module: 'react', |
| importAs: 'React', |
| pragma: '__jsx', |
| property: 'createElement', |
| }, |
| ], |
| [ |
| require('./plugins/optimize-hook-destructuring') as typeof import('./plugins/optimize-hook-destructuring'), |
| { |
| |
| lib: true, |
| }, |
| ], |
| require('next/dist/compiled/babel/plugin-syntax-dynamic-import') as typeof import('next/dist/compiled/babel/plugin-syntax-dynamic-import'), |
| [ |
| require('next/dist/compiled/babel/plugin-syntax-import-attributes') as typeof import('next/dist/compiled/babel/plugin-syntax-import-attributes'), |
| { |
| deprecatedAssertSyntax: true, |
| }, |
| ], |
| require('./plugins/react-loadable-plugin') as typeof import('./plugins/react-loadable-plugin'), |
| |
| |
| |
| options['class-properties'] && [ |
| require('next/dist/compiled/babel/plugin-proposal-class-properties') as typeof import('next/dist/compiled/babel/plugin-proposal-class-properties'), |
| options['class-properties'] || {}, |
| ], |
| [ |
| require('next/dist/compiled/babel/plugin-proposal-object-rest-spread') as typeof import('next/dist/compiled/babel/plugin-proposal-object-rest-spread'), |
| { |
| useBuiltIns: true, |
| }, |
| ], |
| !isServer && [ |
| require('next/dist/compiled/babel/plugin-transform-runtime') as typeof import('next/dist/compiled/babel/plugin-transform-runtime'), |
| { |
| corejs: false, |
| helpers: true, |
| regenerator: true, |
| useESModules: supportsESM && presetEnvConfig.modules !== 'commonjs', |
| absoluteRuntime: isBabelLoader |
| ? dirname( |
| require.resolve( |
| 'next/dist/compiled/@babel/runtime/package.json' |
| ) |
| ) |
| : undefined, |
| ...options['transform-runtime'], |
| }, |
| ], |
| [ |
| isTest && options['styled-jsx'] && options['styled-jsx']['babel-test'] |
| ? (require('styled-jsx/babel-test') as typeof import('styled-jsx/babel-test')) |
| : (require('styled-jsx/babel') as typeof import('styled-jsx/babel')), |
| styledJsxOptions(options['styled-jsx']), |
| ], |
| require('./plugins/amp-attributes') as typeof import('./plugins/amp-attributes'), |
| isProduction && [ |
| require('next/dist/compiled/babel/plugin-transform-react-remove-prop-types') as typeof import('next/dist/compiled/babel/plugin-transform-react-remove-prop-types'), |
| { |
| removeImport: true, |
| }, |
| ], |
| isServer && |
| (require('next/dist/compiled/babel/plugin-syntax-bigint') as typeof import('next/dist/compiled/babel/plugin-syntax-bigint')), |
| |
| |
| require('next/dist/compiled/babel/plugin-proposal-numeric-separator') as typeof import('next/dist/compiled/babel/plugin-proposal-numeric-separator'), |
| require('next/dist/compiled/babel/plugin-proposal-export-namespace-from') as typeof import('next/dist/compiled/babel/plugin-proposal-export-namespace-from'), |
| ].filter(Boolean), |
| } |
| } |
|
|