Minh Nguyen
Minh Nguyen

Reputation: 23

`@babel/runtime` and `@babel/plugin-transform-runtime` versions

Are @babel/runtime and @babel/plugin-transform-runtime supposed to be on the same version (e.g. both 7.2.0 exactly)? Or can I (as a library author) specify @babel/runtime dependency as ^7.0.0, whilst having the latest @babel/plugin-transform-runtime?

I'm aware that during the beta versions of Babel 7, there was a breaking change in beta.56 (see https://stackoverflow.com/a/51686837/2148762), but I'm guessing this should no longer be the case with the current stable version?

The reason I ask this is I'd ideally want the helpers from @babel/runtime to be shared across different packages, and to me leaving the version range open seems like a good idea. But at the same time, I'm not sure how low I should go (^7.0.0 or ^7.2.0), and whether there's an implicit contract between @babel/runtime and @babel/plugin-transform-runtime with regards to version numbers.

Upvotes: 2

Views: 1024

Answers (1)

loganfsmyth
loganfsmyth

Reputation: 161517

By default, @babel/plugin-transform-runtime is only allowed to output references to @babel/runtime that work on ^7.0.0 because it does not know what version you'd otherwise want to use, and doing anything else would cause lots of issues for users. This means that what you want to do is safe. The downside of this is that, if we add new helpers moving forward, your code would not be able to take advantage of the @babel/runtime version of them (because you might still be using a @babel/runtime version that doesn't have them.

Users can specify the version in the arguments for the transform, if you want to specifically make use of helpers that may have been added to Babel since 7.0.0, e.g.

{
  "plugins": [
    ["@babel/plugin-transform-runtime", { version: "^7.2.0" }],
  ]
}

would then require you to have "@babel/runtime": "^7.2.0" in your package.json.

For instance, since support for the newer decorators proposal didn't land until Babel 7.1.5, if you use transform-runtime and non-legacy decorators, the decorators helper will still be inserted into every file where you use decorators, instead of importing it from @babel/runtime. To get the shared helper, you need to specify version: "^7.1.5" in your options for transform-runtime.

Can I (as a library author) specify @babel/runtime dependency as ^7.0.0, whilst having the latest @babel/plugin-transform-runtime?

Yes, this is safe.

I'm guessing this should no longer be the case with the current stable version?

Correct, that issue was because people failed to take the beta versioning into account.

Upvotes: 3

Related Questions