Skip to content

Commit 794b387

Browse files
GatsbyJS Botwardpeet
GatsbyJS Bot
andauthored
fix(babel-preset-gatsby): remove spread operator from node builds (#29346) (#29357)
* fix(babel-preset-gatsby): remove spread operator from node builds * fix transform-classes Co-authored-by: Michal Piechowiak <[email protected]> (cherry picked from commit d163691) Co-authored-by: Ward Peeters <[email protected]>
1 parent 1bbde7f commit 794b387

File tree

4 files changed

+46
-21
lines changed

4 files changed

+46
-21
lines changed

packages/babel-preset-gatsby/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.12.1",
1414
"@babel/plugin-proposal-optional-chaining": "^7.12.1",
1515
"@babel/plugin-syntax-dynamic-import": "^7.8.3",
16+
"@babel/plugin-transform-classes": "^7.12.1",
1617
"@babel/plugin-transform-runtime": "^7.12.1",
1718
"@babel/plugin-transform-spread": "^7.12.1",
1819
"@babel/preset-env": "^7.12.1",

packages/babel-preset-gatsby/src/__tests__/__snapshots__/index.js.snap

+19-17
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,6 @@ Object {
3232
"useESModules": true,
3333
},
3434
],
35-
Array [
36-
"<PROJECT_ROOT>/node_modules/@babel/plugin-transform-spread/lib/index.js",
37-
Object {
38-
"loose": false,
39-
},
40-
],
4135
"<PROJECT_ROOT>/node_modules/babel-plugin-dynamic-import-node/lib/index.js",
4236
],
4337
"presets": Array [
@@ -304,6 +298,12 @@ Object {
304298
"loose": false,
305299
},
306300
],
301+
Array [
302+
"<PROJECT_ROOT>/node_modules/@babel/plugin-transform-classes/lib/index.js",
303+
Object {
304+
"loose": true,
305+
},
306+
],
307307
"<PROJECT_ROOT>/node_modules/babel-plugin-dynamic-import-node/lib/index.js",
308308
Array [
309309
"<PROJECT_ROOT>/node_modules/babel-plugin-transform-react-remove-prop-types/lib/index.js",
@@ -536,7 +536,7 @@ Object {
536536
}
537537
`;
538538

539-
exports[`babel-preset-gatsby should specify proper presets and plugins when stage is build-stage 1`] = `
539+
exports[`babel-preset-gatsby should specify proper presets and plugins when stage is develop 1`] = `
540540
Object {
541541
"plugins": Array [
542542
Array [
@@ -574,6 +574,12 @@ Object {
574574
"loose": false,
575575
},
576576
],
577+
Array [
578+
"<PROJECT_ROOT>/node_modules/@babel/plugin-transform-classes/lib/index.js",
579+
Object {
580+
"loose": true,
581+
},
582+
],
577583
"<PROJECT_ROOT>/node_modules/babel-plugin-dynamic-import-node/lib/index.js",
578584
],
579585
"presets": Array [
@@ -790,7 +796,7 @@ Object {
790796
Array [
791797
"<PROJECT_ROOT>/node_modules/@babel/preset-react/lib/index.js",
792798
Object {
793-
"development": false,
799+
"development": true,
794800
"pragma": "React.createElement",
795801
"runtime": "classic",
796802
"useBuiltIns": true,
@@ -800,7 +806,7 @@ Object {
800806
}
801807
`;
802808

803-
exports[`babel-preset-gatsby should specify proper presets and plugins when stage is develop 1`] = `
809+
exports[`babel-preset-gatsby should specify proper presets and plugins when stage is develop-html 1`] = `
804810
Object {
805811
"plugins": Array [
806812
Array [
@@ -832,12 +838,6 @@ Object {
832838
"useESModules": true,
833839
},
834840
],
835-
Array [
836-
"<PROJECT_ROOT>/node_modules/@babel/plugin-transform-spread/lib/index.js",
837-
Object {
838-
"loose": false,
839-
},
840-
],
841841
"<PROJECT_ROOT>/node_modules/babel-plugin-dynamic-import-node/lib/index.js",
842842
],
843843
"presets": Array [
@@ -1047,14 +1047,16 @@ Object {
10471047
],
10481048
"loose": true,
10491049
"modules": false,
1050-
"targets": undefined,
1050+
"targets": Object {
1051+
"node": "current",
1052+
},
10511053
"useBuiltIns": "usage",
10521054
},
10531055
],
10541056
Array [
10551057
"<PROJECT_ROOT>/node_modules/@babel/preset-react/lib/index.js",
10561058
Object {
1057-
"development": true,
1059+
"development": false,
10581060
"pragma": "React.createElement",
10591061
"runtime": "classic",
10601062
"useBuiltIns": true,

packages/babel-preset-gatsby/src/__tests__/index.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,17 @@ import * as pathSerializer from "../utils/path-serializer"
55
expect.addSnapshotSerializer(pathSerializer)
66

77
describe(`babel-preset-gatsby`, () => {
8-
it.each([`build-stage`, `develop`, `build-javascript`, `build-html`])(
8+
let currentEnv
9+
beforeEach(() => {
10+
currentEnv = process.env.BABEL_ENV
11+
process.env.BABEL_ENV = `production`
12+
})
13+
14+
afterEach(() => {
15+
process.env.BABEL_ENV = currentEnv
16+
})
17+
18+
it.each([`develop-html`, `develop`, `build-javascript`, `build-html`])(
919
`should specify proper presets and plugins when stage is %s`,
1020
stage => {
1121
expect(preset(null, { stage })).toMatchSnapshot()

packages/babel-preset-gatsby/src/index.js

+15-3
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,22 @@ export default function preset(_, options = {}) {
3535
// TODO(v3): Remove process.env.GATSBY_BUILD_STAGE, needs to be passed as an option
3636
const stage = options.stage || process.env.GATSBY_BUILD_STAGE || `test`
3737
const pluginBabelConfig = loadCachedConfig()
38+
let isBrowser
3839
// unused because of cloud builds
3940
// const absoluteRuntimePath = path.dirname(
4041
// require.resolve(`@babel/runtime/package.json`)
4142
// )
42-
4343
if (!targets) {
44-
if (stage === `build-html` || stage === `test`) {
44+
if (
45+
stage === `build-html` ||
46+
stage === `develop-html` ||
47+
stage === `test`
48+
) {
4549
targets = {
4650
node: `current`,
4751
}
4852
} else {
53+
isBrowser = true
4954
targets = pluginBabelConfig.browserslist
5055
}
5156
}
@@ -114,12 +119,19 @@ export default function preset(_, options = {}) {
114119
// absoluteRuntime: absoluteRuntimePath,
115120
},
116121
],
117-
[
122+
// TODO allow loose mode as an option in v3
123+
isBrowser && [
118124
resolve(`@babel/plugin-transform-spread`),
119125
{
120126
loose: false, // Fixes #14848
121127
},
122128
],
129+
isBrowser && [
130+
resolve(`@babel/plugin-transform-classes`),
131+
{
132+
loose: true,
133+
},
134+
],
123135
IS_TEST && resolve(`babel-plugin-dynamic-import-node`),
124136
stage === `build-javascript` && [
125137
// Remove PropTypes from production build

0 commit comments

Comments
 (0)