-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
} | ||
} | ||
|
@@ -114,12 +119,19 @@ export default function preset(_, options = {}) { | |
// absoluteRuntime: absoluteRuntimePath, | ||
}, | ||
], | ||
[ | ||
// TODO allow loose mode as an option in v3 | ||
isBrowser && [ | ||
resolve(`@babel/plugin-transform-spread`), | ||
{ | ||
loose: false, // Fixes #14848 | ||
}, | ||
], | ||
isBrowser && [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of transpiling both class and spread unconditionally, consider check if import { isRequired } from "@babel/helper-compilation-targets";
isRequired("transform-spread", targets)
// returns boolean, whether `transform-spread` is required.
// see https://unpkg.com/browse/@babel/compat-data@7.12.13/data/plugins.json for available feature names You can plug in transform-classes safely because if a target does not support There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TIL There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @babel/helper-compilation-targets didn't accept targets how we handle it and pass it on to preset-env so I have to do some more digging and make it more resilient. I've made a ticket in our issue tracker There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It seems to me a bug of |
||
resolve(`@babel/plugin-transform-classes`), | ||
{ | ||
loose: true, | ||
}, | ||
], | ||
IS_TEST && resolve(`babel-plugin-dynamic-import-node`), | ||
stage === `build-javascript` && [ | ||
// Remove PropTypes from production build | ||
|
There was a problem hiding this comment.
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 whenisBrowser
istrue
? If not, Babel will still throw onsuper(...args)
. Before 7.12.11, babel outputs untranspiledsuper(...args)
without any warnings.There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 enablespread-transform
because of your specified targets, do not assume "iterable is array", which what theloose
mode ofspread-transform
did. Here we are unconditionally transpiling...
even if the target already supports it.There was a problem hiding this comment.
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.