【问题标题】:Remove comments using Grunt and grunt-contrib-cssmin使用 Grunt 和 grunt-contrib-cssmin 删除评论
【发布时间】:2016-01-04 19:41:15
【问题描述】:

我正在尝试使用 Grunt 和 grunt-contrib-cssmin 删除所有 CSS cmets,CSS 文件已编译并缩小,它包含所有 cmets。

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

【问题讨论】:

  • 您没有向cssmin 配置提供任何css 文件。您的 css 被缩小只是因为您的 less 配置具有选项 compress: true
  • 是的,谢谢,我找到了 grunt-strip-css-cmets 的解决方案,我会发布解决方案

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


【解决方案1】:

按照作者提供的答案类型,grunt-decomment 将是一个更通用的解决方案,它可以从任何文件中删除像 ///**/ 这样的 cmets。

【讨论】:

    【解决方案2】:

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

    更正后的代码如下:

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

    【讨论】:

    • 不要使用 Less compress 选项(它已被弃用,将被删除,因为它与某些现代 CSS 功能不兼容)。 (另请注意 yuicompressoptimization 选项几年前已被删除,在当前的 Less 版本中没有任何影响)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    相关资源
    最近更新 更多