Lodash Template JavaScript Parser
This package provides a JavaScript parser/splitter specifically designed for `lodash.template` content. It's currently at version 1.1.1 and appears to be in an active maintenance state, with recent bug fixes and minor feature additions. Its core functionality is to accurately separate JavaScript code segments from template strings, ensuring that the original positional information of the JavaScript code is preserved. A key differentiator is its minimalist approach: unlike more comprehensive tools such as `eslint-plugin-lodash-template`, this library does not include its own JavaScript parser (e.g., espree or babel/parser). This makes it a lightweight solution for use cases like linting tools that need to extract executable JavaScript logic from `lodash` templates for external processing, without requiring a full Abstract Syntax Tree (AST) generation from this package itself.
Common errors
-
TypeError: parseTemplate is not a function
cause Attempting to use `require()` for CommonJS import in an environment that expects ES Modules, or incorrect destructuring of the module export.fixEnsure you are using ES Module syntax: `import { parseTemplate } from 'lodash-template-js-parser';`. If you are in a pure CommonJS environment (e.g., older Node.js scripts), you might need to use dynamic `import()` or configure your build system to transpile. -
Parsing results in empty or incorrect script/template strings despite valid input.
cause The `templateSettings` passed to `parseTemplate` do not match the actual delimiters used in the lodash template, leading to misinterpretation of script blocks.fixVerify that `parserOptions.templateSettings` (specifically `escape`, `evaluate`, and `interpolate`) accurately reflect the custom delimiters configured for `lodash.template` when the template was created. For example, if your template uses `{{...}}` for interpolation, you must set `interpolate: ['{{', '}}']`.
Warnings
- gotcha The parser's behavior heavily relies on the `templateSettings` passed to `parseTemplate`. If these settings do not precisely match the `templateSettings` used by `lodash.template` when the original template was authored, the parsing may yield incorrect or unexpected results.
- gotcha This library acts as a parser/splitter and returns JavaScript code as a plain string. It does *not* include its own JavaScript parser (e.g., for generating an AST). If you require an AST for the extracted JavaScript, you will need to pipe the `script` output to a separate JavaScript parser like `@babel/parser` or `espree`.
- gotcha The package currently maintains positions by padding non-JS parts with whitespace. This means the `script` string might contain significant leading/trailing whitespace and empty lines corresponding to the original template's non-script portions. This can affect downstream tooling expectations.
Install
-
npm install lodash-template-js-parser -
yarn add lodash-template-js-parser -
pnpm add lodash-template-js-parser
Imports
- parseTemplate
const { parseTemplate } = require('lodash-template-js-parser');import { parseTemplate } from 'lodash-template-js-parser'; - parseTemplateOptions
import { parseTemplateOptions } from 'lodash-template-js-parser';import type { parseTemplateOptions } from 'lodash-template-js-parser'; - tokens
import { tokens } from 'lodash-template-js-parser';
Quickstart
import { parseTemplate } from "lodash-template-js-parser";
import assert from 'assert';
const content = `
const age = 18;
<% if (age < 18) { %>
<li><%= name %> (age: <%= age %>)</li>
<% } else { %>
<li>over the age limit!</li>
<% }%>`
const { script, template } = parseTemplate(content, {
templateSettings: {
interpolate: ["#{", "}"] // Example of custom lodash template settings
}
});
assert.strictEqual(script, `
if (age < 18) {
name ; age ;
} else {
}
`);
assert.strictEqual(template, `
const age = 18;
<li> (age: )</li>
<li>over the age limit!</li>
`);
console.log('Script extracted:\n', script);
console.log('Template extracted:\n', template);