/babel-multi-env

`babel` CLI replacement with multiple node version support by babel-preset-env

Primary LanguageJavaScript

babel-multi-env

babel CLI replacement with multiple node version support by babel-preset-env and babel-plugin-transform-runtime. Useful for node.js libraries/modules

Version Travis CI Quality Coverage Dependencies Gitter

Usage

Transforms:

import fs from "fs";
import promisify from "util.promisify";

export async function demoBumpVersion(nextVersion = "1.0.0") {
  const content = await promisify(fs.readFile)("package.json", "utf8");
  const json = JSON.parse(content);
  json.version = nextVersion;
  await promisify(fs.writeFile)(
    "package.json",
    JSON.stringify(json, null, 4),
    "utf8"
  );
  return () => console.log(`Successfully bumped version to: ${nextVersion}`);
}

with the command:

cd example
yarn babel-multi-env --multi-versions 8.0.0 6.0.0 4.0.0 0.12.0 0.10.0 --given src/index.js --out-dir lib

and output these files:

example/lib/index.js
example/lib/index__0.10.0__.js
example/lib/index__0.12.0__.js
example/lib/index__4.0.0__.js
example/lib/index__6.0.0__.js
example/lib/index__8.0.0__.js

where

var gte = require("semver").gte;

var version = process.version;

if (gte(version, "8.0.0")) {
  module.exports = require("./index__8.0.0__.js");
} else if (gte(version, "6.0.0")) {
  module.exports = require("./index__6.0.0__.js");
} else if (gte(version, "4.0.0")) {
  module.exports = require("./index__4.0.0__.js");
} else if (gte(version, "0.12.0")) {
  module.exports = require("./index__0.12.0__.js");
} else {
  module.exports = require("./index__0.10.0__.js");
}
"use strict";
var _interopRequireDefault = require("babel-runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.demoBumpVersion = demoBumpVersion;
var _stringify = _interopRequireDefault(
  require("babel-runtime/core-js/json/stringify")
);
var _fs = _interopRequireDefault(require("fs"));
var _util = _interopRequireDefault(require("util.promisify"));
async function demoBumpVersion(nextVersion = "1.0.0") {
  const content = await (0, _util.default)(_fs.default.readFile)(
    "package.json",
    "utf8"
  );
  const json = JSON.parse(content);
  json.version = nextVersion;
  await (0, _util.default)(_fs.default.writeFile)(
    "package.json",
    (0, _stringify.default)(json, null, 4),
    "utf8"
  );
  return () => console.log(`Successfully bumped version to: ${nextVersion}`);
}
"use strict";
var _interopRequireDefault = require("babel-runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.demoBumpVersion = void 0;
var _stringify = _interopRequireDefault(
  require("babel-runtime/core-js/json/stringify")
);
var _asyncToGenerator2 = _interopRequireDefault(
  require("babel-runtime/helpers/asyncToGenerator")
);
var _fs = _interopRequireDefault(require("fs"));
var _util = _interopRequireDefault(require("util.promisify"));
let demoBumpVersion = (() => {
  var _ref = (0, _asyncToGenerator2.default)(function*(nextVersion = "1.0.0") {
    const content = yield (0, _util.default)(_fs.default.readFile)(
      "package.json",
      "utf8"
    );
    const json = JSON.parse(content);
    json.version = nextVersion;
    yield (0,
    _util.default)(_fs.default.writeFile)("package.json", (0, _stringify.default)(json, null, 4), "utf8");
    return () => console.log(`Successfully bumped version to: ${nextVersion}`);
  });
  return function demoBumpVersion() {
    return _ref.apply(this, arguments);
  };
})();
exports.demoBumpVersion = demoBumpVersion;

……and you get the idea.

More sample code for node@4, node@0.12 and node@0.10
/* lib/index__4.0.0__.js */
"use strict";
var _interopRequireDefault = require("babel-runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.demoBumpVersion = void 0;
var _regenerator = _interopRequireDefault(require("babel-runtime/regenerator"));
var _stringify = _interopRequireDefault(require("babel-runtime/core-js/json/stringify"));
var _asyncToGenerator2 = _interopRequireDefault(require("babel-runtime/helpers/asyncToGenerator"));
var _fs = _interopRequireDefault(require("fs"));
var _util = _interopRequireDefault(require("util.promisify"));

var demoBumpVersion = function () {
  var _ref = (0, _asyncToGenerator2.default)(
  /*#__PURE__*/
  _regenerator.default.mark(function _callee() {
    var nextVersion,
        content,
        json,
        _args = arguments;
    return _regenerator.default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            nextVersion = _args.length > 0 && _args[0] !== undefined ? _args[0] : "1.0.0";
            _context.next = 3;
            return (0, _util.default)(_fs.default.readFile)("package.json", "utf8");

          case 3:
            content = _context.sent;
            json = JSON.parse(content);
            json.version = nextVersion;
            _context.next = 8;
            return (0, _util.default)(_fs.default.writeFile)("package.json", (0, _stringify.default)(json, null, 4), "utf8");

          case 8:
            return _context.abrupt("return", function () {
              return console.log(`Successfully bumped version to: ${nextVersion}`);
            });

          case 9:
          case "end":
            return _context.stop();
        }
      }
    }, _callee, this);
  }));

  return function demoBumpVersion() {
    return _ref.apply(this, arguments);
  };
}();
exports.demoBumpVersion = demoBumpVersion;
/* lib/index__0.12.0__.js */
"use strict";
var _interopRequireDefault = require("babel-runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.demoBumpVersion = void 0;
var _regenerator = _interopRequireDefault(require("babel-runtime/regenerator"));
var _stringify = _interopRequireDefault(require("babel-runtime/core-js/json/stringify"));
var _asyncToGenerator2 = _interopRequireDefault(require("babel-runtime/helpers/asyncToGenerator"));
var _fs = _interopRequireDefault(require("fs"));
var _util = _interopRequireDefault(require("util.promisify"));
var demoBumpVersion = function () {
  var _ref = (0, _asyncToGenerator2.default)(
  /*#__PURE__*/
  _regenerator.default.mark(function _callee() {
    var nextVersion,
        content,
        json,
        _args = arguments;
    return _regenerator.default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            nextVersion = _args.length > 0 && _args[0] !== undefined ? _args[0] : "1.0.0";
            _context.next = 3;
            return (0, _util.default)(_fs.default.readFile)("package.json", "utf8");

          case 3:
            content = _context.sent;
            json = JSON.parse(content);
            json.version = nextVersion;
            _context.next = 8;
            return (0, _util.default)(_fs.default.writeFile)("package.json", (0, _stringify.default)(json, null, 4), "utf8");

          case 8:
            return _context.abrupt("return", function () {
              return console.log("Successfully bumped version to: ".concat(nextVersion));
            });

          case 9:
          case "end":
            return _context.stop();
        }
      }
    }, _callee, this);
  }));

  return function demoBumpVersion() {
    return _ref.apply(this, arguments);
  };
}();
exports.demoBumpVersion = demoBumpVersion;
/* lib/index__0.10.0__.js */
"use strict";
var _interopRequireDefault = require("babel-runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.demoBumpVersion = void 0;
var _regenerator = _interopRequireDefault(require("babel-runtime/regenerator"));
var _stringify = _interopRequireDefault(require("babel-runtime/core-js/json/stringify"));
var _asyncToGenerator2 = _interopRequireDefault(require("babel-runtime/helpers/asyncToGenerator"));
var _fs = _interopRequireDefault(require("fs"));
var _util = _interopRequireDefault(require("util.promisify"));
var demoBumpVersion = function () {
  var _ref = (0, _asyncToGenerator2.default)(
  /*#__PURE__*/
  _regenerator.default.mark(function _callee() {
    var nextVersion,
        content,
        json,
        _args = arguments;
    return _regenerator.default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            nextVersion = _args.length > 0 && _args[0] !== undefined ? _args[0] : "1.0.0";
            _context.next = 3;
            return (0, _util.default)(_fs.default.readFile)("package.json", "utf8");

          case 3:
            content = _context.sent;
            json = JSON.parse(content);
            json.version = nextVersion;
            _context.next = 8;
            return (0, _util.default)(_fs.default.writeFile)("package.json", (0, _stringify.default)(json, null, 4), "utf8");

          case 8:
            return _context.abrupt("return", function () {
              return console.log("Successfully bumped version to: ".concat(nextVersion));
            });

          case 9:
          case "end":
            return _context.stop();
        }
      }
    }, _callee, this);
  }));

  return function demoBumpVersion() {
    return _ref.apply(this, arguments);
  };
}();
exports.demoBumpVersion = demoBumpVersion;

