Template processing options control placeholder patterns, comment handling, parser behavior, and replacement processing.
Main configuration interface for template processing options.
interface PublicOpts extends import("@babel/parser").ParserOptions {
/**
* Set of placeholder names to automatically accept, ignoring pattern matching
* Used with %%foo%% style placeholders to whitelist specific names
*/
placeholderWhitelist?: Set<string>;
/**
* Pattern to search for when looking for Identifier and StringLiteral placeholders
* - RegExp: Custom pattern for placeholder matching
* - false: Disable placeholder searching entirely
* - Default: /^[_$A-Z0-9]+$/
*/
placeholderPattern?: RegExp | false;
/**
* Comment preservation behavior
* - true: Pass through comments from template into resulting AST
* - false: Automatically discard comments (default)
*/
preserveComments?: boolean;
/**
* Placeholder style selection
* - true: Use %%foo%% style syntactic placeholders
* - false: Use legacy placeholders (placeholderPattern/placeholderWhitelist)
* - null: Auto-detect based on template content (default)
*/
syntacticPlaceholders?: boolean | null;
}Usage Examples:
import template from "@babel/template";
import * as t from "@babel/types";
// Custom placeholder pattern
const customPattern = template({
placeholderPattern: /^PLACEHOLDER_\w+$/
});
const buildVar = customPattern("const PLACEHOLDER_NAME = PLACEHOLDER_VALUE;");
// Placeholder whitelist
const whitelisted = template({
placeholderWhitelist: new Set(["CUSTOM_VAR", "ANOTHER_VAR"])
});
const buildCustom = whitelisted("const CUSTOM_VAR = ANOTHER_VAR;");
// Preserve comments
const withComments = template({
preserveComments: true
});
const commentedAst = withComments(`
// This comment will be preserved
const x = %%value%%;
`)({ value: t.numericLiteral(42) });
// Syntactic placeholders
const syntactic = template({
syntacticPlaceholders: true
});
const syntacticBuild = syntactic("const %%name%% = %%value%%;");Types for providing replacement values to template functions.
/**
* Replacement values for template placeholders
* - Object form: Maps placeholder names to replacement values
* - Array form: Positional replacements (indexed as $0, $1, etc.)
*/
type PublicReplacements = { [x: string]: unknown } | Array<unknown>;
/**
* Internal normalized replacement format (object form only)
*/
type TemplateReplacements = { [x: string]: unknown } | void;Usage Examples:
import template from "@babel/template";
import * as t from "@babel/types";
// Object-based replacements
const buildFunction = template("function %%name%%(%%param%%) { return %%body%%; }");
const funcAst = buildFunction({
name: t.identifier("add"),
param: t.identifier("x"),
body: t.binaryExpression("+", t.identifier("x"), t.numericLiteral(1))
});
// Array-based replacements (using $0, $1, $2 placeholders)
const buildArray = template("const result = [$0, $1, $2];");
const arrayAst = buildArray([
t.stringLiteral("first"),
t.stringLiteral("second"),
t.stringLiteral("third")
]);
// Template literal with mixed replacements
const buildMixed = template`
const ${t.identifier("data")} = {
%%key%%: %%value%%
};
`;
const mixedAst = buildMixed({
key: t.identifier("id"),
value: t.numericLiteral(123)
});All @babel/parser options are supported through PublicOpts.
import template from "@babel/template";
// TypeScript parsing
const tsTemplate = template({
plugins: ["typescript"],
sourceType: "module"
});
// JSX support
const jsxTemplate = template({
plugins: ["jsx"],
preserveComments: true
});
// Strict mode
const strictTemplate = template({
sourceType: "script",
strictMode: true
});Different approaches for handling placeholders in templates.
import template from "@babel/template";
import * as t from "@babel/types";
// Default pattern matching (uppercase identifiers)
const defaultTemplate = template("const FOO = BAR;");
const ast1 = defaultTemplate({
FOO: t.identifier("result"),
BAR: t.numericLiteral(42)
});
// Syntactic placeholders (%%name%% style)
const syntacticTemplate = template({
syntacticPlaceholders: true
});
const buildSyntactic = syntacticTemplate("const %%varName%% = %%varValue%%;");
// Custom pattern (prefix-based)
const customTemplate = template({
placeholderPattern: /^TMPL_\w+$/
});
const buildCustomPattern = customTemplate("const TMPL_NAME = TMPL_VALUE;");
// Whitelist approach (specific names only)
const whitelistTemplate = template({
placeholderWhitelist: new Set(["ALLOWED_VAR"]),
placeholderPattern: false // Disable pattern matching
});
const buildWhitelist = whitelistTemplate("const ALLOWED_VAR = NOT_REPLACED;");Control how comments and directives are processed in templates.
import template from "@babel/template";
// Preserve all comments
const preserveTemplate = template({
preserveComments: true
});
const withComments = preserveTemplate(`
"use strict"; // Directive comment
// Function comment
function %%name%%() {
/* Block comment */
return %%value%%;
}
`)({
name: t.identifier("getValue"),
value: t.numericLiteral(100)
});
// Strip comments (default behavior)
const stripTemplate = template({
preserveComments: false // Default
});
const withoutComments = stripTemplate(`
// This comment will be removed
const result = %%computation%%;
`)({
computation: t.callExpression(t.identifier("calculate"), [])
});