Skip to content

Commit 54583ea

Browse files
committed
fixup! fix(@angular/build): ensure Sass importers return global Promise instances
1 parent b0da5ea commit 54583ea

2 files changed

Lines changed: 26 additions & 11 deletions

File tree

‎packages/angular/build/src/tools/esbuild/stylesheets/sass-language_spec.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ describe('sass-language', () => {
288288
});
289289

290290
it('should resolve imports when globalThis.Promise is replaced (e.g., by Zone.js)', async () => {
291-
const OriginalPromise = globalThis.Promise;
291+
const originalPromise = globalThis.Promise;
292292

293293
// Simulate Zone.js replacing globalThis.Promise with a constructor where
294294
// native V8 async function return values fail `instanceof globalThis.Promise`.
@@ -301,7 +301,7 @@ describe('sass-language', () => {
301301
reject: (reason?: unknown) => void,
302302
) => void,
303303
) {
304-
this.#inner = new OriginalPromise(executor);
304+
this.#inner = new originalPromise(executor);
305305
}
306306

307307
then<TResult1 = T, TResult2 = never>(
@@ -338,7 +338,7 @@ describe('sass-language', () => {
338338
}
339339

340340
static all(values: Iterable<unknown>): ZoneLikePromise<unknown[]> {
341-
return ZoneLikePromise.resolve(OriginalPromise.all(values));
341+
return ZoneLikePromise.resolve(originalPromise.all(values));
342342
}
343343
}
344344

@@ -360,7 +360,7 @@ describe('sass-language', () => {
360360
});
361361
expect(nonRebasingResult.css).toContain('content: "project";');
362362
} finally {
363-
globalThis.Promise = OriginalPromise;
363+
globalThis.Promise = originalPromise;
364364
await nonRebasingCompiler.close();
365365
}
366366
});

‎packages/angular/build/src/tools/sass/sass-service.ts‎

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ function isFileImporter(value: Importers): value is FileImporter {
3737
return 'findFileUrl' in value;
3838
}
3939

40+
function resolvePromise<T>(value: T | Promise<T>): T | Promise<T> {
41+
return value && typeof value === 'object' && 'then' in value ? Promise.resolve(value) : value;
42+
}
43+
4044
/**
4145
* A Sass renderer implementation that uses the persistent Dart Sass embedded compiler
4246
* daemon (`sass-embedded`) communicating over standard input/output with protocol buffers,
@@ -113,13 +117,24 @@ export class SassCompiler {
113117
if (!this.rebase) {
114118
return compiler.compileStringAsync(source, {
115119
...options,
116-
importers: options.importers?.map((importer) =>
117-
isFileImporter(importer)
118-
? {
119-
findFileUrl: (url, context) => Promise.resolve(importer.findFileUrl(url, context)),
120-
}
121-
: importer,
122-
),
120+
importers: options.importers?.map((importer) => {
121+
if (isFileImporter(importer)) {
122+
return {
123+
...importer,
124+
findFileUrl: (url, context) => resolvePromise(importer.findFileUrl(url, context)),
125+
};
126+
}
127+
128+
if ('canonicalize' in importer) {
129+
return {
130+
...importer,
131+
canonicalize: (url, context) => resolvePromise(importer.canonicalize(url, context)),
132+
load: (canonicalUrl) => resolvePromise(importer.load(canonicalUrl)),
133+
};
134+
}
135+
136+
return importer;
137+
}),
123138
});
124139
}
125140

0 commit comments

Comments
 (0)