2015-07-21 50 views
2

我正在嘗試在我的項目中使用grunt-usemin來縮小JS和CSS文件,但它不起作用。所以我創建了一個簡單的例子,就像grunt-usemin文檔一樣,這也是行不通的。grunt-usemin src文件爲空

我有簡單的結構:

. 
├── Gruntfile.js 
├── dist 
│ └── js 
├── index.html 
├── js 
│ ├── bar.js 
│ └── foo.js 
└── package.json 

index.html中我具有以下參照2個JS文件塊:

<!-- build:js js/optimized.js --> 
<script src="/js/foo.js"></script> 
<script src="/js/bar.js"></script> 
<!-- endbuild --> 

我Gruntfile.js:

module.exports = function(grunt) { 
// Project configuration. 
    grunt.initConfig({ 
    pkg: grunt.file.readJSON('package.json'), 
    useminPrepare: { 
     html: 'index.html', 
     options: { 
      dest: 'dist' 
     } 
    }, 
    usemin: { 
     html: ['index.html'] 
    }, 
    filerev: { 
     options: { 
     algorithm: 'md5', 
     length: 8 
     }, 
     js: { 
     src: 'js/optimized.js', 
     dest: 'js/' 
     } 
    } 
    }); 

    grunt.loadNpmTasks('grunt-contrib-uglify'); 
    grunt.loadNpmTasks('grunt-contrib-cssmin'); 
    grunt.loadNpmTasks('grunt-contrib-concat'); 
    grunt.loadNpmTasks('grunt-filerev'); 
    grunt.loadNpmTasks('grunt-usemin'); 

    // Register tasks 
    grunt.registerTask('deploy', ['useminPrepare', 'uglify:generated']); 
// grunt.registerTask('deploy', ['useminPrepare', 'uglify:generated', 'usemin']); 

}; 

當我運行grunt deploy -v我期望,js/optimized.js將被創建。但是,我得到這個錯誤:由useminPrepare產生

>> Destination dist/js/optimized.js not written because src files were empty. 
>> No files created. 

任務是這樣的:

有人可以幫我,爲什麼DIST/JS /未創建
Going through index.html to update the config 
Looking for build script HTML comment blocks 

Configuration is now: 

    concat: 
    { generated: 
    { files: 
     [ { dest: '.tmp/concat/js/optimized.js', 
      src: [ 'js/foo.js', 'js/bar.js' ] } ] } } 

    uglify: 
    { generated: 
    { files: 
     [ { dest: 'dist/js/optimized.js', 
      src: [ '.tmp/concat/js/optimized.js' ] } ] } } 

optimized.js?

謝謝!

+0

你如何檢查這是由useminPrepare生成的任務 – Gerfried

回答

0

嗯問題在grunt.registerTask('deploy', ['useminPrepare', 'uglify:generated']);。我忘了調用concat:生成。

所以,解決辦法是:

grunt.registerTask('deploy', ['useminPrepare', 'concat:generated', 'uglify:generated']);