Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
382 views
in Technique[技术] by (71.8m points)

gruntjs - Dynamic mapping for destinations in grunt.js

I have a project with several sub folders that contain JavaScript files I want to concatenate. what would be the right way to configure them?

eg.

source: /modules/$modulename/js/*.js (several files) dest: /modules/$modulename/js/compiled.js

So what I want to do is to compile js-files of an unknown/unconfigured count of subfolders ($modulename) into one file per subfolder.

Is this possible?


The following function (built after hereandnow78's instructions) does the job:

grunt.registerTask('preparemodulejs', 'iterates over all module directories and compiles modules js files', function() {

    // read all subdirectories from your modules folder
    grunt.file.expand('./modules/*').forEach(function(dir){

        // get the current concat config
        var concat = grunt.config.get('concat') || {};

        // set the config for this modulename-directory
        concat[dir] = {
            src: [dir + '/js/*.js', '!' + dir + '/js/compiled.js'],
            dest: dir + '/js/compiled.js'
        };

        // save the new concat config
        grunt.config.set('concat', concat);

    });

});

after that i put preparemodulejs before the concat job in my default configuration.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you will probably need to code your own task, where you iterate over your subfolders, and dynamically append to your concat configuration.

grunt.registerTask("your-task-name", "your description", function() {

  // read all subdirectories from your modules folder
  grunt.file.expand("./modules/*").forEach(function (dir) {

    // get the current concat config
    var concat = grunt.config.get('concat') || {};

    // set the config for this modulename-directory
    concat[dir] = {
     src: ['/modules/' + dir + '/js/*.js', '!/modules/' + dir + '/js/compiled.js'],
     dest: '/modules/' + dir + '/js/compiled.js'
    };

    // save the new concat configuration
    grunt.config.set('concat', concat);
  });
  // when finished run the concatinations
  grunt.task.run('concat');
});

run this with:

$ grunt your-task-name

this code is untested, but i think it should do your job.

HINT: you can put this code into an external file and include in your gruntfile if you want to keep your gruntfile small, e.g. put this into a file inside a tasks-directory:

module.exports = function(grunt) {
  grunt.registerTask("your-task-name", "your description", function() {
    ...
  });
};

and load in in your gruntfile:

grunt.loadTasks("./tasks");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...