Skip to content

Commit e093e2b

Browse files
feat: replace parse-literals with a local implementation
Signed-off-by: Henry <mail@henrygressmann.de>
1 parent 7f76b89 commit e093e2b

8 files changed

Lines changed: 102 additions & 37 deletions

File tree

bun.lock

Lines changed: 24 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/minify-literals/CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# minify-literals
22

3+
## 2.0.1
4+
5+
### Patch Changes
6+
7+
- Replace `parse-literals` with a local implementation
8+
39
## 2.0.0
410

511
### Major Changes
@@ -75,7 +81,7 @@
7581

7682
- [`2ed7279`](https://github.com/explodingcamera/esm/commit/2ed72792bf13fa4b712fb477208ebb7d061a1e8f) - update dependencies
7783

78-
- [`de8e91f`](https://github.com/explodingcamera/esm/commit/de8e91f4f1052fbd6bf4f82b3c8010195b98a7b1) - update depdendencies
84+
- [`de8e91f`](https://github.com/explodingcamera/esm/commit/de8e91f4f1052fbd6bf4f82b3c8010195b98a7b1) - update dependencies
7985

8086
## 1.0.0
8187

packages/minify-literals/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ $ pnpm add minify-literals
2727
import { minifyHTMLLiterals } from "minify-literals";
2828

2929
const source = `
30-
const el = html\`<div > <h1> Hello World </h1 > </div>\`;
31-
const css = css\` .foo { color: red; } \`;
32-
`;
30+
const el = html\`<div > <h1> Hello World </h1 > </div>\`;
31+
const css = css\` .foo { color: red; } \`;
32+
`;
3333

3434
let { code, map } = await minifyHTMLLiterals(source);
3535
// or with options: await minifyHTMLLiterals(source, { fileName: "test.js" });

packages/minify-literals/lib/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import MagicString from "magic-string";
2-
import { type Template, parseLiterals } from "parse-literals";
32
import {
43
combineTemplateParts,
54
type CSSOptions,
@@ -10,6 +9,7 @@ import {
109
minifyHTML,
1110
splitByPlaceholder,
1211
} from "./minify.js";
12+
import { type Template, parseTemplates } from "./literals.js";
1313

1414
type MaybePromise<T> = T | Promise<T>;
1515

@@ -28,7 +28,7 @@ export type CSSMinifier = (css: string) => MaybePromise<string>;
2828
/** Options for {@link minifyHTMLLiterals}. */
2929
export type Options = {
3030
/**
31-
* Source filename used by `parse-literals` and source map generation.
31+
* Source filename used by literal parsing and source map generation.
3232
*/
3333
fileName?: string;
3434

@@ -111,7 +111,7 @@ export async function minifyHTMLLiterals(source: string, options: Options = {}):
111111
const cssOptions = options.css ?? defaultMinifyCSSOptions;
112112
const htmlTags = options.htmlTags ?? defaultHTMLTags;
113113
const cssTags = options.cssTags ?? defaultCSSTags;
114-
const templates = parseLiterals(source, { fileName: options.fileName });
114+
const templates = parseTemplates(source, options.fileName);
115115
const ms = new MagicString(source);
116116
const skipCSS = cssOptions !== false && source.includes("unsafeCSS");
117117
const skipHTML = htmlMinifier !== false && source.includes("unsafeHTML");
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { Parser, type Expression, type Node, type TemplateLiteral } from "acorn";
2+
import { tsPlugin } from "@sveltejs/acorn-typescript";
3+
4+
export type Template = {
5+
tag: string;
6+
parts: TemplatePart[];
7+
};
8+
9+
export type TemplatePart = {
10+
text: string;
11+
start: number;
12+
end: number;
13+
};
14+
15+
export function parseTemplates(source: string, fileName = ""): Template[] {
16+
let ast: Node;
17+
try {
18+
ast = Parser.extend(tsPlugin({ jsx: /\.m?[jt]sx($|[?#])/.test(fileName) })).parse(source, {
19+
ecmaVersion: "latest",
20+
locations: true,
21+
sourceType: "module",
22+
});
23+
} catch {
24+
return [];
25+
}
26+
27+
const templates: Template[] = [];
28+
const nodes = [ast];
29+
30+
for (const node of nodes) {
31+
if (node.type === "TaggedTemplateExpression") {
32+
const { tag, quasi } = node as Node & { tag: Expression; quasi: TemplateLiteral };
33+
templates.push({
34+
tag: source.slice(tag.start, tag.end),
35+
parts: quasi.quasis.map((quasi) => ({
36+
text: quasi.value.raw,
37+
start: quasi.start,
38+
end: quasi.end,
39+
})),
40+
});
41+
}
42+
43+
for (const value of Object.values(node)) {
44+
if (Array.isArray(value)) {
45+
for (const item of value) {
46+
if (item && typeof item === "object" && typeof (item as Node).type === "string") {
47+
nodes.push(item as Node);
48+
}
49+
}
50+
} else if (value && typeof value === "object" && typeof (value as Node).type === "string") {
51+
nodes.push(value as Node);
52+
}
53+
}
54+
}
55+
56+
return templates;
57+
}

packages/minify-literals/lib/minify.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { describe, expect, it } from "bun:test";
2-
import type { TemplatePart } from "parse-literals";
32
import {
43
combineTemplateParts,
54
defaultMinifyOptions,
65
getPlaceholder,
76
minifyHTML,
87
splitByPlaceholder,
8+
type TemplatePart,
99
} from "./minify";
1010

1111
describe("minify helpers", () => {

packages/minify-literals/lib/minify.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { transform, type TransformOptions } from "lightningcss";
22
import { type MinifierOptions as HTMLOptions, minify as minifyHTMLString } from "html-minifier-next";
3-
import type { TemplatePart } from "parse-literals";
3+
import type { TemplatePart } from "./literals.js";
4+
5+
export type { TemplatePart } from "./literals.js";
46

57
export type CSSOptions = Omit<TransformOptions<any>, "filename" | "code">;
68

packages/minify-literals/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "minify-literals",
3-
"version": "2.0.0",
3+
"version": "2.0.1",
44
"description": "Minify CSS and HTML literals",
55
"keywords": [
66
"literals",
@@ -32,10 +32,11 @@
3232
"entry": "lib/index.ts"
3333
},
3434
"dependencies": {
35+
"@sveltejs/acorn-typescript": "^1.0.10",
36+
"acorn": "^8.16.0",
3537
"html-minifier-next": "^6.2.8",
3638
"lightningcss": "^1.32.0",
37-
"magic-string": "^0.30.21",
38-
"parse-literals": "^1.2.1"
39+
"magic-string": "^0.30.21"
3940
},
4041
"engines": {
4142
"node": ">=22.0.0"

0 commit comments

Comments
 (0)