What's the difference between babel-preset-stage-0, babel-preset-stage-1 etc?

后端 未结 5 1533
北海茫月
北海茫月 2020-12-07 13:47

My question is : What\'s the difference between babel-preset-stage-0,babel-preset-stage-1,babel-preset-stage-2 and babel-preset-

5条回答
  •  南方客
    南方客 (楼主)
    2020-12-07 14:14

    This is the best starting point to understand. What are babel presets

    An excerpt from the link:

    Stage 0 - Strawman: just an idea, possible Babel plugin.
    Stage 1 - Proposal: this is worth working on.
    Stage 2 - Draft: initial spec.
    Stage 3 - Candidate: complete spec and initial browser implementations.
    Stage 4 - Finished: will be added to the next yearly release

    Overall Picture:

    1. With time Javascript is evolving and more and more features are being added to the language.
    2. The browsers also have to do a lot of work so that they can implement these new features to be understood by them. This process in general is a lot slower than the pace Javascript is evolving.
    3. But developers want to use the new features of language since it make it easier for them to write, understand, maintain the code.
    4. So developers write their code using the new Javascript features, but before that code reaches browsers, it goes through a build process where using some magic, all the code with new features is transpiled into code understandable by browser. i.e. new features of Javascript but coded using the browser understandable constructs of language.
    5. The build magic can be performed using tools, one of them being Babel.
    6. The way Babel works is that it takes a set of plugins. Each of these plugin could refer to transforming a particular new feature of Javscript into browser understandable constructs of language.
    7. There are hundreds of such plugins, each one referring to different new features of Javascript. These features may or may not be part of the final Javascript spec. And if it never ends up going to final Javascript spec, none of the browsers will implement this feature. So if any developer uses any experimental feature of JS using babel plugin, its the risk that he/she is taking. If it never ends up in spec, that part of code will always have to be transpiled before its deployed to browsers.
    8. This risk is categorised into various levels to signify the chances of any feature reaching the final spec.
    9. Also babel has grouped these plugins into various sets, which is called a preset in babel's terms. And each preset contains plugins from various levels of risk.
    10. preset-0 It means it has plugins for features which are very experimental and hence at high risk of making it out to final spec. Its like an idea that came to a developer that Javascript should have a particular feature, and he did some work to get it to TC-39 proposal process.
    11. preset-1 It contains the plugins for the feature ideas accepted by the TC-39, and they find it worth working on.
    12. preset-2 Plugins for features where an initial draft is ready for the feature. And it goes on..

    So it could happen that a feature in Stage 0 reached Stage 2 in some time and end up being in next release of Javascript some more time later.

    Hence with each version of these Babel Presets, you could find different set of plugins in it. It could also happen that a feature in stage 0 went through some changes and it made breaking changes into how it functions. And it reached, lets say stage-2 with a totally different API. So developers have to make sure that when they are updating these plugins they make necessary changes to their code.

提交回复
热议问题