gulp.js prepend and append string to src and href attributes after using gulp.useref

1k views Asked by At

I'm running some html files i na template engine and all assets are declared like this:

<link rel="stylesheet" href="$href('libs/bootstrap.min.css')"> <script src="$href('libs/jquery.js')"></script>

When I run $.useref it does move files to the correct folders, but leaves the .html files with the absolute and relative paths that should normally have. Is there a way I can edit $.useref "template" for script and link tags pointing to the right place and prepending/appending $href('') inside the src and href attributes? Is there any other gulp plugin that can do this?

1

There are 1 answers

0
Samuel Guimarães On

Finally did it with gulp-replace:

gulp.task('templates', function () {
    return gulp.src('dist/**/*.html')
    .pipe(replace(/src="([^"]*)"/g, 'src="$$href(\'$1\')"'))
    .pipe(replace(/href="([^"]*)"/g, 'href="$$href(\'$1\')"'))
    .pipe(gulp.dest('dist'));
});