Installation

npm i --save-dev babel-multi-env
# or
yarn add --dev babel-multi-env

Note on the peerDependencies

There are currently five peerDependencies listed under the package.json for babel-multi-env. Be sure not to screw up their versions sicne babel@^7 are currently in beta and can be found under the next dist-tags on npm.

dependencies

Your should add these to your dependencies:

  • "babel-runtime": "^7.0.0-beta.3"
  • "semver": "^5.4.1"

since they'll be used in the runtime.

devDependencies

And, these three for the code generation only:

  • "babel-core": "^7.0.0-beta.3",
  • "babel-plugin-transform-runtime": "^7.0.0-beta.3",
  • "babel-preset-env": "^7.0.0-beta.3",

Options

screen shot 2017-11-01 at 10 55 57 am

[babel-multi-env]
  --multi-versions  list of supported semver versions. Example: 8.0.0 6.0.0
                    4.0.0                                     [array] [required]
  --given           source glob patterns                      [array] [required]
  --out-dir         compile into an output directory         [string] [required]

[babel-preset-env]
  --use-built-ins  apply babel-preset-env for polyfills with "useBuiltIns":
                   "usage" (via babel-polyfill)      [string] [choices: "usage"]

[babel-plugin-transform-runtime]
  --helpers      Enables inlined Babel helpers (classCallCheck, extends, etc.)
                 are replaced with calls to moduleName [boolean] [default: true]
  --polyfill     Enables new built-ins (Promise, Set, Map, etc.) are transformed
                 to use a non-global polluting polyfill[boolean] [default: true]
  --regenerator  Enables generator functions are transformed to use a
                 regenerator runtime that does not pollute the global scope
                                                       [boolean] [default: true]
  --module-name  sets the name/path of the module used when importing helpers
                                               [string] [default: babel-runtime]

[babel-core]
  --presets  list of preset names                                        [array]
  --plugins  list of plugins names                                       [array]

Options:
  --help, -h     Show help                                             [boolean]
  --version, -v  Show version number                                   [boolean]

Examples