Vinyl adapter for the file system providing streaming file operations.
—
Stream-based file reading functionality that creates a readable stream of Vinyl File objects from glob patterns. Supports advanced features like encoding transformation, sourcemap handling, and symbolic link resolution.
Creates a readable stream of Vinyl File objects from glob patterns with comprehensive options for file processing.
/**
* Creates a readable stream of Vinyl File objects from glob patterns
* @param {string|string[]} globs - Glob pattern(s) to match files
* @param {Object} [options] - Configuration options
* @returns {Transform} Transform stream that produces Vinyl File objects
* @throws {Error} When glob argument is invalid
*/
function src(globs, options);
interface SrcOptions {
/** Buffer file contents into memory (default: true) */
buffer?: boolean;
/** Whether to read file contents (default: true) */
read?: boolean;
/** Only stream files modified since timestamp */
since?: Date | number;
/** Remove UTF-8 BOM from files (default: true) */
removeBOM?: boolean;
/** File encoding for transcoding (default: 'utf8') */
encoding?: string | boolean;
/** Enable sourcemap support (default: false) */
sourcemaps?: boolean;
/** Resolve symlinks to targets (default: true) */
resolveSymlinks?: boolean;
/** Match dot files in globs (default: false) */
dot?: boolean;
/** Working directory for relative globs */
cwd?: string;
/** Base directory for relative paths */
base?: string;
}Glob patterns are executed in order, so negations should follow positive globs.
// Correct: excludes files starting with 'b'
vfs.src(['*', '!b*']);
// Incorrect: does not exclude files starting with 'b'
vfs.src(['!b*', '*']);Controls whether file contents are loaded into memory or streamed.
// Buffer contents into memory (default)
vfs.src('*.js', { buffer: true });
// Stream contents (file.contents will be a readable stream)
vfs.src('*.js', { buffer: false });Controls whether file contents are read at all.
// Read file contents (default)
vfs.src('*.js', { read: true });
// Skip reading contents (file.contents will be null)
vfs.src('*.js', { read: false });Only processes files modified after the specified time.
// Only files modified in the last hour
const oneHourAgo = new Date(Date.now() - 60 * 60 * 1000);
vfs.src('*.js', { since: oneHourAgo });
// Using timestamp
vfs.src('*.js', { since: 1609459200000 });Controls character encoding for file contents.
// Default UTF-8 encoding
vfs.src('*.txt', { encoding: 'utf8' });
// Latin-1 encoding
vfs.src('*.txt', { encoding: 'latin1' });
// No encoding (raw binary)
vfs.src('*.bin', { encoding: false });Enables sourcemap processing for files.
// Load and process sourcemaps
vfs.src('*.js', { sourcemaps: true });Controls symbolic link handling.
// Resolve symlinks to their targets (default)
vfs.src('**/*', { resolveSymlinks: true });
// Preserve symlinks (file.symlink will contain target path)
vfs.src('**/*', { resolveSymlinks: false });The src function throws errors for invalid inputs:
// Throws: "Invalid glob argument: null"
try {
vfs.src(null);
} catch (error) {
console.error(error.message);
}Basic file reading:
const vfs = require('vinyl-fs');
vfs.src('src/**/*.js')
.on('data', (file) => {
console.log('Processing:', file.path);
})
.on('end', () => {
console.log('All files processed');
});Advanced configuration:
vfs.src(['src/**/*.js', '!src/vendor/**'], {
buffer: false, // Stream large files
since: new Date('2023-01-01'), // Only recent files
sourcemaps: true, // Process sourcemaps
encoding: 'utf8' // Ensure UTF-8 encoding
});Function-based options:
vfs.src('**/*', {
// Dynamic encoding based on file extension
encoding: (file) => {
return file.extname === '.bin' ? false : 'utf8';
},
// Conditional reading
read: (file) => {
return file.extname !== '.tmp';
}
});Install with Tessl CLI
npx tessl i tessl/npm-vinyl-fs