From 15d697b0e21723a4c284a837cddc9c35e86a85a3 Mon Sep 17 00:00:00 2001 From: Florian Dreier Date: Fri, 4 Dec 2020 07:03:50 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=A4=96=20Merge=20PR=20#49420=20[less]=20A?= =?UTF-8?q?dd=20types=20for=20FileManager=20by=20@DreierF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- types/less/index.d.ts | 108 +++++++++++++++++++++++++++++++++++++++ types/less/less-tests.ts | 11 +++- 2 files changed, 118 insertions(+), 1 deletion(-) diff --git a/types/less/index.d.ts b/types/less/index.d.ts index 07d5be9bb0..ac81b56462 100644 --- a/types/less/index.d.ts +++ b/types/less/index.d.ts @@ -30,10 +30,13 @@ declare namespace Less { constructor(less: LessStatic); addPreProcessor(preProcessor: PreProcessor, priority?: number): void; + + addFileManager(fileManager: FileManager): void; } interface Plugin { install: (less: LessStatic, pluginManager: PluginManager) => void; + minVersion?: [number, number, number]; } interface PreProcessor { @@ -52,6 +55,108 @@ declare namespace Less { }; } + interface FileLoadResult { + /** Full resolved path to file. */ + filename: string; + + /** The contents of the file, as a string. */ + contents: string; + } + + interface FileLoadError { + /** Error object if an error occurs. */ + error: unknown; + } + + class FileManager extends AbstractFileManager { + /** + * Returns whether this file manager supports this file for file retrieval + * If true is returned, loadFile will then be called with the file. + */ + supports(filename: string, currentDirectory: string, options: LoadFileOptions, environment: Environment): boolean; + + /** + * Loads a file asynchronously. Expects a promise that either rejects with an error or fulfills with a FileLoadResult. + */ + loadFile(filename: string, currentDirectory: string, options: LoadFileOptions, environment: Environment): Promise; + + /** + * Loads a file synchronously. Expects an immediate return with wither a FileLoadResult or FileLoadError. + */ + loadFileSync(filename: string, currentDirectory: string, options: LoadFileOptions, environment: Environment): FileLoadResult | FileLoadError; + } + + class AbstractFileManager { + /** + * Given the full path to a file, return the path component. + */ + getPath(filename: string): string; + + /** + * Append a .less extension if appropriate. Only called if less thinks one could be added. + */ + tryAppendLessExtension(filename: string): string; + + /** + * Whether the rootpath should be converted to be absolute. + * The browser ovverides this to return true because urls must be absolute. + */ + alwaysMakePathsAbsolute(): boolean; + + /** + * Returns whether a path is absolute. + */ + isPathAbsolute(path: string): boolean; + + /** + * Joins together 2 paths. + */ + join(basePath: string, laterPath: string): string; + + /** + * Returns the difference between 2 paths + * E.g. url = a/ baseUrl = a/b/ returns ../ + * url = a/b/ baseUrl = a/ returns b/ + */ + pathDiff(url: string, baseUrl: string): string; + + /** + * Returns whether this file manager supports this file for syncronous file retrieval + * If true is returned, loadFileSync will then be called with the file. + */ + supportsSync(filename: string, currentDirectory: string, options: LoadFileOptions, environment: Environment): boolean; + } + + interface LoadFileOptions { + paths?: string[]; + prefixes?: string[]; + ext?: string; + rawBuffer?: any; + syncImport?: boolean; + } + + interface Environment { + /** + * Converts a string to a base 64 string + */ + encodeBase64(str: string): string; + + /** + * Lookup the mime-type of a filename + */ + mimeLookup(filename: string): string; + + /** + * Look up the charset of a mime type + */ + charsetLookup(mime: string): string; + + /** + * Gets a source map generator + */ + getSourceMapGenerator(): any; + } + interface SourceMapOption { sourceMapURL?: string; sourceMapBasepath?: string; @@ -167,6 +272,9 @@ interface LessStatic { version: number[]; watch(): void; + + FileManager: typeof Less.FileManager; + PluginManager: typeof Less.PluginManager; } declare module "less" { diff --git a/types/less/less-tests.ts b/types/less/less-tests.ts index e20f592a14..1f78f67f35 100644 --- a/types/less/less-tests.ts +++ b/types/less/less-tests.ts @@ -23,11 +23,20 @@ var preProcessor: Less.PreProcessor = { } }; +class AliasResolvingFileManager extends less.FileManager { + loadFile(filename: string, currentDirectory: string, options: Less.LoadFileOptions, environment: Less.Environment) { + filename = filename.replace('foo', 'bar'); + return super.loadFile(filename, currentDirectory, options, environment); + } +} + var myPlugin: Less.Plugin = { install: (less, pluginManager) => { alert(less.version[2]); pluginManager.addPreProcessor(preProcessor, 1000); - } + pluginManager.addFileManager(new AliasResolvingFileManager()); + }, + minVersion: [ 3, 0, 0 ] }; /** Reference to: