-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
index.js
129 lines (121 loc) · 3.55 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
const path = require(`path`)
const {
CORE_JS_POLYFILL_EXCLUDE_LIST: polyfillsToExclude,
} = require(`gatsby-legacy-polyfills/dist/exclude`)
const resolve = m => require.resolve(m)
const IS_TEST = (process.env.BABEL_ENV || process.env.NODE_ENV) === `test`
export function loadCachedConfig() {
let pluginBabelConfig = {}
if (!IS_TEST) {
try {
pluginBabelConfig = require(path.join(
process.cwd(),
`./.cache/babelState.json`
))
} catch (err) {
if (err.message.includes(`Cannot find module`)) {
// This probably is being used outside of the Gatsby CLI.
throw Error(
`\`babel-preset-gatsby\` has been loaded, which consumes config generated by the Gatsby CLI. Set \`NODE_ENV=test\` to bypass, or run \`gatsby build\` first.`
)
} else {
throw err
}
}
}
return pluginBabelConfig
}
export default function preset(_, options = {}) {
let { targets = null } = options
// TODO(v3): Remove process.env.GATSBY_BUILD_STAGE, needs to be passed as an option
const stage = options.stage || process.env.GATSBY_BUILD_STAGE || `test`
const pluginBabelConfig = loadCachedConfig()
// unused because of cloud builds
// const absoluteRuntimePath = path.dirname(
// require.resolve(`@babel/runtime/package.json`)
// )
if (!targets) {
if (stage === `build-html` || stage === `test`) {
targets = {
node: `current`,
}
} else {
targets = pluginBabelConfig.browserslist
}
}
return {
presets: [
[
resolve(`@babel/preset-env`),
{
corejs: 3,
loose: true,
modules: stage === `test` ? `commonjs` : false,
useBuiltIns: `usage`,
targets,
// debug: true,
exclude: [
// Exclude transforms that make all code slower (https://github.com/facebook/create-react-app/pull/5278)
`transform-typeof-symbol`,
// we already have transforms for these
`transform-spread`,
`proposal-nullish-coalescing-operator`,
`proposal-optional-chaining`,
...polyfillsToExclude,
],
},
],
[
resolve(`@babel/preset-react`),
{
useBuiltIns: true,
pragma: `React.createElement`,
development: stage === `develop`,
},
],
],
plugins: [
[
resolve(`./optimize-hook-destructuring`),
{
lib: true,
},
],
[
resolve(`@babel/plugin-proposal-class-properties`),
{
loose: true,
},
],
[resolve(`@babel/plugin-proposal-nullish-coalescing-operator`)],
[resolve(`@babel/plugin-proposal-optional-chaining`)],
resolve(`babel-plugin-macros`),
resolve(`@babel/plugin-syntax-dynamic-import`),
[
resolve(`@babel/plugin-transform-runtime`),
{
corejs: false,
helpers: true,
regenerator: true,
useESModules: stage !== `test`,
// this breaks cloud builds so we disable it for now
// absoluteRuntime: absoluteRuntimePath,
},
],
[
resolve(`@babel/plugin-transform-spread`),
{
loose: false, // Fixes #14848
},
],
IS_TEST && resolve(`babel-plugin-dynamic-import-node`),
stage === `build-javascript` && [
// Remove PropTypes from production build
resolve(`babel-plugin-transform-react-remove-prop-types`),
{
removeImport: true,
},
],
].filter(Boolean),
}
}