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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
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)/

/**
 * The properties defined here are the conditions with which subsets of inputs
 * can be identified that are able to share a common Babel config.  For example,
 * in dev mode, different transforms must be applied to a source file depending
 * on whether you're compiling for the client or for the server - thus `isServer`
 * is germane.
 *
 * However, these characteristics need not protect against circumstances that
 * will not be encountered in Next.js.  For example, a source file may be
 * transformed differently depending on whether we're doing a production compile
 * or for HMR in dev mode.  However, those two circumstances will never be
 * encountered within the context of a single V8 context (and, thus, shared
 * cache).  Therefore, hasReactRefresh is _not_ germane to caching.
 *
 * NOTE: This approach does not support multiple `.babelrc` files in a
 * single project.  A per-cache-key config will be generated once and,
 * if `.babelrc` is present, that config will be used for any subsequent
 * transformations.
 */
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,
  }
}

/**
 * Return an array of Babel plugins, conditioned upon loader options and
 * source file characteristics.
 */
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$/

/**
 * While this function does block execution while reading from disk, it
 * should not introduce any issues.  The function is only invoked when
 * generating a fresh config, and only a small handful of configs should
 * be generated during compilation.
 */
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
/**
 * 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

  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 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`
      )
    }
  }
}

/**
 * Generate a new, flat Babel config, ready to be handed to Babel-traverse.
 * This config should have no unresolved overrides, presets, etc.
 */
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,

    // Ensure that Webpack will get a full absolute path in the sourcemap
    // so that it can properly map the module back to its internal cached
    // modules.
    sourceFileName: filename,
    sourceMaps: this.sourceMap,
  } as any

  const baseCaller = {
    name: 'next-babel-turbo-loader',
    supportsStaticESM: true,
    supportsDynamicImport: true,

    // Provide plugins with insight into webpack target.
    // https://github.com/babel/babel-loader/issues/787
    target: target,

    // Webpack 5 supports TLA behind a flag. We enable it by default
    // for Babel, and then webpack will throw an error if the experimental
    // flag isn't enabled.
    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)

    // Set the default sourcemap behavior based on Webpack's mapping flag,
    // but allow users to override if they want.
    options.sourceMaps =
      loaderOptions.sourceMaps === undefined
        ? this.sourceMap
        : loaderOptions.sourceMaps

    options.plugins = [
      ...getPlugins(loaderOptions, cacheCharacteristics),
      ...reactCompilerPluginsIfEnabled,
      ...(customConfig?.plugins || []),
    ]

    // target can be provided in babelrc
    options.target = isServer ? undefined : customConfig?.target

    // env can be provided in babelrc
    options.env = customConfig?.env

    options.presets = (() => {
      // If presets is defined the user will have next/babel in their babelrc
      if (customConfig?.presets) {
        return customConfig.presets
      }

      // If presets is not defined the user will likely have "env" in their babelrc
      if (customConfig) {
        return undefined
      }

      // If no custom config is provided the default is to use next/babel
      return ['next/babel']
    })()

    options.overrides = loaderOptions.overrides

    options.caller = {
      ...baseCaller,
      hasJsxRuntime,
    }
  }

  // Babel does strict checks on the config so undefined is not allowed
  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
}

/**
 * Each key returned here corresponds with a Babel config that can be shared.
 * The conditions of permissible sharing between files is dependent on specific
 * file attributes and Next.js compiler states: `CharacteristicsGermaneToCaching`.
 */
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) {
    // Ensures webpack invalidates the cache for this loader when the config file changes
    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
}