forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preset.ts
181 lines (167 loc) · 5.14 KB
/
preset.ts
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
import { PluginItem } from '@babel/core'
const env = process.env.NODE_ENV
const isProduction = env === 'production'
const isDevelopment = env === 'development'
const isTest = env === 'test'
type StyledJsxPlugin = [string, any] | string
type StyledJsxBabelOptions =
| {
plugins?: StyledJsxPlugin[]
'babel-test'?: boolean
}
| undefined
// Resolve styled-jsx plugins
function styledJsxOptions(options: StyledJsxBabelOptions) {
if (!options) {
return {}
}
if (!Array.isArray(options.plugins)) {
return options
}
options.plugins = options.plugins.map(
(plugin: StyledJsxPlugin): StyledJsxPlugin => {
if (Array.isArray(plugin)) {
const [name, options] = plugin
return [require.resolve(name), options]
}
return require.resolve(plugin)
}
)
return options
}
type NextBabelPresetOptions = {
'preset-env'?: any
'preset-react'?: any
'class-properties'?: any
'transform-runtime'?: any
'experimental-modern-preset'?: PluginItem
'styled-jsx'?: StyledJsxBabelOptions
}
type BabelPreset = {
presets?: PluginItem[] | null
plugins?: PluginItem[] | null
sourceType?: 'script' | 'module' | 'unambiguous'
overrides?: any[]
}
// Taken from https://github.com/babel/babel/commit/d60c5e1736543a6eac4b549553e107a9ba967051#diff-b4beead8ad9195361b4537601cc22532R158
function supportsStaticESM(caller: any) {
return !!(caller && caller.supportsStaticESM)
}
module.exports = (
api: any,
options: NextBabelPresetOptions = {}
): BabelPreset => {
const supportsESM = api.caller(supportsStaticESM)
const isServer = api.caller((caller: any) => !!caller && caller.isServer)
const isModern = api.caller((caller: any) => !!caller && caller.isModern)
const isLaxModern =
isModern ||
(options['preset-env'] &&
options['preset-env'].targets &&
options['preset-env'].targets.esmodules === true)
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
modules: 'auto',
exclude: ['transform-typeof-symbol'],
...options['preset-env'],
}
// When transpiling for the server or tests, target the current Node version
// if not explicitly specified:
if (
(isServer || isTest) &&
(!presetEnvConfig.targets ||
!(
typeof presetEnvConfig.targets === 'object' &&
'node' in presetEnvConfig.targets
))
) {
presetEnvConfig.targets = {
// Targets the current process' version of Node. This requires apps be
// built and deployed on the same version of Node.
node: 'current',
}
}
// spefify a preset to use instead of @babel/preset-env:
const customModernPreset =
isLaxModern && options['experimental-modern-preset']
return {
sourceType: 'unambiguous',
presets: [
customModernPreset || [
require('@babel/preset-env').default,
presetEnvConfig,
],
[
require('@babel/preset-react'),
{
// This adds @babel/plugin-transform-react-jsx-source and
// @babel/plugin-transform-react-jsx-self automatically in development
development: isDevelopment || isTest,
pragma: '__jsx',
...options['preset-react'],
},
],
require('@babel/preset-typescript'),
],
plugins: [
[
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',
},
],
[
require('./plugins/optimize-hook-destructuring'),
{
// only optimize hook functions imported from React/Preact
lib: true,
},
],
require('@babel/plugin-syntax-dynamic-import'),
require('./plugins/react-loadable-plugin'),
[
require('@babel/plugin-proposal-class-properties'),
options['class-properties'] || {},
],
[
require('@babel/plugin-proposal-object-rest-spread'),
{
useBuiltIns: true,
},
],
[
require('@babel/plugin-transform-runtime'),
{
corejs: 2,
helpers: true,
regenerator: true,
useESModules: supportsESM && presetEnvConfig.modules !== 'commonjs',
absoluteRuntime: (process.versions as any).pnp
? __dirname
: undefined,
...options['transform-runtime'],
},
],
[
isTest && options['styled-jsx'] && options['styled-jsx']['babel-test']
? require('styled-jsx/babel-test')
: require('styled-jsx/babel'),
styledJsxOptions(options['styled-jsx']),
],
require('./plugins/amp-attributes'),
isProduction && [
require('babel-plugin-transform-react-remove-prop-types'),
{
removeImport: true,
},
],
].filter(Boolean),
}
}