2013-11-20 80 views
2

我一直在嘗試一段時間來以特定方式組合兩個Grunt JS插件。 基本上,我想使用UglifyJS來縮小目錄中的所有JS文件。在此之後,我想使用版本控制插件(在這種情況下是grunt-static-versioning)來實現緩存清除。Grunt Uglify通配符和版本號

我gruntfile如下:

module.exports = function(grunt) { 
grunt.initConfig({ 
    clean: ['dest/js'], 
    uglify: { 
    options: { 
     report: 'min', 
     mangle: true 
    }, 
    my_target: { 
     files: [{ 
     expand: true, 
     cwd: 'src/js', 
     src: '**/*.js', 
     dest: 'dest/js' 
     }] 
    } 
    }, 
    cssmin: { 
    options: { 
    report:'min' 
    }, 
    minify: { 
     expand:true, 
    cwd: 'src/css', 
    src: '**/*.css', 
    dest: 'dest/css', 
    } 
    }, 
    imagemin: { 
    dynamic: { 
     options: { 
     optimizationLevel: 7 
     }, 
     files: [{ 
     expand:true, 
     cwd: 'src/assets', 
     src: ['**/*.{png,jpg,gif}'], 
     dest: 'dest/assets' 
     }] 
    } 
    }, 
    htmlmin: { 
    mini :{ 
     options: { 
     removeComments: true, 
     collapseWhitespace: true, 
     collapseBooleanAttributes: true, 
     removeAttributeQuotes: true, 
     removeRedundantAttributes: true, 
     removeEmptyAttributes: true, 
     useShortDoctype: true 
     }, 
     files: [{ 
     expand:true, 
     cwd: 'src', 
     src: '**/*.html', 
     dest: 'dest' 
     }] 
    } 
    }, 
    versioning: {    // Task 
    options: {    // Task options 
     cwd: '' 
    }, 
    dist: {     // Target 
     options: {    // Target options 
     }, 
     files: [{ 
     assets: '<%= uglify.my_target.files %>', 
     key: 'global', 
     dest: 'dest/js', 
     type: 'js', 
     ext: '.js' 
     }] 
    } 
    } 
}); 
grunt.loadNpmTasks('grunt-contrib-clean'); 
grunt.loadNpmTasks('grunt-static-versioning'); 
grunt.loadNpmTasks('grunt-contrib-uglify'); 
grunt.loadNpmTasks('grunt-contrib-cssmin'); 
grunt.loadNpmTasks('grunt-contrib-imagemin'); 
grunt.loadNpmTasks('grunt-contrib-htmlmin'); 


grunt.registerTask('default', ['clean', 'uglify', 'cssmin', 'imagemin', 'htmlmin', 'versioning']);} 

然而,在執行時出現以下錯誤:

Running "versioning:dist" (versioning) task 
Warning: Unable to read "dest/js" file (Error code: EISDIR). Use --force to continue. 

我理解,因爲DEST/JS不是一個文件,而是一個目錄出現此錯誤,但我不知道如何告訴版本正確的文件名。有沒有特別的Grunt JS格式來做到這一點?

回答

3

似乎是有一些額外的選項被啓用,導致上述錯誤。 我已更正這些並做了一些更改。 運行下面的grunt文件,看看你是否可以沒有任何錯誤地執行。

module.exports = function(grunt) { 
grunt.initConfig({ 
    clean : [ 'dest/js' ], 
    uglify : { 
     options : { 
      report : 'min', 
      mangle : true 
     }, 
     my_target : { 
      files : [ { 
       src : 'src/js/*.js', 
       dest : 'dest/js/main.min.js' 
      } ] 
     } 
    }, 
    cssmin : { 
     options : { 
      report : 'min' 
     }, 
     minify : { 
      expand : true, 
      cwd : 'src/css', 
      src : '**/*.css', 
      dest : 'dest/css', 
     } 
    }, 
    imagemin : { 
     dynamic : { 
      options : { 
       optimizationLevel : 7 
      }, 
      files : [ { 
       expand : true, 
       cwd : 'src/assets', 
       src : [ '**/*.{png,jpg,gif}' ], 
       dest : 'dest/assets' 
      } ] 
     } 
    }, 
    htmlmin : { 
     mini : { 
      options : { 
       removeComments : true, 
       collapseWhitespace : true, 
       collapseBooleanAttributes : true, 
       removeAttributeQuotes : true, 
       removeRedundantAttributes : true, 
       removeEmptyAttributes : true, 
       useShortDoctype : true 
      }, 
      files : [ { 
       expand : true, 
       cwd : 'src', 
       src : '**/*.html', 
       dest : 'dest' 
      } ] 
     } 
    }, 
    versioning : { // Task 
     options : { // Task options 
      cwd : 'public', 
      outputConfigDir : 'public/config' 
     }, 
     dist : { // Target 
      options : { // Target options 
      }, 
      files : [ { 
       assets : '<%= uglify.my_target.files %>', 
       key : 'global', 
       dest : 'dest/js', 
       type : 'js', 
       ext : '.js' 
      } ] 
     } 
    } 
}); 

grunt.loadNpmTasks('grunt-contrib-clean'); 
grunt.loadNpmTasks('grunt-static-versioning'); 
grunt.loadNpmTasks('grunt-contrib-uglify'); 
grunt.loadNpmTasks('grunt-contrib-cssmin'); 
grunt.loadNpmTasks('grunt-contrib-imagemin'); 
grunt.loadNpmTasks('grunt-contrib-htmlmin'); 

grunt.registerTask('default', [ 'clean', 'uglify', 'cssmin', 'imagemin', 
     'htmlmin', 'versioning' ]); 
}