2014-07-25 101 views
0

我對Grunt還是比較陌生的,所以也許有更多經驗的人可以幫助我。出於某種原因,我運行grunt watch時,uglify任務正在執行多次,所以我的scripts.min.js文件被多次編譯。Grunt Uglify Loop

這是我的Gruntfile.js目前的樣子:

'use strict'; 
module.exports = function(grunt) { 
    // Show elapsed time 
    require('time-grunt')(grunt); 

    // Comment out any unused components 
    var jsFiles = [ 
    //'assets/vendor/bootstrap-sass-official/assets/javascripts/bootstrap/affix.js', 
    //'assets/vendor/bootstrap-sass-official/assets/javascripts/bootstrap/alert.js', 
    //'assets/vendor/bootstrap-sass-official/assets/javascripts/bootstrap/button.js', 
    //'assets/vendor/bootstrap-sass-official/assets/javascripts/bootstrap/carousel.js', 
    'assets/vendor/bootstrap-sass-official/assets/javascripts/bootstrap/collapse.js', 
    //'assets/vendor/bootstrap-sass-official/assets/javascripts/bootstrap/dropdown.js', 
    //'assets/vendor/bootstrap-sass-official/assets/javascripts/bootstrap/modal.js', 
    //'assets/vendor/bootstrap-sass-official/assets/javascripts/bootstrap/popover.js', 
    //'assets/vendor/bootstrap-sass-official/assets/javascripts/bootstrap/scrollspy.js', 
    //'assets/vendor/bootstrap-sass-official/assets/javascripts/bootstrap/tab.js', 
    //'assets/vendor/bootstrap-sass-official/assets/javascripts/bootstrap/tooltip.js', 
    'assets/vendor/bootstrap-sass-official/assets/javascripts/bootstrap/transition.js', 
    'assets/js/*.js' 
    ]; 

    // Configure tasks. 
    grunt.initConfig({ 
    sass: { 
     dist: { 
     options: { 
      style: 'compressed' 
     }, 
     files: { 
      'assets/css/main.min.css': 'assets/sass/app.scss', 
      'assets/css/custom-login.min.css': 'assets/sass/_login.scss', 
      'assets/css/magnific.min.css': 'assets/sass/plugins/magnific/magnific.scss' 
     } 
     } 
    }, 
    uglify: { 
     dist: { 
     files: { 
      'assets/js/scripts.min.js': [ jsFiles ] 
     } 
     } 
    }, 
    watch: { 
     sass: { 
     files: [ 'assets/sass/*.scss' ], 
     tasks: [ 'sass' ] 
     }, 
     js: { 
     files: [ jsFiles ], 
     tasks: [ 'uglify' ] 
     } 
    } 
    }); 

    // Load tasks. 
    grunt.loadNpmTasks('grunt-contrib-uglify'); 
    grunt.loadNpmTasks('grunt-contrib-watch'); 
    grunt.loadNpmTasks('grunt-contrib-sass'); 

    // Register tasks. 
    grunt.registerTask('default', [ 
    'watch', 
    'sass', 
    'uglify' 
    ]); 
}; 

任何人有,爲什麼醜化任務執行多次任何想法?

回答

2

問題是你有所有的js文件來檢查和縮小。主要問題是:

'assets/js/*.js' 

正在壓縮所有js文件,而且你正在看的話,那麼當你醜化所有js文件,他們改變了,因爲這個原因,手錶執行任務,然後再縮小再....那就是無限循環。

您應該更改grunt文件,例如刪除資產文件夾的scripts.min.js或removimg assets/js/*。js並手動添加所有js文件。我建議申請第二個。

'use strict'; 
module.exports = function(grunt) { 
// Show elapsed time 
require('time-grunt')(grunt); 

// Comment out any unused components 
var jsFiles = [ 
//'assets/vendor/bootstrap-sass-official/assets/javascripts/bootstrap/affix.js', 
//'assets/vendor/bootstrap-sass-official/assets/javascripts/bootstrap/alert.js', 
//'assets/vendor/bootstrap-sass-official/assets/javascripts/bootstrap/button.js', 
//'assets/vendor/bootstrap-sass-official/assets/javascripts/bootstrap/carousel.js', 
'assets/vendor/bootstrap-sass-official/assets/javascripts/bootstrap/collapse.js', 
//'assets/vendor/bootstrap-sass-official/assets/javascripts/bootstrap/dropdown.js', 
//'assets/vendor/bootstrap-sass-official/assets/javascripts/bootstrap/modal.js', 
//'assets/vendor/bootstrap-sass-official/assets/javascripts/bootstrap/popover.js', 
//'assets/vendor/bootstrap-sass-official/assets/javascripts/bootstrap/scrollspy.js', 
//'assets/vendor/bootstrap-sass-official/assets/javascripts/bootstrap/tab.js', 
//'assets/vendor/bootstrap-sass-official/assets/javascripts/bootstrap/tooltip.js', 
'assets/vendor/bootstrap-sass-official/assets/javascripts/bootstrap/transition.js' 
]; 

// Configure tasks. 
grunt.initConfig({ 
sass: { 
    dist: { 
    options: { 
     style: 'compressed' 
    }, 
    files: { 
     'assets/css/main.min.css': 'assets/sass/app.scss', 
     'assets/css/custom-login.min.css': 'assets/sass/_login.scss', 
     'assets/css/magnific.min.css': 'assets/sass/plugins/magnific/magnific.scss' 
    } 
    } 
}, 
uglify: { 
    dist: { 
    files: { 
     'assets/js/scripts.min.js': [ jsFiles ] 
    } 
    } 
}, 
watch: { 
    sass: { 
    files: [ 'assets/sass/*.scss' ], 
    tasks: [ 'sass' ] 
    }, 
    js: { 
    files: [ jsFiles ], 
    tasks: [ 'uglify' ] 
    } 
} 
}); 

// Load tasks. 
grunt.loadNpmTasks('grunt-contrib-uglify'); 
grunt.loadNpmTasks('grunt-contrib-watch'); 
grunt.loadNpmTasks('grunt-contrib-sass'); 

// Register tasks. 
grunt.registerTask('default', [ 
    'watch', 
    'sass', 
    'uglify' 
]); 
}; 

問候。

+0

不錯!做了一個魅力! –