使用 gulp-htmlbuild 而不将所有文件连接成一个文件

using gulp-htmlbuild without concatenating all files into a single file

本文关键字:文件 一个 连接 使用 gulp-htmlbuild      更新时间:2023-09-26

我想:

  1. 读取 htmlbuild:js 块中的所有 js 文件
  2. 将它们通过管道进入一系列构建步骤,如丑化、语法检查等。
  3. 将它们(同时保留其目录结构)复制到目标文件夹(实际上是 html 构建目标)
  4. 再次在 htmlbuild:js 中添加指向所有这些链接的链接

包的默认示例中的用例类似于我想要的,但不是步骤 3 和 4,它只是将它们全部连接到具有预定义名称的单个文件中,并写入指向该文件的单个脚本标记。

您在

htmlbuild 回调函数中收到的block是一个可写流。无论你写什么,最终都会成为块的替换。在示例中,只有一条路径写入它,但没有什么可以阻止您编写多个路径:

// then write the build result path to it 
block.write('path1.js');
block.write('path2.js');
block.write('path3.js');
block.write('path4.js');
block.end();

由于它也是一个可读的流,你可以通过管道将它传送到自身或对其应用转换:

// First build the files that are in the block
block
  .pipe(buildSteps)
  .pipe(gulp.dest(targetDir));
// Then rename all the paths and write back to the block
block
  .pipe(renameToDestination)
  .pipe(block);

在此示例中,renameToDestination将是一个转换流,它接受路径作为字符串并将其重命名为目标目录。