feat: add Prettier plugin for Bootstrap class ordering#42359
feat: add Prettier plugin for Bootstrap class ordering#42359pierluigilenoci wants to merge 1 commit intotwbs:mainfrom
Conversation
Add prettier-plugin-bootstrap, a Prettier v3+ plugin that automatically sorts Bootstrap CSS classes following the framework's recommended order. The plugin sorts classes into categories that mirror Bootstrap's architecture: layout → components → helpers → utilities. Within the utilities category, classes follow the key order of the $utilities Sass map defined in scss/_utilities.scss. Responsive variants (e.g. d-md-flex) sort immediately after their base class. Supports HTML, JSX/TSX, Vue, Angular, and Astro parsers. Closes twbs#38397 Signed-off-by: Pierluigi Lenoci <pierluigilenoci@gmail.com>
|
neat project! I'm not a decision-maker on this at all, but just a thought - would it be better to have this as a separate project on GitHub and published to npm and then it could be used as a dev dependency on different projects (including this one). I think I'd be interested in using it if it was on npm! |
|
Thanks @coliff! That's a great suggestion — a standalone npm package would make it reusable across projects beyond Bootstrap. I'll look into extracting it to its own repo with proper test coverage and documentation. Would you be interested in collaborating on defining the plugin's API/options? |
|
Really cool initiative @pierluigilenoci 🙌 As mentioned by @coliff (#42359 (comment)), this would be better as a standalone tool so it can be reused across different projects. For example, our Stylelint config lives in a separate repository (https://github.com/twbs/stylelint-config-twbs-bootstrap/) for that exact reason, as it’s useful beyond just this repo. The second point is maintenance. To be transparent, maintaining the library is already quite demanding, and introducing an additional tool here would increase that burden further. If you’re able to move forward with a standalone version, feel free to ping us, we’d be happy to evaluate if and how it could be integrated. Closing this PR for now. |
|
Following the feedback from @coliff and @julien-deramond, I've extracted the plugin into a standalone npm package: 📦 npm: I've also opened #42383 to integrate it into the Bootstrap docs site as a dev dependency. |
Summary
Adds
prettier-plugin-bootstrap, a Prettier v3+ plugin that automatically sorts Bootstrap CSS classes following the framework's recommended order — similar to what prettier-plugin-tailwindcss provides for Tailwind CSS.Closes #38397
How it works
The plugin hooks into Prettier's existing parsers (HTML, JSX, Vue, Angular, Astro) and re-orders
class/classNameattribute values according to Bootstrap's architecture:container,row,col-*btn,card,modal,navbar, etc. (followingbootstrap.scssimport order)clearfix,visually-hidden,stretched-link, etc.$utilitiesmap inscss/_utilities.scssResponsive variants (e.g.
d-md-flex) sort immediately after their base class (d-flex). Unknown/custom classes are pushed to the end, preserving their original relative order.Before
After
What's included
prettier-plugin-bootstrap/src/class-order.mjs— canonical Bootstrap class ordering (derived frombootstrap.scssimport stack +$utilitiesmap)prettier-plugin-bootstrap/src/index.mjs— Prettier plugin entry point (parser wrappers for HTML, JSX, Vue, Angular, Astro)prettier-plugin-bootstrap/tests/— 24 unit + integration tests using Node.js built-in test runnerbootstrapAttributes(additional attributes to sort) andbootstrapFunctions(function calls containing class lists)Design decisions
.mjsfiles, no TypeScript/bundler neededKnown limitations / future work
bootstrapFunctionsoption (for sorting insideclsx(),classNames()etc.) is declared but the JSX/TS parser integration for function arguments is not yet fully wired up — this can be added incrementally$utilitiesSass map directlyclass={`btn ${variant}`})Test plan
cd prettier-plugin-bootstrap && node --test tests/*.test.mjsnpx prettier --plugin=./prettier-plugin-bootstrap/src/index.mjs --write test.html