使用Grunt和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']); }; 

在作者提供的答案types之后, grunt-decomment将是一个更通用的解决scheme,它可以从任何文件中删除像/**//**/这样的注释。

固定 – 我find了一个使用grunt-strip-css-comments的解决scheme,它在文件缩小后删除所有注释:

正确的代码如下:

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