Skip to content

Commit

Permalink
fix: skip match helper error
Browse files Browse the repository at this point in the history
don't break build if matchHelper throws because it can't find the passed selector
  • Loading branch information
cossssmin committed Sep 22, 2022
1 parent 0882b90 commit d5de926
Showing 1 changed file with 26 additions and 24 deletions.
50 changes: 26 additions & 24 deletions src/transformers/removeInlinedSelectors.js
Expand Up @@ -28,36 +28,38 @@ const plugin = () => tree => {
const {selector} = rule
const prop = get(rule.nodes[0], 'prop')

// If we find the selector in the HTML...
tree.match(matchHelper(selector), n => {
const parsedAttrs = parseAttrs(n.attrs)
const classAttr = get(parsedAttrs, 'class', [])
const styleAttr = get(parsedAttrs, 'style', {})
try {
// If we find the selector in the HTML...
tree.match(matchHelper(selector), n => {
const parsedAttrs = parseAttrs(n.attrs)
const classAttr = get(parsedAttrs, 'class', [])
const styleAttr = get(parsedAttrs, 'style', {})

// If the class is in the style attribute (inlined), remove it
if (has(styleAttr, prop)) {
// Remove the class attribute
remove(classAttr, s => selector.includes(s))

// Remove the rule in the <style> tag
rule.remove()
}
// If the class is in the style attribute (inlined), remove it
if (has(styleAttr, prop)) {
// Remove the class attribute
remove(classAttr, s => selector.includes(s))

/**
* Remove from <style> selectors that were used to create shorthand declarations
* like `margin: 0 0 0 16px` (transformed with mergeLonghand when inlining).
*/
Object.keys(styleAttr).forEach(key => {
if (prop && prop.includes(key)) {
// Remove the rule in the <style> tag
rule.remove()
remove(classAttr, s => selector.includes(s))
}
})

n.attrs = parsedAttrs.compose()
/**
* Remove from <style> selectors that were used to create shorthand declarations
* like `margin: 0 0 0 16px` (transformed with mergeLonghand when inlining).
*/
Object.keys(styleAttr).forEach(key => {
if (prop && prop.includes(key)) {
rule.remove()
remove(classAttr, s => selector.includes(s))
}
})

n.attrs = parsedAttrs.compose()

return n
})
return n
})
} catch {}
})

node.content = root.toString()
Expand Down

0 comments on commit d5de926

Please sign in to comment.