1. babel-template
Generate an AST from a string template.
babel-template
Package: babel-template
Created by: babel
Last modified: Fri, 21 Jul 2023 15:42:49 GMT
Version: 6.26.0
License: MIT
Downloads: 13,179,328
Repository: https://github.com/babel/babel/tree/master/packages/babel-template

Install

npm install babel-template
yarn add babel-template

babel-template

Generate an AST from a string template.

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

Install

 npm install --save-dev babel-template

Usage

 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);
 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.

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code