@tomera/babel-preset-node v2.0.1
Babel Preset Node
A configurable babel preset with straightforward options for developing and publishing ES2015+ Node modules.
Install
Install with yarn:
$ yarn add -D @tomera/babel-preset-nodeOr with npm:
$ npm i -D @tomera/babel-preset-nodeIf the "fill" option of the preset is set to anything but false (it defaults to "pony"),
then add @babel/runtime-corejs3 to your dependencies using yarn add @babel/runtime-corejs3 or npm i @babel/runtime-corejs3.
NOTE: This package provides a minimally transpiled variant through the "module" field of package.json.
Usage
Add @tomera/node to the "presets" array of your .babelrc file or other Babel configuration file:
.babelrc
{
"presets": ["@tomera/node"]
}Options
Type:
"amd" | "umd" | "systemjs" | "commonjs" | "cjs" | "auto" | falseDefault:
falseConfigure transformation of ES6 module syntax to another module type.
Set to
falseto disable module transformation (useful if using a bundler that handles module transformation).NOTE:
"cjs"is just an alias for"commonjs"."fill"Type:
"poly"|"pony"|falseDefault:
"pony"Configure implementation of features unsupported by the target Node version.
Set to
"poly"to enable polyfilling using the"useBuiltIns": "entry"option of@babel/preset-env.Set to
"pony"to enable ponyfilling using the transform-runtime Babel plugin.Set to
falseto disable implementation of features unsupported by the target Node version (useful if you expect the consumer of your library to implement the unsupported features)."support"Type:
"modern" | "legacy"Default:
"modern"Specify if the code should be transpiled to a modern Node version (your currently installed version) or to a legacy Node version (version 8).
Type:
booleanDefault:
falseSpecify if debug information should be output using
console.log.
Examples
Most of the following examples require you to use the Babel CLI or a bundler, such as Rollup or Webpack, to read your Babel configuration file and transpile your JavaScript code.
Some libraries or frameworks, such as Jest, automatically read your configuration file and transpile your
code as long as the proper dependencies are installed (babel-jest in Jest's case).
A configuration for writing Jest tests in ES2015+ JavaScript:
.babelrc
{ "env": { "test": { "presets": [ [ "@tomera/node", { "module": "commonjs", "support": "modern" } ] ] } } }A configuration for writing a library or CLI application in ES2015+ JavaScript:
.babelrc
{ "presets": [ [ "@tomera/node", { "module": "commonjs", "fill": "pony", "support": "legacy" } ] ] }If all of your dependencies support Node versions as far back as 8 then you can add the following to your
package.jsonto indicate your library or CLI application supports Node versions as far back as 8:package.json
{ "engines": { "node": ">=8.0.0" } }If you want to support Node versions as far back as 8, but one of your dependencies does not, then either switch out the nonsupporting dependency in favor of another or polyfill your dependencies instead of ponyfilling them (polyfills mutate the global namespace):
{ "presets": [ [ "@tomera/node", { "module": "commonjs", "fill": "poly", "support": "legacy" } ] ] }If your code resides in a
srcdirectory and you have installed@babel/clias a dev dependency (usingyarn add -D @babel/cliornpm i -D @babel/cli), then you can transpile your code to alibdirectory by running the following command from the root directory of your project:$ babel -D src -d libEnsure the
"main"field ofpackage.jsonpoints to the entry point of your package in thelibdirectory and that the files in thelibdirectory are included in your package (perhaps using the"files"field ofpackage.json).Lastly, you may choose to create a script for transpiling your code using the
"scripts"field ofpackage.jsonand set it to run prior to publishing:{ "scripts": { "build": "babel -D src -d lib", "prepublishOnly": "npm run build" } }You can now transpile your code by running
yarn buildornpm run build.A configuration file for writing a library that sets the
"main"and"module"fields ofpackage.json:.babelrc
{ "env": { "main": { "presets": [ [ "@tomera/node", { "module": "commonjs", "support": "legacy" } ] ] }, "module": { "presets": [ [ "@tomera/node", { "module": false, "fill": false, "support": "modern" } ] ] } } }The same caveats regarding supporting legacy Node versions in the previous example apply to the
"main"environment of the current example.To transpile your code similarly to the previous example, add
cross-envas a dev dependency (usingyarn add -D cross-envornpm i -D cross-env) and run the following commands to transpile your code for the"main"and"module"targets, respectively:$ cross-env BABEL_ENV=main babel -D src -d lib/main $ cross-env BABEL_ENV=module babel -D src -d lib/moduleEnsure the
"main"and"module"fields ofpackage.jsonpoint to the entry points of your package in thelib/mainandlib/moduledirectories, respectively, and that the files in thelibdirectory are included in your package (perhaps using the"files"field ofpackage.json).Lastly, you may choose to create a script for transpiling your code using the
"scripts"field ofpackage.jsonand set it to run prior to publishing:{ "scripts": { "build:main": "cross-env BABEL_ENV=main babel -D src -d lib/main", "build:module": "cross-env BABEL_ENV=module babel -D src -d lib/module", "build": "run-p build:*", "prepublishOnly": "npm run build" } }You'll need to add
npm-run-allas a dev dependency (usingyarn add -D npm-run-allandnpm i -D npm-run-all) for therun-pcommand and"build"script to work.You can now transpile your code for the
"main"and"module"targets concurrently by runningyarn buildornpm run build.
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Development
Setup
- Clone the repository:
- SSH:
$ git clone git@github.com:TomerAberbach/babel-preset-node.git - HTTPS:
$ git clone https://github.com/TomerAberbach/babel-preset-node.git
- SSH:
- Change your current working directory to the newly created
babel-preset-nodedirectory:$ cd babel-preset-node - Install dependencies:
- Yarn:
$ yarn install - NPM
$ npm i
- Yarn:
Linting
Run yarn lint or npm run lint.
Testing
Run yarn test or npm test.
Building
Run yarn build or npm run build.
Author
Tomer Aberbach
License
Copyright © 2020 Tomer Aberbach Released under the MIT license.