首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Grunt和grunt contrib-cssmin删除注释

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

Stack Overflow用户
提问于 2015-10-07 22:09:32
回答 2查看 1.4K关注 0票数 2

我正在尝试使用Grunt和grunt-contrib-cssmin删除所有的CSS注释,CSS文件被编译并缩小了所有注释。

注释应与行: keepSpecialComments: 0一起删除

代码语言:javascript
复制
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']);
};
EN

回答 2

Stack Overflow用户

发布于 2016-01-05 21:13:41

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

票数 -1
EN

Stack Overflow用户

发布于 2015-10-08 18:31:01

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

下面更正的代码:

代码语言:javascript
复制
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']);
};
票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32994523

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档