I'm running a grunt concat task on one of my projects and it looks something like this:

/**
 * Concatenate | Dependencies Scripts
 */

concat: {
    dependencies: {
        files: {        
            "./Ditcoop/js/plugins.min.js": ["./Ditcoop/js/vendor/**/*.min.js", "!./Ditcoop/js/vendor/modernizr/*.js", "!./Ditcoop/js/vendor/jquery/*.js"],
            "./Global/js/plugins.min.js": ["./Global/js/vendor/**/*.min.js", "!./Global/js/vendor/modernizr/*.js", "!./Global/js/vendor/jquery/*.js"],
            "./Webshop/js/plugins.min.js": ["./Webshop/js/vendor/**/*.min.js", "!./Webshop/js/vendor/modernizr/*.js", "!./Webshop/js/vendor/jquery/*.js"]
        }
    }
}

My question would be if I could somehow make that more dynamic without having to specify each root folder. I was thinking of something like this:

    concat: {
    dependencies: {
        files: {        
            "./*/js/plugins.min.js": ["./*/js/vendor/**/*.min.js", "!./*/js/vendor/modernizr/*.js", "!./*/js/vendor/jquery/*.js"],
        }
    }
}

I'm pretty sure I cannot do it this way, but I could use the expand option, I'm just not sure how I could use it so I can do that under the right root folder, so I won't create the same destination file as many times I run the concat.

有帮助吗?

解决方案

Always remember Gruntfiles are javascript :)

grunt.initConfig({
  concat: {
    dependencies: {
      files: (function() {
        var files = Object.create(null);
        grunt.file.expand({filter: 'isDirectory'}, '*').forEach(function(dir) {
          files[dir + '/js/plugins.min.js'] = [
            dir + '/js/vendor/**/*.min.js',
            '!' + dir + '/js/vendor/modernizr/*.js',
            '!' + dir + '/js/vendor/jquery/*.js'
          ];
        });
        return files;
      }()),
    },
  },
});

But if your dependency handling logic is this complex you may want to consider using a module loader such as browserify or requirejs. The concat task is really just for joining simple files together.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top