UNPKG

11.1 kBTypeScriptView Raw
1import {RawSourceMap} from 'source-map-js';
2
3import {Options, StringOptions} from './options';
4
5/**
6 * The result of compiling Sass to CSS. Returned by {@link compile}, {@link
7 * compileAsync}, {@link compileString}, and {@link compileStringAsync}.
8 *
9 * @category Compile
10 */
11export interface CompileResult {
12 /**
13 * The generated CSS.
14 *
15 * Note that this *never* includes a `sourceMapUrl` comment—it's up to the
16 * caller to determine where to save the source map and how to link to it from
17 * the stylesheet.
18 */
19 css: string;
20
21 /**
22 * The canonical URLs of all the stylesheets that were loaded during the
23 * Sass compilation. The order of these URLs is not guaranteed.
24 */
25 loadedUrls: URL[];
26
27 /**
28 * The object representation of the source map that maps locations in the
29 * generated CSS back to locations in the Sass source code.
30 *
31 * This typically uses absolute `file:` URLs to refer to Sass files, although
32 * this can be controlled by having a custom {@link Importer} return {@link
33 * ImporterResult.sourceMapUrl}.
34 *
35 * This is set if and only if {@link Options.sourceMap} is `true`.
36 */
37 sourceMap?: RawSourceMap;
38}
39
40/**
41 * The result of creating a synchronous compiler. Returned by
42 * {@link initCompiler}.
43 *
44 * @category Compile
45 */
46export class Compiler {
47 /**
48 * Throws an error if constructed directly, instead of via
49 * {@link initCompiler}.
50 */
51 private constructor();
52
53 /**
54 * The {@link compile} method exposed through a Compiler instance while it is
55 * active. If this is called after {@link dispose} on the Compiler
56 * instance, an error will be thrown.
57 *
58 * During the Compiler instance's lifespan, given the same input, this will
59 * return an identical result to the {@link compile} method exposed at the
60 * module root.
61 */
62 compile(path: string, options?: Options<'sync'>): CompileResult;
63
64 /**
65 * The {@link compileString} method exposed through a Compiler instance while
66 * it is active. If this is called after {@link dispose} on the Compiler
67 * instance, an error will be thrown.
68 *
69 * During the Compiler instance's lifespan, given the same input, this will
70 * return an identical result to the {@link compileString} method exposed at
71 * the module root.
72 */
73 compileString(source: string, options?: StringOptions<'sync'>): CompileResult;
74
75 /**
76 * Ends the lifespan of this Compiler instance. After this is invoked, all
77 * calls to the Compiler instance's {@link compile} or {@link compileString}
78 * methods will result in an error.
79 */
80 dispose(): void;
81}
82
83/**
84 * The result of creating an asynchronous compiler. Returned by
85 * {@link initAsyncCompiler}.
86 *
87 * @category Compile
88 */
89export class AsyncCompiler {
90 /**
91 * Throws an error if constructed directly, instead of via
92 * {@link initAsyncCompiler}.
93 */
94 private constructor();
95
96 /**
97 * The {@link compileAsync} method exposed through an Async Compiler instance
98 * while it is active. If this is called after {@link dispose} on the Async
99 * Compiler instance, an error will be thrown.
100 *
101 * During the Async Compiler instance's lifespan, given the same input, this
102 * will return an identical result to the {@link compileAsync} method exposed
103 * at the module root.
104 */
105 compileAsync(
106 path: string,
107 options?: Options<'async'>
108 ): Promise<CompileResult>;
109
110 /**
111 * The {@link compileStringAsync} method exposed through an Async Compiler
112 * instance while it is active. If this is called after {@link dispose} on the
113 * Async Compiler instance, an error will be thrown.
114 *
115 * During the Async Compiler instance's lifespan, given the same input, this
116 * will return an identical result to the {@link compileStringAsync} method
117 * exposed at the module root.
118 */
119 compileStringAsync(
120 source: string,
121 options?: StringOptions<'async'>
122 ): Promise<CompileResult>;
123
124 /**
125 * Ends the lifespan of this Async Compiler instance. After this is invoked,
126 * all subsequent calls to the Compiler instance's `compileAsync` or
127 * `compileStringAsync` methods will result in an error.
128 *
129 * Any compilations that are submitted before `dispose` will not be cancelled,
130 * and will be allowed to settle.
131 *
132 * After all compilations have been settled and Sass completes any internal
133 * task cleanup, `dispose` will resolve its promise.
134 */
135 dispose(): Promise<void>;
136}
137
138/**
139 * Synchronously compiles the Sass file at `path` to CSS. If it succeeds it
140 * returns a {@link CompileResult}, and if it fails it throws an {@link
141 * Exception}.
142 *
143 * This only allows synchronous {@link Importer}s and {@link CustomFunction}s.
144 *
145 * **Heads up!** When using the [sass-embedded] npm package for single
146 * compilations, **{@link compileAsync} is almost always faster than
147 * {@link compile}**, due to the overhead of emulating synchronous messaging
148 * with worker threads and concurrent compilations being blocked on main thread.
149 *
150 * If you are running multiple compilations with the [sass-embedded] npm
151 * package, using a {@link Compiler} will provide some speed improvements over
152 * the module-level methods, and an {@link AsyncCompiler} will be much faster.
153 *
154 * [sass-embedded]: https://www.npmjs.com/package/sass-embedded
155 *
156 * @example
157 *
158 * ```js
159 * const sass = require('sass');
160 *
161 * const result = sass.compile("style.scss");
162 * console.log(result.css);
163 * ```
164 *
165 * @category Compile
166 * @compatibility dart: "1.45.0", node: false
167 */
168export function compile(path: string, options?: Options<'sync'>): CompileResult;
169
170/**
171 * Asynchronously compiles the Sass file at `path` to CSS. Returns a promise
172 * that resolves with a {@link CompileResult} if it succeeds and rejects with an
173 * {@link Exception} if it fails.
174 *
175 * This only allows synchronous or asynchronous {@link Importer}s and
176 * {@link CustomFunction}s.
177 *
178 * **Heads up!** When using the `sass` npm package, **{@link compile} is almost
179 * twice as fast as {@link compileAsync}**, due to the overhead of making the
180 * entire evaluation process asynchronous.
181 *
182 * @example
183 *
184 * ```js
185 * const sass = require('sass');
186 *
187 * const result = await sass.compileAsync("style.scss");
188 * console.log(result.css);
189 * ```
190 *
191 * @category Compile
192 * @compatibility dart: "1.45.0", node: false
193 */
194export function compileAsync(
195 path: string,
196 options?: Options<'async'>
197): Promise<CompileResult>;
198
199/**
200 * Synchronously compiles a stylesheet whose contents is `source` to CSS. If it
201 * succeeds it returns a {@link CompileResult}, and if it fails it throws an
202 * {@link Exception}.
203 *
204 * This only allows synchronous {@link Importer}s and {@link CustomFunction}s.
205 *
206 * **Heads up!** When using the [sass-embedded] npm package for single
207 * compilations, **{@link compileStringAsync} is almost always faster than
208 * {@link compileString}**, due to the overhead of emulating synchronous
209 * messaging with worker threads and concurrent compilations being blocked on
210 * main thread.
211 *
212 * If you are running multiple compilations with the [sass-embedded] npm
213 * package, using a {@link Compiler} will provide some speed improvements over
214 * the module-level methods, and an {@link AsyncCompiler} will be much faster.
215 *
216 * [sass-embedded]: https://www.npmjs.com/package/sass-embedded
217 *
218 * @example
219 *
220 * ```js
221 * const sass = require('sass');
222 *
223 * const result = sass.compileString(`
224 * h1 {
225 * font-size: 40px;
226 * code {
227 * font-face: Roboto Mono;
228 * }
229 * }`);
230 * console.log(result.css);
231 * ```
232 *
233 * @category Compile
234 * @compatibility dart: "1.45.0", node: false
235 */
236export function compileString(
237 source: string,
238 options?: StringOptions<'sync'>
239): CompileResult;
240
241/**
242 * Asynchronously compiles a stylesheet whose contents is `source` to CSS.
243 * Returns a promise that resolves with a {@link CompileResult} if it succeeds
244 * and rejects with an {@link Exception} if it fails.
245 *
246 * This only allows synchronous or asynchronous {@link Importer}s and {@link
247 * CustomFunction}s.
248 *
249 * **Heads up!** When using the `sass` npm package, **{@link compileString} is
250 * almost twice as fast as {@link compileStringAsync}**, due to the overhead
251 * of making the entire evaluation process asynchronous.
252 *
253 * @example
254 *
255 * ```js
256 * const sass = require('sass');
257 *
258 * const result = await sass.compileStringAsync(`
259 * h1 {
260 * font-size: 40px;
261 * code {
262 * font-face: Roboto Mono;
263 * }
264 * }`);
265 * console.log(result.css);
266 * ```
267 *
268 * @category Compile
269 * @compatibility dart: "1.45.0", node: false
270 */
271export function compileStringAsync(
272 source: string,
273 options?: StringOptions<'async'>
274): Promise<CompileResult>;
275
276/**
277 * Creates a synchronous {@link Compiler}. Each compiler instance exposes the
278 * {@link compile} and {@link compileString} methods within the lifespan of the
279 * Compiler. Given identical input, these methods will return results identical
280 * to their counterparts exposed at the module root. To use asynchronous
281 * compilation, use {@link initAsyncCompiler}.
282 *
283 * When calling the compile functions multiple times, using a compiler instance
284 * with the [sass-embedded] npm package is much faster than using the top-level
285 * compilation methods or the [sass] npm package.
286 *
287 * [sass-embedded]: https://www.npmjs.com/package/sass-embedded
288 *
289 * [sass]: https://www.npmjs.com/package/sass
290 *
291 * @example
292 *
293 * ```js
294 * const sass = require('sass');
295 * function setup() {
296 * const compiler = sass.initCompiler();
297 * const result1 = compiler.compileString('a {b: c}').css;
298 * const result2 = compiler.compileString('a {b: c}').css;
299 * compiler.dispose();
300 *
301 * // throws error
302 * const result3 = sass.compileString('a {b: c}').css;
303 * }
304 * ```
305 * @category Compile
306 * @compatibility dart: "1.70.0", node: false
307 */
308export function initCompiler(): Compiler;
309
310/**
311 * Creates an asynchronous {@link AsyncCompiler}. Each compiler
312 * instance exposes the {@link compileAsync} and {@link compileStringAsync}
313 * methods within the lifespan of the Compiler. Given identical input, these
314 * methods will return results identical to their counterparts exposed at the
315 * module root. To use synchronous compilation, use {@link initCompiler};
316 *
317 * When calling the compile functions multiple times, using a compiler instance
318 * with the [sass-embedded] npm package is much faster than using the top-level
319 * compilation methods or the [sass] npm package.
320 *
321 * [sass-embedded]: https://www.npmjs.com/package/sass-embedded
322 *
323 * [sass]: https://www.npmjs.com/package/sass
324 *
325 * @example
326 *
327 * ```js
328 * const sass = require('sass');
329 * async function setup() {
330 * const compiler = await sass.initAsyncCompiler();
331 * const result1 = await compiler.compileStringAsync('a {b: c}').css;
332 * const result2 = await compiler.compileStringAsync('a {b: c}').css;
333 * await compiler.dispose();
334 *
335 * // throws error
336 * const result3 = await sass.compileStringAsync('a {b: c}').css;
337 * }
338 * ```
339 * @category Compile
340 * @compatibility dart: "1.70.0", node: false
341 */
342export function initAsyncCompiler(): Promise<AsyncCompiler>;