-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
105 lines (92 loc) · 3.09 KB
/
index.js
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
/* eslint-disable global-require */
const NODE_MODULES_REGEX = /node_modules/
const PRECOMPILED_PACKAGES = ['core-js', 'lodash', 'react', 'react-dom', 'whatwg-fetch']
const PRECOMPILED_PACKAGES_REGEX = new RegExp(`node_modules/(${PRECOMPILED_PACKAGES.join('|')})/`)
// by default, enable most advanced optimizations to roughly match the behavior of `loose` mode,
// trading off on absolute spec compliance for output size and efficiency
const assumptions = {
constantSuper: true,
ignoreFunctionLength: true,
ignoreToPrimitiveHint: true,
mutableTemplateObject: true,
noClassCalls: true,
noDocumentAll: true,
noNewArrows: true,
objectRestNoSymbols: true,
privateFieldsAsProperties: true,
pureGetters: true,
setClassMethods: true,
setComputedProperties: true,
setPublicClassFields: true,
setSpreadProperties: true,
skipForOfIteratorClosing: true,
superIsCallableConstructor: true,
}
const SUPPORTED_ENVIRONMENTS = ['development', 'production', 'esm', 'cjs', 'test']
function getUnsupportedEnvMessage(env) {
const supportedEnvsString = SUPPORTED_ENVIRONMENTS.join(', ')
return `Unexpected Babel environment: \`${env}\`. This preset supports: ${supportedEnvsString}.`
}
function getDefaultTargets(env) {
if (env === 'test') return {node: true, browsers: []}
if (env === 'esm' || env === 'cjs') return {node: '18.12', browsers: []}
return undefined
}
module.exports = (babel, options) => {
const env = babel.env()
if (!SUPPORTED_ENVIRONMENTS.includes(env)) {
throw new Error(getUnsupportedEnvMessage(env))
}
const {
emotion = false,
modules = env === 'test' || env === 'cjs' ? 'commonjs' : false,
react = {},
reactRefresh = env === 'development' && react && {},
runtime = true,
targets = getDefaultTargets(env),
typescript = {},
...rest
} = options
const nodeModules = {
include: NODE_MODULES_REGEX,
compact: true,
sourceType: 'unambiguous',
}
const nonPrecompiledPackages = {
exclude: PRECOMPILED_PACKAGES_REGEX,
assumptions,
plugins: [
runtime && [
require('@babel/plugin-transform-runtime').default,
{version: require('@babel/runtime/package.json').version},
],
].filter(Boolean),
presets: [
[
require('@babel/preset-env').default,
{...rest, modules, targets, bugfixes: true, corejs: 3, useBuiltIns: 'entry'},
],
],
}
const nonNodeModules = {
exclude: NODE_MODULES_REGEX,
plugins: [
reactRefresh && [require('react-refresh/babel'), {skipEnvCheck: true, ...reactRefresh}],
emotion && [require('@emotion/babel-plugin').default, {...emotion}],
].filter(Boolean),
presets: [
typescript && [require('@babel/preset-typescript').default, typescript],
react && [
require('@babel/preset-react').default,
{
development: env === 'development',
importSource: emotion ? '@emotion/react' : undefined,
useSpread: true,
...react,
runtime: 'automatic',
},
],
].filter(Boolean),
}
return {overrides: [nodeModules, nonPrecompiledPackages, nonNodeModules]}
}