2

I client of mine asked to have a lot of comments in the code - including css files.

So I thought about using CSScomb to reorganize the css code and add comments automatically. It doesn't make sense but why not.

So the idea would be to change the config file which states:

[
    "font",
    "font-family"
],
[
    "padding",
    "margin"
]
...

but get an output that writes:

/* FONT STYLE */
font: ....;
font-family: ....;

Any ideas ?

1 Answer 1

1

DIY Group Comments

You can add this functionality yourself by editing the sort-order.js file in the CSScomb package.

  1. With Sublime Text open, from the menu choose Preferences > Browse Packages…; This will open the Packages folder.
  2. From the Packages folder, navigate to CSScomb/node_modules/csscomb/lib/options/sort-order.js.
  3. Create a copy of this file for retrieval in case you want to revert your changes. I gave my copy a name of sort-order-original.js.
    Create a copy of this file in a different directory for retrieval in case you want to revert your changes. I moved my copy up one level into a new directory ../originals/options/sort-order.js.
    Note: If you simply rename the copy in the existing directory it may get included and run by the module; so, it is safest to just move it into a new location.
  4. Open sort-order.js in Sublime Text for editing.
  5. Consult this diff for the necessary changes to be made.
    Consult this diff for the necessary changes to be made. (This latest version adds new logic to prevent the group comments from being duplicated when running CSScomb multiple times.)
  6. If you feel comfortable with these changes, copy & paste them in their entirety to replace the contents of sort-order.js. You may choose to edit further to suit your needs. Save.
    Note: Essentially, these changes are extending each sorted object with an additional property that contains a CSS comment optionally provided by you in your User Settings. I will show you where to add the comments in the next step.
  7. Now, you're ready to add comments by group. From the Sublime Text menu, choose Preferences > Package Settings > CSScomb > Settings – User. If you haven't already configured your own settings, you can get started by copying the contents of the Settings – Default into Settings – User.
  8. In the user settings file, find the "sort-order" property. It is either an array of property names or an array of arrays of property names. By default, CSScomb will add a blank line between the nested array groups, but we've modified the file that does this.
  9. You may now optionally add a comment as the first property of any nested array. The sort-order.js file will detect this and output it at the top of the group. If no comment is defined, the default blank line is output instead.

Example User Settings:

"sort-order": [
    [
        "/* LAYOUT */",
        "position",
        "z-index",
        "top",
        "right",
        "bottom",
        "left"
    ],
    [
        "/* DISPLAY */",
        "display",
        …
        "flex-align"
    ],
    [
        "/* TYPOGRAPHY */",
        "font",
        …
        "line-height"
    ],
    [
        …
    ]
]

Before running CSScomb:

.selector {
    font-family: Arial;
    line-height: 1;
    position: relative;
    display: block;
    background-color: red;
}

After running CSScomb:

.selector {

    /* LAYOUT */
    position: relative;

    /* DISPLAY */
    display: block;

    /* TYPOGRAPHY */
    font-family: Arial;
    line-height: 1;

    background-color: red;
}
Sign up to request clarification or add additional context in comments.

8 Comments

Very nice, thank you so much ! One last problem: if I launch it twice... it will add these comments a second time... any solution for that ?
Not in this implementation. But you could play around with sort-order.js or another lib file to see about adding some sort of logic check for duplicate comments.
Ok I've been trying to find a self-made solution for 2 days and I'm stuck. Meaning that your solution can only be applied one time at the end of the compilation - so my whole idea (not your solution) becomes useless...
I'll post a solution for you. ;) stand by.
@YannickHelmut I have updated my answer. See the latest diff which adds new logic to prevent the group comments from being duplicated when running CSScomb multiple times. Enjoy!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.