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 isLaxModern =
    isModern ||
71
    (options['preset-env']?.targets &&
72 73
      options['preset-env'].targets.esmodules === true)

74 75 76
  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
77
    modules: 'auto',
78
    exclude: ['transform-typeof-symbol'],
79
    ...options['preset-env'],
80
  }
J
Jason Miller 已提交
81

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

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

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