拓冰建站拓冰建站
首页 / 资讯中心 / 正文

gulp 从内存 Buffer 构建流:绕过 gulp.src() 的 Vinyl 流创建实战

gulp 从内存 Buffer 构建流绕过 gulp.src() 的 Vinyl 流创建实战【免费下载链接】gulpA toolkit to automate enhance your workflow项目地址: https://gitcode.com/gh_mirrors/gu/gulp导读在 gulp 中几乎每个任务都以gulp.src()读取磁盘文件作为起点但现实中的构建场景并非总是如此——你可能需要把一段已经存在于内存变量中的内容例如拼接好的代码、模板渲染结果、版本号文件内容直接包装成一个可继续pipe()的 gulp 流而根本不触碰文件系统。本文基于仓库中的 make-stream-from-buffer.md 配方系统讲解“从内存内容创建流”的完整方案先分析为什么不能用gulp.src()直接完成这类任务再给出一个可运行的“按版本拼接 lib 文件”完整示例深入剖析vinyl-source-stream、vinyl-buffer、gulp-tap、event-stream在其中的分工最后结合仓库源码说明 gulp 的 Vinyl 抽象、任务编排与监听机制帮助你掌握这种无需磁盘中转的数据流构建技巧。场景内容在变量里不在磁盘上gulp.src()的作用是“从文件系统读取 Vinyl 对象” —— 这是 gulp 默认的流起点。但在下面这类需求中它并不适用有一个目录存放若干 JS 库文件另一个目录存放某个模块的多个版本文件。构建目标是为每个版本生成一个 JS 文件内容为“所有库文件拼接结果 该版本文件内容”。按逻辑拆解构建步骤为加载 lib 文件拼接 lib 文件内容加载版本文件对每个版本文件把 libs 拼接结果与版本内容再拼接对每个版本文件把最终结果输出成一个文件。假设源文件结构如下├── libs │ ├── lib1.js │ └── lib2.js └── versions ├── version.1.js └── version.2.js期望的输出是└── output ├── version.1.complete.js # lib1.js lib2.js version.1.js └── version.2.complete.js # lib1.js lib2.js version.2.js问题在于第 4、5 步的数据完全存在于内存变量中拼接后的字符串它没有对应的物理文件路径。若先写临时文件再gulp.src()读取既低效又增加出错面。因此需要一种“凭空造出一个 gulp 流”的手段——这正是本配方要解决的核心问题。核心思路把字符串写入一个全新的流gulp 的流是 Node 的可写/可读流管道中的每个文件都是一个Vinyl 对象虚拟文件包含path、contents、stat等元数据。因此“从内存造流”的本质是三步用一个假文件名vinyl-source-stream创建流它会把后续write()进来的字符串/ Buffer 包装成带path的 Vinyl 对象将字符串内容write()进该流把流接进标准管道vinyl-buffer()将内容规整为 Buffer 形式再交给gulp.dest()落盘。对应地任务中还需要两个配套工具gulp-tap用于“偷看”流中每个文件的contents并缓存到内存event-stream用于合并多个流的结束事件避免任务提前完成。完整示例代码var gulp require(gulp); var source require(vinyl-source-stream); var vinylBuffer require(vinyl-buffer); var tap require(gulp-tap); var concat require(gulp-concat); var size require(gulp-size); var path require(path); var es require(event-stream); var memory {}; // well keep our assets in memory // task of loading the files contents in memory gulp.task(load-lib-files, function() { // read the lib files from the disk return gulp.src(src/libs/*.js) // concatenate all lib files into one .pipe(concat(libs.concat.js)) // tap into the stream to get each files data .pipe(tap(function(file) { // save the file contents in memory memory[path.basename(file.path)] file.contents.toString(); })); }); gulp.task(load-versions, function() { memory.versions {}; // read the version files from the disk return gulp.src(src/versions/version.*.js) // tap into the stream to get each files data .pipe( tap(function(file) { // save the file contents in the assets memory.versions[path.basename(file.path)] file.contents.toString(); })); }); gulp.task(write-versions, function() { // we store all the different version file names in an array var availableVersions Object.keys(memory.versions); // we make an array to store all the stream promises var streams []; availableVersions.forEach(function(v) { // make a new stream with fake file name var stream source(final. v); var streamEnd stream; // we load the data from the concatenated libs var fileContents memory[libs.concat.js] // we add the versions data \n memory.versions[v]; // write the file contents to the stream stream.write(fileContents); process.nextTick(function() { // in the next process cycle, end the stream stream.end(); }); streamEnd streamEnd // transform the raw data into the stream, into a vinyl object/file .pipe(vinylBuffer()) //.pipe(tap(function(file) { /* do something with the file contents here */ })) .pipe(gulp.dest(output)); // add the end of the stream, otherwise the task would finish before all the processing // is done streams.push(streamEnd); }); return es.merge.apply(this, streams); }); // our main task gulp.task(default, gulp.series( // load the files in parallel gulp.parallel(load-lib-files, load-versions), // ready to write once all resources are in memory write-versions ) ); // our watcher task // only watch after having run default once so that all resources // are already in memory gulp.task(watch, gulp.series( default, function() { gulp.watch(./src/libs/*.js, gulp.series( load-lib-files, write-versions )); gulp.watch(./src/versions/*.js, gulp.series( load-lib-files, write-versions )); } ));关键点逐段拆解内存缓存memory对象与gulp-tapload-lib-files与load-versions两个任务负责把磁盘内容搬进内存对象gulp.src(src/libs/*.js)读入库文件concat(libs.concat.js)把它们合并为一个名为libs.concat.js的虚拟文件内容仍是流中的 Vinyl 对象并未落盘tap()在流经过时回调每个file用file.contents.toString()取出 Buffer 内容并以path.basename(file.path)为键存入memory。这里memory是一个普通的模块级对象跨任务共享——这是“先把资源加载到内存再集中使用”这一策略的载体。注意file.contents此时是 Buffer可直接调用toString()参见 docs/api/vinyl.md 中对contents属性“ReadableStream / Buffer / null”的说明若内容为流则需先缓冲才能同步读取。凭空造流vinyl-source-streamwrite-versions任务的核心是下面这段var stream source(final. v); // 假文件名 var fileContents memory[libs.concat.js] \n memory.versions[v]; stream.write(fileContents); // 把字符串写入流 process.nextTick(function() { stream.end(); // 下一轮事件循环结束流 }); streamEnd streamEnd .pipe(vinylBuffer()) .pipe(gulp.dest(output));要点source(final. v)创建一个流同时给它一个“假文件名”如final.version.1.js。vinyl-source-stream负责把写入的原始数据字符串或 Buffer转换成带有path的 Vinyl 对象——这是它替代gulp.src()的位置。stream.write(fileContents)把拼接好的内容写入流。由于写入方write-versions任务与消费方下游管道在同一同步代码段内需要在下一个事件循环周期再stream.end()即用process.nextTick包裹保证流能先处理已写入的数据这是该配方中容易踩坑的关键细节。.pipe(vinylBuffer())把流式内容转换为 Buffer 形态的 Vinyl 对象contents为 Buffer确保后续gulp.dest()能正常落盘示例中还注释了一行tap()提示你可以在落盘前对最终文件内容做二次处理如压缩、注入时间戳等。.pipe(gulp.dest(output))是流的终点把每个内存构造出的 Vinyl 对象写到output目录文件名取流创建时给定的假名final.version.1.js等。dest()会依据 Vinyl 对象的base/path计算输出路径详见 docs/api/dest.md。异步完成为什么必须es.merge多个流write-versions需要为每个版本生成一个独立流然后返回合并结果给 gulp 作为任务完成信号streams.push(streamEnd); // ... return es.merge.apply(this, streams);gulp 任务通过“返回值”来判定是否完成——返回流、Promise、EventEmitter、child process 或 observable 均可见 docs/getting-started/4-async-completion.md。这里没有返回单个流而是多个流因此必须用event-stream的merge把所有流的结束事件合并成一个否则任务会在各流尚未写完时就提前结束导致输出文件不完整。注释里也明确写到add the end of the stream, otherwise the task would finish before all the processing is done。任务编排series / parallel 的正确姿势gulp.task(default, gulp.series( gulp.parallel(load-lib-files, load-versions), write-versions ));两个“加载”任务互不依赖用gulp.parallel并行执行加速资源准备write-versions依赖内存中的全部资源必须放在series的第二个位置串行执行gulp.series/gulp.parallel是本仓库 index.js 中从undertaker继承的任务编排能力Gulp.prototype上绑定了series、parallel、task、watch等方法遵循 error-first 完成约定任一任务出错都会中断整个组合。监听任务watch 的先后顺序gulp.task(watch, gulp.series( default, function() { gulp.watch(./src/libs/*.js, gulp.series(load-lib-files, write-versions)); gulp.watch(./src/versions/*.js, gulp.series(load-lib-files, write-versions)); } ));这里有个刻意设计的细节watch任务先执行default一次把 libs 与 versions 都载入内存再启动监听。因为后续监听回调复用了memory缓存若内存中没有初始数据write-versions会拿到空对象。gulp.watch()支持 globs 与组合任务事件触发时默认有 200ms 延迟合并、queue排队等行为具体选项见 docs/api/watch.md。与源码的印证gulp 为何能“凭空造流”从仓库源码看这种做法的可行性根植于 gulp 对 Vinyl 的抽象在 index.js 中Gulp.prototype.src vfs.src; Gulp.prototype.dest vfs.dest;即src()/dest()来自vinyl-fs这个“Vinyl 适配器”详见 docs/api/concepts.md。src()只是“产生 Vinyl 对象”的一种来源而非唯一来源——只要流中流动的是合法 Vinyl 对象dest()并不关心它来自磁盘还是内存。在 docs/api/vinyl.md 中Vinyl 被定义为“虚拟文件格式”src()读取文件时生成 Vinyl 对象包含路径、内容与元数据当需要自行创建 Vinyl 对象时应使用外部的vinyl模块。vinyl-source-stream正是这条思路的实践它用假路径创建 Vinyl把写入的内容填进contents。在 docs/api/dest.md 中dest()的职责是“把 Vinyl 对象写到文件系统”并在写盘后更新对象的cwd、base、path与stat。这说明整条管道的语义是“Vinyl 对象流”入口无关紧要。仓库 package.json 中的依赖vinyl-fs、undertaker、glob-watcher分别支撑了src/dest、任务编排与watch而配方用到的vinyl-source-stream、vinyl-buffer、gulp-tap、event-stream属于生态插件与本仓库无直接耦合——这也解释了为何该配方可以独立于 gulp 核心版本演进。其他可行方案直接用vinyl模块构造配方给出的是一条“插件组合”路线。若希望更底层地控制也可以脱离vinyl-source-stream直接用vinyl模块构造 Vinyl 对象并放入流中Readable或through2.objconst Vinyl require(vinyl); const { Readable } require(stream); const file new Vinyl({ path: final.version.1.js, contents: Buffer.from(...拼接好的内容...) }); const stream Readable.from([file]); stream.pipe(dest(output));这种写法更贴近 docs/api/vinyl.md 的官方用法new Vinyl({ path, contents })适合需要精细控制cwd、base、stat等元数据的场景而配方中的vinyl-source-stream路线胜在写法直观、与流式管道衔接自然。二者本质相同向管道注入携带内容与路径的 Vinyl 对象。小结本配方展示了 gulp 管道的一种通用能力管道的输入不必来自文件系统。通过vinyl-source-stream或vinyl模块为内存内容配上假文件名写入后经vinyl-buffer规整再交给gulp.dest()落盘即可完成“从 Buffer 构建流”。配套要点包括用gulp-tap把流中文件内容缓存进内存对象实现跨任务共享用process.nextTick延迟stream.end()保证写入先于结束用event-stream的merge合并多个流作为任务的异步完成信号用series/parallel精确控制加载与写出的先后关系用“先跑一遍default再watch”保证内存缓存就绪。掌握这一模式后凡是“数据已在内存、却想复用 gulp 管道与插件生态”的场景——模板渲染、代码拼接、动态生成清单文件等——都可以绕开磁盘中转直接用 gulp 的流式能力完成。【免费下载链接】gulpA toolkit to automate enhance your workflow项目地址: https://gitcode.com/gh_mirrors/gu/gulp创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
分享:

看完干货,该让你的企业上线了

免费需求沟通 · 48 小时内出具建站方案 · 河南本地可上门