Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including gulp-useref-cdn with all npm packages installed. Try it out:

var gulpUserefCdn = require("gulp-useref-cdn")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

gulp-useref-cdn v0.1.0

Parse build blocks in HTML files to replace references and automatically upload to qiniu cdn.


Build Status

Parse build blocks in HTML files to replace references and automatically upload to qiniu cdn.

Inspired by grunt-useref and gulp-useref. It can handle file concatenation but not minification. And it can upload the concatenated file to cdn automatically. Files are then passed down the stream. For minification of assets or other modifications, use gulp-if to conditionally handle specific types of assets.

Note: lot of code from gulp-useref. gulp-useref doesn't apply a way to manage html file analysis or a callback after html file analysis. This is why I don't make gulp-useref as lib dependency and rewrite based on it.


Install with npm

npm install --save-dev gulp-useref-cdn


The following example will parse the build blocks in the HTML, replace them, upload to cdn, and pass those files through. Assets inside the build blocks will be concatenated and passed through in a stream as well.

var gulp = require('gulp'),
    useref = require('gulp-useref-cdn');

gulp.task('default', function () {
    var assets = useref.assets();

    return gulp.src('app/*.html')

More details: All gulp-useref's syntax and usage are supported by gulp-useref-cdn.

What's important here is that an extended syntax/usage is added!

<!-- build:<type>(alternate search path) <path> <attrs> -->

Before, type could be either js, css or remove. And the new cdn is added now.

When you use cdn as the type, no need to write js or css anymore. The plugin will detect it automatically, and it will do the work as expected when you use js|css.

And with the cdn type, the plugin will upload the concatenated file to qiniu cdn.

An example of this in completed form can be seen below:

    <!-- build:cdn css/combined.css -->
    <link href="css/one.css" rel="stylesheet">
    <link href="css/two.css" rel="stylesheet">
    <!-- endbuild -->
    <!-- build:cdn scripts/combined.js <bucket='xxx'> <key='yyy'>-->
    <script type="text/javascript" src="scripts/one.js"></script>
    <script type="text/javascript" src="scripts/two.js"></script>
    <!-- endbuild -->
import cdn from 'gulp-useref-cdn';
let assets = cdn.assets({
    accessKey: 'ACCESS_KEY',
    secretKey: 'SECRET_KEY',
    domain: 'DOMAIN',
    bucket: 'BUCKET', // could be replaced if you rewrite at html <bucket='xxx'>
    keyPrefix: ''

    .on('error', (err) => {

The resulting HTML would be:

    <link rel="stylesheet" href=""/>
    <script src=""></script>

And you will see combined-ce2a0c47.js and combined-f9ebd308.css in your qiniu cdn's bucket.



Returns a stream with the asset replaced resulting HTML files. Supports all options from useref.


Returns a stream with the concatenated asset files from the build blocks inside the HTML.


Type: String or Array
Default: none

Specify the location to search for asset files, relative to the current working directory. Can be a string or array of strings.


Type: Boolean
Default: false

Skip concatenation and add all assets to the stream instead.


Type: Boolean
Default: true

Set to false if you want to see messages except error.


Type: Number
Default: 8


Type: String
Default: gulp-useref-cdn/

The above two are all about key. Key is defined by Qiniu. Key can be set clearly via <key='yyy'> in comment.

When key is not offered, plugin will generate the key:

prefix + filename + '-' + md5 + '.' + filetype;
  • prefix --- options.keyPrefix
  • md5 --- the md5 string of the file, options.md5 specify md5's length(after cut).
  • filename, filetype are offered by plugin.


Brings back the previously filtered out HTML files.


Copyright (c) 2015 creeperyang. Licensed under the MIT license.


RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free