File size: 3,013 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import path from 'path'
import type { ReactCompilerOptions } from '../server/config-shared'

function getReactCompiler() {
  try {
    // It's in peerDependencies, so it should be available
    // eslint-disable-next-line import/no-extraneous-dependencies
    return require.resolve('babel-plugin-react-compiler')
  } catch {
    throw new Error(
      'Failed to load the `babel-plugin-react-compiler`. It is required to use the React Compiler. Please install it.'
    )
  }
}

const getReactCompilerPlugins = (
  options: boolean | ReactCompilerOptions | undefined,
  isDev: boolean,
  isServer: boolean
) => {
  if (!options || isServer) {
    return undefined
  }

  const compilerOptions = typeof options === 'boolean' ? {} : options
  if (options) {
    return [
      [
        getReactCompiler(),
        {
          panicThreshold: isDev ? undefined : 'NONE',
          ...compilerOptions,
        },
      ],
    ]
  }
  return undefined
}

const getBabelLoader = (
  useSWCLoader: boolean | undefined,
  babelConfigFile: string | undefined,
  isServer: boolean,
  distDir: string,
  pagesDir: string | undefined,
  cwd: string,
  srcDir: string,
  dev: boolean,
  isClient: boolean,
  reactCompilerOptions: boolean | ReactCompilerOptions | undefined,
  reactCompilerExclude: ((excludePath: string) => boolean) | undefined
) => {
  if (!useSWCLoader) {
    return {
      loader: require.resolve('./babel/loader/index'),
      options: {
        transformMode: 'default',
        configFile: babelConfigFile,
        isServer,
        distDir,
        pagesDir,
        cwd,
        srcDir: path.dirname(srcDir),
        development: dev,
        hasReactRefresh: dev && isClient,
        hasJsxRuntime: true,
        reactCompilerPlugins: getReactCompilerPlugins(
          reactCompilerOptions,
          dev,
          isServer
        ),
        reactCompilerExclude,
      },
    }
  }

  return undefined
}

/**
 * Get a separate babel loader for the react compiler, only used if Babel is not
 * configured through e.g. .babelrc. If user have babel config, this should be configured in the babel loader itself.
 * Note from react compiler:
 * > For best results, compiler must run as the first plugin in your Babel pipeline so it receives input as close to the original source as possible.
 */
const getReactCompilerLoader = (
  options: boolean | ReactCompilerOptions | undefined,
  cwd: string,
  isDev: boolean,
  isServer: boolean,
  reactCompilerExclude: ((excludePath: string) => boolean) | undefined
) => {
  const reactCompilerPlugins = getReactCompilerPlugins(options, isDev, isServer)
  if (!reactCompilerPlugins) {
    return undefined
  }

  const config: any = {
    loader: require.resolve('./babel/loader/index'),
    options: {
      transformMode: 'standalone',
      cwd,
      reactCompilerPlugins,
    },
  }

  if (reactCompilerExclude) {
    config.options.reactCompilerExclude = reactCompilerExclude
  }

  return config
}

export { getBabelLoader, getReactCompilerLoader }