Dynamic mapping for destinations in grunt.js

前端 未结 1 592
北恋
北恋 2020-12-13 10:32

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: /mo

相关标签:
1条回答
  • 2020-12-13 11:08

    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");
    
    0 讨论(0)
提交回复
热议问题