【问题标题】:cssmin and grunt - file append issuecssmin 和 grunt - 文件追加问题
【发布时间】:2016-01-20 04:25:27
【问题描述】:

我刚刚开始使用 gruntcssmin 来自动化 CSS 缩小过程。这在第一次工作正常,但后来文件内容被附加到缩小文件而不是覆盖它,并且它的重复文件大小变得越来越高!

我的 gruntfile.js 文件内容如下。

module.exports = function (grunt) {
    grunt.initConfig({

    // define source files and their destinations
    cssmin: {
        target: {
            files: [{ 
                src: ['assets/front/css/*.css', '!*.min.css'],  // source files mask
                dest: 'assets/front/css/',    // destination folder
                expand: true,    // allow dynamic building
                flatten: true,   // remove all unnecessary nesting
                ext: '.min.css'   // replace .css to .min.css
            }],
        }
    },
    watch: {
        css:  { files: 'assets/front/css/*.css', tasks: [ 'cssmin' ] },
    }
});

// load plugins
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-cssmin');

// register at least this one task
grunt.registerTask('default', [ 'cssmin', 'watch' ]);


};

请指教,我哪里弄错了。

【问题讨论】:

    标签: node.js gruntjs npm grunt-contrib-cssmin


    【解决方案1】:

    我相信GitHub上的这个issue是相关的,其中讨论了两个解决方案:

    1) 在缩小之前使用clean

       clean: ['your_path/css/*.min.css', '!your_path/css/*.css'],
       cssmin: {
          minify: {
            expand: true,
            cwd: 'your_path/css/',
            src: ['*.css'],
            dest: 'your_path/css/',
            ext: '.min.css'
          }
        }
    

    2) 使用绝对文件路径而不是 */**.css 路径,例如:

    cssmin: {
      combine: {
        files: {
          'path/to/output.css': ['path/to/input_one.css', 'path/to/input_two.css']
        }
      }
    }
    

    【讨论】:

    • 谢谢。使用绝对文件路径并不实用,因为我在目录中有许多 css 文件。我不想合并,只想分别缩小每个文件。有什么建议吗?
    • 在这种情况下,您是否尝试过使用 clean ?我更新了我的初始帖子。据说这个包的作者对在文件上使用 * 选择器持谨慎态度,因为出于某种原因它会重用相同的文件并且不会覆盖。
    • 我没用过 clean,不过现在可以正常使用了。问题在于我提到的路径! '!*.min.css' 而不是 '!assets/front/css/*.min.css'
    猜你喜欢
    • 1970-01-01
    • 2018-10-15
    • 1970-01-01
    • 2017-08-12
    • 2016-01-04
    • 1970-01-01
    • 2023-03-23
    • 2014-03-07
    • 1970-01-01
    相关资源
    最近更新 更多