preset.ts 5.7 KB
Newer Older
G
Guy Bedford 已提交
1
import { PluginItem } from '@babel/core'
2 3 4 5 6 7
const env = process.env.NODE_ENV
const isProduction = env === 'production'
const isDevelopment = env === 'development'
const isTest = env === 'test'

type StyledJsxPlugin = [string, any] | string
8 9
type StyledJsxBabelOptions =
  | {
J
Joe Haddad 已提交
10
      plugins?: StyledJsxPlugin[]
11
      'babel-test'?: boolean
12 13
    }
  | undefined
14 15

// Resolve styled-jsx plugins
16
function styledJsxOptions(options: StyledJsxBabelOptions) {
17 18 19 20 21 22 23 24
  if (!options) {
    return {}
  }

  if (!Array.isArray(options.plugins)) {
    return options
  }

25 26 27
  options.plugins = options.plugins.map(
    (plugin: StyledJsxPlugin): StyledJsxPlugin => {
      if (Array.isArray(plugin)) {
28 29
        const [name, pluginOptions] = plugin
        return [require.resolve(name), pluginOptions]
30
      }
31

32 33 34
      return require.resolve(plugin)
    }
  )
35 36 37 38 39

  return options
}

type NextBabelPresetOptions = {
40 41 42 43
  'preset-env'?: any
  'preset-react'?: any
  'class-properties'?: any
  'transform-runtime'?: any
44
  'experimental-modern-preset'?: PluginItem
45
  'styled-jsx'?: StyledJsxBabelOptions
46
  'preset-typescript'?: any
47 48 49
}

type BabelPreset = {
50 51
  presets?: PluginItem[] | null
  plugins?: PluginItem[] | null
52
  sourceType?: 'script' | 'module' | 'unambiguous'
53
  overrides?: Array<{ test: RegExp } & Omit<BabelPreset, 'overrides'>>
54 55
}

56
// Taken from https://github.com/babel/babel/commit/d60c5e1736543a6eac4b549553e107a9ba967051#diff-b4beead8ad9195361b4537601cc22532R158
57
function supportsStaticESM(caller: any): boolean {
58
  return !!caller?.supportsStaticESM
59 60
}

61 62 63 64
module.exports = (
  api: any,
  options: NextBabelPresetOptions = {}
): BabelPreset => {
65
  const supportsESM = api.caller(supportsStaticESM)
J
Jason Miller 已提交
66
  const isServer = api.caller((caller: any) => !!caller && caller.isServer)
67
  const isModern = api.caller((caller: any) => !!caller && caller.isModern)
68 69 70
  const hasJsxRuntime = Boolean(
    api.caller((caller: any) => !!caller && caller.hasJsxRuntime)
  )
71

72 73
  const isLaxModern =
    isModern ||
74
    (options['preset-env']?.targets &&
75 76
      options['preset-env'].targets.esmodules === true)

77 78 79
  const presetEnvConfig = {
    // In the test environment `modules` is often needed to be set to true, babel figures that out by itself using the `'auto'` option
    // In production/development this option is set to `false` so that webpack can handle import/export with tree-shaking
80
    modules: 'auto',
81
    exclude: ['transform-typeof-symbol'],
82
    ...options['preset-env'],
83
  }
J
Jason Miller 已提交
84

85 86
  // When transpiling for the server or tests, target the current Node version
  // if not explicitly specified:
J
Jason Miller 已提交
87
  if (
88
    (isServer || isTest) &&
89 90 91 92 93
    (!presetEnvConfig.targets ||
      !(
        typeof presetEnvConfig.targets === 'object' &&
        'node' in presetEnvConfig.targets
      ))
J
Jason Miller 已提交
94 95 96 97 98 99 100 101
  ) {
    presetEnvConfig.targets = {
      // Targets the current process' version of Node. This requires apps be
      // built and deployed on the same version of Node.
      node: 'current',
    }
  }

102
  // specify a preset to use instead of @babel/preset-env
103 104 105
  const customModernPreset =
    isLaxModern && options['experimental-modern-preset']

106
  return {
107
    sourceType: 'unambiguous',
108
    presets: [
109
      customModernPreset || [
G
Guy Bedford 已提交
110
        require('@babel/preset-env').default,
111 112
        presetEnvConfig,
      ],
113
      [
G
Guy Bedford 已提交
114
        require('@babel/preset-react'),
115 116 117 118
        {
          // This adds @babel/plugin-transform-react-jsx-source and
          // @babel/plugin-transform-react-jsx-self automatically in development
          development: isDevelopment || isTest,
119
          ...(hasJsxRuntime ? { runtime: 'automatic' } : { pragma: '__jsx' }),
120 121 122
          ...options['preset-react'],
        },
      ],
123 124 125 126
      [
        require('@babel/preset-typescript'),
        { allowNamespaces: true, ...options['preset-typescript'] },
      ],
127 128
    ],
    plugins: [
129
      !hasJsxRuntime && [
130 131 132 133 134 135 136 137 138 139 140
        require('./plugins/jsx-pragma'),
        {
          // This produces the following injected import for modules containing JSX:
          //   import React from 'react';
          //   var __jsx = React.createElement;
          module: 'react',
          importAs: 'React',
          pragma: '__jsx',
          property: 'createElement',
        },
      ],
141 142 143 144 145 146 147
      [
        require('./plugins/optimize-hook-destructuring'),
        {
          // only optimize hook functions imported from React/Preact
          lib: true,
        },
      ],
G
Guy Bedford 已提交
148
      require('@babel/plugin-syntax-dynamic-import'),
149
      require('./plugins/react-loadable-plugin'),
150
      [
G
Guy Bedford 已提交
151
        require('@babel/plugin-proposal-class-properties'),
152 153 154
        options['class-properties'] || {},
      ],
      [
G
Guy Bedford 已提交
155
        require('@babel/plugin-proposal-object-rest-spread'),
156 157 158 159
        {
          useBuiltIns: true,
        },
      ],
160
      !isServer && [
G
Guy Bedford 已提交
161
        require('@babel/plugin-transform-runtime'),
162
        {
163
          corejs: false,
164 165 166
          helpers: true,
          regenerator: true,
          useESModules: supportsESM && presetEnvConfig.modules !== 'commonjs',
167
          absoluteRuntime: process.versions.pnp ? __dirname : undefined,
168 169 170
          ...options['transform-runtime'],
        },
      ],
J
Joe Haddad 已提交
171 172 173 174 175 176
      [
        isTest && options['styled-jsx'] && options['styled-jsx']['babel-test']
          ? require('styled-jsx/babel-test')
          : require('styled-jsx/babel'),
        styledJsxOptions(options['styled-jsx']),
      ],
177
      require('./plugins/amp-attributes'),
178
      isProduction && [
G
Guy Bedford 已提交
179
        require('babel-plugin-transform-react-remove-prop-types'),
180 181 182 183
        {
          removeImport: true,
        },
      ],
G
Guy Bedford 已提交
184
      isServer && require('@babel/plugin-syntax-bigint'),
185 186 187
      // Always compile numeric separator because the resulting number is
      // smaller.
      require('@babel/plugin-proposal-numeric-separator'),
188
      require('@babel/plugin-proposal-export-namespace-from'),
189
    ].filter(Boolean),
190 191
  }
}