简体   繁体   English

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

[英]Grunt - Minifying/processing multiples files into one

I'm new to Grunt and I'm lost in all it's option. 我是Grunt的新手,我迷失了所有的选择。 I'm trying to do two things : 我正在尝试做两件事:

  • Minify all my js files into one each time I change one of them 每次更改其中一个文件时,将我的所有js文件缩小为一个
  • Process a specific scss file when one of my scss file is changed 当我的一个scss文件被更改时处理特定的scss文件

Here is the current Gruntfile.js I got : 这是我当前得到的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' ]);


};

How could I achieve this? 我怎样才能做到这一点?

Use the grunt-contrib-concat 使用grunt-contrib-concat

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

You should wrap all you files in an anonymous function and define variables using var so you do not get variable conflicts between files. 您应该将所有文件包装在一个匿名函数中,并使用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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM