Javascript 用gulp按文件夹打包文件

Javascript 用gulp按文件夹打包文件,javascript,node.js,filestream,gulp,Javascript,Node.js,Filestream,Gulp,我想要这个: |-向导 ||-_guide.hbs ||-guide.hbs |`-index.hbs |-index.hbs `-诺鲁特商学院 变成这样: |-common.js `-guides.js 如您所见,guides文件夹被压缩为guides.js和。文件夹压缩为common.js 下面是我丑陋的解决方案。 受此启发 实现这一目标的最佳途径是什么?请至少分享一些有指导意义的工具 编辑2 我需要去掉getFolderMap函数,用纯函数解决这个问题 溪流。我能得到的最接近的结果

我想要这个:

|-向导 ||-_guide.hbs ||-guide.hbs |`-index.hbs |-index.hbs `-诺鲁特商学院

变成这样:

|-common.js `-guides.js

如您所见,guides文件夹被压缩为guides.js和。文件夹压缩为common.js

下面是我丑陋的解决方案。 受此启发

实现这一目标的最佳途径是什么?请至少分享一些有指导意义的工具

编辑2

我需要去掉getFolderMap函数,用纯函数解决这个问题 溪流。我能得到的最接近的结果是:

var directoryFilter = $.filter(function (file) {
    return file.isDirectory();
});

gulp.src([paths.src + + '/templates/**'])
        .pipe(directoryFilter)
        .pipe(

        //here I have list of directories
        //I need a gulp plugin to get the contents
        // so i can process them.
        );
这个问题是相关的

最终解决方案

这是基于spiralx的答案的最终解决方案。

这是我的第一个猜测,我在工作中使用forEachModule函数为每个模块执行子任务,然后合并结果-有助于解决路径问题的一些插件,包括gulp stylus IIRC:

var gulp=需要“gulp”; var plugins=需要'gulp-load-plugins'; var es=需要“事件流”; var path=需要“路径”; var config=require./build.config'; // ------------------ /* *对于每个模块,执行subtaskFunc并获取结果流,然后 *合并所有流并返回该流。 * *@param[{Object}]modules模块对象数组 *@param{Function}subtaskFunc functionmodule->stream *@return{Object}流 */ 函数forEachModulemodules,subtaskFunc{ var subtaskStreams=modules.mapsubtaskFunc; 返回es.concat.applynull,子任务流; } // ------------------ 任务“模板”,函数{ var dest=path.joinconfig.dest,“模板”; 返回forEachModuleconfig.modules、functionmodule{ var src=path.joinconfig.src,“模板”,模块,***.hbs'; //编译.hbs文件,封装在CommonJS模块中,合并成“.js”并写入dest。 返回gulp.src .pipeplugins.handlebar.on'error',plugins.util.log .pipeplugins.defineModule'plain' .pipeplugins.declare{ 命名空间:“app.templates.”+模块 } .pipeplugins.concatmodule+'.js' .pippeglp.dest; } //是否将所有模块模板文件合并为一个? .pipeplugins.concat'templates.js' .pipegulp.destconfig.dest; }; 编辑: 这段代码与前面的代码几乎相同,但在模块的“src/app”下查找目录,而不是预先定义列表,并且不会在gulp任务函数本身之外生成任何配置

这样做的原因是,当a使用@include或任何引用相对路径的内容时,或者b将选项传递到触控笔本身或Nib之类的插件时,在流(如src/app/***.styl)上使用gulp触控笔会出现问题

var modules=fs.readdirSync'src/app'.filterfunctionname{ 返回fs.statSync'src/app/'+name.isDirectory }; // ------------------ 吞咽。任务“风格”,功能{ var streams=modules.mapfunctionname{ 返回gulp.src'src/app/'+name+'/***.styl' .pipeplugins.stylus{ 使用:[ 需要“nib”, 需要“bootstrap3-stylus” ], 路径:[ “src/common” ] } .pipeplugins.concatname+'.css' .pippegulp.dest'dist/css' .pipeplugins.minifyCss .pipeplugins.rename{ 后缀:'.min' } .pippegulp.dest'dist/css'; }; 返回plugins.mergeStreamstreams .pipeplugins.concat'app.css' .pippegulp.dest'dist/css'; };
我不确定你到底想要什么。您是否正在寻求一种在目录和文件上使用gulp.src的方法,以便在开始时可以替换使用fs.readdirSync?
var directoryFilter = $.filter(function (file) {
    return file.isDirectory();
});

gulp.src([paths.src + + '/templates/**'])
        .pipe(directoryFilter)
        .pipe(

        //here I have list of directories
        //I need a gulp plugin to get the contents
        // so i can process them.
        );