How to ignore multi-line comments in sass?

Is there a way to make sass ignore multi-line comments when creating a css file:

// these comments are ignored 

This is not (ignored in compressed mode):

 /* * multiline comments * */ 

I found this ticket on Github where the author says:

If you really want this, you can secure Sass to disable / * * / comments.

But I donโ€™t know what it means with monkeypatch sass, so how can I do this?

+4
source share
1 answer

Hooray! I found out how a monkey corrected SASS by answering this question:

Sass mixin recursion @include loop

And now I can help too!

1) Install the compass

For this solution you will need Compass . Install it with:

 gem install compass 

2) Set up the compass

Create a compass.rb file in the root of your project and define the directories in which you save the SASS and CSS code, for example. g :.

 css_dir = "stylesheets" sass_dir = "sass" 

3) Create a monkey patch

Create a file called remove-all-comments-monkey-patch.rb in the root of the project:

 class Sass::Tree::Visitors::Perform < Sass::Tree::Visitors::Base # Removes all comments completely def visit_comment(node) return [] end end 

4) Require monkey patch from config.rb

In config.rb add:

 # Removing all comments by applying a monkey patch to SASS compiler require "./remove-all-comments-monkey-patch" 

5) Compile your project with Compass

Use compass compile to compile SASS into CSS. You can also use compass watch so that the Compass command-line tool constantly monitors your code for changes and recompiles the details you are changing.

Questions

This will not remove comments with line numbers generated by SASS. To disable them, comment out the line_comments = true in config.rb or set it to false.

To re-enable multi-line comments, just comment out the line requiring the monkey patch and do compass clean .

Do not use it! Use single-line comments with Ctrl + /.

Although this solution is portable and will work for everyone without cracking the SASS code manually, you should really consider using the IDE, which allows you to comment entire paragraphs with single-line comments using a single keystroke. For me it's Ctrl + / .

Here I shot a short video to show that using line comments is actually faster and more efficient than using multi-line comments: http://www.youtube.com/watch?feature=player_detailpage&v=DTyMAPZrwyc

Linear comments also allow you to comment on comments without breaking code.

You have the following code:

 foo /* Bla bla */ bar baz 

And you need to comment on all of this. If you wrap it all up with /* */ ...

 /*foo /* Bla bla */ bar baz*/ 

... then you broke the code! Now you have a comment starting with /*foo and ending with bla */ , as well as a syntax error in baz*/ .

Instead, just select the entire code and press Ctrl + / (assuming you use some kind of IDE or programmer notepad), everything will be commented out immediately:

 //foo // ///* Bla bla */ //bar // //baz 

And, of course, later it can be uncommented with the same hotkey.

+16
source

All Articles