# babel-unifyer

> Babel joiner to one executable script file for NodeJS or web browser

Latest version **1.0.7** (published 2020-04-22) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install babel-unifyer
pnpm add babel-unifyer
yarn add babel-unifyer
bun add babel-unifyer
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.7 |
| Published | 2020-04-22 |
| First published | 2020-02-04 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 27.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | "Karkael Haunter" |
| Maintainers | karkael |
| Keywords | babel, unifyer, joiner, promisify |

## Links

- npm: https://www.npmjs.com/package/babel-unifyer
- Repository: https://github.com/karkael64/babel-unifyer
- Homepage: https://github.com/karkael64/babel-unifyer#readme
- Issues: https://github.com/karkael64/babel-unifyer
- npm.io page: https://npm.io/package/babel-unifyer

## Dependencies (2)

- [@babel/core](https://npm.io/package/@babel/core.md) ^7.8.4
- [@babel/preset-env](https://npm.io/package/@babel/preset-env.md) ^7.8.4

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 1.0.7 (latest) — 2020-04-22
- 1.0.6 — 2020-04-22
- 1.0.5 — 2020-04-22
- 1.0.4 — 2020-04-06
- 1.0.3 — 2020-02-14
- 1.0.2 — 2020-02-05
- 1.0.1 — 2020-02-04
- 1.0.0 — 2020-02-04

## README

# Babel-Unifyer

Helps to translate files with Babel.js and unifyer them in a __single script file__.

The __required files__ are added in the single script, the __non-js__ files are transformed (if the transformation is registered) or read as strings for then execution of Javascript.

## Table of Contents

1. [Installation](#installation)
2. [Simple usage](#simple-usage)
3. [Configure](#configure)
    1. [Babel](#babel)
    2. [Added configuration](#added-configuration)
    3. [Prepare non-js files](#prepare-non-js-files)
4. [Documentation](#documentation)


## Installation

`npm install --save-dev babel-unifyer`


## Simple usage

```js
const vm = require("vm")
const babelUnifyer = require("babel-unifyer")

;(async function () {
  const buffer = await babelUnifyer(__dirname + "/a")
  const script = new vm.Script(buffer)
  const ctx = vm.createContext({ console })
  await script.runInContext(ctx)
})()
```

## Configure

### Babel

Second parameter is the Babel.js configurator (below default configuration).

```js
const babelUnifyer = require("babel-unifyer")
babelUnifyer(filename, {
  presets: ["@babel/env"],
  ast: true,
  sourceMap: true,
  comments: false
})
```

Warn: `sourceMap` parameter here should be `false` for disabling, or anything else for enabling it.


### Added configuration

In order to help translation of unified scripts like TypeScript, you can set `allowedExtensions` as an array of filename extensions allowed. Default value is `[".js"]`.

```js
const babelUnifyer = require("babel-unifyer")
babelUnifyer(filename, {
  allowedExtensions: [".js", ".jsx", ".mjs"]
})
```


### Prepare non-js files

With this module, you can load files that are not written in JavaScript. The translated contents are then sent to Babel.js for integration in the unified script.

```js
const {setTranslator} = require("babel-unifyer")
setTranslator(".xml", function (contents) {
  // your translation
  return contents
})
```

For every extensions not listed, you can configure translator `""` for default translation.


## Documentation

#### Load module

```js
const { unifyScript, setTranslator } = require("babel-unifyer")
// or
const unifyScript = require("babel-unifyer")
const { setTranslator } = unifyScript
```


#### function unifyScript(src, opts)

Function unifyScript translate a script entry `src` to an only one executable script with its required files.

Parameters:
 * `{object} opts` the options for the babel translation ;
 * `{string} src` the entry script path.

Returns: `{Promise.<{vm.Script} script>.<{Error} err>}` reject an error if :
 * a file required is not reachable ;
 * there is an error at compilation.


#### function setTranslator(extname, fn)

Set a translator function `fn` for every files with extension name `extname`. Default extension name is `""`.

Parameters:
 * `{string} extname` with dot ;
 * `{function} fn({string} contents, {string} filename, {object} opts) : {string|Buffer|Promise.<{string|Buffer} contents>}` translator.

---
_Source: https://npm.io/package/babel-unifyer · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
