|
| 1 | +type CssTimeUnit = "s" | "ms" |
| 2 | +type CssDistanceUnit = "cm" | "em" | "ex" | "in" | "mm" | "pc" | "pt" | "px" |
| 3 | +type CssNumberUnit = "%" | " " | CssDistanceUnit | CssTimeUnit |
| 4 | +class SassNumber { |
| 5 | + constructor(value: number, unit?: CssNumberUnit) |
| 6 | + getValue(): number |
| 7 | + getUnit(): CssNumberUnit |
| 8 | + setValue(value: number): void |
| 9 | + setUnit(unit: CssNumberUnit): void |
| 10 | +} |
| 11 | +class SassString { |
| 12 | + constructor(value: string) |
| 13 | + getValue(): string |
| 14 | + setValue(value: string): void |
| 15 | +} |
| 16 | +class SassColor { |
| 17 | + constructor(argb: number) |
| 18 | + constructor(r: number, g: number, b: number, a?: number) |
| 19 | + getR(): number |
| 20 | + getG(): number |
| 21 | + getB(): number |
| 22 | + getA(): number |
| 23 | + setR(value: number): void |
| 24 | + setG(value: number): void |
| 25 | + setB(value: number): void |
| 26 | + setA(value: number): void |
| 27 | +} |
| 28 | +class SassBoolean<T extends boolean = boolean> { |
| 29 | + static TRUE: SassBoolean<true> |
| 30 | + static FALSE: SassBoolean<false> |
| 31 | + constructor(value: T) |
| 32 | + getValue(): T |
| 33 | +} |
| 34 | +class SassList<Length extends number = number> { |
| 35 | + constructor(length: Length, commaSeparator?: boolean) |
| 36 | + getValue(index: number): SassValue |
| 37 | + getLength(): Length |
| 38 | + getSeparator(): boolean |
| 39 | + setValue(index: number, value: SassValue): void |
| 40 | + setSeparator(isComma: boolean): void |
| 41 | +} |
| 42 | +class SassMap<Length extends number = number> { |
| 43 | + constructor(length: Length) |
| 44 | + getLength(): Length |
| 45 | + getValue(index: number): SassValue |
| 46 | + setValue(index: number, value: SassValue): void |
| 47 | + getKey(index: number): SassValue |
| 48 | + setKey(index: number, value: SassValue): void |
| 49 | +} |
| 50 | +class SassNull { |
| 51 | + static NULL: SassNull |
| 52 | + constructor() |
| 53 | +} |
| 54 | +type SassValue = types[keyof types] |
| 55 | +export const types = { |
| 56 | + Number: SassNumber, |
| 57 | + String: SassString, |
| 58 | + Color: SassColor, |
| 59 | + Boolean: SassBoolean, |
| 60 | + List: SassList, |
| 61 | + Map: SassMap, |
| 62 | + Null: SassNull |
| 63 | +} |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | +/** |
| 68 | + * Define a handler for `import` calls in sass |
| 69 | + * @param url the path in import as-is, which [LibSass](https://github.com/sass/libsass) encountered |
| 70 | + * @param prev the previously resolved path |
| 71 | + * @param done a callback function to invoke on async completion, takes an object literal containing |
| 72 | + * |
| 73 | + */ |
| 74 | +type Importer = (url: string, prev: string, done: (data: ImporterReturn) => void) => ImporterReturn |
| 75 | +type ImporterReturn = { file: string } | { contents: string } | Error | null |
| 76 | + |
| 77 | +type RenderOptions = Partial<{ |
| 78 | + file: PathLike; |
| 79 | + data: string; |
| 80 | + /** |
| 81 | + * Handles when [LibSass](https://github.com/sass/libsass) encounters the @import directive. A custom importer allows extension of the [LibSass](https://github.com/sass/libsass) engine in both a synchronous and asynchronous manner. In both cases, the goal is to either return or call `done()` with an object literal. Depending on the value of the object literal, one of two things will happen. |
| 82 | + * - importers can return error and LibSass will emit that error in response. For instance: |
| 83 | +```js |
| 84 | +done(new Error('doesn\'t exist!')); |
| 85 | +// or return synchronously |
| 86 | +return new Error('nothing to do here'); |
| 87 | +``` |
| 88 | + */ |
| 89 | + importer: Importer | Importer[]; |
| 90 | + /** |
| 91 | + * **This is an experimental LibSass feature. Use with caution.** |
| 92 | + * |
| 93 | + * `functions` is an `Object` that holds a collection of custom functions that may be invoked by the sass files being compiled. They may take zero or more input parameters and must return a value either synchronously (`return ...;`) or asynchronously (`done();`). Those parameters will be instances of one of the constructors contained in the `require('node-sass').types` hash. The return value must be of one of these types as well. See the list of available types below: |
| 94 | + * |
| 95 | + * @example |
| 96 | + * ```js |
| 97 | +sass.renderSync({ |
| 98 | + data: '#{headings(2,5)} { color: #08c; }', |
| 99 | + functions: { |
| 100 | + 'headings($from: 0, $to: 6)': function(from, to) { |
| 101 | + var i, f = from.getValue(), t = to.getValue(), |
| 102 | + list = new sass.types.List(t - f + 1); |
| 103 | +
|
| 104 | + for (i = f; i <= t; i++) { |
| 105 | + list.setValue(i - f, new sass.types.String('h' + i)); |
| 106 | + } |
| 107 | +
|
| 108 | + return list; |
| 109 | + } |
| 110 | + } |
| 111 | +}); |
| 112 | +``` |
| 113 | + */ |
| 114 | + functions: { [key: string]: Function } |
| 115 | + /** An array of paths that LibSass can look in to attempt to resolve your `@import` declarations. When using `data`, it is recommended that you use this. */ |
| 116 | + includePaths: string[]; |
| 117 | + /** |
| 118 | + * `true` values enable [Sass Indented Syntax](https://sass-lang.com/documentation/file.INDENTED_SYNTAX.html) for parsing the data string or file. |
| 119 | + * __Note:__ node-sass/libsass will compile a mixed library of scss and indented syntax (.sass) files with the Default setting (false) as long as .sass and .scss extensions are used in filenames. |
| 120 | + */ |
| 121 | + indentedSyntax: boolean |
| 122 | + /** Used to determine whether to use space or tab character for indentation. */ |
| 123 | + indentType: "space" | "tab" |
| 124 | + /** |
| 125 | + * Used to determine the number of spaces or tabs to be used for indentation. |
| 126 | + * |
| 127 | + * Maximum: `10` |
| 128 | + */ |
| 129 | + indentWidth: number; |
| 130 | + /** Used to determine whether to use `cr`, `crlf`, `lf` or `lfcr` sequence for line break. */ |
| 131 | + linefeed: "cr" | "crlf" | "lf" | "lfcr"; |
| 132 | + /** |
| 133 | + * **Special:** When using this, you should also specify `outFile` to avoid unexpected behavior. |
| 134 | + * |
| 135 | + * `true` values disable the inclusion of source map information in the output file. |
| 136 | + */ |
| 137 | + omitSourceMapUrl: boolean; |
| 138 | + /** |
| 139 | + * **Special:** Required when `sourceMap` is a truthy value |
| 140 | +
|
| 141 | +Specify the intended location of the output file. Strongly recommended when outputting source maps so that they can properly refer back to their intended files. |
| 142 | +
|
| 143 | +**Attention** enabling this option will **not** write the file on disk for you, it's for internal reference purpose only (to generate the map for example). |
| 144 | +
|
| 145 | +Example on how to write it on the disk |
| 146 | +
|
| 147 | +```javascript |
| 148 | +sass.render({ |
| 149 | + ... |
| 150 | + outFile: yourPathTotheFile, |
| 151 | + }, function(error, result) { // node-style callback from v3.0.0 onwards |
| 152 | + if(!error){ |
| 153 | + // No errors during the compilation, write this result on the disk |
| 154 | + fs.writeFile(yourPathTotheFile, result.css, function(err){ |
| 155 | + if(!err){ |
| 156 | + //file written on disk |
| 157 | + } |
| 158 | + }); |
| 159 | + } |
| 160 | + }); |
| 161 | +}); |
| 162 | +``` |
| 163 | +
|
| 164 | + */ |
| 165 | + outFile: string; |
| 166 | + /** Determines the output format of the final CSS style. */ |
| 167 | + outputStyle: "nested" | "expanded" | "compact" | "compressed"; |
| 168 | + /** Used to determine how many digits after the decimal will be allowed. For instance, if you had a decimal number of `1.23456789` and a precision of `5`, the result will be `1.23457` in the final CSS. */ |
| 169 | + precision: number; |
| 170 | + /** `true` Enables the line number and file where a selector is defined to be emitted into the compiled CSS as a comment. Useful for debugging, especially when using imports and mixins. */ |
| 171 | + sourceComments: boolean; |
| 172 | + /** |
| 173 | + * **Special:** Setting the `sourceMap` option requires also setting the `outFile` option |
| 174 | +
|
| 175 | +Enables the outputting of a source map during `render` and `renderSync`. When `sourceMap === true`, the value of `outFile` is used as the target output location for the source map. When `typeof sourceMap === "string"`, the value of `sourceMap` will be used as the writing location for the file. |
| 176 | + */ |
| 177 | + sourceMap: boolean; |
| 178 | + /** `true` includes the `contents` in the source map information */ |
| 179 | + sourceMapContents: boolean; |
| 180 | + /** `true` embeds the source map as a data URI */ |
| 181 | + sourceMapEmbed: boolean; |
| 182 | + /** the value will be emitted as `sourceRoot` in the source map information */ |
| 183 | + sourceMapRoot: boolean; |
| 184 | + outFile: string; |
| 185 | +}>; |
| 186 | +class RenderError extends Error { |
| 187 | + /** The error message. */ |
| 188 | + message: string |
| 189 | + /** The line number of error. */ |
| 190 | + line: number |
| 191 | + /** The column number of error. */ |
| 192 | + column: number |
| 193 | + /** The status code. */ |
| 194 | + status: number |
| 195 | + /** The filename of error. In case `file` option was not set (in favour of `data`), this will reflect the value `stdin`. */ |
| 196 | + file: string |
| 197 | +} |
| 198 | +type RenderResult = { |
| 199 | + /** The compiled CSS. Write this to a file, or serve it out as needed. */ |
| 200 | + css: Uint8Array; |
| 201 | + map: Buffer | undefined; |
| 202 | + stats: { |
| 203 | + entry: string; |
| 204 | + start: number; |
| 205 | + end: number; |
| 206 | + duration: number; |
| 207 | + includedFiles: string[]; |
| 208 | + }; |
| 209 | +}; |
| 210 | +export function render(options: RenderOptions, callback: (err: RenderError | undefined, result: RenderResult) => void): void |
| 211 | +export function renderSync(options: RenderOptions): RenderResult |
0 commit comments