|
| 1 | +# minify-html-literals |
| 2 | + |
| 3 | +_Minify HTML markup inside JavaScript template literal strings._ |
| 4 | + |
| 5 | +## Why? |
| 6 | + |
| 7 | +Template literals are often used in JavaScript to write HTML and CSS markup (ex. [lit-html](https://www.npmjs.com/package/lit-html)). This library allows a developer to minify markup that is normally ignored by JavaScript minifiers. |
| 8 | + |
| 9 | +## Usage |
| 10 | + |
| 11 | +```js |
| 12 | +import { minifyHTMLLiterals } from 'minify-html-literals'; |
| 13 | +// const minifyHTMLLiterals = require('minify-html-literals').minifyHTMLLiterals |
| 14 | + |
| 15 | +const result = minifyHTMLLiterals( |
| 16 | + `function render(title, items) { |
| 17 | + return html\` |
| 18 | + <style> |
| 19 | + .heading { |
| 20 | + color: blue; |
| 21 | + } |
| 22 | + </style> |
| 23 | + <h1 class="heading">\${title}</h1> |
| 24 | + <ul> |
| 25 | + \${items.map(item => { |
| 26 | + return getHTML()\` |
| 27 | + <li>\${item}</li> |
| 28 | + \`; |
| 29 | + })} |
| 30 | + </ul> |
| 31 | + \`; |
| 32 | + }`, |
| 33 | + { |
| 34 | + fileName: 'render.js' |
| 35 | + } |
| 36 | +); |
| 37 | + |
| 38 | +console.log(result.code); |
| 39 | +// function render(title, items) { |
| 40 | +// return html`<style>.heading{color:#00f}</style><h1 class=heading>${title}</h1><ul>${items.map(item => { |
| 41 | +// return getHTML()`<li>${item}</li>`; |
| 42 | +// })}</ul>`; |
| 43 | +// } |
| 44 | + |
| 45 | +console.log(result.map); |
| 46 | +// { |
| 47 | +// "version": 3, |
| 48 | +// "file": "render.js.map", |
| 49 | +// "sources": ["render.js"], |
| 50 | +// "sourcesContent": [null], |
| 51 | +// "names": [], |
| 52 | +// "mappings": "AAAA;gBACgB,qDAMU,QAAQ,SAE1B;2BACmB,IACX,OAAO,KACb;WACC,KAEP;" |
| 53 | +// } |
| 54 | +``` |
| 55 | + |
| 56 | +### ES5 Transpiling Warning |
| 57 | + |
| 58 | +Be sure to minify template literals _before_ transpiling to ES5. Otherwise, the API will not be able to find any template literal (`` `${}` ``) strings. |
| 59 | + |
| 60 | +## Supported Source Syntax |
| 61 | + |
| 62 | +- JavaScript |
| 63 | +- TypeScript |
| 64 | + |
| 65 | +## Options |
| 66 | + |
| 67 | +### Basic |
| 68 | + |
| 69 | +The following options are common to typical use cases. |
| 70 | + |
| 71 | +| Property | Type | Default | Description | |
| 72 | +| ---------------- | -------------------------------------------------------------------------------------------- | ---------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 73 | +| `fileName` | string | | _Required._ The name of the file, used for syntax parsing and source maps. | |
| 74 | +| `minifyOptions?` | [html-minifier options](https://www.npmjs.com/package/html-minifier#options-quick-reference) | `defaultMinifyOptions` | Defaults to production-ready minification. | |
| 75 | +| `shouldMinify?` | function | `defaultShouldMinify` | A function that determines whether or not a template should be minified. Defaults to minify all tagged templates whose tag name contains "html" (case insensitive). | |
| 76 | + |
| 77 | +### Advanced |
| 78 | + |
| 79 | +All aspects of the API are exposed and customizable. The following options will not typically be used unless you need to change how a certain aspect of the API handles a use case. |
| 80 | + |
| 81 | +| Property | Type | Default | Description | |
| 82 | +| ----------------------- | ------------------------------------------------------------------------- | -------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 83 | +| `generateSourceMap?` | boolean or `(ms: MagicString, fileName: string) => SourceMap | undefined` | `defaultGenerateSourceMap` | Set to `false` to disable source maps, or a custom function to control how source maps are generated from a `MagicString` instance. | |
| 84 | +| `strategy?` | object | `defaultStrategy` | An object with methods defining how to minify HTML. The default strategy uses [html-minifier](https://www.npmjs.com/package/html-minifier). | |
| 85 | +| `validate?` | boolean or object | `defaultValidation` | Set to `false` to disable strategy validation checks, or to a custom set of validation functions. This is only useful when implementing a custom strategy. | |
| 86 | +| `parseLiterals?` | function | [parse-literals](https://www.npmjs.com/package/parse-literals) | Override the function used to parse template literals from a source string. | |
| 87 | +| `parseLiteralsOptions?` | object | | Additional options to pass to `parseLiterals()` | |
| 88 | +| `MagicString?` | function | [MagicString](https://www.npmjs.com/package/magic-string) | Override the MagicString-like constructor to use for manipulating the source string and generating source maps. | |
| 89 | + |
| 90 | +## Customization Examples |
| 91 | + |
| 92 | +### Do not minify CSS |
| 93 | + |
| 94 | +```js |
| 95 | +import { minifyHTMLLiterals, defaultMinifyOptions } from 'minify-html-literals'; |
| 96 | + |
| 97 | +minifyHTMLLiterals(source, { |
| 98 | + fileName: 'render.js', |
| 99 | + minifyOptions: { |
| 100 | + ...defaultMinifyOptions, |
| 101 | + minifyCSS: false |
| 102 | + } |
| 103 | +}); |
| 104 | +``` |
| 105 | + |
| 106 | +### Minify non-tagged templates |
| 107 | + |
| 108 | +```js |
| 109 | +import { minifyHTMLLiterals, defaultShouldMinify } from 'minify-html-literals'; |
| 110 | + |
| 111 | +minifyHTMLLiterals( |
| 112 | + `function render() { |
| 113 | + return html\` |
| 114 | + <h1>This tagged template is minified</h1> |
| 115 | + \${\` |
| 116 | + <div>and so is this non-tagged template</div> |
| 117 | + \`} |
| 118 | + \`; |
| 119 | + }`, |
| 120 | + { |
| 121 | + fileName: 'render.js', |
| 122 | + shouldMinify(template) { |
| 123 | + return ( |
| 124 | + defaultShouldMinify(template) || |
| 125 | + template.parts.some(part => { |
| 126 | + return part.text.includes('<div>'); |
| 127 | + }) |
| 128 | + ); |
| 129 | + } |
| 130 | + } |
| 131 | +); |
| 132 | +``` |
| 133 | + |
| 134 | +### Modify generated SourceMap |
| 135 | + |
| 136 | +```js |
| 137 | +minifyHTMLLiterals(source, { |
| 138 | + fileName: 'render.js', |
| 139 | + generateSourceMap(ms, fileName) { |
| 140 | + return ms.generateMap({ |
| 141 | + file: `${fileName}-converted.map`, // change file name |
| 142 | + source: fileName, |
| 143 | + includeContent: true // include source contents |
| 144 | + }); |
| 145 | + } |
| 146 | +}); |
| 147 | +``` |
0 commit comments