javascriptgulpmain-bower-files

How to parse path dynamically in gulp.src.options.base


I'm trying to write gulp-based application using main-bower-files. I want to copy all my and vendor assets files to %build_dir%/assets folder, but fonts files should be copied in %build_dir%/assets/fonts. Without vendor files it can be done easy via gulp.src.options.base option. But i can't understand how to do it with vendor files. Now i have

gulp.task('assets', ['less'], function() {
    return gulp.src('src/less/fonts/*', {base: 'src/less'})
        .pipe(addSrc('src/assets/**.*'))
        .pipe(addSrc(mainBowerFiles(/.*woff|woff2|otf|ttf/, {includeDev: true})))
        .pipe(gulp.dest(buildDir + '/assets'));
});

But it copies vendor fonts to %build_dir%/assets. So, ideal case is to write base as regexp that parses path to vendor fonts and take all path before /fonts. How to do it?

Vendor folder has next structure:

/bower_components
   /lib1
      /fonts
         font1.woff
   /lib2
      /fonts
         font2.otf
   /lib3
      /fonts
         font3.ttf

Solution

  • So, ideal case is to write base as regexp that parses path to vendor fonts and take all path before /fonts. How to do it?

    Not possible. The base option doesn't support regexes. Only strings.

    You don't need the base option anyway. Just create two streams each with their own gulp.dest(). Then merge those streams using merge-stream:

    var merge = require('merge-stream');
    
    gulp.task('assets', ['less'], function() {
      return merge(
        gulp.src(['src/less/fonts/*'].concat(mainBowerFiles(/.*woff|woff2|otf|ttf/, {includeDev: true})))
            .pipe(gulp.dest(buildDir + '/assets/fonts')),
        gulp.src('src/assets/**.*')
            .pipe(gulp.dest(buildDir + '/assets')));
    });
    

    This also means you don't need gulp-add-src anymore.