diff options
author | 2021-06-14 14:17:56 -0400 | |
---|---|---|
committer | 2021-06-14 14:17:56 -0400 | |
commit | 73a43d93013f23ba17bf067fff64836e7f5fc2cb (patch) | |
tree | d0fc0fe39590906c11c3fc939500505e45348271 | |
parent | 3d3d4cfe0b53a133acbc116d97e669b1ce312001 (diff) | |
download | astro-73a43d93013f23ba17bf067fff64836e7f5fc2cb.tar.gz astro-73a43d93013f23ba17bf067fff64836e7f5fc2cb.tar.zst astro-73a43d93013f23ba17bf067fff64836e7f5fc2cb.zip |
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
4 files changed, 32 insertions, 1 deletions
diff --git a/.changeset/silent-rocks-relax.md b/.changeset/silent-rocks-relax.md new file mode 100644 index 000000000..f32eb4f00 --- /dev/null +++ b/.changeset/silent-rocks-relax.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Prevent dev from locking up on empty selectors diff --git a/packages/astro/src/compiler/transform/postcss-scoped-styles/index.ts b/packages/astro/src/compiler/transform/postcss-scoped-styles/index.ts index dd885ffb2..a10eba77a 100644 --- a/packages/astro/src/compiler/transform/postcss-scoped-styles/index.ts +++ b/packages/astro/src/compiler/transform/postcss-scoped-styles/index.ts @@ -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); + } }, }; } diff --git a/packages/astro/test/astro-basic.test.js b/packages/astro/test/astro-basic.test.js index 29346f043..a93e5bac0 100644 --- a/packages/astro/test/astro-basic.test.js +++ b/packages/astro/test/astro-basic.test.js @@ -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(); diff --git a/packages/astro/test/fixtures/astro-basic/src/pages/empty-class.astro b/packages/astro/test/fixtures/astro-basic/src/pages/empty-class.astro new file mode 100644 index 000000000..1dc19c7a0 --- /dev/null +++ b/packages/astro/test/fixtures/astro-basic/src/pages/empty-class.astro @@ -0,0 +1,15 @@ +--- + +--- +<html> +<head> + <title>Testing</title> + <style> + .author { + } + </style> +</head> +<body> + <div class="author"></div> +</body> +</html>
\ No newline at end of file |