Grunt Uglify Loop

时间:2014-07-25 06:50:03

标签: gruntjs grunt-contrib-watch grunt-contrib-uglify

我还是Grunt的新手,所以也许有经验丰富的人可以帮助我。出于某种原因,我运行grunt watch时,uglify任务被多次执行,因此我的scripts.min.js文件被多次编译。

这就是我的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'
  ]);
};

任何人都有任何想法为什么uglify任务多次执行?

1 个答案:

答案 0 :(得分:2)

问题是你要检查和缩小所有js文件。主要问题是:

'assets/js/*.js'

你正在压缩所有的js文件,但是你正在观看,所以当你uglify所有js文件时,它们都会改变,因此监视任务会执行并再次缩小....而且'无限循环。

您应该更改gruntfile,例如删除assets文件夹的scripts.min.js,或者删除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'
]);
};

问候。