繁体   English   中英

首先咕con concat目录递归文件

[英]Grunt concat directory recursive files first

我需要递归合并目录中包含的JavaScript文件,但要确保在挖掘其他目录之前添加当前目录中的文件,例如:

src/
 - module1/
    - config.js
    - index.js
 - module2/
    - index.js
 - main.js

使用此处提供的解决方案,我能够以此递归获取文件:

// get all module directories
    grunt.file.expand("src/*").forEach(function (dir) {
        // get the module name from the directory name
        var dirName = dir.substr(dir.lastIndexOf('/') + 1);

        // create a subtask for each module, find all src files
        // and combine into a single js file per module
        concat.main.src.push(dir + '/**/*.js');
    });

    console.log(concat.main.src);

但是当我检查concat.main.src时,该命令并不是我所需的方式:

//console.log output
['src/module1/**/*.js',
 'src/module2/**/*.js',
 'src/main.js']

我需要的顺序是:

['src/main.js',
 'src/module1/**/*.js',
 'src/module2/**/*.js']

关于如何实现此目标的任何想法?

快速解决方案是在按字母顺序处理时将main.js重命名为app.js。

但是,如果您想保留main.js,请尝试先将其推入数组。 然后,如果更改文件扩展的glob以仅查看src的子目录,则它将跳过main.js并继续将模块添加到数组中

concat.main.src.push('src/main.js');
// get all module directories
grunt.file.expand("src/**").forEach(function (dir) {
    // get the module name from the directory name
    var dirName = dir.substr(dir.lastIndexOf('/') + 1);

    // create a subtask for each module, find all src files
    // and combine into a single js file per module
    concat.main.src.push(dir + '/**/*.js');
});

console.log(concat.main.src);

非常感谢!!!

洗完澡后,我的头变得清晰起来,仔细阅读了Grunts文档,我找到了一个解决方案,可以让其他感兴趣的人使用。 它使用的是grunt.file.recurse而不是grunt.file.expand。

var sourceSet = [];
grunt.file.recurse("src/main/", function callback(abspath, rootdir, subdir, filename) {
    sourceSet.push(abspath);
});

concat.main.src = concat.main.src.concat(sourceSet.sort());

现在可以正常使用!!!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM