Registry / serialization / lodash-template-js-parser

lodash-template-js-parser

JSON →
library1.1.1jsnpmunverified

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.

npm install lodash-template-js-parser
INSTALL
IMPORT
SIG · LODASH-TEMPLATE-JS
L
lodash-template-js-parser
serializationjavascriptv1.1.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

parseTemplate
import { parseTemplate } from 'lodash-template-js-parser';
const { parseTemplate } = require('lodash-template-js-parser');
The library primarily uses ES Module syntax and ships TypeScript types. While CommonJS `require` might work in some environments, it's not the idiomatic or type-safe approach.
parseTemplateOptions
import type { parseTemplateOptions } from 'lodash-template-js-parser';
import { parseTemplateOptions } from 'lodash-template-js-parser';
This is a TypeScript interface for options. Use `import type` to avoid bundling it at runtime if you are using TypeScript.
tokens
import { tokens } from 'lodash-template-js-parser';
Introduced in v1.1.0, this export provides the underlying MicroTemplate tokens for advanced parsing scenarios.

Demonstrates how to parse a lodash template, extract its JavaScript and template content, and apply custom template settings.

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);
Debug
Known issues
gotchaThe 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.
fix
Always ensure the `templateSettings` provided to `parseTemplate` accurately reflect those used by `_.template` for the specific template being processed. These settings define the delimiters for `escape`, `evaluate`, and `interpolate` tags.
affects: >=1.0.0
gotchaThis 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`.
fix
If an AST is needed, integrate an external JavaScript parser:
```typescript
import { parseTemplate } from 'lodash-template-js-parser';
import * as espree from 'espree'; // or '@babel/parser'

const { script } = parseTemplate('<% var x = 1; %>', {});
const ast = espree.parse(script, { ecmaVersion: 2020 });
console.log(ast.body.declarations.id.name); // 'x'
```
affects: >=1.0.0
gotchaThe 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.
fix
Tools consuming the `script` output should be prepared to handle and potentially trim or ignore leading/trailing whitespace. For AST parsers, this padding usually doesn't affect syntax trees but might impact source map generation if not accounted for.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: parseTemplate is not a function
Attempting to use `require()` for CommonJS import in an environment that expects ES Modules, or incorrect destructuring of the module export.
fix
Ensure 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.
The `templateSettings` passed to `parseTemplate` do not match the actual delimiters used in the lodash template, leading to misinterpretation of script blocks.
fix
Verify 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: ['{{', '}}']`.
Upgrade
Version history
1.1.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
8 hits · last 30 days
node
8
Resources
lodash-template-js-parser — npm install lodash-template-js-parser · libregistry