66 * found in the LICENSE file at https://angular.io/license
77 */
88
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' ;
1018import { MessageChannel , Worker } from 'worker_threads' ;
1119import { maxWorkers } from '../utils/environment-options' ;
1220
@@ -18,7 +26,7 @@ const MAX_RENDER_WORKERS = maxWorkers;
1826/**
1927 * The callback type for the `dart-sass` asynchronous render function.
2028 */
21- type RenderCallback = ( error ?: SassException , result ?: Result ) => void ;
29+ type RenderCallback = ( error ?: Exception , result ?: CompileResult ) => void ;
2230
2331/**
2432 * An object containing the contextual information for a specific render request.
@@ -27,16 +35,16 @@ interface RenderRequest {
2735 id : number ;
2836 workerIndex : number ;
2937 callback : RenderCallback ;
30- importers ?: Importer [ ] ;
38+ importers ?: ( SyncImporter | AsyncImporter ) [ ] ;
3139}
3240
3341/**
3442 * A response from the Sass render Worker containing the result of the operation.
3543 */
3644interface RenderResponseMessage {
3745 id : number ;
38- error ?: SassException ;
39- result ?: Result ;
46+ error ?: Exception ;
47+ result ?: CompileResult ;
4048}
4149
4250/**
@@ -73,7 +81,7 @@ export class SassWorkerImplementation {
7381 * @param options The `dart-sass` options to use when rendering the stylesheet.
7482 * @param callback The function to execute when the rendering is complete.
7583 */
76- render ( options : Options , callback : RenderCallback ) : void {
84+ render ( options : Options < 'async' > , callback : RenderCallback ) : void {
7785 // The `functions`, `logger` and `importer` options are JavaScript functions that cannot be transferred.
7886 // If any additional function options are added in the future, they must be excluded as well.
7987 const { functions, importer, logger, ...serializableOptions } = options ;
@@ -142,7 +150,7 @@ export class SassWorkerImplementation {
142150 // The results are expected to be Node.js `Buffer` objects but will each be transferred as
143151 // a Uint8Array that does not have the expected `toString` behavior of a `Buffer`.
144152 const { css, map, stats } = response . result ;
145- const result : Result = {
153+ const result : CompileResult = {
146154 // This `Buffer.from` override will use the memory directly and avoid making a copy
147155 css : Buffer . from ( css . buffer , css . byteOffset , css . byteLength ) ,
148156 stats,
@@ -199,16 +207,21 @@ export class SassWorkerImplementation {
199207 }
200208
201209 private async processImporters (
202- importers : Iterable < Importer > ,
210+ importers : Iterable < SyncImporter | AsyncImporter > ,
203211 url : string ,
204212 prev : string ,
205213 fromImport : boolean ,
206- ) : Promise < ImporterReturnType > {
214+ ) : Promise < ImporterResult > {
207215 let result = null ;
208216 for ( const importer of importers ) {
209- result = await new Promise < ImporterReturnType > ( ( resolve ) => {
217+ result = await new Promise < ImporterResult > ( ( resolve ) => {
210218 // 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+ ) ;
212225 if ( innerResult !== undefined ) {
213226 resolve ( innerResult ) ;
214227 }
@@ -225,7 +238,7 @@ export class SassWorkerImplementation {
225238 private createRequest (
226239 workerIndex : number ,
227240 callback : RenderCallback ,
228- importer : Importer | Importer [ ] | undefined ,
241+ importer : SyncImporter | AsyncImporter | ( SyncImporter | AsyncImporter ) [ ] | undefined ,
229242 ) : RenderRequest {
230243 return {
231244 id : this . idCounter ++ ,
0 commit comments