【问题标题】:using gulp-htmlbuild without concatenating all files into a single file使用 gulp-htmlbuild 而不将所有文件连接到一个文件中
【发布时间】:2023-03-30 14:55:02
【问题描述】:

我想:

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

default example of the package 中的用例与我想要的类似,但不是第 3 步和第 4 步,它只是将它们全部连接到具有预定义名称的单个文件中,并写入指向该文件的单个脚本标记。

【问题讨论】:

    标签: javascript html build gulp


    【解决方案1】:

    您在 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 将是一个转换流,它接受作为字符串的路径并将它们重命名为目标目录。

    【讨论】:

    • block.pipe(block) 是我想要的!但是这里还是有点问题!当我使用package example 中的gulpSrc 函数创建源流时,流中的所有文件都会丢失其路径,并且将流通过管道传输到gulp.dest("myTarget") 将输出myTarget 文件夹中的所有文件而不保留相对路径!
    • 我通过将{base: "MySrcDirectory"} 传递给gulp.src 的第二个参数options 有点想通了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多