Gulp plugin for renaming files in build pipelines with flexible path transformation capabilities
npx @tessl/cli install tessl/npm-gulp-rename@2.1.0Gulp rename is a Gulp plugin that provides flexible file renaming capabilities in build pipelines. It supports multiple renaming approaches including string paths, mutation functions, mapping functions, and configuration objects, enabling comprehensive path manipulation with built-in sourcemap handling.
npm install gulp-renameconst rename = require("gulp-rename");const gulp = require("gulp");
const rename = require("gulp-rename");
// Rename to a fixed path
gulp.src("./src/main/text/hello.txt")
.pipe(rename("main/text/ciao/goodbye.md"))
.pipe(gulp.dest("./dist"));
// Rename using configuration object
gulp.src("./src/**/hello.txt")
.pipe(rename({
dirname: "main/text/ciao",
basename: "aloha",
prefix: "bonjour-",
suffix: "-hola",
extname: ".md"
}))
.pipe(gulp.dest("./dist"));Creates a Transform stream that renames files based on the provided renaming parameter.
/**
* Creates a Transform stream that renames files
* @param {string|function|object} obj - Renaming parameter defining how files should be renamed
* @param {object} [options] - Configuration options for the plugin
* @returns {Stream.Transform} Transform stream that renames files
* @throws {Error} "Unsupported renaming parameter type supplied" for invalid parameter types
*/
function gulpRename(obj, options);Sets the output file path directly by replacing the entire relative path.
/**
* @param {string} obj - Non-empty string defining the new file path
*/Usage Example:
// Replace entire path
gulp.src("test/fixtures/hello.txt")
.pipe(rename("hola.md"))
.pipe(gulp.dest("./dist"));
// Result: ./dist/hola.md
// Set relative path with directory globs
gulp.src("test/**/hello.txt")
.pipe(rename("fixtures/hola.md"))
.pipe(gulp.dest("./dist"));
// Result: ./dist/test/fixtures/hola.mdTransforms paths using either mutating or mapping functions for dynamic renaming logic.
/**
* @param {function} obj - Function that receives parsed path and file objects
* @param {object} parsedPath - Object with dirname, basename, extname properties
* @param {object} file - Complete vinyl file object with all file metadata
* @returns {object|undefined} New path object (replaces parsedPath) or undefined (uses modified parsedPath)
*/Mutating Function Usage:
// Modify path object in-place
gulp.src("./src/**/hello.txt")
.pipe(rename(function (path) {
path.dirname += "/ciao";
path.basename += "-goodbye";
path.extname = ".md";
}))
.pipe(gulp.dest("./dist"));
// Result: ./dist/main/text/ciao/hello-goodbye.mdMapping Function Usage:
// Return completely new path object
gulp.src("./src/**/hello.txt")
.pipe(rename(function (path) {
return {
dirname: path.dirname + "/ciao",
basename: path.basename + "-goodbye",
extname: ".md"
};
}))
.pipe(gulp.dest("./dist"));
// Result: ./dist/main/text/ciao/hello-goodbye.mdDeclaratively configures path component replacements using a configuration object.
/**
* @param {object} obj - Configuration object for path component replacements
* @param {string} [obj.dirname] - Replaces the directory path
* @param {string} [obj.prefix] - Prepends to the basename
* @param {string} [obj.suffix] - Appends to the basename
* @param {string} [obj.basename] - Replaces the basename
* @param {string} [obj.extname] - Replaces the file extension
*/Usage Examples:
// Replace directory
gulp.src("test/**/hello.txt")
.pipe(rename({ dirname: "elsewhere" }))
.pipe(gulp.dest("./dist"));
// Result: ./dist/test/elsewhere/hello.txt
// Add prefix and suffix
gulp.src("test/**/hello.txt")
.pipe(rename({
prefix: "bonjour-",
suffix: "-hola"
}))
.pipe(gulp.dest("./dist"));
// Result: ./dist/test/fixtures/bonjour-hello-hola.txt
// Change extension
gulp.src("test/**/hello.txt")
.pipe(rename({ extname: ".md" }))
.pipe(gulp.dest("./dist"));
// Result: ./dist/test/fixtures/hello.md
// Remove directory (flatten)
gulp.src("test/**/hello.txt")
.pipe(rename({ dirname: "" }))
.pipe(gulp.dest("./dist"));
// Result: ./dist/test/hello.txtOptional configuration parameter that modifies plugin behavior.
/**
* @param {object} [options] - Configuration options
* @param {boolean} [options.multiExt=false] - Include multiple extensions in extname (e.g., ".min.js" instead of just ".js")
*/Usage Example:
// Handle multiple extensions
gulp.src("test/fixtures/hello.min.txt")
.pipe(rename({ basename: "goodbye" }, { multiExt: true }))
.pipe(gulp.dest("./dist"));
// Without multiExt: goodbye.txt (basename: "hello.min", extname: ".txt")
// With multiExt: goodbye.min.txt (basename: "hello", extname: ".min.txt")/**
* Path object structure used in function parameters
*/
interface ParsedPath {
/** Directory path relative to base */
dirname: string;
/** Filename without extension */
basename: string;
/** File extension including the dot */
extname: string;
}
/**
* Options object for configuring plugin behavior
*/
interface RenameOptions {
/** Include multiple extensions in extname when true */
multiExt?: boolean;
}
/**
* Configuration object for declarative path renaming
*/
interface RenameConfig {
/** Replaces the directory path */
dirname?: string;
/** Prepends to the basename */
prefix?: string;
/** Appends to the basename */
suffix?: string;
/** Replaces the basename */
basename?: string;
/** Replaces the file extension */
extname?: string;
}The plugin validates the renaming parameter and throws errors for unsupported types:
// These will throw errors
gulp.src("test/**/hello.txt")
.pipe(rename()) // undefined
.pipe(rename(null)) // null
.pipe(rename("")) // empty string
.pipe(rename(true)) // boolean
.pipe(rename(123)) // numberThe plugin automatically handles sourcemaps when present. When a file has an associated sourcemap, the plugin updates the sourcemap's file property to match the renamed file's relative path.
const sourcemaps = require("gulp-sourcemaps");
gulp.src("src/**/*.js")
.pipe(sourcemaps.init())
.pipe(rename({ suffix: ".min" }))
.pipe(sourcemaps.write())
.pipe(gulp.dest("dist"));
// Sourcemap file property automatically updated to match renamed fileThe dirname property behavior depends on the glob pattern used in gulp.src():
dirname is "."*.txt): dirname is "."**/, *, [...], {...}): dirname is the path from the directory glob to the filegulp.src() uses base option, dirname is the path from the given base directory to the fileBy default, extensions are parsed using Node.js Path.extname() behavior:
hello.txt → basename: "hello", extname: ".txt"hello.min.txt → basename: "hello.min", extname: ".txt"With multiExt: true option:
hello.min.txt → basename: "hello", extname: ".min.txt"When using function parameters, a second file argument provides the complete vinyl file object with properties like path, base, basename, relative, and extname.