Skip to content

Commit 7d31d3a

Browse files
fix: kv-pairs minification issues
Signed-off-by: Henry Gressmann <[email protected]>
1 parent 70dd9d3 commit 7d31d3a

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

.changeset/tasty-queens-yell.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"minify-literals": patch
3+
---
4+
5+
fix issues with kv-pairs

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { afterEach, beforeEach, describe, expect, it, test } from "vitest";
1+
import { afterEach, beforeEach, describe, expect, it, test, vi } from "vitest";
22

33
import MagicString, { SourceMapOptions } from "magic-string";
44
import { ParseLiteralsOptions, Template, TemplatePart, parseLiterals } from "parse-literals";
@@ -13,11 +13,21 @@ import {
1313
} from "./";
1414
import { defaultMinifyOptions, defaultStrategy } from "./strategy";
1515

16+
const consoleMock = {
17+
log: vi.fn(),
18+
warn: vi.fn(),
19+
error: vi.fn(),
20+
};
21+
22+
vi.stubGlobal("console", consoleMock);
23+
1624
// https://github.com/explodingcamera/esm/issues/1
1725
describe("handle key value pairs correctly", () => {
1826
it("should minify html", async () => {
1927
const source = `const css = css\`:host{\${"color"}: \${"red"}}\``;
20-
expect((await minifyHTMLLiterals(source))?.code).toMatchInlineSnapshot("undefined");
28+
expect((await minifyHTMLLiterals(source))?.code).toMatchInlineSnapshot(
29+
'"const css = css`:host{${\\"color\\"}:${\\"red\\"}}`"',
30+
);
2131
});
2232
});
2333

@@ -30,8 +40,10 @@ describe("handle comments correctly", () => {
3040
});
3141

3242
it("templates inside of html comments are not minified", async () => {
43+
const spy = vi.spyOn(console, "warn");
3344
const source = `const el = html\`<div><!-- \${console.log(1)} --></div>\``;
3445
expect(await minifyHTMLLiterals(source)).toBe(null);
46+
expect(spy).toHaveBeenCalledTimes(1);
3547
});
3648

3749
it("comments inside of literals are not removed", async () => {
@@ -57,7 +69,9 @@ describe("don't minify code with unsafeCSS / unsafeHTML", () => {
5769
\`
5870
`.trim();
5971

72+
const spy = vi.spyOn(console, "warn");
6073
expect(await minifyHTMLLiterals(source)).toBe(null);
74+
expect(spy).toHaveBeenCalledTimes(1);
6175
});
6276
});
6377

packages/minify-literals/lib/strategy.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export const defaultStrategy: Strategy<HTMLOptions, CleanCSS.Options> = {
112112

113113
if (html.match(/<!--(.*?)@TEMPLATE_EXPRESSION\(\);(.*?)-->/g)) {
114114
console.warn(
115-
"minify-html-literals:: HTML minification is not supported for template expressions inside comments. Minification for this file will be skipped.",
115+
"minify-html-literals: HTML minification is not supported for template expressions inside comments. Minification for this file will be skipped.",
116116
);
117117
return html;
118118
}
@@ -170,7 +170,7 @@ export const defaultStrategy: Strategy<HTMLOptions, CleanCSS.Options> = {
170170
async minifyCSS(css, options = {}) {
171171
const adjustedOptions = adjustMinifyCSSOptions(options);
172172

173-
css = css.replaceAll(/@TEMPLATE_EXPRESSION\(\);:/g, "@TEMPLATE_EXPRESSION():");
173+
css = css.replaceAll(/@TEMPLATE_EXPRESSION\(\);:/g, "--TEMPLATE-EXPRESSION:");
174174
const output = await new CleanCSS({
175175
...adjustedOptions,
176176
returnPromise: true,
@@ -187,7 +187,7 @@ export const defaultStrategy: Strategy<HTMLOptions, CleanCSS.Options> = {
187187
return css.replace(/(\n)|(\r)/g, "");
188188
}
189189

190-
css = css.replaceAll(/@TEMPLATE_EXPRESSION\(\);:/g, "@TEMPLATE_EXPRESSION():");
190+
output.styles = output.styles.replaceAll("--TEMPLATE-EXPRESSION:", "@TEMPLATE_EXPRESSION();:");
191191
output.styles = fixCleanCssTidySelectors(css, output.styles);
192192
return output.styles;
193193
},

0 commit comments

Comments
 (0)