-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
index.js
106 lines (96 loc) · 2.41 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
106
const r = require(`./resolver`)
function preset(context, options = {}) {
const {
browser = false,
debug = false,
nodeVersion = `18.0.0`,
esm = false,
availableCompilerFlags = [`GATSBY_MAJOR`],
keepDynamicImports = null
} = options
const {
NODE_ENV,
BABEL_ENV,
COMPILER_OPTIONS = `GATSBY_MAJOR=5`,
} = process.env
const IS_TEST = (BABEL_ENV || NODE_ENV) === `test`
const browserConfig = {
useBuiltIns: false,
}
if (browser) {
if (esm) {
browserConfig.targets = {
esmodules: true,
}
} else {
browserConfig.targets = {
browsers: [`last 2 versions`, `not ie <= 11`, `not android 4.4.3`],
}
}
}
const nodeConfig = {
corejs: 3,
useBuiltIns: `entry`,
targets: {
node: nodeVersion,
},
}
let parsedCompilerOptions = {}
if (COMPILER_OPTIONS) {
// COMPILER_OPTIONS syntax is key=value,key2=value2
parsedCompilerOptions = COMPILER_OPTIONS.split(`,`).reduce((acc, curr) => {
const [key, value] = curr.split(`=`)
if (key) {
acc[key] = value
}
return acc
}, Object.create(null))
}
return {
presets: [
[
r(`@babel/preset-env`),
Object.assign(
{
loose: true,
debug: !!debug,
shippedProposals: true,
modules: esm ? false : `commonjs`,
bugfixes: esm,
},
browser ? browserConfig : nodeConfig
),
],
[r(`@babel/preset-react`)],
r(`@babel/preset-flow`),
],
plugins: [
r(`@babel/plugin-proposal-nullish-coalescing-operator`),
r(`@babel/plugin-proposal-optional-chaining`),
r(`@babel/plugin-transform-runtime`),
r(`@babel/plugin-syntax-dynamic-import`),
IS_TEST && r(`babel-plugin-dynamic-import-node`),
[
r(`./babel-transform-compiler-flags`),
{
flags: parsedCompilerOptions,
availableFlags: availableCompilerFlags,
},
],
r(`babel-plugin-lodash`),
Array.isArray(keepDynamicImports) && keepDynamicImports.length > 0 && [
r(`./babel-transform-mark-to-keep-dynamic-import`),
{
keepDynamicImports,
},
]
].filter(Boolean),
overrides: [
{
test: [`**/*.ts`, `**/*.tsx`],
plugins: [[`@babel/plugin-transform-typescript`, { isTSX: true }]],
},
],
}
}
module.exports = preset