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: search for browserslist if esmodules is falsy #11124

Merged
merged 4 commits into from Feb 24, 2020

Conversation

fengzilong
Copy link
Contributor

@fengzilong fengzilong commented Feb 11, 2020

Q                       A
Fixed Issues? Fixes #11123
Patch: Bug Fix? Yes
Major: Breaking Change? Maybe for some user
Minor: New Feature? No
Tests Added + Pass? No
Documentation PR Link No
Any Dependency Changes? No
License MIT

As stated in issue #11123, with targets

{
  presets: [
    [
      '@babel/preset-env',
      {
        targets: {
          esmodules: false
        }
      }
    ]
  ]
}

babel won't search for browserslist config

@@ -189,7 +189,8 @@ export default function getTargets(
// Parse browsers target via browserslist
const browsersquery = validateBrowsers(targets.browsers);

const hasTargets = Object.keys(targets).length > 0;
const hasTargets = targets.esmodules ||
Copy link
Contributor

@JLHwung JLHwung Feb 11, 2020

Choose a reason for hiding this comment

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

I think we should normalize target { esmodules: false } to {} in

const normalizeTargets = targets => {

so if target.esmodules is accessed later, if must be true and we don't have to worry that it can be false or any other fancy falsy values.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmmm...what I worry about is that some package use babel-helper-compilation-targets directly, e.g. @vue/babel-preset-app. should we fix the bug for those packages

https://github.com/vuejs/vue-cli/blob/c8cecffedbf7b19cf930bb2821b5c352bc716a67/packages/%40vue/babel-preset-app/index.js#L17

Copy link
Contributor

Choose a reason for hiding this comment

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

I see. Since target.esmodules is used as a shortcut to set target.browsers to be those with esmoduels support. We can delete it after it is consumed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems reasonable ❤️. Updated

@JLHwung JLHwung added pkg: preset-env PR: Bug Fix 🐛 A type of pull request used for our changelog categories labels Feb 11, 2020
Copy link
Contributor

@JLHwung JLHwung left a comment

Choose a reason for hiding this comment

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

Thanks!

@@ -184,6 +184,9 @@ export default function getTargets(
targets.browsers = Object.keys(supportsESModules)
.map(browser => `${browser} ${supportsESModules[browser]}`)
.join(", ");
} else {
// remove falsy esmodules to fix `hasTargets` below
delete targets.esmodules;
Copy link
Member

Choose a reason for hiding this comment

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

Should we also delete it if it is truthy, since at this point we have already handled 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.

User may invoke this method twice or more, keep the truthy value is safer for this case

const getTargets = require( '@babel/helper-compilation-targets' ).default
const targets = { esmodules: true }
const result1 = getTargets( targets )
const result2 = getTargets( targets )
// result1 should be equal to result2 here

Copy link
Member

Choose a reason for hiding this comment

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

Wouldn't they still be the same?

After the first call, targets has a browsers property which makes its behaviour identical to esmodules: true.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops, sorry I didn't realize targets.browsers have already been assigned to targets. Updated

@nicolo-ribaudo nicolo-ribaudo merged commit 2d1bac9 into babel:master Feb 24, 2020
@nicolo-ribaudo
Copy link
Member

Thanks!

@fengzilong fengzilong deleted the patch-1 branch February 25, 2020 01:05
@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label May 26, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 26, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: preset-env PR: Bug Fix 🐛 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

babel skip searching for browserslist config in some unexpected condition
3 participants