Gulp: Grab files from your CDN and add them to your build pipeline
13 October 2015 by @ddprrt | Posted in: gulp, tools
This one is a shorty, but that’s what it makes it so nice. Imagine that you have only one dependency in your project, which is some third party library your code builds upon, like jQuery. Instead of having the complete dependency management stack on your shoulders, you just want to use that single file.
Usually, you would use a CDN, but you prefer it having it added to your bundle.
With a library called
request, which allows you to fetch files from a webserver
and access the contents in a stream format, you can do the following in Gulp:
- We request the latest jQuery version from the jQuery CDN. The
requestpackage allows for streaming. What we get in return is a readable stream.
- We create a valid vinyl file object with
vinyl-source-stream. This makes it compatible with Gulp
- Our main file is selected from the file system as usual
merge2package allows us to combine both streams
- The contents of both streams are converted to text buffers
gulp-concatcan handle them.
The use case for this might be limited, but it’s good to know that you can pipe any web resource to your build pipeline. Think of assets that are available online, or any data you want to prepare for your static site generator.
The awesome power of streams makes this possible.
Me again. The Gulp, Yeoman, Bower book is pretty sweet. Just saying.
Comments? Shoot me a tweet!