Refactoring and Removing Unused CSS from SASS/Less Files

How do I refactor my CSS?

Split your css into separate files.

  1. Put in one file the CSS reset (if you use one)
  2. Then create a global.css file where you will put global styles that
    apply to many-all pages
  3. Then create individual files for your individual pages

Then start styling your pages. Every time you find a style rule that is reusable on many pages make it a CSS class and put it in the global.css file. Avoid using css ID's. You will find that you more often reuse things or will reuse in the future. In this case you use of course CSS classes.

Eventually you will find out that in your global.css you will find mostly CSS classes rules and html tag rules.

In your individual page CSS files you will find specific styles for each page.

That should give you a good first level of organization in your CSS. You can try to keep this separation through the whole development process, and for releases merge the CSS files into one and minify it.

Tree shaking sass

With sass if you set the :line_comments option to true the generated output will contain the line number and source file where each rule was defined. You should get output like:

/* line 20, sass/_reset.sass */
body {
line-height: 1;
color: black;
background: white;
}

With node-sass the option is sourceComments: true.

gulp.task('styles', function() {
return gulp.src('src/sass/**/*.scss')
.pipe(sass({
style: 'expanded',
sourceComments: true
}))
.pipe(gulp.dest('path/to/file.css'))

So do something like that, then you can do:

grep '^/\* line \d*, .*\*/' path/to/file.css

and you will get output like:

path/to/file.css:/* line 20, sass/_reset.sass */

And then you'll just have to write some script to remove the files that don't appear in that list.

CSS Normalizer tool?

For finding duplicate/unused/unnecessary CSS based on your markup, you can try out WARI, or Dust-Me Selectors (as @Aaron D mentioned), or CSS Crunch. The last two are browser extensions (for Firefox and IE, respectively), which will only look at one page, while WARI is intended to look at an entire site. However, I've not had much luck getting WARI to work.

CSS Best practices when starting a new project

CSS Frameworks

For big projects, you'll likely want extra functionality on top of 'regular' css, like nesting, inheritance and mixins. Either one of these should get the job done:

  • SASS
  • xCSS
  • LESS
  • OOCSS

Performance optimization

Also, you'll want to do automatic performance optimization (concatenation, minification, and compression of source files), so take a look at:

  • Minify

or whatever suits your development platform.

Naming

Many large sites use some kind of prefix on class names to separate style classes from script classes. E.g.:

<div class="navigation dynHomepageNav">(...)</div>

Where the dyn* class is used as a selector in scripts, while the navigation class is used for styling. The benefit is you can have coders refactoring scripts without touching the design, and designers changing the templates without worrying about breaking functionality.

However, with modern Javascript frameworks and HTML5 you can do better; use semantic naming for IDs and classes, apply style using those IDs and classes, and use data-* attributes for all script hooks instead. Example:

<section class="navigation" data-hook="homepageNav">(...)</div>

Which you will style using the class identifier:

.navigation {
border: 1px dotted #9c9;
padding: 12px;
}

And script using the data hook (using James Padolsey's data selector attribute for jQuery):

$('section:data(hook="homepageNav")').fadeIn();

It may not be as concise or look as familiar as the good old use-semantic-classes-for-everything method, but it will create a neat separation of style and behavioral properties, which you'll appreciate once you have 50.000 lines of HTML and you need to revamp the design.

F# naming convention

Yes, there is confusion, because F# has morphed from OCaml to .Net over the years. Basically, the naming conventions are a "breaking change" - old code is inconsistent with new code.

However, the May 2009 CTP has settled the issue.

The Release Notes say...

Standard Library Naming Conventions

The naming conventions adopted for the F# library are as follows:

  • All .NET and F# OO code uses PascalCase according to existing .NET guidelines

  • The F# functional programming operators such as List.map are for use in F# internal implementation code. This kind of code uses camelCase for operator names

  • Underscores should not be used.

So, your question...

Class.MyFunctionName or Module.my_function_name 

The answer is

Class.MyFunctionName and Module.MyFunctionName (but see edit below!)

(applying rule 1 above).

-- Edit. Nov 2, 2019 --

The current guidelines recommend camelCase for functions at module level, so it's

Module.myFunctionName

Which then makes production code consistent with the F# libraries (eg. List.averageBy)



Related Topics



Leave a reply



Submit