Skip to content

feat: support postcss 8#114

Merged
madyankin merged 3 commits intomadyankin:masterfrom
yyx990803:postcss-8
Nov 29, 2020
Merged

feat: support postcss 8#114
madyankin merged 3 commits intomadyankin:masterfrom
yyx990803:postcss-8

Conversation

@yyx990803
Copy link
Copy Markdown
Contributor

No description provided.

@madyankin
Copy link
Copy Markdown
Owner

Wow. that's freaking awesome, thanks! Would you mind moving the postcss package to peerDependencies?

@yyx990803
Copy link
Copy Markdown
Contributor Author

done!

@madyankin
Copy link
Copy Markdown
Owner

Thanks. I think I can make a release tomorrow

@ludofischer
Copy link
Copy Markdown
Contributor

I suspect this code might require refactoring to avoid issues using postcss-modules together with some other PostCSS 8 plugins. You can get the test suite to fail like this:

  • in tests/test.js switch the test plugin from Once() to Root(): ludofischer@00b2df8
  • three tests fail: only calls plugins once when it preserves comments, only calls plugins once when it composes rules, only calls plugins once when it processes values

I think the only calls plugins once when it preserves comments failure might be serious as the expected lines seem to disappear completely, while the other two failures might just show a different execution order with no impact on the generated CSS.

In https://github.com/ludofischer/postcss-strange-cases there's another reproduction where other plugins do not run correctly with PostCSS 8, if a plugin creates anotherpostcss() instance inside a visitor (just like postcss-modules).
You probably can't detetct the issue if you only test with autoprefixer since it uses Once().

@yyx990803
Copy link
Copy Markdown
Contributor Author

I don't think it's possible for this plugin to ensure compatibility with all plugins out there - I assume most plugins will be using Once anyway. I tested this with the plugins Vue's SFC compilers are using and they do work fine.

Either way, I think it's better to get a new version out so people can test it and then fix potential issues if there are any.

@madyankin
Copy link
Copy Markdown
Owner

@ludofischer good point, this stuff has definitely to be checked twice.

I’m going to do this later this or next week. I wouldn’t like to release something that breaks existing code if it’s possible.

@ludofischer
Copy link
Copy Markdown
Contributor

I suspect any plugins that would normally run after postcss-modules don't get called properly, because postcss-modules runs on a different tree than the other plugins.
If you need a release ASAP, the cheapest improvement might be switching to OnceExit() ludofischer@ce553b7, then other plugins will run correctly before postcss-modules 'breaks' the AST traversal at the end. You need to update two test snapshots, but I hope those are not meaningful changes, just plugin order getting swapped with the same end result.

I've had similar problems working on cssnano, so here's more detail about how I see the issue (but I haven't tested all the ideas below yet):

  1. PostCSS 7 would call plugins in order and they would perform their own AST traversal one after the other, but PostCSS 8 can also register plugins as visitors during a single AST traversal. Since postcss-modules starts its own AST traversal by calling postcss().process(), plugins that expect to be called in the main AST traversal can't 'see' the changes generated by postcss-modules. That could probably be roughly solved with OnceExit(). The 'best' solution (I haven't tried yet) might be to run the plugins in postcss-modules on the same tree as all other plugins. Can maybe be achieved by exposing the internal postcss instance directly, since a plugin can also be postcss instance (see API docs).

  2. postcss-modules seems interested in what plugins run earlier and how many times plugins get called (for example here and in tests), but in PostCSS 8 you can't guarantee any order unless everything uses Once(). But I hope we don't have to force everyone to use Once(): if we ensure the postcss-modules plugins run in the correct order all together at the end, the ordering of plugins that run before should not matter.

@yyx990803
Copy link
Copy Markdown
Contributor Author

yyx990803 commented Nov 25, 2020

I agree the best refactor would be completely overhauling this plugin to avoid the "sub processor" thing which seems hacky in the first place. But I also think first getting a version out that is usable with Postcss 8 for those who don't have conflicting plugins now would be beneficial. Mostly, this module's release is blocking @vue/compiler-sfc to release a new version supporting Postcss 8, which in turn blocks Vue users from using Tailwind 2 without duplicated versions of postcss.

P.S switched to OnceExit and updated snapshots

@madyankin madyankin merged commit ff77512 into madyankin:master Nov 29, 2020
@madyankin
Copy link
Copy Markdown
Owner

Merging, @yyx990803 thank you!

I would like to make a total refactoring of the plugin to match the new PostCSS architecture and ditch out all the deps. Hope I will have some time in the future for this.

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.

3 participants