@literals/parser
TypeScript icon, indicating that this package has built-in type declarations

2.0.1 • Public • Published

parse-literals

Because sometimes you literally need to parse template literals.

npm

Why?

Template literals are often used in JavaScript for HTML and CSS. This library allows developers to extract the strings from the literals for post-processing, such as minifying or linting.

Usage

[!IMPORTANT]
All @literals/* packages are published as ESM-only!

import * as pl from '@literals/parser';

const templates = pl.parseLiterals(`
  render() {
    return html\`
      <h1>\${"Hello World"}</h1>
    \`;
  }
`);

console.log(templates);
// [
//   {
//     "tag": "html",
//     "parts": [
//       {
//         "text": "\n      <h1>",
//         "start": 30,
//         "end": 41
//       },
//       {
//         "text": "</h1>\n    ",
//         "start": 57,
//         "end": 67
//       }
//     ]
//   }
// ]

Supported Source Syntax

  • JavaScript
  • TypeScript

Package Sidebar

Install

npm i @literals/parser

Weekly Downloads

439

Version

2.0.1

License

MIT

Unpacked Size

13.7 kB

Total Files

19

Last publish

Collaborators

  • julian.cataldo