UNPKG

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