Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
176 lines (157 loc) · 5.5 KB

File metadata and controls

176 lines (157 loc) · 5.5 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
const selectorParser = require('postcss-selector-parser');
const DEFAULT_OPTIONS = {
addNoJs: true,
check: decl =>
/\.(jpe?g|png)(?!(\.webp|.*[&?]format=webp))/i.test(decl.value),
modules: false,
noJsClass: 'no-js',
noWebpClass: 'no-webp',
rename: oldName => oldName.replace(/\.(jpe?g|png)/gi, '.webp'),
webpClass: 'webp'
}
module.exports = (opts = {}) => {
let { addNoJs, check, modules, noJsClass, noWebpClass, rename, webpClass } = {
...DEFAULT_OPTIONS,
...opts
}
function removeHtmlPrefix(className) {
return className.replace(/html ?\./, '')
}
/**
* Process background value to remove unnecessary parts
* @param {string} value - background value
*/
function processBackgroundValue(value) {
// check for image-set
// https://developer.mozilla.org/en-US/docs/Web/CSS/image/image-set
let clearValue =
value.includes('image-set') ?
/(image-set.*\))/gm.exec(value) :
/(url.*\))/gm.exec(value)
if (!clearValue) {
return value;
}
return clearValue[0] ?? value;
}
/**
* Add .webp and .no-webp classes
* @param {Rule} rule - PostCSS Rule
* @param {string} className - class name to add
*/
function addClass(rule, className) {
let initialClassName = className
let internalNoWebpClass = noWebpClass.includes('html') ? removeHtmlPrefix(noWebpClass) : noWebpClass
if (className.includes('html')) {
className = removeHtmlPrefix(className)
}
/**
* @param {parser.Container} selectors
* @param {string} targetClassName
*/
let transformWithClass = (selectors, targetClassName) => {
selectors.each(selector => {
let isBodyFound = false
let isHtmlFound = false
// detect body & html, add class to body
selector.walkTags(currentSelector => {
if (currentSelector.value === 'body') {
isBodyFound = true
currentSelector.parent.insertAfter(currentSelector, selectorParser.className({value: targetClassName}))
}
else if (currentSelector.value === 'html') {
isHtmlFound = true
}
})
let bodyCombinator = selectorParser.combinator({value: ' '})
let bodyTag = selectorParser.tag({value: 'body'})
let bodyClass = selectorParser.className({value: targetClassName})
// html found, no body tags
if (isHtmlFound && !isBodyFound) {
let isHtmlFoundHere = false
selector.walk(currentSelector => {
if (currentSelector.value === 'html') {
isHtmlFoundHere = true
}
else if (isHtmlFoundHere && currentSelector.type === 'combinator') {
let selectorParent = currentSelector.parent
selectorParent.insertAfter(currentSelector, bodyTag)
selectorParent.insertAfter(bodyTag, bodyClass)
selectorParent.insertAfter(bodyClass, bodyCombinator)
}
})
}
else if (!isHtmlFound && !isBodyFound) {
if (selector.first.spaces.before === ' ') {
bodyTag.spaces.before = ' '
}
selector.first.spaces.before = ' '
selector.prepend(bodyClass)
selector.prepend(bodyTag)
}
})
if (addNoJs && initialClassName === noWebpClass) {
selectors.each(selector => {
let selectorClone = selector.clone()
selectorClone.walk(subSelector => {
if (subSelector.type === 'class' && subSelector.value === internalNoWebpClass) {
subSelector.value = noJsClass
}
})
selectorClone.first.spaces.before = ' '
selectors.insertAfter(selector, selectorClone)
})
}
if (modules) {
let validClass = [webpClass, internalNoWebpClass, noJsClass]
selectors.each(selector => {
selector.walk((subSelector) => {
if (subSelector.type === 'class' && validClass.includes(subSelector.value)) {
let newPseudo = selectorParser.pseudo({value: ':global(.' + subSelector.value + ')'})
subSelector.replaceWith(newPseudo)
}
})
})
}
}
let transform = selectors => {
transformWithClass(selectors, className)
}
return selectorParser(transform).processSync(rule.selector)
}
return {
Declaration(decl) {
if (check(decl)) {
let rule = decl.parent
if (rule.selector.includes(`.${removeHtmlPrefix(noWebpClass)}`)) return
// check for image-set with types & skip processing
if (decl.value.includes('image-set') && decl.value.includes(') type(')) return
let webp = rule.cloneAfter()
webp.each(i => {
if (i.prop !== decl.prop && i.value !== decl.value) i.remove()
})
webp.selector = addClass(webp, webpClass)
webp.each(i => {
if (
rename &&
Object.prototype.toString.call(rename) === '[object Function]'
) {
i.value = rename(i.value)
i.prop = 'background-image'
}
i.value = processBackgroundValue(i.value)
})
let noWebp = rule.cloneAfter()
noWebp.each(i => {
if (i.prop !== decl.prop && i.value !== decl.value) i.remove()
})
noWebp.selector = addClass(noWebp, noWebpClass)
noWebp.each(i => {
i.value = processBackgroundValue(i.value)
i.prop = 'background-image'
})
}
},
postcssPlugin: 'webp-in-css/plugin'
}
}
module.exports.postcss = true
Morty Proxy This is a proxified and sanitized view of the page, visit original site.