Skip to content

Commit 38b82cd

Browse files
committed
fix(@angular/build): ensure Sass importers return global Promise instances
`sass-embedded` checks whether an importer's `canonicalize` or `findFileUrl` method returned a promise using `result instanceof Promise`. When SSR executes in the same process during `ng serve` for an application using Zone.js, `zone.js/node` replaces `globalThis.Promise` with `ZoneAwarePromise`. Because native V8 `async` functions always return instances of the intrinsic V8 `%Promise%` rather than `globalThis.Promise`, `result instanceof Promise` evaluates to `false` and `sass-embedded` treats the returned Promise as a synchronous URL value (`"[object Promise]"` / `"%5Bobject%20Promise%5D"`). Use `Promise.resolve(...)` instead of a native `async` method in `AsyncModuleUrlRebasingImporter.canonicalize` and wrap `FileImporter.findFileUrl` with `Promise.resolve(...)` so the returned promises are constructed via the active `globalThis.Promise`. Closes #34309
1 parent f0a74e8 commit 38b82cd

3 files changed

Lines changed: 96 additions & 5 deletions

File tree

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

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,5 +286,83 @@ describe('sass-language', () => {
286286
pathToFileURL(join(projectRoot, 'node_modules', 'theme', '_index.scss')).href,
287287
);
288288
});
289+
290+
it('should resolve imports when globalThis.Promise is replaced (e.g., by Zone.js)', async () => {
291+
const OriginalPromise = globalThis.Promise;
292+
293+
// Simulate Zone.js replacing globalThis.Promise with a constructor where
294+
// native V8 async function return values fail `instanceof globalThis.Promise`.
295+
class ZoneLikePromise<T> {
296+
#inner: Promise<T>;
297+
298+
constructor(
299+
executor: (
300+
resolve: (value: T | PromiseLike<T>) => void,
301+
reject: (reason?: unknown) => void,
302+
) => void,
303+
) {
304+
this.#inner = new OriginalPromise(executor);
305+
}
306+
307+
then<TResult1 = T, TResult2 = never>(
308+
onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null,
309+
onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null,
310+
): ZoneLikePromise<TResult1 | TResult2> {
311+
return new ZoneLikePromise((resolve, reject) => {
312+
this.#inner.then(onfulfilled, onrejected).then(resolve, reject);
313+
});
314+
}
315+
316+
catch<TResult = never>(
317+
onrejected?: ((reason: unknown) => TResult | PromiseLike<TResult>) | null,
318+
): ZoneLikePromise<T | TResult> {
319+
return this.then(undefined, onrejected);
320+
}
321+
322+
finally(onfinally?: (() => void) | null): ZoneLikePromise<T> {
323+
return new ZoneLikePromise((resolve, reject) => {
324+
this.#inner.finally(onfinally).then(resolve, reject);
325+
});
326+
}
327+
328+
static resolve<U>(value?: U | PromiseLike<U>): ZoneLikePromise<Awaited<U>> {
329+
if (value instanceof ZoneLikePromise) {
330+
return value;
331+
}
332+
333+
return new ZoneLikePromise((resolve) => resolve(value as Awaited<U>));
334+
}
335+
336+
static reject<U = never>(reason?: unknown): ZoneLikePromise<U> {
337+
return new ZoneLikePromise((_, reject) => reject(reason));
338+
}
339+
340+
static all(values: Iterable<unknown>): ZoneLikePromise<unknown[]> {
341+
return ZoneLikePromise.resolve(OriginalPromise.all(values));
342+
}
343+
}
344+
345+
const nonRebasingCompiler = new SassCompiler(false);
346+
globalThis.Promise = ZoneLikePromise as unknown as PromiseConstructor;
347+
try {
348+
const application = await compile(buttonStylesheet);
349+
expect(application).toContain('content: "project";');
350+
351+
const nonRebasingResult = await nonRebasingCompiler.compileStringAsync("@use 'theme';", {
352+
importers: [
353+
{
354+
findFileUrl: async (url) =>
355+
url === 'theme'
356+
? pathToFileURL(join(projectRoot, 'node_modules', 'theme', '_index.scss'))
357+
: null,
358+
},
359+
],
360+
});
361+
expect(nonRebasingResult.css).toContain('content: "project";');
362+
} finally {
363+
globalThis.Promise = OriginalPromise;
364+
await nonRebasingCompiler.close();
365+
}
366+
});
289367
});
290368
});

‎packages/angular/build/src/tools/sass/rebasing-importer.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -344,14 +344,18 @@ export class AsyncModuleUrlRebasingImporter implements Importer<'async'> {
344344
this.load = this.load.bind(this);
345345
}
346346

347-
async canonicalize(url: string, options: CanonicalizeContext): Promise<URL | null> {
347+
canonicalize(url: string, options: CanonicalizeContext): Promise<URL | null> | URL | null {
348348
if (url.startsWith('file://')) {
349349
return this.relativeImporter.canonicalize(url, options);
350350
}
351351

352-
const result = await this.finder(url, options);
353-
354-
return result ? this.relativeImporter.canonicalize(result.href, options) : null;
352+
// Use `Promise.resolve` instead of an `async` method so the returned promise is an
353+
// instance of the current `globalThis.Promise` (e.g., `ZoneAwarePromise` when Zone.js
354+
// is loaded during SSR). `sass-embedded` checks `result instanceof Promise`, which fails
355+
// for native `async` function return values when `globalThis.Promise` has been replaced.
356+
return Promise.resolve(this.finder(url, options)).then((result) =>
357+
result ? this.relativeImporter.canonicalize(result.href, options) : null,
358+
);
355359
}
356360

357361
load(canonicalUrl: URL): ImporterResult | null {

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,16 @@ export class SassCompiler {
111111
const compiler = await this.#ensureAsyncCompiler();
112112

113113
if (!this.rebase) {
114-
return compiler.compileStringAsync(source, options);
114+
return compiler.compileStringAsync(source, {
115+
...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+
),
123+
});
115124
}
116125

117126
const { functions, importers, importer, url, logger, ...serializableOptions } = options;

0 commit comments

Comments
 (0)