gulp-importer
Version:
A simple gulp plugin that allows importing any kind of file to any kind of file. Nevertheless, gulp-importer looks up through dependant files and automatically updates dependency.
136 lines (135 loc) • 5.54 kB
TypeScript
/// <reference types="node" />
import File from 'vinyl';
import { Transform } from 'stream';
declare type FileCache = Record<string, Record<string, File>>;
declare type Transformation = (src: NodeJS.ReadWriteStream) => Transform;
interface ImporterOptions {
[key: string]: any;
regexPattern?: RegExp;
regexGroup?: number;
encoding?: BufferEncoding;
importOnce?: boolean;
importRecursively?: boolean;
dependencyOutput?: "primary" | "dependant" | "all";
disableLog?: boolean;
detailedLog?: boolean;
requireExtension?: boolean;
}
/**
* Provides the gulp API for importing any kind of file to any kind of file.
*/
declare class Importer {
protected readonly options: ImporterOptions;
/**
* Initializes a new instance of this class.
* @param options The configuration options.
*/
constructor(options?: ImporterOptions);
private _cache;
/** The dependancy cache under watch. */
get cache(): FileCache;
/** A stream specific resolve stack, used to insure all chunks in a stream were appropriatly resolved. */
protected _streamResolveStack: string[];
/**
* Resolves the import statements in the recieved buffers/streams.
* @returns {Transform} The transform stream to be added to the pipe chain.
*/
execute(innerPl?: Transformation): Transform;
/**
* Updates imports for dependancies when a primary file gets modified.
* @returns The transform stream to be added to the pipe chain.
*/
updateDependency(): Transform;
/**
* Iterates throught and modifies dependant files.
* @param path The path to the primary file.
* @param predicate The action to resolve a dependant file.
* @returns The promise that represents the asynchronous operation, containing the resolved files.
*/
private iterateCache;
/**
* Resolves buffers for the specified file.
* @param file The file to resolve buffers for.
* @returns The promise that represents the asynchronous operation, containing the resolved file.
*/
private resolveBufferRef;
/**
* Resolves streaming content for the specified file.
* @param file The file whose streaming contents should be resolved.
* @returns The promise that represents the asynchronous operation, containing the resolved file.
*/
private resolveStreamRef;
/**
* Validates an input file.
* @param stream The pipe stream.
* @param file The input file.
* @param cb The transform callback.
* @returns The flag indicating whether the input file is valid.
*/
private validate;
/**
* Appends the speicifed dependancy path to primary relative cache to be triggered on modify.
* @param dpnPath The dependant file to be added to the update cache.
* @param prmPath The primary file that triggers the update.
*/
private appendCache;
/**
* Returns the base64 encoded version of the specified value.
* @param value The value to be encoded.
* @returns The incoded version of the input value.
*/
private static encode;
/**
* Resolves the buffers for the specified file.
* @param file The file whose buffer should be resolved.
* @returns The resolved buffers.
*/
private resolveBuffer;
/**
* Resolves the streaming content for the specified file.
* @param file The file whose streaming content should be resolved.
* @returns The transformed stream for the specified file.
*/
private resolveStream;
/**
* Applies imports on the specified content.
* @param file The file apply imports for.
* @param content The content to apply imports on.
* @param resolveStack The resolve stack to cache the resolved imports.
* @returns The resolved version of the specified content.
*/
private replace;
/**
* Gets the content of the file at the given path and applies the specified transformation, if any.
* @param path The path of the desired file.
* @param transformation The optional transformation pipeline to be applied.
* @returns The promise that represents the asynchronous operation, containing the processed dependency content.
*/
private getDependencyContent;
/**
* Executes the specified transformation for the given input file.
* @param path The path of the file to initiate the pipeline.
* @param transformation The action for building the transformation pipeline.
* @returns The content that's returned by the transformation operation.
*/
private transform;
/**
* Reads the content of the file at the specified path.
* @param path The path of the desired file.
* @returns The promise that represents the asynchronous operation, containing the content of the file, if exists.
*/
private readFile;
/**
* Returns a readable stream of the file at the specified path.
* @param path The path of the desired file.
* @returns The promise that represents the asynchronous operation, containing the readable stream of the file, if exists.
*/
private readStream;
/**
* Normalizes the specified extensionless file path.
* @param path The path to be normalized.
* @returns The normalized version of the specified path.
*/
private normalizePath;
}
export default Importer;