diff --git a/knip.config.ts b/knip.config.ts index a2ee5a77c268..70cb2f575776 100644 --- a/knip.config.ts +++ b/knip.config.ts @@ -79,8 +79,9 @@ const config: KnipConfig = { 'static/app': { ...frontendWorkspace, // Locale modules are loaded with a template-string import. + // core-js is imported explicitly by utils/polyfills.ts, so it is no longer + // only an SWC-injected dependency in this workspace. ignoreDependencies: [ - ...frontendWorkspace.ignoreDependencies, 'moment', '@figma/code-connect', // Figma mappings are parsed by the CLI via pnpm dlx. ], diff --git a/static/app/utils/polyfills.spec.tsx b/static/app/utils/polyfills.spec.tsx new file mode 100644 index 000000000000..b3e526c8b482 --- /dev/null +++ b/static/app/utils/polyfills.spec.tsx @@ -0,0 +1,53 @@ +describe('polyfills', () => { + const methods = ['toSorted', 'toReversed', 'toSpliced', 'with', 'findLast'] as const; + const originals = Object.fromEntries( + methods.map(name => [name, Object.getOwnPropertyDescriptor(Array.prototype, name)]) + ); + const originalMapGroupBy = Object.getOwnPropertyDescriptor(Map, 'groupBy'); + + afterEach(() => { + for (const name of methods) { + const descriptor = originals[name]; + if (descriptor) { + // eslint-disable-next-line no-extend-native -- restore the native method + Object.defineProperty(Array.prototype, name, descriptor); + } + } + if (originalMapGroupBy) { + Object.defineProperty(Map, 'groupBy', originalMapGroupBy); + } + }); + + it('installs ES2023 array methods when the engine lacks them', () => { + for (const name of methods) { + // Simulate an older engine (e.g. iOS 15 WebKit) + delete (Array.prototype as any)[name]; + } + expect((Array.prototype as any).toSorted).toBeUndefined(); + + jest.isolateModules(() => { + require('sentry/utils/polyfills'); + }); + + const input = [3, 1, 2]; + expect(input.toSorted((a, b) => a - b)).toEqual([1, 2, 3]); + expect(input).toEqual([3, 1, 2]); + expect(input.toReversed()).toEqual([2, 1, 3]); + expect(input.toSpliced(0, 1)).toEqual([1, 2]); + expect(input.with(0, 9)).toEqual([9, 1, 2]); + expect(input.findLast(n => n > 1)).toBe(2); + }); + + it('installs Map.groupBy when the engine lacks it', () => { + delete (Map as any).groupBy; + expect((Map as any).groupBy).toBeUndefined(); + + jest.isolateModules(() => { + require('sentry/utils/polyfills'); + }); + + const groups = Map.groupBy([1, 2, 3, 4], n => (n % 2 === 0 ? 'even' : 'odd')); + expect(groups.get('even')).toEqual([2, 4]); + expect(groups.get('odd')).toEqual([1, 3]); + }); +}); diff --git a/static/app/utils/polyfills.ts b/static/app/utils/polyfills.ts new file mode 100644 index 000000000000..5c23249d20ec --- /dev/null +++ b/static/app/utils/polyfills.ts @@ -0,0 +1,25 @@ +/** + * Explicit polyfills for ES2023+ built-ins that are used throughout the app. + * + * Our swc loader injects core-js polyfills in `usage` mode, but only for the + * browsers listed in `browserslist.production` (package.json). Those targets + * all ship these methods natively, so no polyfill is ever injected and older + * engines that still load the app (e.g. iOS 15 WebKit) crash with errors like + * `toSorted is not a function` before the UI can render. + * + * Importing them here, from the first module in each entrypoint, makes them + * available everywhere. core-js skips installing anything the browser already + * supports natively. + * + * The no-unnecessary-polyfills rule checks against the same browserslist + * targets, which is exactly the gap these imports exist to cover. + */ +/* oxlint-disable unicorn-js/no-unnecessary-polyfills */ +import 'core-js/actual/array/find-last'; +import 'core-js/actual/array/find-last-index'; +import 'core-js/actual/array/to-reversed'; +import 'core-js/actual/array/to-sorted'; +import 'core-js/actual/array/to-spliced'; +import 'core-js/actual/array/with'; +import 'core-js/actual/map/group-by'; +import 'core-js/actual/object/group-by'; diff --git a/static/app/utils/setupStatics.tsx b/static/app/utils/setupStatics.tsx index b806db1b7d0e..cb848754cf29 100644 --- a/static/app/utils/setupStatics.tsx +++ b/static/app/utils/setupStatics.tsx @@ -1,3 +1,6 @@ +// Must run before any app code that relies on ES2023 built-ins. +import './polyfills'; + declare global { var __zod_globalConfig: {jitless?: boolean} | undefined; }