Prevent postcss from crashing when scoping class without a body (#430)

* Prevent postcss from crashing when scoping class without a body

For some reason postcss will keep running the plugin over and over on a class without a body if we modify the `selector` (this triggers it as being "dirty"). It doesn't do this for selectors with a body. But the fix was easy enough, only scope a rule once.

Closes #340

* Add a changeset
This commit is contained in:
Matthew Phillips 2021-06-14 14:17:56 -04:00 committed by GitHub
parent 3d3d4cfe0b
commit 73a43d9301
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 1 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Prevent dev from locking up on empty selectors

View file

@ -96,10 +96,14 @@ export function scopeRule(selector: string, className: string) {
/** PostCSS Scope plugin */
export default function astroScopedStyles(options: AstroScopedOptions): Plugin {
const rulesScopedCache = new WeakSet();
return {
postcssPlugin: '@astrojs/postcss-scoped-styles',
Rule(rule) {
rule.selector = scopeRule(rule.selector, options.className);
if(!rulesScopedCache.has(rule)) {
rule.selector = scopeRule(rule.selector, options.className);
rulesScopedCache.add(rule);
}
},
};
}

View file

@ -40,4 +40,11 @@ Basics('Correctly serializes boolean attributes', async ({ runtime }) => {
assert.equal($('h2').attr('not-data-ok'), '');
});
Basics('Selector with an empty body', async ({ runtime }) => {
const result = await runtime.load('/empty-class');
const html = result.contents;
const $ = doc(html);
assert.equal($('.author').length, 1, 'author class added');
});
Basics.run();

View file

@ -0,0 +1,15 @@
---
---
<html>
<head>
<title>Testing</title>
<style>
.author {
}
</style>
</head>
<body>
<div class="author"></div>
</body>
</html>