Search code examples
javascriptnode.jsgulpbuild-system

Gulp is not outputing files to correct folders in build folder


In my project I have a directory structure that looks like this in the src folder:

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

What I want to do is to be able to process the index.html file in the root directory and at the same time process all the html files in the templates folder.

What I have in my gulp file is this:

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

However when I run the task, files in the templates folder also end up being written directly to the build folder along with index.html, no templates folder gets created in the build folder:

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

When I use this as the call to gulp.src:

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

I do get what I want and the templates folder get created in the build folder but I'm not sure if that's the way to do it properly, mabye it's less performant and it will include every single html file in the src directory not just under templates.


Solution

  • If I understand correctly, the problem is that gulp doesn't know where is the "base" folder when you give gulp.src an array.

    try doing the following:

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

    This way, gulp knows which folder is your base folder and will first copy the folder structure from 'src'. After that, gulp will copy the files to the right location.