Gulp: Grabbing JavaScript assets from a CDN to add to your build pipeline
var gulp = require('gulp'); | |
var source = require('vinyl-source-stream'); | |
var request = require('request'); | |
var merge = require('merge2'); | |
var concat = require('gulp-concat'); | |
var buffer = require('gulp-buffer'); | |
/** | |
* 1. We request the latest jQuery version from the jQuery CDN. The | |
* request package allows for streaming. What we get in return | |
* is a readable stream. | |
* 2. We create a valid vinyl file object with vinyl source stream | |
* This makes it compatible with Gulp | |
* 3. Our main file is selected from the file system as usual | |
* 4. The merge2 package allows us to combine both streams | |
* 5. The contents of both streams are converted to text buffers | |
* so gulp-concat can handle them. | |
*/ | |
gulp.task('js', function() { | |
var jquery = request('http://code.jquery.com/jquery-latest.js') /* 1 */ | |
.pipe(source('jquery.js')); /* 2 */ | |
var main = gulp.src('main.js'); /* 3 */ | |
return merge(jquery, main) /* 4 */ | |
.pipe(buffer()) /* 5 */ | |
.pipe(concat('main.min.js')) | |
.pipe(gulp.dest('dist')); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment