6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
9
- import { Importer , ImporterReturnType , Options , Result , SassException } from 'sass' ;
9
+ import {
10
+ LegacyAsyncImporter as AsyncImporter ,
11
+ LegacyResult as CompileResult ,
12
+ LegacyException as Exception ,
13
+ LegacyImporterResult as ImporterResult ,
14
+ LegacyImporterThis as ImporterThis ,
15
+ LegacyOptions as Options ,
16
+ LegacySyncImporter as SyncImporter ,
17
+ } from 'sass' ;
10
18
import { MessageChannel , Worker } from 'worker_threads' ;
11
19
import { maxWorkers } from '../utils/environment-options' ;
12
20
@@ -18,7 +26,7 @@ const MAX_RENDER_WORKERS = maxWorkers;
18
26
/**
19
27
* The callback type for the `dart-sass` asynchronous render function.
20
28
*/
21
- type RenderCallback = ( error ?: SassException , result ?: Result ) => void ;
29
+ type RenderCallback = ( error ?: Exception , result ?: CompileResult ) => void ;
22
30
23
31
/**
24
32
* An object containing the contextual information for a specific render request.
@@ -27,16 +35,16 @@ interface RenderRequest {
27
35
id : number ;
28
36
workerIndex : number ;
29
37
callback : RenderCallback ;
30
- importers ?: Importer [ ] ;
38
+ importers ?: ( SyncImporter | AsyncImporter ) [ ] ;
31
39
}
32
40
33
41
/**
34
42
* A response from the Sass render Worker containing the result of the operation.
35
43
*/
36
44
interface RenderResponseMessage {
37
45
id : number ;
38
- error ?: SassException ;
39
- result ?: Result ;
46
+ error ?: Exception ;
47
+ result ?: CompileResult ;
40
48
}
41
49
42
50
/**
@@ -73,7 +81,7 @@ export class SassWorkerImplementation {
73
81
* @param options The `dart-sass` options to use when rendering the stylesheet.
74
82
* @param callback The function to execute when the rendering is complete.
75
83
*/
76
- render ( options : Options , callback : RenderCallback ) : void {
84
+ render ( options : Options < 'async' > , callback : RenderCallback ) : void {
77
85
// The `functions`, `logger` and `importer` options are JavaScript functions that cannot be transferred.
78
86
// If any additional function options are added in the future, they must be excluded as well.
79
87
const { functions, importer, logger, ...serializableOptions } = options ;
@@ -142,7 +150,7 @@ export class SassWorkerImplementation {
142
150
// The results are expected to be Node.js `Buffer` objects but will each be transferred as
143
151
// a Uint8Array that does not have the expected `toString` behavior of a `Buffer`.
144
152
const { css, map, stats } = response . result ;
145
- const result : Result = {
153
+ const result : CompileResult = {
146
154
// This `Buffer.from` override will use the memory directly and avoid making a copy
147
155
css : Buffer . from ( css . buffer , css . byteOffset , css . byteLength ) ,
148
156
stats,
@@ -199,16 +207,21 @@ export class SassWorkerImplementation {
199
207
}
200
208
201
209
private async processImporters (
202
- importers : Iterable < Importer > ,
210
+ importers : Iterable < SyncImporter | AsyncImporter > ,
203
211
url : string ,
204
212
prev : string ,
205
213
fromImport : boolean ,
206
- ) : Promise < ImporterReturnType > {
214
+ ) : Promise < ImporterResult > {
207
215
let result = null ;
208
216
for ( const importer of importers ) {
209
- result = await new Promise < ImporterReturnType > ( ( resolve ) => {
217
+ result = await new Promise < ImporterResult > ( ( resolve ) => {
210
218
// Importers can be both sync and async
211
- const innerResult = importer . call ( { fromImport } , url , prev , resolve ) ;
219
+ const innerResult = ( importer as AsyncImporter ) . call (
220
+ { fromImport } as ImporterThis ,
221
+ url ,
222
+ prev ,
223
+ resolve ,
224
+ ) ;
212
225
if ( innerResult !== undefined ) {
213
226
resolve ( innerResult ) ;
214
227
}
@@ -225,7 +238,7 @@ export class SassWorkerImplementation {
225
238
private createRequest (
226
239
workerIndex : number ,
227
240
callback : RenderCallback ,
228
- importer : Importer | Importer [ ] | undefined ,
241
+ importer : SyncImporter | AsyncImporter | ( SyncImporter | AsyncImporter ) [ ] | undefined ,
229
242
) : RenderRequest {
230
243
return {
231
244
id : this . idCounter ++ ,
0 commit comments