Giter Club home page Giter Club logo

Comments (5)

nicolo-ribaudo avatar nicolo-ribaudo commented on May 13, 2024

What is the reason that it recommends "method": "usage-pure" and not "method": "usage-global"?

Because usage-pure is the equivalent of what transform-runtime does: it doesn't modify the global objects to install the shims/polyfills. usage-global is more similar to @babel/preset-env's useBuiltIns option.

As a user who wants to do the following, using "method": "usage-global" is more correct for me, right?

  • I want to transform my code to my target environment
  • I want to add polyfills automatically in my code (not caring about polluting the global namespace)
  • I want the babel runtime helpers to be re-used via imports (without repetition of babel helpers in each file)

Yes! I would configure Babel like this:

{
  "targets": { ... }, // you can define your targets globally, so that ever plugin can use them to optimize the output
  "presets": ["@babel/preset-env"], // compile JS
  "plugins": [
    ["polyfill-corejs3", { "method": "usage-global" }], // or "polyfill-corejs2", or "polyfill-es-shims"
    "@babel/transform-runtime" // to deduplicate Babel-specific helpers
  ] 
}

from babel-polyfills.

thernstig avatar thernstig commented on May 13, 2024

@nicolo-ribaudo you are amazing! That is exactly the config I had started to code to use. Great to get it confirmed.

There has been a lot of confusion along the way. But I can definitely confirm that the new way you handle this with the motivations https://github.com/babel/babel-polyfills#history-and-motivation feels much more intuitive for me who has been reading up extensively on this the past weeks as a side-project.

If preset-env could in the future only handle transforms, and have polyfills separated as explained in this repo, I do think it will make more sense for users. Removing the polyfill options from present-env and transform-runtime would make this easier for users to understand on how to use this. So you get a thumbs on from me on this path you have taken 👍

from babel-polyfills.

nicolo-ribaudo avatar nicolo-ribaudo commented on May 13, 2024

Thanks for the feedback! 😄

from babel-polyfills.

nyngwang avatar nyngwang commented on May 13, 2024

Yes! I would configure Babel like this:

{
  "targets": { ... }, // you can define your targets globally, so that ever plugin can use them to optimize the output
  "presets": ["@babel/preset-env"], // compile JS
  "plugins": [
    ["polyfill-corejs3", { "method": "usage-global" }], // or "polyfill-corejs2", or "polyfill-es-shims"
    "@babel/transform-runtime" // to deduplicate Babel-specific helpers
  ] 
}

To @nicolo-ribaudo:

Is the ordering of plugins correct? Shouldn't we apply @babel/transform-runtime first and then do polyfill-corejs3? Reference to the migration guide: https://github.com/babel/babel-polyfills/blob/main/docs/migration.md#core-js3:

  "plugins": [
+    "@babel/transform-runtime",
+    ["polyfill-corejs3", {
+      "method": "usage-pure"
+    }]
  ]
}

I'm here from discussion: babel/babel#12605 (reply in thread)

from babel-polyfills.

nicolo-ribaudo avatar nicolo-ribaudo commented on May 13, 2024

The plugin order does not matter in this case.

from babel-polyfills.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.