Skip to content

Fix two broken mixin examples in README - #179

Merged
ai merged 1 commit into
postcss:mainfrom
wahidrizka:fix-readme-mixin-examples
Oct 1, 2026
Merged

ai merged 1 commit into
postcss:mainfrom
wahidrizka:fix-readme-mixin-examples

Conversation

@wahidrizka

Copy link
Copy Markdown
Contributor

Two examples in the README do not work as shown:

  • The icons function mixin calls mixin.replaceWith(rule) for every file. After the first call the @mixin node is no longer in the tree, so the next calls do nothing and only the first icon ends up in the output (.icon.icon-back, without .icon.icon-secret). This was reported in Cannot read property 'length' of undefined #67, where the fix was mixin.parent.insertBefore(mixin, rule), but the README kept the old loop. It now uses mixin.before(rule), which does the same, and postcss-mixins removes the @mixin node afterwards.
  • The JS version of the @mixin-content example, added in chore: Add @mixin-context example #81, is a syntax error (SyntaxError: Unexpected token ':'), and it also had no .isIE & selector and was named isIe while the usage below calls isIE. It now returns { '.isIE &': { '@mixin-content': {} } }, the same shape as in the test "does not throw error on brackets in at-rules inside function mixins".

I ran the CSS and JS examples of the README (all but the two with … placeholders) with postcss-mixins 12.1.2, whose index.js is the same as on main, and postcss-nested 8.0.1. With this change, icons (with readdirSync returning back.svg and secret.svg) gives exactly the output shown below it, and the JS isIE mixin gives the same .foo and .isIE .foo output as the CSS one. Without the .isIE & selector the content stays in .foo, and with the old name it fails with Undefined mixin isIE.

The icons function mixin called mixin.replaceWith() for every file,
so only the first rule was inserted: replaceWith() does nothing once
the node has left the tree. Insert each rule with mixin.before().

The JS version of the @mixin-content example was a syntax error, had
no .isIE & selector, and was named isIe while the CSS calls isIE.
Return the same rule the CSS mixin defines.
@ai
ai merged commit 3a5c50c into postcss:main Oct 1, 2026
@ai

ai commented Oct 1, 2026

Copy link
Copy Markdown
Member

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants