找到你要的答案

Q:Remove comments using Grunt and grunt-contrib-cssmin

Q:拆下用咕噜声和咕噜有谷值的评论

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

我试图删除所有与使用咕噜咕噜谷值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']);
};
answer1: 回答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.

以下的答案,作者提供的类型,咕噜decomment将会是一个更通用的解决方案,它可以消除评论/ / / * * /从任何文件。

answer2: 回答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']);
};

固定-我已经找到了解决方案采用CSS咕噜条评论,删除所有的评论后,文件被压缩:

修正后的代码如下:

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']);
};
css  node.js  gruntjs  less  grunt-contrib-cssmin