From 2388372cca0771260b71715de50cd27af7df2acd Mon Sep 17 00:00:00 2001 From: Sirz Benjie <142067137+SirzBenjie@users.noreply.github.com> Date: Sat, 19 Jul 2025 10:06:32 -0600 Subject: [PATCH] [Dev] Add support for manually rolling polyfills. Add polyfill for `promise.withResolvers` (#6103) * Add polyfill for Promise.withResolvers * Import polyfills in main * Apply kev's suggestions from code review Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com> * Address comments from code review --------- Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com> --- src/main.ts | 3 +++ src/polyfills.ts | 21 +++++++++++++++++++++ tsconfig.json | 11 +++++++++++ 3 files changed, 35 insertions(+) create mode 100644 src/polyfills.ts diff --git a/src/main.ts b/src/main.ts index 6c0fcacd521..9063ac0ec5b 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,3 +1,6 @@ +import "#app/polyfills"; +// All polyfills MUST be loaded first for side effects + import { InvertPostFX } from "#app/pipelines/invert"; import { initI18n } from "#app/plugins/i18n"; import { version } from "#package.json"; diff --git a/src/polyfills.ts b/src/polyfills.ts new file mode 100644 index 00000000000..8666629fceb --- /dev/null +++ b/src/polyfills.ts @@ -0,0 +1,21 @@ +/* +Manual rolling of polyfills desired by the project. + +IMPORTANT: When adding / removing polyfills, ensure that typescript becomes +aware of their existence, either by creating `src/typings/polyfills.d.ts` +and defining them there, or or by adding the appropriate field polyfill to the +`lib` property in `tsconfig.json`. +*/ + +if (typeof Promise.withResolvers === "undefined") { + Promise.withResolvers = () => { + // Bangs are OK here; they are guaranteed to be defined when the promise is invoked. + let resolve!: (value: T | PromiseLike) => void; + let reject!: (reason?: unknown) => void; + const promise = new Promise((res, rej) => { + resolve = res; + reject = rej; + }); + return { promise, resolve, reject }; + }; +} diff --git a/tsconfig.json b/tsconfig.json index 39b99cf9ca9..b7ef84869e5 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,6 +2,17 @@ "compilerOptions": { "target": "ES2020", "module": "ES2020", + // Modifying this option requires all values to be set manually because the defaults get overridden + // Values other than "ES2024.Promise" taken from https://github.com/microsoft/TypeScript/blob/main/src/lib/es2020.full.d.ts + "lib": [ + "ES2020", + "ES2024.Promise", + "DOM", + "DOM.AsyncIterable", + "DOM.Iterable", + "ScriptHost", + "WebWorker.ImportScripts" + ], "moduleResolution": "bundler", "resolveJsonModule": true, "esModuleInterop": true,