Front-Matter Based File Renaming Middleware
raw JSON →middleware-rename-file is a Node.js middleware designed for dynamically renaming Vinyl file objects by leveraging properties defined within their YAML front-matter. Currently at version 1.0.0, the package appears to be in a maintenance state, with no active feature development. It targets older Node.js environments (engines >=0.10.0) and is primarily integrated with established templating and static site generation frameworks such as Assemble, Verb, Generate, and Templates. The core functionality allows developers to specify new `basename` and `dirname` attributes directly in a file's front-matter, which the middleware then applies to the file's destination path early in the build pipeline, typically via an `onLoad` event. This declarative approach to file renaming is a key differentiator, providing a structured way to manage file output within its target ecosystems.
Common errors
error TypeError: rename is not a function ↓
require: const renameMiddleware = require('middleware-rename-file');. error File not renamed as expected ↓
rename key and its sub-properties. If a filter function is used, ensure it returns true for files that should be renamed. Warnings
gotcha This package is designed for CommonJS environments and does not provide native ES module support. Attempting to use `import` syntax will result in errors. ↓
gotcha The package relies on a specific front-matter structure (`rename: { basename: '...', dirname: '...' }`). Incorrectly formatted or missing `rename` properties will prevent files from being renamed as expected. ↓
deprecated The package specifies Node.js engine compatibility as `>=0.10.0`, indicating it was developed for very old Node.js versions. While its simple functionality might still work, compatibility with modern Node.js versions is not explicitly guaranteed or tested. ↓
gotcha This middleware is specifically designed to integrate with the 'templates' ecosystem (Assemble, Verb, Generate). Its usage outside of these frameworks, particularly with generic Gulp or other build systems, may require manual integration steps for parsing front-matter and handling Vinyl file objects. ↓
Install
npm install middleware-rename-file yarn add middleware-rename-file pnpm add middleware-rename-file Imports
- renameMiddleware wrong
import renameMiddleware from 'middleware-rename-file';correctconst renameMiddleware = require('middleware-rename-file');
Quickstart
const renameMiddleware = require('middleware-rename-file');
// Mock a Vinyl-like file object with front-matter
const fileWithFrontMatter = {
path: 'scaffolds/layouts/base.hbs',
contents: Buffer.from('---\nrename:\n basename: default.hbs\n dirname: templates\n---\n\n<!DOCTYPE html>...'),
data: { // `data` property where front-matter is usually parsed
rename: {
basename: 'default.hbs',
dirname: 'templates'
}
},
// Simulate Vinyl file properties
basename: 'base.hbs',
dirname: 'scaffolds/layouts',
extname: '.hbs',
stem: 'base'
};
const fileWithoutFrontMatter = {
path: 'src/pages/index.html',
contents: Buffer.from('<html>...</html>'),
data: {},
basename: 'index.html',
dirname: 'src/pages',
extname: '.html',
stem: 'index'
};
const mockApp = {
emit: (event, file) => {
// In a real app, this would be `app.emit('file', file)`
// or similar, passing the transformed file down the pipeline.
console.log(`
Processed file: ${file.path}`);
console.log(` New basename: ${file.basename}`);
console.log(` New dirname: ${file.dirname}`);
}
};
// Initialize the middleware
const rename = renameMiddleware();
// Apply middleware to the first file
rename(fileWithFrontMatter, mockApp);
// Apply middleware to the second file with a filter that prevents renaming 'index' files
const renameWithFilter = renameMiddleware(file => file.stem !== 'index');
renameWithFilter(fileWithoutFrontMatter, mockApp);