|
| 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 | +}); |
0 commit comments