Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: removing unused css #853

Merged
merged 1 commit into from
Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/transformers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ exports.process = async (html, config) => {
html = await inline(html, config)
html = await shorthandInlineCSS(html, config)
html = await removeUnusedCSS(html, config)
html = await removeInlinedClasses(html, config)
html = await removeInlineSizes(html, config)
html = await removeInlineBgColor(html, config)
html = await removeAttributes(html, config)
Expand All @@ -54,7 +53,7 @@ exports.addURLParams = (html, config) => addURLParams(html, config, true)
exports.preventWidows = (html, config) => preventWidows(html, config)
exports.replaceStrings = (html, config) => replaceStrings(html, config, true)
exports.safeClassNames = (html, config) => safeClassNames(html, config, true)
exports.removeUnusedCSS = (html, config) => removeUnusedCSS(html, config, true)
exports.removeUnusedCSS = (html, config) => removeUnusedCSS(html, config)
exports.removeAttributes = (html, config) => removeAttributes(html, config, true)
exports.attributeToStyle = (html, config) => attributeToStyle(html, config, true)
exports.removeInlineSizes = (html, config) => removeInlineSizes(html, config, true)
Expand Down
37 changes: 20 additions & 17 deletions src/transformers/removeInlinedSelectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,26 @@ const plugin = posthtmlOptions => tree => {
// For each style tag...
if (node.tag === 'style') {
const {root} = postcss().process(node.content)
const preservedClasses = []

root.walkRules(rule => {
// Skip media queries and such...
if (rule.parent.type === 'atrule') {
return
// Preserve selectors in at rules
root.walkAtRules(rule => {
if (['media', 'supports'].includes(rule.name)) {
rule.walkRules(rule => {
preservedClasses.push(rule.selector)
})
}
})

root.walkRules(rule => {
const {selector} = rule
const prop = get(rule.nodes[0], 'prop')

// Preserve pseudo selectors
if (selector.includes(':')) {
preservedClasses.push(selector)
}

try {
// If we find the selector in the HTML...
tree.match(matchHelper(selector), n => {
Expand All @@ -39,23 +49,16 @@ const plugin = posthtmlOptions => tree => {

// 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 class as long as it's not a preserved class
if (!preservedClasses.some(item => item.endsWith(selector) || item.startsWith(selector))) {
remove(classAttr, classToRemove => selector.includes(classToRemove))
}

// Remove the rule in the <style> tag
rule.remove()
}

/**
* 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)) {
if (rule.parent.type !== 'atrule') {
rule.remove()
remove(classAttr, s => selector.includes(s))
}
})
}

n.attrs = parsedAttrs.compose()

Expand Down
14 changes: 6 additions & 8 deletions src/transformers/removeUnusedCss.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
const {comb} = require('email-comb')
const {get, merge} = require('lodash')
const removeInlinedClasses = require('./removeInlinedSelectors')

module.exports = async (html, config = {}, direct = false) => {
module.exports = async (html, config = {}) => {
// If it's explicitly disabled, return the HTML
if (get(config, 'removeUnusedCSS') === false) {
return html
}

if (!direct && !get(config, 'removeUnusedCSS')) {
return html
}

const safelist = [
'*body*', // Gmail
'.gmail*', // Gmail
Expand All @@ -36,9 +34,9 @@ module.exports = async (html, config = {}, direct = false) => {
whitelist: [...get(config, 'whitelist', []), ...safelist]
}

const options = typeof config === 'boolean' && config ?
defaultOptions :
merge(defaultOptions, get(config, 'removeUnusedCSS', config))
const options = merge(defaultOptions, get(config, 'removeUnusedCSS', config))

html = await removeInlinedClasses(html, options)

return comb(html, options).result
}
2 changes: 2 additions & 0 deletions test/expected/components/kitchen-sink.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<!DOCTYPE html>
<html lang="en" xmlns:v="urn:schemas-microsoft-com:vml">
<head>
<style>
</style>
</head>
<body class="bg-red-500">
<h1>H1 in fill:template</h1>
Expand Down
6 changes: 4 additions & 2 deletions test/test-posthtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ test('components', async t => {
components: {
folders: ['test/stubs/layouts', 'test/stubs/components']
}
}
},
removeUnusedCSS: false
},
beforeRender(html, config) {
config.foo = 'bar'
Expand Down Expand Up @@ -89,7 +90,8 @@ test('components (backwards compatibility)', async t => {
delimiters: ['[[', ']]']
}
}
}
},
removeUnusedCSS: false
}
}

Expand Down
72 changes: 20 additions & 52 deletions test/test-transformers.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,11 @@ test('remove unused CSS', async t => {
<html>
<head>
<style>
@media (screen) {
.ignore {color: yellow}
}
.foo {color: red}
.foo:hover {color: blue}
.bar-baz {color: blue}
.baz {color: white}
</style>
Expand All @@ -172,70 +176,38 @@ test('remove unused CSS', async t => {
</body>
</html>`

const enabledResult = `<!DOCTYPE html>
const withOptionsResult = `<!DOCTYPE html>
<html>
<head>
<style>
.foo {color: red}
</style>
.foo:hover {color: blue}
.bar-baz {color: blue}</style>
</head>
<body>
<div class="foo {{ test }}">test div with some text</div>
</body>
</html>`

const withOptionsResult = `<!DOCTYPE html>
const unsetResult = `<!DOCTYPE html>
<html>
<head>
<style>
.foo {color: red}
.bar-baz {color: blue}
</style>
.foo:hover {color: blue}</style>
</head>
<body>
<div class="foo {{ test }}">test div with some text</div>
</body>
</html>`

const enabled = await Maizzle.removeUnusedCSS(html)
const disabled = await Maizzle.removeUnusedCSS(html, {removeUnusedCSS: false})
const withOptions = await Maizzle.removeUnusedCSS(html, {whitelist: ['.bar*']})
const unset = await Maizzle.removeUnusedCSS(html)

t.is(enabled, enabledResult)
t.is(disabled, html)
t.is(withOptions, withOptionsResult)
})

test('remove unused CSS (disabled)', async t => {
const html = `<!DOCTYPE html>
<html>
<head>
<style>
.foo {color: red}
</style>
</head>
<body>
<div class="foo">test div with some text</div>
</body>
</html>`

const result = `<!DOCTYPE html>
<html>
<head>
<style>
.foo {color: red}
</style>
</head>
<body>
<div class="foo">test div with some text</div>
</body>
</html>`

const disabled = await Maizzle.removeUnusedCSS(html, {removeUnusedCSS: false})
const unset = await Maizzle.removeUnusedCSS(html)

t.is(disabled, result)
t.is(unset, result)
t.is(unset, unsetResult)
})

test('remove attributes', async t => {
Expand Down Expand Up @@ -539,13 +511,9 @@ test('remove inlined selectors', async t => {
color: #00a8ff;
}

.m-0 {margin: 0}

.mb-4 {margin-bottom: 16px}

.mt-0 {margin-top: 0}

.remove {color: red}
.padded {
padding: 0 20px;
}

[data-ogsc] .hidden {display: none}

Expand All @@ -569,10 +537,10 @@ test('remove inlined selectors', async t => {
</style>
</head>
<body>
<div no-value id="keepId" class="remove keep ignore foo-class" style="color: red; display: inline">
<h1 class="m-0 mb-4 mt-0 hover-text-blue" style="margin: 0 0 16px;">Title</h1>
<div no-value id="keepId" class="keep ignore foo-class" style="color: red; display: inline">
<h1 class="padded hover-text-blue" style="padding: 0 20px">Title</h1>
<img src="https://example.com/image.jpg" style="border: 0; vertical-align: middle">
<div id="keepId" class="remove keep ignore" style="color: red; display: inline">text</div>
<div id="keepId" class="keep ignore" style="color: red; display: inline">text</div>
</div>
</body>
</html>`
Expand All @@ -599,10 +567,10 @@ test('remove inlined selectors', async t => {
<style>.keep {margin: 0}</style>
</head>
<body>
<div no-value id="keepId" class="keep foo-class" style="color: red; display: inline">
<h1 class="hover-text-blue" style="margin: 0 0 16px">Title</h1>
<div no-value id="keepId" class="keep ignore foo-class" style="color: red; display: inline">
<h1 class="hover-text-blue" style="padding: 0 20px">Title</h1>
<img src="https://example.com/image.jpg" style="border: 0; vertical-align: middle">
<div id="keepId" class="keep" style="color: red; display: inline">text</div>
<div id="keepId" class="keep ignore" style="color: red; display: inline">text</div>
</div>
</body>
</html>`
Expand Down