shri shri - 11 months ago 95
Node.js Question

Change src of script from bower_components to cdn though gulp

I have some scripts tag

<script type="text/javascript" src="bower_components/angular/angular.min.js" cdn=""></script>

<script type="text/javascript" src="bower_components/angular-messages/angular-messages.min.js" cdn=""></script>

which I want to change to

<script type="text/javascript" src=""></script>

<script type="text/javascript" src=""></script>

I've tried cdnify but we have to code everything in gulp itself.
I want more control from HTML file itself, so it will be easy to control if I am using different cdn urls.

Answer Source

You can utilize gulp-replace to alter your html files, and use a regex /src="(.+?)"\scdn="(.+?)"/gmi

This should work:

var replace = require('gulp-replace');

gulp.task('replace-cdn', function(){
    .pipe(replace(/src="(.+?)"\scdn="(.+?)"/gmi, 'src="$2"'))
    .pipe(gulp.dest('build '));