2

I'm trying to remove all CSS comments using Grunt and grunt-contrib-cssmin, the CSS file is compiled and minified it has all comments.

Comments should be removed with the line: keepSpecialComments: 0

module.exports = function(grunt) {
  require('jit-grunt')(grunt);

  grunt.initConfig({
    less: {
        development: {
            options: {
               compress: true,
               yuicompress: true,
               optimization: 2
            },
            files: {
              "css/main.css": "less/bootstrap.less" // destination file and source file
            }
        }
    },
    watch: {
        styles: {
            files: ['less/**/*.less'], // which files to watch
            tasks: ['less'],
            options: {
              nospawn: true
            }
        },
    },
    cssmin: {
        options: {
            keepSpecialComments: 0
        }
    }
  });

  grunt.loadNpmTasks('grunt-contrib-cssmin');
  grunt.registerTask('default', ['less','cssmin', 'watch']);
};
Augusto Triste
  • 461
  • 7
  • 8

2 Answers2

-1

Following the type of answer that the author provided, grunt-decomment would be a more generic solution, which can remove comments like // and /**/ from any file.

vitaly-t
  • 24,279
  • 15
  • 116
  • 138
-2

Fixed - I have found a solution using grunt-strip-css-comments, it removes all comments after the file has been minified:

The corrected code below:

module.exports = function(grunt) {
  require('jit-grunt')(grunt);
  require('load-grunt-tasks')(grunt);

  grunt.initConfig({
    less: {
        development: {
            options: {
               compress: true,
               yuicompress: true,
               optimization: 2
            },
            files: {
              "public/library/css/bootstrap.min.css": "public/library/less/bootstrap.less"
            }
        }
    },
    watch: {
        styles: {
            files: ['public/library/less/**/*.less'],
            tasks: ['less', 'stripCssComments'],
            options: {
              nospawn: true,
              livereload: 1342
            }
        },
    },
    stripCssComments: {
        dist: {
            files: {
                'public/library/css/bootstrap.min.css': 'public/library/css/bootstrap.min.css'
            },
            options: {
                preserve: false
            }
        }
    }
  });

  grunt.loadNpmTasks('grunt-contrib-watch');

  grunt.registerTask('default', ['less', 'stripCssComments', 'watch']);
};
Augusto Triste
  • 461
  • 7
  • 8
  • Don't use Less `compress` option (it's deprecated and will be removed as incopatible with certain modern CSS features). (note also `yuicompress` and `optimization` options were removed years ago and do not have any effect in current Less versions). – seven-phases-max Oct 08 '15 at 21:11