Skip to content

Commit cadf7c2

Browse files
committed
feat: initial release
0 parents  commit cadf7c2

16 files changed

+7580
-0
lines changed

.editorconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# editorconfig.org
2+
3+
root = true
4+
5+
[*]
6+
indent_style = space
7+
indent_size = 2
8+
end_of_line = lf
9+
charset = utf-8
10+
trim_trailing_whitespace = true
11+
insert_final_newline = true

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/node_modules
2+
/coverage
3+
/.nyc_output
4+
**/*.js
5+
**/*.map
6+
**/*.d.ts

.npmignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/node_modules
2+
/coverage
3+
/.nyc_output
4+
.*
5+
/test
6+
*.json

.prettierrc.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"singleQuote": true
3+
}

LICENSE.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright 2018 Elizabeth Mitchell
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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+
```

index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './src/minifyHTMLLiterals';
2+
export * from './src/strategy';

jasmine.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"spec_files": ["**/*spec.js"],
3+
"stopSpecOnExpectationFailure": false,
4+
"random": true
5+
}

0 commit comments

Comments
 (0)