A Babel
preset for transforming JavaScript
code with modern language features into code compatible with
njs (NGINX JavaScript). This preset
includes support for all the ECMAScript features that TypeScript
supports (Stage 3+) — see
Plugins for a complete list.
Installation
npm install --save-dev babel-preset-njs
yarn add --dev babel-preset-njs
Usage
With a Configuration File (Recommended)
{
"presets": ["babel-preset-njs"]
}
{
"presets": [
["babel-preset-njs", { "assumeArrayIterables": false }]
]
}
Via CLI
babel --presets babel-preset-njs script.js
Via Node API
require('@babel/core').transform('code', {
presets: [require('babel-preset-njs')],
})
Options
assumeArrayIterables
boolean
, defaults to true
Assume that all iterables used in for...of
loops, array spread
syntax
and array
destructuring
are arrays. This produces more compact and significantly more performant
code, but it will fail in runtime if the iterable is not an array.
TypeScript transpiler uses the same approach for ES5 target when
downlevelIteration
is disabled. It also indicates error when the
iterable is not an array.
It sets the following plugin options:
asyncHelpers
'external'
, 'local'
, 'inline'
Toggles how
async-to-promises
injects its helpers:
-
'external'
— injects import from
babel-plugin-transform-async-to-promises/helpers
into each file
with async/await
-
'local'
— injects the needed helper functions into each file with
async/await
-
'inline'
— inlines the needed helper code in place of each
occurrence of async/await
Defaults to 'external'
if Rollup or webpack is detected, otherwise
'local'
.
looseClasses
boolean
, defaults to true
Define class methods and properties using a simple assignment expression
instead of Object.defineProperty()
. This produces more compact and
performant code, but it might produce unexpected results in some
(corner) cases. TypeScript transpiler uses the same approach.
It sets the following plugin options:
looseObjectRestSpread
boolean
, defaults to true
Transform object rest/spread
properties
using Object.assign()
instead of Babel’s objectSpread
helper. This
produces more compact and performant code, but it might produce
unexpected results in some (corner) cases (when overwriting read-only
target property). TypeScript transpiler uses the same approach.
It sets the following plugin options:
looseParameters
boolean
, defaults to true
In loose mode, function parameters with default values will be counted
into the arity of the function. This is not spec behaviour where these
parameters do not add to function arity, but it produces more compact
and performant code. TypeScript transpiler uses the same approach.
It sets the following plugin options:
Plugins
The list of included Babel plugins is available
here.
References
License
This project is licensed under MIT
License.