Grunt - 将多个文件缩小/处理为一个

时间:2015-04-04 12:04:19

标签: javascript gruntjs

我是Grunt的新手,我迷失了它的所有选择。 我试图做两件事:

  • 每次更改其中一个文件时,将所有js文件缩小为一个
  • 在我的某个scss文件发生更改时处理特定的scss文件

这是我当前的Gruntfile.js:

module.exports = function (grunt) {
    grunt.initConfig({

    // define source files and their destinations
    uglify: {
        files: { 
            src: 'js/*.js',  // source files mask
            dest: 'jsm/',    // destination folder
            expand: true,    // allow dynamic building
            flatten: true,   // remove all unnecessary nesting
            ext: '.min.js'   // replace .js to .min.js
        }
    },
    watch: {
        js:  { files: 'js/*.js', tasks: [ 'uglify' ] },
    }
});

// load plugins
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-uglify');

// register at least this one task
grunt.registerTask('default', [ 'uglify' ]);


};

我怎么能实现这个目标?

1 个答案:

答案 0 :(得分:1)

使用grunt-contrib-concat

grunt.initConfig({    
  concat: {
    dist: {
      src: ['files/**.min.js'],
      dest: 'project.js',
    },
  }
});

您应该将所有文件包装在匿名函数中并使用var定义变量,这样就不会在文件之间产生变量冲突。

(function(){

  // foo has function scope
  var foo = 3; 
  console.log(foo);

  // FOO is a global variable and 
  // can be accessed between files
  window.FOO = 3;
  console.log(FOO);

}).call()
相关问题