Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(babel-preset-gatsby): remove spread operator from node builds #29346

Merged
merged 3 commits into from
Feb 5, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@ Object {
"useESModules": true,
},
],
Array [
"<PROJECT_ROOT>/node_modules/@babel/plugin-transform-spread/lib/index.js",
Object {
"loose": false,
},
],
"<PROJECT_ROOT>/node_modules/babel-plugin-dynamic-import-node/lib/index.js",
],
"presets": Array [
Expand Down Expand Up @@ -536,7 +530,7 @@ Object {
}
`;

exports[`babel-preset-gatsby should specify proper presets and plugins when stage is build-stage 1`] = `
exports[`babel-preset-gatsby should specify proper presets and plugins when stage is develop 1`] = `
Object {
"plugins": Array [
Array [
Expand Down Expand Up @@ -790,7 +784,7 @@ Object {
Array [
"<PROJECT_ROOT>/node_modules/@babel/preset-react/lib/index.js",
Object {
"development": false,
"development": true,
"pragma": "React.createElement",
"runtime": "classic",
"useBuiltIns": true,
Expand All @@ -800,7 +794,7 @@ Object {
}
`;

exports[`babel-preset-gatsby should specify proper presets and plugins when stage is develop 1`] = `
exports[`babel-preset-gatsby should specify proper presets and plugins when stage is develop-html 1`] = `
Object {
"plugins": Array [
Array [
Expand Down Expand Up @@ -832,12 +826,6 @@ Object {
"useESModules": true,
},
],
Array [
"<PROJECT_ROOT>/node_modules/@babel/plugin-transform-spread/lib/index.js",
Object {
"loose": false,
},
],
"<PROJECT_ROOT>/node_modules/babel-plugin-dynamic-import-node/lib/index.js",
],
"presets": Array [
Expand Down Expand Up @@ -1047,14 +1035,16 @@ Object {
],
"loose": true,
"modules": false,
"targets": undefined,
"targets": Object {
"node": "current",
},
"useBuiltIns": "usage",
},
],
Array [
"<PROJECT_ROOT>/node_modules/@babel/preset-react/lib/index.js",
Object {
"development": true,
"development": false,
"pragma": "React.createElement",
"runtime": "classic",
"useBuiltIns": true,
Expand Down
12 changes: 11 additions & 1 deletion packages/babel-preset-gatsby/src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,17 @@ import * as pathSerializer from "../utils/path-serializer"
expect.addSnapshotSerializer(pathSerializer)

describe(`babel-preset-gatsby`, () => {
it.each([`build-stage`, `develop`, `build-javascript`, `build-html`])(
let currentEnv
beforeEach(() => {
currentEnv = process.env.BABEL_ENV
process.env.BABEL_ENV = `production`
})

afterEach(() => {
process.env.BABEL_ENV = currentEnv
})

it.each([`develop-html`, `develop`, `build-javascript`, `build-html`])(
`should specify proper presets and plugins when stage is %s`,
stage => {
expect(preset(null, { stage })).toMatchSnapshot()
Expand Down
11 changes: 8 additions & 3 deletions packages/babel-preset-gatsby/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,22 @@ export default function preset(_, 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()
let isBrowser
// unused because of cloud builds
// const absoluteRuntimePath = path.dirname(
// require.resolve(`@babel/runtime/package.json`)
// )

if (!targets) {
if (stage === `build-html` || stage === `test`) {
if (
stage === `build-html` ||
stage === `develop-html` ||
stage === `test`
) {
targets = {
node: `current`,
}
} else {
isBrowser = true
targets = pluginBabelConfig.browserslist
}
}
Expand Down Expand Up @@ -114,7 +119,7 @@ export default function preset(_, options = {}) {
// absoluteRuntime: absoluteRuntimePath,
},
],
[
isBrowser && [
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will class-transform be provided when isBrowser is true? If not, Babel will still throw on super(...args). Before 7.12.11, babel outputs untranspiled super(...args) without any warnings.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will work unless people have a custom browserslist where spread is not needed 🤔. Good call

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider switch to assumptions after Babel 7.13 is released. Ideally what we need is if you enable spread-transform because of your specified targets, do not assume "iterable is array", which what the loose mode of spread-transform did. Here we are unconditionally transpiling ... even if the target already supports it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! Didn't know about assumptions yet. That will fix it. In the next major I'll add loose as a parameter so people can configure preset-env so we don't have to add a separate module for spread.

resolve(`@babel/plugin-transform-spread`),
{
loose: false, // Fixes #14848
Expand Down