# babel-template

> Generate an AST from a string template.

Latest version **6.26.0** (published 2017-08-16) · MIT license · 0 weekly downloads

## Install

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

## Health

**Score 38/100 (D)** — status: abandoned.

Positive: has types package; no vulnerabilities; high maintenance score; popular repo.

Warnings: low downloads; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 6.26.0 |
| Published | 2017-08-16 |
| First published | 2015-10-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/babel-template) |
| Module format | CommonJS |
| Dependencies | 5 |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 43996 |
| Author | Sebastian McKenzie |
| Maintainers | thejameskyle, sebmck, danez, hzoo, loganfsmyth |

## Links

- npm: https://www.npmjs.com/package/babel-template
- Repository: https://github.com/babel/babel/tree/master/packages/babel-template
- Homepage: https://babeljs.io/
- npm.io page: https://npm.io/package/babel-template

## Dependencies (5)

- [lodash](https://npm.io/package/lodash.md) ^4.17.4
- [babylon](https://npm.io/package/babylon.md) ^6.18.0
- [babel-types](https://npm.io/package/babel-types.md) ^6.26.0
- [babel-runtime](https://npm.io/package/babel-runtime.md) ^6.26.0
- [babel-traverse](https://npm.io/package/babel-traverse.md) ^6.26.0

## Recent versions

- 6.26.0 (latest) — 2017-08-16
- 7.0.0-beta.3 (next) — 2017-10-15
- 7.0.0-beta.2 — 2017-09-26
- 7.0.0-beta.1 — 2017-09-19
- 7.0.0-beta.0 — 2017-09-12
- 7.0.0-alpha.20 — 2017-08-30
- 7.0.0-alpha.19 — 2017-08-07
- 7.0.0-alpha.18 — 2017-08-03
- 7.0.0-alpha.17 — 2017-07-26
- 7.0.0-alpha.16 — 2017-07-25
- 7.0.0-alpha.15 — 2017-07-12
- 7.0.0-alpha.14 — 2017-07-12
- 6.25.0 — 2017-06-08
- 7.0.0-alpha.12 — 2017-05-31
- 7.0.0-alpha.11 — 2017-05-31
- … 31 more at https://npm.io/package/babel-template/versions

## README

# babel-template

> Generate an AST from a string template.

In computer science, this is known as an implementation of quasiquotes.

## Install

```sh
npm install --save-dev babel-template
```

## Usage

```js
import template from "babel-template";
import generate from "babel-generator";
import * as t from "babel-types";

const buildRequire = template(`
  var IMPORT_NAME = require(SOURCE);
`);

const ast = buildRequire({
  IMPORT_NAME: t.identifier("myModule"),
  SOURCE: t.stringLiteral("my-module")
});

console.log(generate(ast).code);
```

```js
const myModule = require("my-module");
```

## API

### `template(code, [opts])`

#### code

Type: `string`

#### options

`babel-template` accepts all of the options from [babylon], and specifies
some defaults of its own:

* `allowReturnOutsideFunction` is set to `true` by default.
* `allowSuperOutsideMethod` is set to `true` by default.

##### preserveComments

Type: `boolean`
Default: `false`

Set this to `true` to preserve any comments from the `code` parameter.

#### Return value

`babel-template` returns a `function` which is invoked with an optional object
of replacements. See the usage section for an example.

[babylon]: https://github.com/babel/babylon#options

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