mirror of
https://github.com/withastro/astro.git
synced 2025-01-27 22:19:04 -05:00
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:
parent
3d3d4cfe0b
commit
73a43d9301
4 changed files with 32 additions and 1 deletions
5
.changeset/silent-rocks-relax.md
Normal file
5
.changeset/silent-rocks-relax.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Prevent dev from locking up on empty selectors
|
|
@ -96,10 +96,14 @@ export function scopeRule(selector: string, className: string) {
|
||||||
|
|
||||||
/** PostCSS Scope plugin */
|
/** PostCSS Scope plugin */
|
||||||
export default function astroScopedStyles(options: AstroScopedOptions): Plugin {
|
export default function astroScopedStyles(options: AstroScopedOptions): Plugin {
|
||||||
|
const rulesScopedCache = new WeakSet();
|
||||||
return {
|
return {
|
||||||
postcssPlugin: '@astrojs/postcss-scoped-styles',
|
postcssPlugin: '@astrojs/postcss-scoped-styles',
|
||||||
Rule(rule) {
|
Rule(rule) {
|
||||||
rule.selector = scopeRule(rule.selector, options.className);
|
if(!rulesScopedCache.has(rule)) {
|
||||||
|
rule.selector = scopeRule(rule.selector, options.className);
|
||||||
|
rulesScopedCache.add(rule);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,4 +40,11 @@ Basics('Correctly serializes boolean attributes', async ({ runtime }) => {
|
||||||
assert.equal($('h2').attr('not-data-ok'), '');
|
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();
|
Basics.run();
|
||||||
|
|
15
packages/astro/test/fixtures/astro-basic/src/pages/empty-class.astro
vendored
Normal file
15
packages/astro/test/fixtures/astro-basic/src/pages/empty-class.astro
vendored
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
---
|
||||||
|
|
||||||
|
---
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Testing</title>
|
||||||
|
<style>
|
||||||
|
.author {
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="author"></div>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
Add table
Reference in a new issue