动态映射grunt.js中的目的地 [英] Dynamic mapping for destinations in grunt.js

查看:99
本文介绍了动态映射grunt.js中的目的地的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目包含几个包含我想要连接的JavaScript文件的子文件夹。



例如,

source:/ modules / $ modulename / js / *。js(几个文件)
dest:/modules/$modulename/js/compiled.js

所以我想要做的是编译将未知/未配置的子文件夹计数($ modulename)的js文件合并到每个子文件夹中的一个文件中。



这可能吗?






下面的函数(根据hereandnow78的指令构建)完成这项工作:

  grunt.registerTask('preparemodulejs','迭代所有模块目录并编译模块js文件',function(){

//读取全部从你的模块文件夹的子目录
grunt.file.expand('./ modules / *')。forEach(function(dir){

//获取当前的concat配置
var concat = grunt.config.get('concat')|| {};

//设置这个modulename-directory的配置
concat [dir] = {
src:[dir +'/js/*.js','!'+ dir +'/js/compiled.js'],
dest:dir +'/js/compiled.js'
};

//保存新的连接配置
grunt.config.set('concat',concat);

});

});

之后,我把preparemodulejs放在我的默认配置中的concat作业之前。

$ b $你可能需要编写你自己的任务,在迭代你的子文件夹的地方,动态地追加到你的concat配置。

$ b $

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

// read all从你的模块文件夹的子目录
grunt.file.expand(./ modules / *)。forEach(function(dir){

//获取当前的concat配置
var concat = grunt.config.get('concat')|| {};

//设置这个modulename-directory的配置
concat [dir] = {
src:['/ modules /'+ dir +'/js/*.js','!/ modules /'+ dir +'/js/compiled.js'],
dest:'/ modules /' + dir +'/js/compiled.js'
};

//保存新的连接配置
grunt.config.set('concat ,CONCAT);
});
//结束运行时concatinations
grunt.task.run(的concat);
});

运行这个:

  $ grunt your-task-name 

这段代码未经测试,但我认为它应该做你的工作。



提示:如果你想保持你的grunt文件很小,你可以把这段代码放到一个外部文件中并包含在你的grunt文件中,例如把它放到任务目录中的一个文件中:

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

并载入您的gruntfile:

  grunt.loadTasks( ./任务); 


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.

解决方案

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");

这篇关于动态映射grunt.js中的目的地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