forked from hplush/slowreader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml-keeper.ts
More file actions
46 lines (44 loc) · 1.37 KB
/
html-keeper.ts
File metadata and controls
46 lines (44 loc) · 1.37 KB
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
// PostCSS plugin to avoid any CSS changes by Vite in index.html inline styles.
// We use this inline styles to show some loading state.
//
// These changes are not useful in our case. So we save CSS document before
// any other PostCSS plugin and then restore it after all other plugins.
//
// Also we remove all whitespaces.
import type { Plugin, Root } from 'postcss'
export default {
postcssPlugin: 'html-keeper',
prepare(result) {
if (result.opts.from?.includes('.html')) {
let before: Root
return {
Once(root) {
before = root.clone()
before.walkDecls(decl => {
decl.raws = { before: '', between: ':' }
decl.value = decl.value.replace(/,\s+/g, ',').replace(/0\./g, '.')
})
before.walkRules(rule => {
rule.raws = { after: '', before: '', between: '' }
})
before.walkAtRules(atrule => {
atrule.params = atrule.params
.replace(/:\s+/g, ':')
.replace(/\s\s+/g, ' ')
atrule.raws = { after: '', afterName: ' ', before: '' }
})
before.walkComments(comment => {
comment.remove()
})
},
OnceExit(root) {
root.raws = {}
root.nodes = []
root.append(before.nodes)
}
}
} else {
return {}
}
}
} satisfies Plugin