UNPKG

9.57 kBTypeScriptView Raw
1// Type definitions for LESS 3.x
2// Project: http://lesscss.org/
3// Definitions by: Tom Hasner <https://github.com/thasner>
4// Pranay Prakash <https://github.com/pranaygp>
5// Daniel Waxweiler <https://github.com/dwaxweiler>
6// Richard Lea <https://github.com/chigix>
7// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
8
9declare namespace Less {
10 // https://github.com/less/less.js/blob/master/lib/less/import-manager.js#L10
11 interface RootFileInfo {
12 /** whether to adjust URL's to be relative */
13 rewriteUrls?: boolean | undefined;
14 /** full resolved filename of current file */
15 filename: string;
16 relativeUrls: boolean;
17 /** path to append to normal URLs for this node */
18 rootpath: string;
19 /** path to the current file, absolute */
20 currentDirectory: string;
21 /** absolute path to the entry file */
22 entryPath: string;
23 /** filename of the base file */
24 rootFilename: string;
25 /** whether the file should not be output and only output parts that are referenced */
26 reference: boolean;
27 }
28
29 class PluginManager {
30 constructor(less: LessStatic);
31
32 addPreProcessor(preProcessor: PreProcessor, priority?: number): void;
33
34 addFileManager(fileManager: FileManager): void;
35 }
36
37 interface Plugin {
38 install: (less: LessStatic, pluginManager: PluginManager) => void;
39 minVersion?: [number, number, number] | undefined;
40 }
41
42 interface PreProcessor {
43 process: (src: string, extra: PreProcessorExtraInfo) => string;
44 }
45
46 interface PreProcessorExtraInfo {
47 context: {
48 pluginManager: PluginManager;
49 };
50
51 fileInfo: RootFileInfo;
52
53 imports: {
54 [key: string]: any;
55 };
56 }
57
58 interface FileLoadResult {
59 /** Full resolved path to file. */
60 filename: string;
61
62 /** The contents of the file, as a string. */
63 contents: string;
64 }
65
66 interface FileLoadError {
67 /** Error object if an error occurs. */
68 error: unknown;
69 }
70
71 class FileManager extends AbstractFileManager {
72 /**
73 * Returns whether this file manager supports this file for file retrieval
74 * If true is returned, loadFile will then be called with the file.
75 */
76 supports(filename: string, currentDirectory: string, options: LoadFileOptions, environment: Environment): boolean;
77
78 /**
79 * Loads a file asynchronously. Expects a promise that either rejects with an error or fulfills with a FileLoadResult.
80 */
81 loadFile(filename: string, currentDirectory: string, options: LoadFileOptions, environment: Environment): Promise<FileLoadResult>;
82
83 /**
84 * Loads a file synchronously. Expects an immediate return with wither a FileLoadResult or FileLoadError.
85 */
86 loadFileSync(filename: string, currentDirectory: string, options: LoadFileOptions, environment: Environment): FileLoadResult | FileLoadError;
87 }
88
89 class AbstractFileManager {
90 /**
91 * Given the full path to a file, return the path component.
92 */
93 getPath(filename: string): string;
94
95 /**
96 * Append a .less extension if appropriate. Only called if less thinks one could be added.
97 */
98 tryAppendLessExtension(filename: string): string;
99
100 /**
101 * Whether the rootpath should be converted to be absolute.
102 * The browser ovverides this to return true because urls must be absolute.
103 */
104 alwaysMakePathsAbsolute(): boolean;
105
106 /**
107 * Returns whether a path is absolute.
108 */
109 isPathAbsolute(path: string): boolean;
110
111 /**
112 * Joins together 2 paths.
113 */
114 join(basePath: string, laterPath: string): string;
115
116 /**
117 * Returns the difference between 2 paths
118 * E.g. url = a/ baseUrl = a/b/ returns ../
119 * url = a/b/ baseUrl = a/ returns b/
120 */
121 pathDiff(url: string, baseUrl: string): string;
122
123 /**
124 * Returns whether this file manager supports this file for syncronous file retrieval
125 * If true is returned, loadFileSync will then be called with the file.
126 */
127 supportsSync(filename: string, currentDirectory: string, options: LoadFileOptions, environment: Environment): boolean;
128 }
129
130 interface LoadFileOptions {
131 paths?: string[] | undefined;
132 prefixes?: string[] | undefined;
133 ext?: string | undefined;
134 rawBuffer?: any;
135 syncImport?: boolean | undefined;
136 }
137
138 interface Environment {
139 /**
140 * Converts a string to a base 64 string
141 */
142 encodeBase64(str: string): string;
143
144 /**
145 * Lookup the mime-type of a filename
146 */
147 mimeLookup(filename: string): string;
148
149 /**
150 * Look up the charset of a mime type
151 */
152 charsetLookup(mime: string): string;
153
154 /**
155 * Gets a source map generator
156 */
157 getSourceMapGenerator(): any;
158 }
159
160 interface SourceMapOption {
161 sourceMapURL?: string | undefined;
162 sourceMapBasepath?: string | undefined;
163 sourceMapRootpath?: string | undefined;
164 outputSourceFiles?: boolean | undefined;
165 sourceMapFileInline?: boolean | undefined;
166 }
167
168 interface StaticOptions {
169 async: boolean;
170 fileAsync: boolean;
171 modifyVars: { [variable: string]: string };
172 }
173
174 interface ImportManager {
175 contents: { [fileName: string]: string };
176 }
177
178 /**
179 * Reference to:
180 * * https://github.com/less/less.js/blob/master/bin/lessc
181 * * http://lesscss.org/usage/#less-options
182 *
183 * @interface Options
184 */
185 interface Options {
186 sourceMap?: SourceMapOption | undefined;
187 /** Filename of the main file to be passed to less.render() */
188 filename?: string | undefined;
189 /** The locations for less looking for files in @import rules */
190 paths?: string[] | undefined;
191 /** True, if run the less parser and just reports errors without any output. */
192 lint?: boolean | undefined;
193 /** Pre-load global Less.js plugins */
194 plugins?: Plugin[] | undefined;
195 /** @deprecated If true, compress using less built-in compression. */
196 compress?: boolean | undefined;
197 strictImports?: boolean | undefined;
198 /** If true, allow imports from insecure https hosts. */
199 insecure?: boolean | undefined;
200 depends?: boolean | undefined;
201 maxLineLen?: number | undefined;
202 /** @deprecated If false, No color in compiling. */
203 color?: boolean | undefined;
204 /** @deprecated False by default. */
205 ieCompat?: boolean | undefined;
206 /** @deprecated If true, enable evaluation of JavaScript inline in `.less` files. */
207 javascriptEnabled?: boolean | undefined;
208 /** Whether output file information and line numbers in compiled CSS code. */
209 dumpLineNumbers?: "comment" | string | undefined;
210 /** Add a path to every generated import and url in output css files. */
211 rootpath?: string | undefined;
212 /** Math mode options for avoiding symbol conficts on math expressions. */
213 math?: 'always' | 'strict' | 'parens-division' | 'parens' | 'strict-legacy' | number | undefined;
214 /** If true, stops any warnings from being shown. */
215 silent?: boolean | undefined;
216 /** Without this option, Less attempts to guess at the output unit when it does maths. */
217 strictUnits?: boolean | undefined;
218 /** Defines a variable that can be referenced by the file. */
219 globalVars?: {
220 [key: string] : string,
221 } | undefined;
222 /** Puts Var declaration at the end of base file. */
223 modifyVars?: {
224 [key: string] : string,
225 } | undefined;
226 /** Read files synchronously in Node.js */
227 syncImport?: boolean | undefined;
228 }
229
230 interface RenderError {
231 column: number;
232 extract: string[];
233 filename: string;
234 index: number;
235 line: number;
236 message: string;
237 type: string;
238 }
239
240 interface RenderOutput {
241 css: string;
242 map: string;
243 imports: string[];
244 }
245
246 interface RefreshOutput {
247 endTime: Date;
248 startTime: Date;
249 sheets: number;
250 totalMilliseconds: number;
251 }
252}
253
254interface LessStatic {
255 options: Less.StaticOptions;
256
257 importManager?: Less.ImportManager | undefined;
258 sheets: HTMLLinkElement[];
259
260 modifyVars(vars: { [name: string]: string }): Promise<Less.RefreshOutput>;
261
262 refreshStyles(): void;
263
264 render(input: string, callback: (error: Less.RenderError, output: Less.RenderOutput | undefined) => void): void;
265 render(input: string, options: Less.Options, callback: (error: Less.RenderError, output: Less.RenderOutput | undefined) => void): void;
266
267 render(input: string): Promise<Less.RenderOutput>;
268 render(input: string, options: Less.Options): Promise<Less.RenderOutput>;
269
270 refresh(reload?: boolean, modifyVars?: { [variable: string]: string }, clearFileCache?: boolean): Promise<Less.RefreshOutput>;
271
272 version: number[];
273
274 watch(): void;
275
276 FileManager: typeof Less.FileManager;
277 PluginManager: typeof Less.PluginManager;
278}
279
280declare module "less" {
281 export = less;
282}
283
284declare var less: LessStatic;