@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-node
Or with npm
:
$ npm i -D @tomera/babel-preset-node
If 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" | false
Default:
false
Configure transformation of ES6 module syntax to another module type.
Set to
false
to disable module transformation (useful if using a bundler that handles module transformation).NOTE:
"cjs"
is just an alias for"commonjs"
."fill"
Type:
"poly"
|"pony"
|false
Default:
"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
false
to 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:
boolean
Default:
false
Specify 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.json
to 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
src
directory and you have installed@babel/cli
as a dev dependency (usingyarn add -D @babel/cli
ornpm i -D @babel/cli
), then you can transpile your code to alib
directory by running the following command from the root directory of your project:$ babel -D src -d lib
Ensure the
"main"
field ofpackage.json
points to the entry point of your package in thelib
directory and that the files in thelib
directory 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.json
and 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 build
ornpm 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-env
as a dev dependency (usingyarn add -D cross-env
ornpm 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/module
Ensure the
"main"
and"module"
fields ofpackage.json
point to the entry points of your package in thelib/main
andlib/module
directories, respectively, and that the files in thelib
directory 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.json
and 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-all
as a dev dependency (usingyarn add -D npm-run-all
andnpm i -D npm-run-all
) for therun-p
command and"build"
script to work.You can now transpile your code for the
"main"
and"module"
targets concurrently by runningyarn build
ornpm 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-node
directory:$ 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.