forked from rehypejs/rehype-minify
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
64 lines (56 loc) · 1.56 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* rehype plugin to minify media attributes.
*
* ## What is this?
*
* This package is a plugin that can minify the contents of media attributes on
* `<link>`, `<source>`, and `<style>` elements.
*
* ## When should I use this?
*
* You can use this plugin when you want to improve the size of HTML documents.
*
* ## API
*
* ### `unified().use(rehypeMinifyMediaAttribute)`
*
* Minify media attributes.
* There are no options.
*
* @example
* <link rel="stylesheet" media="only screen and (min-width: 320px)" href="index.css">
* <link rel="stylesheet" media="all" href="index.css">
*/
/**
* @typedef {import('hast').Root} Root
*/
import CleanCSS from 'clean-css'
import {visit} from 'unist-util-visit'
import {isElement} from 'hast-util-is-element'
const clean = new CleanCSS()
const prefix = '@media '
const suffix = '{i{color:red}}'
/**
* Minify media attributes.
*
* @type {import('unified').Plugin<Array<void>, Root>}
*/
export default function rehypeMinifyMediaAttribute() {
return (tree) => {
visit(tree, 'element', (node) => {
const props = node.properties
if (props && isElement(node, ['link', 'source', 'style'])) {
let value = props.media
if (typeof value === 'string') {
try {
const output = clean.minify(prefix + value + suffix)
value = output.styles.slice(prefix.length, -suffix.length)
// Potential third party errors?
/* c8 ignore next */
} catch {}
props.media = value === 'all' || !value ? null : value
}
}
})
}
}