I previously tried an approach to scope bindings from var to scope but it didn't catch all cases. This is evident in this bug: https://phabricator.babeljs.io/T2892 Where even after transforming a const to a var we still get an error that it's read-only. This approach will go through and delete every existing let and const binding and creates a new one with the kind "var"
babel-plugin-transform-es2015-block-scoping
Compile ES2015 block scoping (const and let) to ES5
Installation
$ npm install babel-plugin-transform-es2015-block-scoping
Usage
Via .babelrc (Recommended)
.babelrc
{
"plugins": ["transform-es2015-block-scoping"]
}
Via CLI
$ babel --plugins transform-es2015-block-scoping script.js
Via Node API
require("babel-core").transform("code", {
plugins: ["transform-es2015-block-scoping"]
});