babel vs babel-core vs babel-runtime

前端 未结 3 1224
孤独总比滥情好
孤独总比滥情好 2020-12-24 04:28

My node webpack project uses three babel libraries. What\'s the difference between these and how are they being used?

\"dependencies\": {
  \"babel-runtime\"         


        
3条回答
  •  孤独总比滥情好
    2020-12-24 05:16

    TL;DR The things to compare here are:

    1. babel (use for 5.x.x) vs babel-cli+babel-core (pick one for 6.x.x)
    2. babel-polyfill (use for non-libraries) vs babel-runtime+babel-plugin-transform-runtime (use for libraries)

    From https://babeljs.io/blog/2015/10/31/setting-up-babel-6:

    The babel package is no more. Previously, it was the entire compiler and all the transforms plus a bunch of CLI tools, but this lead to unnecessarily large downloads and was a bit confusing. Now we’ve split it up into two separate packages: babel-cli and babel-core.

    npm install --global babel-cli

    or

    npm install --save-dev babel-core

    If you want to use Babel from the CLI you can install babel-cli or if you want to use the Node API you can install babel-core.

    babel-runtime just allows polyfills that don't pollute the global space, unlike babel-polyfill which pollutes your global space. From http://babeljs.io/docs/plugins/transform-runtime/:

    [babel-runtime] automatically polyfills your code without polluting globals. (This plugin is recommended in a library/tool)

    If you use babel-runtime, you should also

    npm install --save-dev babel-plugin-transform-runtime

    In most cases, you should install babel-plugin-transform-runtime as a development dependency (with --save-dev) and babel-runtime as a production dependency (with --save).

    The transformation plugin is typically used only in development, but the runtime itself will be depended on by your deployed/published code.

    Also, babel-runtime+babel-plugin-transform-runtime and babel-polyfill are generally mutually exclusive--meaning you should only use one or the other. From a comment here http://jamesknelson.com/the-six-things-you-need-to-know-about-babel-6/:

    You should be using either babel-polyfill or babel-runtime. They are mutually exclusive—unless of course you know what you are doing. But they are essentially the same thing. These are just helpers. babel-polyfill achieves the same goal by mutating globals whereas babel-runtime does so modularly. Unless you are developing a library, I’d recommend you use the polyfill.

提交回复
热议问题