0

Instead of mention every js seperatly, is this the way to minify and concatinate a whole js folder?

module.exports = function(grunt) {

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

};
Elad Benda
  • 35,076
  • 87
  • 265
  • 471
  • Possible duplicate of [How to minify multiple Javascript files in a folder with UglifyJS?](http://stackoverflow.com/questions/17008472/how-to-minify-multiple-javascript-files-in-a-folder-with-uglifyjs) – Julio Marins Mar 31 '16 at 18:21

2 Answers2

1

Yes, that's correct if you only want to concatenate and minify all .js files in the scripts directory one level deep.

For example, if scripts/ contains a.js and b.js and the foo/ directory, you'd get the concatenation and minified result of a.js + b.js but nothing in the foo/ directory.

What if you want to get everything in the foo/ directory (and all other nested directories) as well? Change the expression from ['scripts/*.js'] to ['scripts/**/*.js'] -- or any minimatch expression: https://github.com/gruntjs/grunt/blob/master/docs/api_file.md#gruntfileexpand

You're able to use any minimatch expression since the grunt min task uses the expandFiles function: https://github.com/gruntjs/grunt/blob/master/tasks/min.js#L21

The downside to using a minimatch expression with this task is it's hard to understand what order the files will be concatenated in, which is often very important. Be careful if this matters.

Also, please note that a new version of grunt (0.4) is coming out very soon. This will make this answer obsolete, as the min task has been changed in 0.4 (but will still support minimatch expression).

smithclay
  • 3,376
  • 1
  • 19
  • 25
0

If your folder consist only js files you are right but if your folder have nested folders such as, foo is our main js folder in which we have another nested folder loo and inside it we also have some js files such as :

 foo:
        mu.js
        su.js
        loo:
              ku.js
              wu.js

In this case you have to modify your code in such manner :

module.exports = function(grunt) {

grunt.initConfig({
  min: {
    dist: {
      src:  'foo/**/*.js',
      dest: 'dist/foo.min.js'
    }
  }
});

};

by doing in such a way you can minimize your all js files of foo folder even nested folder files too. I suggest cocat js files before minimizing.

Anshul
  • 9,312
  • 11
  • 57
  • 74