Gulp 未将文件输出到构建文件夹中的正确文件夹

Gulp is not outputing files to correct folders in build folder

在我的项目中,src 文件夹中的目录结构如下所示:

|-/css/
|-/templates/
|------------template1.html
|-index.html

我想做的是能够处理根目录下的index.html文件,同时处理templates文件夹下的所有html文件

我的 gulp 文件中的内容是这样的:

gulp.src(["src/*.html", "src/templates/**/*"])
.pipe(gulp.dest("build/"));

然而,当我 运行 任务时,模板文件夹中的文件也最终与 index.html 一起直接写入构建文件夹,构建文件夹中没有创建模板文件夹:

|-/css/
|-template1.html
|-index.html

当我使用它作为对 gulp.src 的调用时:

gulp.src("src/**/*.html")

我确实得到了我想要的东西,并且在构建文件夹中创建了模板文件夹,但我不确定这是否是正确的方法,也许它的性能较差,但它将包含每个 html 文件在 src 目录中,而不仅仅是在模板下。

如果我理解正确,问题是当你给 gulp.src 一个数组时 gulp 不知道 "base" 文件夹在哪里。

尝试执行以下操作:

gulp.src(["src/*.html", "src/templates/**/*"], {
             base: 'src'
        }).pipe(gulp.dest("build/"));

这样,gulp 就知道哪个文件夹是您的基本文件夹,并且会首先从 'src' 复制文件夹结构。之后,gulp 会将文件复制到正确的位置。