-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
Copy pathindex.js
115 lines (108 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
106
107
108
109
110
111
112
113
114
115
const path = require(`path`)
const resolve = m => require.resolve(m)
const IS_TEST = (process.env.BABEL_ENV || process.env.NODE_ENV) === `test`
const 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
}
module.exports = 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()
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: 2,
loose: true,
modules: stage === `test` ? `commonjs` : false,
useBuiltIns: `usage`,
targets,
// Exclude transforms that make all code slower (https://github.com/facebook/create-react-app/pull/5278)
exclude: [`transform-typeof-symbol`],
},
],
[
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: stage === `develop` || stage === `test`,
regenerator: true,
useESModules: stage !== `test`,
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),
}
}