Notes // Adding a Header Comment with Gulp

How to add a header comment to a file using gulp.

I was uglifying some javascript using node and gulp and wanted to add a comment header to the top of the file. Gulp-header to the rescue!

This feeds the information from package.json:

var gulp = require("gulp");
var header = require("gulp-header");

// Import package.json as a local data source
var pkg = require("./package.json");
var comment = [
    "/**",
    " * <%= pkg.name %> - <%= pkg.description %>",
    " * @author <%= pkg.author %>",
    " * @version v<%= pkg.version %>",
    " * @link <%= pkg.homepage %>",
    " * @license <%= pkg.license %>",
    " */",
    ""
].join("\n");

gulp.task("add-header", function() {
    gulp
        .src(["sourcefile.js"])
        .pipe(header(comment, { pkg: pkg }))
        .pipe(gulp.dest("."));
});

The output in the JavaScript file will look something like:

/**
 * Name - Description
 * @author Destin Moulton
 * @version v0.3.0
 * @link https://github.com/destinmoulton/projectname
 * @license MIT
 */
Written by
Published
Email destin{AT}destinmoulton.com if you have any comments or questions.