|
|
import { readFileSync } from 'fs' |
|
|
import JSON5 from 'next/dist/compiled/json5' |
|
|
|
|
|
import { createConfigItem, loadOptions } from 'next/dist/compiled/babel/core' |
|
|
import loadConfig from 'next/dist/compiled/babel/core-lib-config' |
|
|
|
|
|
import type { NextBabelLoaderOptions, NextJsLoaderContext } from './types' |
|
|
import { consumeIterator } from './util' |
|
|
import * as Log from '../../output/log' |
|
|
import jsx from 'next/dist/compiled/babel/plugin-syntax-jsx' |
|
|
import { isReactCompilerRequired } from '../../swc' |
|
|
|
|
|
const nextDistPath = |
|
|
/(next[\\/]dist[\\/]shared[\\/]lib)|(next[\\/]dist[\\/]client)|(next[\\/]dist[\\/]pages)/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
interface CharacteristicsGermaneToCaching { |
|
|
isServer: boolean |
|
|
isPageFile: boolean |
|
|
isNextDist: boolean |
|
|
hasModuleExports: boolean |
|
|
fileNameOrExt: string |
|
|
} |
|
|
|
|
|
const fileExtensionRegex = /\.([a-z]+)$/ |
|
|
function getCacheCharacteristics( |
|
|
loaderOptions: NextBabelLoaderOptions, |
|
|
source: string, |
|
|
filename: string, |
|
|
transformMode: 'default' | 'standalone' |
|
|
): CharacteristicsGermaneToCaching { |
|
|
const { isServer, pagesDir } = loaderOptions |
|
|
const isPageFile = filename.startsWith(pagesDir) |
|
|
const isNextDist = nextDistPath.test(filename) |
|
|
const hasModuleExports = source.indexOf('module.exports') !== -1 |
|
|
const fileNameOrExt = |
|
|
transformMode === 'default' |
|
|
? fileExtensionRegex.exec(filename)?.[1] || 'unknown' |
|
|
: filename |
|
|
|
|
|
return { |
|
|
isServer, |
|
|
isPageFile, |
|
|
isNextDist, |
|
|
hasModuleExports, |
|
|
fileNameOrExt, |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function getPlugins( |
|
|
loaderOptions: NextBabelLoaderOptions, |
|
|
cacheCharacteristics: CharacteristicsGermaneToCaching |
|
|
) { |
|
|
const { isServer, isPageFile, isNextDist, hasModuleExports } = |
|
|
cacheCharacteristics |
|
|
|
|
|
const { development } = loaderOptions |
|
|
const hasReactRefresh = |
|
|
loaderOptions.transformMode !== 'standalone' |
|
|
? loaderOptions.hasReactRefresh |
|
|
: false |
|
|
|
|
|
const applyCommonJsItem = hasModuleExports |
|
|
? createConfigItem( |
|
|
require('../plugins/commonjs') as typeof import('../plugins/commonjs'), |
|
|
{ type: 'plugin' } |
|
|
) |
|
|
: null |
|
|
const reactRefreshItem = hasReactRefresh |
|
|
? createConfigItem( |
|
|
[ |
|
|
require('next/dist/compiled/react-refresh/babel') as typeof import('next/dist/compiled/react-refresh/babel'), |
|
|
{ skipEnvCheck: true }, |
|
|
], |
|
|
{ type: 'plugin' } |
|
|
) |
|
|
: null |
|
|
const pageConfigItem = |
|
|
!isServer && isPageFile |
|
|
? createConfigItem( |
|
|
[ |
|
|
require('../plugins/next-page-config') as typeof import('../plugins/next-page-config'), |
|
|
], |
|
|
{ |
|
|
type: 'plugin', |
|
|
} |
|
|
) |
|
|
: null |
|
|
const disallowExportAllItem = |
|
|
!isServer && isPageFile |
|
|
? createConfigItem( |
|
|
[ |
|
|
require('../plugins/next-page-disallow-re-export-all-exports') as typeof import('../plugins/next-page-disallow-re-export-all-exports'), |
|
|
], |
|
|
{ type: 'plugin' } |
|
|
) |
|
|
: null |
|
|
const transformDefineItem = createConfigItem( |
|
|
[ |
|
|
require.resolve('next/dist/compiled/babel/plugin-transform-define'), |
|
|
{ |
|
|
'process.env.NODE_ENV': development ? 'development' : 'production', |
|
|
'typeof window': isServer ? 'undefined' : 'object', |
|
|
'process.browser': isServer ? false : true, |
|
|
}, |
|
|
'next-js-transform-define-instance', |
|
|
], |
|
|
{ type: 'plugin' } |
|
|
) |
|
|
const nextSsgItem = |
|
|
!isServer && isPageFile |
|
|
? createConfigItem([require.resolve('../plugins/next-ssg-transform')], { |
|
|
type: 'plugin', |
|
|
}) |
|
|
: null |
|
|
const commonJsItem = isNextDist |
|
|
? createConfigItem( |
|
|
require('next/dist/compiled/babel/plugin-transform-modules-commonjs') as typeof import('next/dist/compiled/babel/plugin-transform-modules-commonjs'), |
|
|
{ type: 'plugin' } |
|
|
) |
|
|
: null |
|
|
const nextFontUnsupported = createConfigItem( |
|
|
[ |
|
|
require('../plugins/next-font-unsupported') as typeof import('../plugins/next-font-unsupported'), |
|
|
], |
|
|
{ type: 'plugin' } |
|
|
) |
|
|
|
|
|
return [ |
|
|
reactRefreshItem, |
|
|
pageConfigItem, |
|
|
disallowExportAllItem, |
|
|
applyCommonJsItem, |
|
|
transformDefineItem, |
|
|
nextSsgItem, |
|
|
commonJsItem, |
|
|
nextFontUnsupported, |
|
|
].filter(Boolean) |
|
|
} |
|
|
|
|
|
const isJsonFile = /\.(json|babelrc)$/ |
|
|
const isJsFile = /\.js$/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function getCustomBabelConfig(configFilePath: string) { |
|
|
if (isJsonFile.exec(configFilePath)) { |
|
|
const babelConfigRaw = readFileSync(configFilePath, 'utf8') |
|
|
return JSON5.parse(babelConfigRaw) |
|
|
} else if (isJsFile.exec(configFilePath)) { |
|
|
return require(configFilePath) |
|
|
} |
|
|
throw new Error( |
|
|
'The Next.js Babel loader does not support .mjs or .cjs config files.' |
|
|
) |
|
|
} |
|
|
|
|
|
let babelConfigWarned = false |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
|
|
|
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 that 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` |
|
|
) |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function getFreshConfig( |
|
|
this: NextJsLoaderContext, |
|
|
cacheCharacteristics: CharacteristicsGermaneToCaching, |
|
|
loaderOptions: NextBabelLoaderOptions, |
|
|
target: string, |
|
|
filename: string, |
|
|
inputSourceMap?: object | null |
|
|
) { |
|
|
const hasReactCompiler = await (async () => { |
|
|
if ( |
|
|
loaderOptions.reactCompilerPlugins && |
|
|
loaderOptions.reactCompilerPlugins.length === 0 |
|
|
) { |
|
|
return false |
|
|
} |
|
|
|
|
|
if (/[/\\]node_modules[/\\]/.test(filename)) { |
|
|
return false |
|
|
} |
|
|
|
|
|
if ( |
|
|
loaderOptions.reactCompilerExclude && |
|
|
loaderOptions.reactCompilerExclude(filename) |
|
|
) { |
|
|
return false |
|
|
} |
|
|
|
|
|
if (!(await isReactCompilerRequired(filename))) { |
|
|
return false |
|
|
} |
|
|
|
|
|
return true |
|
|
})() |
|
|
|
|
|
const reactCompilerPluginsIfEnabled = hasReactCompiler |
|
|
? loaderOptions.reactCompilerPlugins ?? [] |
|
|
: [] |
|
|
|
|
|
let { isServer, pagesDir, srcDir, development } = loaderOptions |
|
|
|
|
|
let options = { |
|
|
babelrc: false, |
|
|
cloneInputAst: false, |
|
|
filename, |
|
|
inputSourceMap: inputSourceMap || undefined, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sourceFileName: filename, |
|
|
sourceMaps: this.sourceMap, |
|
|
} as any |
|
|
|
|
|
const baseCaller = { |
|
|
name: 'next-babel-turbo-loader', |
|
|
supportsStaticESM: true, |
|
|
supportsDynamicImport: true, |
|
|
|
|
|
|
|
|
|
|
|
target: target, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
supportsTopLevelAwait: true, |
|
|
|
|
|
isServer, |
|
|
srcDir, |
|
|
pagesDir, |
|
|
isDev: development, |
|
|
|
|
|
...loaderOptions.caller, |
|
|
} |
|
|
|
|
|
if (loaderOptions.transformMode === 'standalone') { |
|
|
if (!reactCompilerPluginsIfEnabled.length) { |
|
|
return null |
|
|
} |
|
|
|
|
|
options.plugins = [jsx, ...reactCompilerPluginsIfEnabled] |
|
|
options.presets = [ |
|
|
[ |
|
|
require('next/dist/compiled/babel/preset-typescript') as typeof import('next/dist/compiled/babel/preset-typescript'), |
|
|
{ allowNamespaces: true }, |
|
|
], |
|
|
] |
|
|
options.caller = baseCaller |
|
|
} else { |
|
|
let { configFile, hasJsxRuntime } = loaderOptions |
|
|
let customConfig: any = configFile |
|
|
? getCustomBabelConfig(configFile) |
|
|
: undefined |
|
|
|
|
|
checkCustomBabelConfigDeprecation(customConfig) |
|
|
|
|
|
|
|
|
|
|
|
options.sourceMaps = |
|
|
loaderOptions.sourceMaps === undefined |
|
|
? this.sourceMap |
|
|
: loaderOptions.sourceMaps |
|
|
|
|
|
options.plugins = [ |
|
|
...getPlugins(loaderOptions, cacheCharacteristics), |
|
|
...reactCompilerPluginsIfEnabled, |
|
|
...(customConfig?.plugins || []), |
|
|
] |
|
|
|
|
|
|
|
|
options.target = isServer ? undefined : customConfig?.target |
|
|
|
|
|
|
|
|
options.env = customConfig?.env |
|
|
|
|
|
options.presets = (() => { |
|
|
|
|
|
if (customConfig?.presets) { |
|
|
return customConfig.presets |
|
|
} |
|
|
|
|
|
|
|
|
if (customConfig) { |
|
|
return undefined |
|
|
} |
|
|
|
|
|
|
|
|
return ['next/babel'] |
|
|
})() |
|
|
|
|
|
options.overrides = loaderOptions.overrides |
|
|
|
|
|
options.caller = { |
|
|
...baseCaller, |
|
|
hasJsxRuntime, |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (typeof options.target === 'undefined') { |
|
|
delete options.target |
|
|
} |
|
|
|
|
|
Object.defineProperty(options.caller, 'onWarning', { |
|
|
enumerable: false, |
|
|
writable: false, |
|
|
value: (reason: any) => { |
|
|
if (!(reason instanceof Error)) { |
|
|
reason = new Error(reason) |
|
|
} |
|
|
this.emitWarning(reason) |
|
|
}, |
|
|
}) |
|
|
|
|
|
const loadedOptions = loadOptions(options) |
|
|
const config = consumeIterator(loadConfig(loadedOptions)) |
|
|
|
|
|
return config |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function getCacheKey(cacheCharacteristics: CharacteristicsGermaneToCaching) { |
|
|
const { isServer, isPageFile, isNextDist, hasModuleExports, fileNameOrExt } = |
|
|
cacheCharacteristics |
|
|
|
|
|
const flags = |
|
|
0 | |
|
|
(isServer ? 0b0001 : 0) | |
|
|
(isPageFile ? 0b0010 : 0) | |
|
|
(isNextDist ? 0b0100 : 0) | |
|
|
(hasModuleExports ? 0b1000 : 0) |
|
|
|
|
|
return fileNameOrExt + flags |
|
|
} |
|
|
|
|
|
type BabelConfig = any |
|
|
const configCache: Map<any, BabelConfig> = new Map() |
|
|
const configFiles: Set<string> = new Set() |
|
|
|
|
|
export default async function getConfig( |
|
|
this: NextJsLoaderContext, |
|
|
{ |
|
|
source, |
|
|
target, |
|
|
loaderOptions, |
|
|
filename, |
|
|
inputSourceMap, |
|
|
}: { |
|
|
source: string |
|
|
loaderOptions: NextBabelLoaderOptions |
|
|
target: string |
|
|
filename: string |
|
|
inputSourceMap?: object | null |
|
|
} |
|
|
): Promise<BabelConfig> { |
|
|
const cacheCharacteristics = getCacheCharacteristics( |
|
|
loaderOptions, |
|
|
source, |
|
|
filename, |
|
|
loaderOptions.transformMode |
|
|
) |
|
|
|
|
|
if (loaderOptions.transformMode === 'default' && loaderOptions.configFile) { |
|
|
|
|
|
this.addDependency(loaderOptions.configFile) |
|
|
} |
|
|
|
|
|
const cacheKey = getCacheKey(cacheCharacteristics) |
|
|
if (configCache.has(cacheKey)) { |
|
|
const cachedConfig = configCache.get(cacheKey) |
|
|
if (!cachedConfig) { |
|
|
return null |
|
|
} |
|
|
|
|
|
return { |
|
|
...cachedConfig, |
|
|
options: { |
|
|
...cachedConfig.options, |
|
|
cwd: loaderOptions.cwd, |
|
|
root: loaderOptions.cwd, |
|
|
filename, |
|
|
sourceFileName: filename, |
|
|
}, |
|
|
} |
|
|
} |
|
|
|
|
|
if ( |
|
|
loaderOptions.transformMode === 'default' && |
|
|
loaderOptions.configFile && |
|
|
!configFiles.has(loaderOptions.configFile) |
|
|
) { |
|
|
configFiles.add(loaderOptions.configFile) |
|
|
Log.info( |
|
|
`Using external babel configuration from ${loaderOptions.configFile}` |
|
|
) |
|
|
} |
|
|
|
|
|
const freshConfig = await getFreshConfig.call( |
|
|
this, |
|
|
cacheCharacteristics, |
|
|
loaderOptions, |
|
|
target, |
|
|
filename, |
|
|
inputSourceMap |
|
|
) |
|
|
|
|
|
configCache.set(cacheKey, freshConfig) |
|
|
|
|
|
return freshConfig |
|
|
} |
|
|
|