Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -286,5 +286,69 @@ describe('sass-language', () => {
pathToFileURL(join(projectRoot, 'node_modules', 'theme', '_index.scss')).href,
);
});

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

// Simulate Zone.js replacing globalThis.Promise with a constructor where
// native V8 async function return values fail `instanceof globalThis.Promise`.
class ZoneLikePromise<T> {
#inner: Promise<T>;

constructor(
executor: (
resolve: (value: T | PromiseLike<T>) => void,
reject: (reason?: unknown) => void,
) => void,
) {
this.#inner = new originalPromise(executor);
}

then<TResult1 = T, TResult2 = never>(
onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null,
onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null,
): ZoneLikePromise<TResult1 | TResult2> {
return new ZoneLikePromise((resolve, reject) => {
this.#inner.then(onfulfilled, onrejected).then(resolve, reject);
});
}

catch<TResult = never>(
onrejected?: ((reason: unknown) => TResult | PromiseLike<TResult>) | null,
): ZoneLikePromise<T | TResult> {
return this.then(undefined, onrejected);
}

finally(onfinally?: (() => void) | null): ZoneLikePromise<T> {
return new ZoneLikePromise((resolve, reject) => {
this.#inner.finally(onfinally).then(resolve, reject);
});
}

static resolve<U>(value?: U | PromiseLike<U>): ZoneLikePromise<Awaited<U>> {
if (value instanceof ZoneLikePromise) {
return value;
}

return new ZoneLikePromise((resolve) => resolve(value as Awaited<U>));
}

static reject<U = never>(reason?: unknown): ZoneLikePromise<U> {
return new ZoneLikePromise((_, reject) => reject(reason));
}

static all(values: Iterable<unknown>): ZoneLikePromise<unknown[]> {
return ZoneLikePromise.resolve(originalPromise.all(values));
}
}

globalThis.Promise = ZoneLikePromise as unknown as PromiseConstructor;
try {
const application = await compile(buttonStylesheet);
expect(application).toContain('content: "project";');
} finally {
globalThis.Promise = originalPromise;
}
});
});
});
12 changes: 8 additions & 4 deletions packages/angular/build/src/tools/sass/rebasing-importer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,14 +344,18 @@ export class AsyncModuleUrlRebasingImporter implements Importer<'async'> {
this.load = this.load.bind(this);
}

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

const result = await this.finder(url, options);

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

load(canonicalUrl: URL): ImporterResult | null {
Expand Down
Loading