使用Grunt和grunt-contrib-cssmin删除注释

时间:2015-10-07 14:09:32

标签: css node.js gruntjs less grunt-contrib-cssmin

我试图使用Grunt和grunt-contrib-cssmin删除所有CSS注释,CSS文件被编译并缩小它有所有注释。

评论应该用以下行删除: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']);
};

2 个答案:

答案 0 :(得分:-1)

根据作者提供的答案类型,grunt-decomment将是一个更通用的解决方案,可以从任何文件中删除///**/等评论。

答案 1 :(得分:-2)

修复 - 我找到了一个使用grunt-strip-css-comments的解决方案,它会在文件缩小后删除所有注释:

以下更正后的代码:

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']);
};