Gruntjs Grunt在维护文件夹结构的同时,在多个文件夹中保存多个文件

Gruntjs Grunt在维护文件夹结构的同时,在多个文件夹中保存多个文件,gruntjs,grunt-contrib-concat,Gruntjs,Grunt Contrib Concat,我正在我的项目中使用Grunt concat。我想在文件夹级别合并文件。 例如 进入: 有没有一种方法可以做到这一点,而不用在自己的concat行中专门添加每个“child”?在您的concat任务中使用。要引用链接文档,请执行以下操作: 单独指定所有源文件路径通常是不切实际的, 所以Grunt支持文件名扩展(也称为globbing) 我最终做了我在中发现的以下事情 然后从注册表中调用taskName即可。我知道这个主题很老,但我想与大家分享我的解决方案,因为我无法在web上找到一个简单的解决方

我正在我的项目中使用Grunt concat。我想在文件夹级别合并文件。 例如

进入:

有没有一种方法可以做到这一点,而不用在自己的concat行中专门添加每个“child”?

在您的concat任务中使用。要引用链接文档,请执行以下操作:

单独指定所有源文件路径通常是不切实际的, 所以Grunt支持文件名扩展(也称为globbing)


我最终做了我在中发现的以下事情


然后从注册表中调用taskName即可。

我知道这个主题很老,但我想与大家分享我的解决方案,因为我无法在web上找到一个简单的解决方案

concat: {
        files: [
            {
                expand: true,
                src: ["**/*.js"],
                dest: "dest/js",
                cwd: "src/js",
                rename: function(dst, src) {
                    const srcParts = String(src).split("/");
                    let dstPath = "";
                    let dstName = srcParts[0].split(".")[0];
                    if (srcParts.length > 1) {
                        dstName = srcParts[srcParts.length - 2];
                        dstPath =
                            srcParts
                                .slice(0, srcParts.length - 1)
                                .join("/") + "/";
                    }
                    return `${dst}/${dstPath + dstName}.js`;
                }
            }
        ]
    }
我希望这对某人有所帮助:)

js >
  parent >
     child1 >
       child1combined.js
     child2 >
        child2combined.js
grunt.registerTask("taskName", "Task Description", function() {

            // get all module directories
            grunt.file.expand("src/js/parent/*").forEach(function (dir) {

                // get the module name from the directory name
                var dirName = dir.substr(dir.lastIndexOf('/')+1);

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

                // create a subtask for each module, find all src files
                // and combine into a single js file per module
                concat[dirName] = {
                    src: [dir + '/**/*.js'],
                    dest: 'build/js/parent/' + dirName + '/combined.js'
                };

                // add module subtasks to the concat task in initConfig
                grunt.config.set('concat', concat);
            });
        });
concat: {
        files: [
            {
                expand: true,
                src: ["**/*.js"],
                dest: "dest/js",
                cwd: "src/js",
                rename: function(dst, src) {
                    const srcParts = String(src).split("/");
                    let dstPath = "";
                    let dstName = srcParts[0].split(".")[0];
                    if (srcParts.length > 1) {
                        dstName = srcParts[srcParts.length - 2];
                        dstPath =
                            srcParts
                                .slice(0, srcParts.length - 1)
                                .join("/") + "/";
                    }
                    return `${dst}/${dstPath + dstName}.js`;
                }
            }
        ]
    }