2016-01-20 71 views
0

我剛開始使用gruntcssmin來自動執行CSS縮小過程。這在第一次可以正常工作,然而後來文件內容被追加到縮小文件而不是覆蓋文件,並且其重複的文件大小變得高而高!cssmin和grunt - 文件附加問題

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


}; 

請指教,我在哪裏犯錯。

回答

1

我相信這issue GitHub上是相關的,其中兩個方案進行了討論:涅槃之前

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'] 
    } 
    } 
} 
+0

謝謝。 使用絕對文件路徑並不實用,因爲我在目錄中有許多css文件。我不想結合,只是想分別縮小每個文件。有任何建議嗎? – Vaishak

+0

在這種情況下,你嘗試過使用乾淨嗎?我更新了我的初始帖子。這個軟件包的作者表示對文件使用*選擇器保持警惕,因爲它重用相同的文件並且不覆蓋。 – fzxt

+1

我沒有使用乾淨的,但現在工作正常。問題出在我提到的道路上! ''!*。min.css''而不是''assets/front/css/*。min.css'' – Vaishak