Search code examples
javascriptgulppug

How to replace string in Gulp?


I'm trying to replace the default pug tag to october cms partial. I can't replace string in file. Pug file for example:

include frames/header.pug

img(src="img/123123.jpg", alt="")
img(src="img/1.jpg", alt="")
include frames/footer.pug

How I can replace:

include frames/footer.pug

on

{% partial 'frames/footer' %}

but the name of included file (footer in this example) can't be known.

I have a function for this, but it's not working.

.pipe(replace(/include frames\/{1}\/.pug/g, "{% partial 'frames/$1' %}"))

Solution

  • Try :

    // .pipe(replace(/frames/(.*).pug/g, "{% partial 'frames/$1' %}"))

    var gulp = require("gulp");
    var  replace  =  require('gulp-replace');
    
    gulp.task('replace', function () {
    
      return gulp.src(['app.pug'])
         .pipe(replace(/frames\/(.*)\.pug/g, "{% partial 'frames/$1' %}"))
        .pipe(gulp.dest('dist'));
    });
    

    app.pug (before):

    include frames/header.pug
    
    img(src="img/123123.jpg", alt="")
    img(src="img/1.jpg", alt="")
    
    include frames/footer.pug
    

    dist/app.pug (after):

    include {% partial 'frames/header' %}
    
    img(src="img/123123.jpg", alt="")
    img(src="img/1.jpg", alt="")
    
    include {% partial 'frames/footer' %}