From 73a43d93013f23ba17bf067fff64836e7f5fc2cb Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Mon, 14 Jun 2021 14:17:56 -0400 Subject: [PATCH] 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 --- .changeset/silent-rocks-relax.md | 5 +++++ .../transform/postcss-scoped-styles/index.ts | 6 +++++- packages/astro/test/astro-basic.test.js | 7 +++++++ .../astro-basic/src/pages/empty-class.astro | 15 +++++++++++++++ 4 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 .changeset/silent-rocks-relax.md create mode 100644 packages/astro/test/fixtures/astro-basic/src/pages/empty-class.astro 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 @@ +--- + +--- + + + Testing + + + +
+ + \ No newline at end of file