Skip to content

Commit a5c765c

Browse files
test: cover the embedded surface in one case
Every shape a host and a nested body can take, in one build each: - A document webpack parses as an entry, one it parses as a module, and one it only emits — each with an inline `<style>`, a `style` attribute, a `<script>`, a JSON `<script>`, an `<svg>` subtree and an `<iframe srcdoc>` carrying its own `<style>`, and each `<style>` carrying a `data:` SVG two levels down. Only the emitted-only document keeps its `<script>` inline, so it alone reaches a JavaScript minimizer there. - A stylesheet's `url()` payload in every language a media type names, plain and base64, plus an `image/png` naming none. - Every `exportType`: the one that emits a stylesheet, the one that exports its text, and the two that inject it — those last on a target whose runtime works without a document. - An `asset/source` and an `asset/inline` module of every language.
1 parent 16813ee commit a5c765c

23 files changed

Lines changed: 450 additions & 0 deletions
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2+
3+
exports[`ConfigCacheTestCases embedded everything everything should compile 1`] = `
4+
Object {
5+
"assetPage": "<!doctype html><html lang=en><head><title>asset</title><style>.assetStyle{background:url(\\"data:image/svg+xml,<svg> <rect fill='red' /> </svg>\\");color:red}</style><script>var assetScript=1;function f(){return assetScript}</script><script type=application/json>{\\"assetJson\\":1}</script></head><body>
6+
<p style=color:#0f0>hi</p>
7+
<svg viewBox=\\"0 0 2 2\\"> <rect fill=red /> </svg>
8+
<iframe srcdoc=\\"<style>.assetSrcdoc{color:#00f}</style>\\"></iframe>
9+
10+
</body></html>",
11+
"entryPage": "<!doctype html><html lang=en><head><title>entry</title><style>.entryStyle{background:url(\\"data:image/svg+xml,<svg> <rect fill='red' /> </svg>\\");color:red}</style><script src=__html_2a0885fc_0.js></script><script type=application/json>{\\"entryJson\\":1}</script></head><body>
12+
<p style=color:#0f0>hi</p>
13+
<svg viewBox=\\"0 0 2 2\\"> <rect fill=red /> </svg>
14+
<iframe srcdoc=\\"<style>.entrySrcdoc{color:#00f}</style>\\"></iframe>
15+
16+
</body></html>",
17+
"sheet": "@import url(data:text/css,.imported{color:red});.svg{background:url(\\"data:image/svg+xml,<svg> <rect fill='red' /> </svg>\\")}.html{background:url(\\"data:text/html,<div> <p>hi</p> </div>\\")}.json{background:url('data:application/json,{\\"a\\":1}')}.javascript{background:url(data:text/javascript,var\\\\ a=1;)}.svgBase64{background:url(data:image/svg+xml;base64,PHN2Zz4gPHJlY3QgZmlsbD0ncmVkJyAvPiA8L3N2Zz4=)}.cssBase64{background:url(data:text/css;base64,LmlubmVye2NvbG9yOnJlZH0=)}.png{background:url(data:image/png;base64,AAAA)}.sheet_asset_css{color:red;margin:10px}",
18+
}
19+
`;
20+
21+
exports[`ConfigCacheTestCases embedded everything exported tests minifies a \`data:\` payload of every language a media type names 1`] = `"@import url(data:text/css,.imported{color:red});.svg{background:url(\\"data:image/svg+xml,<svg> <rect fill='red' /> </svg>\\")}.html{background:url(\\"data:text/html,<div> <p>hi</p> </div>\\")}.json{background:url('data:application/json,{\\"a\\":1}')}.javascript{background:url(data:text/javascript,var\\\\ a=1;)}.svgBase64{background:url(data:image/svg+xml;base64,PHN2Zz4gPHJlY3QgZmlsbD0ncmVkJyAvPiA8L3N2Zz4=)}.cssBase64{background:url(data:text/css;base64,LmlubmVye2NvbG9yOnJlZH0=)}.png{background:url(data:image/png;base64,AAAA)}.sheet_text_css{color:red;margin:10px}"`;
22+
23+
exports[`ConfigCacheTestCases embedded everything exported tests minifies every body a document nests 1`] = `
24+
"<!doctype html><html lang=en><head><title>module</title><style>.moduleStyle{background:url(\\"data:image/svg+xml,<svg> <rect fill='red' /> </svg>\\");color:red}</style><script src=__html_fc45afb8_0.js></script><script type=application/json>{\\"moduleJson\\":1}</script></head><body>
25+
<p style=color:#0f0>hi</p>
26+
<svg viewBox=\\"0 0 2 2\\"> <rect fill=red /> </svg>
27+
<iframe srcdoc=\\"<style>.moduleSrcdoc{color:#00f}</style>\\"></iframe>
28+
29+
</body></html>"
30+
`;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2+
3+
exports[`ConfigTestCases embedded everything everything should compile 1`] = `
4+
Object {
5+
"assetPage": "<!doctype html><html lang=en><head><title>asset</title><style>.assetStyle{background:url(\\"data:image/svg+xml,<svg> <rect fill='red' /> </svg>\\");color:red}</style><script>var assetScript=1;function f(){return assetScript}</script><script type=application/json>{\\"assetJson\\":1}</script></head><body>
6+
<p style=color:#0f0>hi</p>
7+
<svg viewBox=\\"0 0 2 2\\"> <rect fill=red /> </svg>
8+
<iframe srcdoc=\\"<style>.assetSrcdoc{color:#00f}</style>\\"></iframe>
9+
10+
</body></html>",
11+
"entryPage": "<!doctype html><html lang=en><head><title>entry</title><style>.entryStyle{background:url(\\"data:image/svg+xml,<svg> <rect fill='red' /> </svg>\\");color:red}</style><script src=__html_2a0885fc_0.js></script><script type=application/json>{\\"entryJson\\":1}</script></head><body>
12+
<p style=color:#0f0>hi</p>
13+
<svg viewBox=\\"0 0 2 2\\"> <rect fill=red /> </svg>
14+
<iframe srcdoc=\\"<style>.entrySrcdoc{color:#00f}</style>\\"></iframe>
15+
16+
</body></html>",
17+
"sheet": "@import url(data:text/css,.imported{color:red});.svg{background:url(\\"data:image/svg+xml,<svg> <rect fill='red' /> </svg>\\")}.html{background:url(\\"data:text/html,<div> <p>hi</p> </div>\\")}.json{background:url('data:application/json,{\\"a\\":1}')}.javascript{background:url(data:text/javascript,var\\\\ a=1;)}.svgBase64{background:url(data:image/svg+xml;base64,PHN2Zz4gPHJlY3QgZmlsbD0ncmVkJyAvPiA8L3N2Zz4=)}.cssBase64{background:url(data:text/css;base64,LmlubmVye2NvbG9yOnJlZH0=)}.png{background:url(data:image/png;base64,AAAA)}.sheet_asset_css{color:red;margin:10px}",
18+
}
19+
`;
20+
21+
exports[`ConfigTestCases embedded everything exported tests minifies a \`data:\` payload of every language a media type names 1`] = `"@import url(data:text/css,.imported{color:red});.svg{background:url(\\"data:image/svg+xml,<svg> <rect fill='red' /> </svg>\\")}.html{background:url(\\"data:text/html,<div> <p>hi</p> </div>\\")}.json{background:url('data:application/json,{\\"a\\":1}')}.javascript{background:url(data:text/javascript,var\\\\ a=1;)}.svgBase64{background:url(data:image/svg+xml;base64,PHN2Zz4gPHJlY3QgZmlsbD0ncmVkJyAvPiA8L3N2Zz4=)}.cssBase64{background:url(data:text/css;base64,LmlubmVye2NvbG9yOnJlZH0=)}.png{background:url(data:image/png;base64,AAAA)}.sheet_text_css{color:red;margin:10px}"`;
22+
23+
exports[`ConfigTestCases embedded everything exported tests minifies every body a document nests 1`] = `
24+
"<!doctype html><html lang=en><head><title>module</title><style>.moduleStyle{background:url(\\"data:image/svg+xml,<svg> <rect fill='red' /> </svg>\\");color:red}</style><script src=__html_fc45afb8_0.js></script><script type=application/json>{\\"moduleJson\\":1}</script></head><body>
25+
<p style=color:#0f0>hi</p>
26+
<svg viewBox=\\"0 0 2 2\\"> <rect fill=red /> </svg>
27+
<iframe srcdoc=\\"<style>.moduleSrcdoc{color:#00f}</style>\\"></iframe>
28+
29+
</body></html>"
30+
`;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import sheetObject from "./sheet-sheet.css";
2+
import "./sheet-style.css";
3+
4+
it("hands every export type that embeds a stylesheet the minified text", () => {
5+
expect(typeof sheetObject).toBe("object");
6+
});
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import inlineCss from "./inline.css";
2+
import inlineHtml from "./inline.html";
3+
import inlineJs from "./inline.js";
4+
import inlineJson from "./inline.json";
5+
import inlineSvg from "./inline.svg";
6+
import modulePage from "./page-module.html";
7+
import sheetText from "./sheet-text.css";
8+
import sourceCss from "./source.css";
9+
import sourceHtml from "./source.html";
10+
import sourceJs from "./source.js";
11+
import sourceJson from "./source.json";
12+
import sourceSvg from "./source.svg";
13+
import "./page-asset.html";
14+
import "./sheet-asset.css";
15+
16+
it("minifies every body a document nests", () => {
17+
expect(modulePage).toMatchSnapshot();
18+
// An inline `<style>`, a `style` attribute, a JSON `<script>`, an `<svg>`
19+
// subtree, and the document an `<iframe srcdoc>` holds.
20+
expect(modulePage).toContain("<style>.moduleStyle{");
21+
expect(modulePage).toContain("<p style=color:#0f0>hi</p>");
22+
expect(modulePage).toContain(
23+
'<script type=application/json>{"moduleJson":1}</script>'
24+
);
25+
expect(modulePage).toContain(
26+
'<svg viewBox="0 0 2 2"> <rect fill=red /> </svg>'
27+
);
28+
expect(modulePage).toContain(
29+
'<iframe srcdoc="<style>.moduleSrcdoc{color:#00f}</style>">'
30+
);
31+
// Two deep: the SVG in the `<style>`'s own `data:` url.
32+
expect(modulePage).toContain(
33+
"url(\"data:image/svg+xml,<svg> <rect fill='red' /> </svg>\")"
34+
);
35+
// A document webpack parses extracts its inline `<script>` into a chunk, so
36+
// only the emitted-only document reaches a JavaScript minimizer inline.
37+
expect(modulePage).toContain("<script src=");
38+
});
39+
40+
it("minifies a `data:` payload of every language a media type names", () => {
41+
expect(sheetText).toMatchSnapshot();
42+
expect(sheetText).toContain("@import url(data:text/css,.imported{color:red})");
43+
expect(sheetText).toContain(
44+
"url(\"data:image/svg+xml,<svg> <rect fill='red' /> </svg>\")"
45+
);
46+
expect(sheetText).toContain(
47+
'url("data:text/html,<div> <p>hi</p> </div>")'
48+
);
49+
expect(sheetText).toContain('url(\'data:application/json,{"a":1}\')');
50+
expect(sheetText).toContain("url(data:text/javascript,var\\ a=1;)");
51+
// Reached through base64 it stays base64: re-encoding it as text would be a
52+
// different url.
53+
expect(sheetText).toContain(
54+
"url(data:image/svg+xml;base64,PHN2Zz4gPHJlY3QgZmlsbD0ncmVkJyAvPiA8L3N2Zz4=)"
55+
);
56+
expect(sheetText).toContain(
57+
"url(data:text/css;base64,LmlubmVye2NvbG9yOnJlZH0=)"
58+
);
59+
// A media type naming no language webpack knows keeps its payload.
60+
expect(sheetText).toContain("url(data:image/png;base64,AAAA)");
61+
// And the sheet itself is minified, whatever it carries.
62+
expect(sheetText).toContain(".sheet_text_css{color:red;margin:10px}");
63+
});
64+
65+
it("minifies an `asset/source` module of every language", () => {
66+
expect(sourceCss).toBe(".sourceCss{color:red}");
67+
expect(sourceJs).toBe("var sourceJs=1;function f(){return sourceJs}");
68+
expect(sourceJson).toBe('{"sourceJson":1,"b":[1,2]}');
69+
expect(sourceSvg).toBe(
70+
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 10"> <rect width="10" height="10" /> </svg>'
71+
);
72+
// Whitespace between elements is meaningful, so only the comment goes.
73+
expect(sourceHtml).toContain("<div class=a>");
74+
expect(sourceHtml).not.toContain("dropped");
75+
});
76+
77+
it("encodes an `asset/inline` module of every language as a `data:` URI", () => {
78+
// What each one carries is decoded in test.config.js, where a base64 decoder
79+
// exists; referencing them here is also what keeps them in the bundle.
80+
expect(inlineCss).toContain("data:text/css;base64,");
81+
expect(inlineHtml).toContain("data:text/html;base64,");
82+
expect(inlineJs).toContain("data:text/javascript;base64,");
83+
expect(inlineJson).toContain("data:application/json;base64,");
84+
expect(inlineSvg).toContain("data:image/svg+xml;base64,");
85+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.inlineCss {
2+
color : #ff0000 ;
3+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<div class="a">
2+
<!-- dropped -->
3+
<p>hi</p>
4+
</div>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
var inlineJs = 1 ; function f ( ) { return inlineJs }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "inlineJson" : 1 , "b" : [ 1 , 2 ] }
Lines changed: 3 additions & 0 deletions
Loading
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!doctype html>
2+
<html lang="en"><head><title>asset</title>
3+
<style> .assetStyle { background : url("data:image/svg+xml,<svg> <rect fill='red' /> </svg>") ; color : #ff0000 } </style>
4+
<script> var assetScript = 1 ; function f ( ) { return assetScript } </script>
5+
<script type="application/json"> { "assetJson" : 1 } </script>
6+
</head><body>
7+
<p style=" color : #00ff00 ; ">hi</p>
8+
<svg viewBox="0 0 2 2"> <rect fill="red" /> </svg>
9+
<iframe srcdoc="&lt;style&gt; .assetSrcdoc { color : #0000ff } &lt;/style&gt;"></iframe>
10+
</body></html>

0 commit comments

Comments
 (0)