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-parserVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to parse a lodash template, extract its JavaScript and template content, and apply custom template settings.
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.
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'
```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.
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.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: ['{{', '}}']`.No dependency data recorded yet.