Gulp Header is a Gulp plugin that adds customizable headers to files in your build pipeline. It supports both static text headers and dynamic template-based headers using lodash template syntax or ES6 template strings, allowing for the inclusion of package metadata, build information, and custom data.
npm install --save-dev gulp-headerconst header = require('gulp-header');For ES6 modules:
import header from 'gulp-header';const gulp = require('gulp');
const header = require('gulp-header');
// Add literal text header
gulp.src('./src/*.js')
.pipe(header('/* This is a header */\n'))
.pipe(gulp.dest('./dist/'));
// Add template-based header with data
const pkg = require('./package.json');
const banner = [
'/**',
' * <%= pkg.name %> - <%= pkg.description %>',
' * @version v<%= pkg.version %>',
' * @license <%= pkg.license %>',
' */',
''
].join('\n');
gulp.src('./src/*.js')
.pipe(header(banner, { pkg: pkg }))
.pipe(gulp.dest('./dist/'));Main plugin function that creates a transform stream to add headers to files in the Gulp pipeline.
/**
* Creates a Gulp transform stream that adds headers to files
* @param {string} headerText - The header text to add (supports templates)
* @param {Object|false} data - Template data object or false to disable templating
* @returns {NodeJS.Transform} Transform stream for Gulp pipeline
*/
function header(headerText, data);
// Type definitions for Vinyl file objects used in templates
interface VinylFile {
path: string; // Full file path
relative: string; // Path relative to base
base: string; // Base directory path
contents: Buffer | NodeJS.ReadableStream | null;
data?: any; // Custom data attached to file
sourceMap?: any; // Source map information
isBuffer(): boolean; // Check if contents is Buffer
isStream(): boolean; // Check if contents is Stream
isDirectory(): boolean; // Check if file is directory
}headerText (string, optional)
'' (empty string)<%= variable %> (e.g., <%= pkg.name %>)${variable} (e.g., ${pkg.version})data (Object|false, optional)
{} (empty object)false, disables template processing and treats headerText as literal stringfile, filename) and any custom file.data propertiesfile.data, which overrides built-in variablesThe following variables are automatically available in templates:
file.path, file.relative, file.base)path.basename(file.path))Gulp Header supports two template syntax formats:
<%= variable %> - Standard lodash template format${variable} - ES6-style variable interpolationReturns a Node.js transform stream compatible with Gulp's streaming system that:
gulp.src('./src/*.js')
.pipe(header('/* Copyright 2023 My Company */\n'))
.pipe(gulp.dest('./dist/'));const pkg = require('./package.json');
const banner = [
'/**',
' * <%= pkg.name %> v<%= pkg.version %>',
' * <%= pkg.description %>',
' * @author <%= pkg.author %>',
' * @license <%= pkg.license %>',
' */',
''
].join('\n');
gulp.src('./src/*.js')
.pipe(header(banner, { pkg: pkg }))
.pipe(gulp.dest('./dist/'));// ES6 template syntax with custom data
gulp.src('./src/*.js')
.pipe(header('/* Built on ${date} by ${author} */\n', {
date: new Date().toISOString(),
author: 'Build System'
}))
.pipe(gulp.dest('./dist/'));
// ES6 syntax with file properties
gulp.src('./src/*.js')
.pipe(header('/* Processing: ${file.relative} */\n'))
.pipe(gulp.dest('./dist/'));const fs = require('fs');
const headerText = fs.readFileSync('header.txt', 'utf8');
gulp.src('./src/*.js')
.pipe(header(headerText, { version: '1.0.0' }))
.pipe(gulp.dest('./dist/'));// Use false as second parameter to treat header as literal text
gulp.src('./src/*.js')
.pipe(header('/* Literal text with <%= symbols %> */\n', false))
.pipe(gulp.dest('./dist/'));// Using filename and file properties
gulp.src('./src/*.js')
.pipe(header('/* File: <%= filename %> - Path: <%= file.relative %> - Generated: <%= date %> */\n', {
date: new Date().toLocaleDateString()
}))
.pipe(gulp.dest('./dist/'));
// Accessing file data properties
gulp.src('./src/*.js')
.pipe(header('/* License: <%= license %> - Version: <%= version %> */\n'))
.pipe(gulp.dest('./dist/'));
// Note: This assumes files have data.license and data.version propertiesconst sourcemaps = require('gulp-sourcemaps');
const coffee = require('gulp-coffee');
gulp.src('./src/*.coffee')
.pipe(header(banner, { pkg: pkg })) // Add header before sourcemaps
.pipe(sourcemaps.init())
.pipe(coffee({ bare: true }))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('./dist/'));The plugin handles different file types appropriately:
The plugin includes built-in error handling and file validation:
isExistingFile() to check if a file is valid for processingGulp Header automatically preserves and updates source map information when present, ensuring debugging capabilities are maintained in the build output. Source maps are handled through the concat-with-sourcemaps dependency.
The plugin relies on these key dependencies for its functionality: