|
| 1 | +/** |
| 2 | + * Copyright 2026, Optimizely |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +import type { Plugin } from 'vite'; |
| 17 | + |
| 18 | +// Vitest 5's in-browser runtime (the tester page and the pre-bundled vitest |
| 19 | +// runtime) calls a few built-ins that the oldest browsers we test don't have: |
| 20 | +// |
| 21 | +// Array/String/TypedArray.prototype.at Chromium 92, Firefox 90, Safari 15.4 |
| 22 | +// Object.hasOwn Chromium 93, Firefox 92, Safari 15.4 |
| 23 | +// Array.prototype.findLast(Index) Chromium 97, Firefox 104, Safari 15.4 |
| 24 | +// |
| 25 | +// Without them the runtime throws before it can report back, and the run hangs |
| 26 | +// until it times out (seen on Edge 89 and 90). Vitest 3 didn't depend on these, so the |
| 27 | +// root setup doesn't need this. |
| 28 | +// |
| 29 | +// The polyfills go into both pages Vitest loads: the orchestrator page (via |
| 30 | +// `browser.orchestratorScripts`, see legacyPolyfillsOrchestratorScript) and the |
| 31 | +// test iframe (via this plugin's transformIndexHtml). They run before any other |
| 32 | +// script, so the SDK code under test also sees them. The SDK source (lib/) doesn't use any of |
| 33 | +// these today. If it starts to, tests on these old browsers would no longer |
| 34 | +// catch it. |
| 35 | +const POLYFILLS = ` |
| 36 | +(function () { |
| 37 | + function define(target, name, value) { |
| 38 | + if (target && !target[name]) { |
| 39 | + Object.defineProperty(target, name, { value: value, writable: true, configurable: true }); |
| 40 | + } |
| 41 | + } |
| 42 | +
|
| 43 | + function at(index) { |
| 44 | + var length = this.length; |
| 45 | + var i = Math.trunc(index) || 0; |
| 46 | + if (i < 0) i += length; |
| 47 | + return i < 0 || i >= length ? undefined : this[i]; |
| 48 | + } |
| 49 | +
|
| 50 | + function findLast(predicate, thisArg) { |
| 51 | + for (var i = this.length - 1; i >= 0; i--) { |
| 52 | + if (predicate.call(thisArg, this[i], i, this)) return this[i]; |
| 53 | + } |
| 54 | + return undefined; |
| 55 | + } |
| 56 | +
|
| 57 | + function findLastIndex(predicate, thisArg) { |
| 58 | + for (var i = this.length - 1; i >= 0; i--) { |
| 59 | + if (predicate.call(thisArg, this[i], i, this)) return i; |
| 60 | + } |
| 61 | + return -1; |
| 62 | + } |
| 63 | +
|
| 64 | + var typedArrayPrototype = Object.getPrototypeOf(Int8Array.prototype); |
| 65 | +
|
| 66 | + define(Array.prototype, 'at', at); |
| 67 | + define(String.prototype, 'at', at); |
| 68 | + define(typedArrayPrototype, 'at', at); |
| 69 | + define(Array.prototype, 'findLast', findLast); |
| 70 | + define(Array.prototype, 'findLastIndex', findLastIndex); |
| 71 | + define(typedArrayPrototype, 'findLast', findLast); |
| 72 | + define(typedArrayPrototype, 'findLastIndex', findLastIndex); |
| 73 | + define(Object, 'hasOwn', function (object, key) { |
| 74 | + return Object.prototype.hasOwnProperty.call(Object(object), key); |
| 75 | + }); |
| 76 | +})(); |
| 77 | +`; |
| 78 | + |
| 79 | +// For `browser.orchestratorScripts`. The orchestrator page doesn't go through |
| 80 | +// transformIndexHtml. A classic script (not a module) runs while the page is |
| 81 | +// parsed, before Vitest's module scripts. |
| 82 | +export const legacyPolyfillsOrchestratorScript = { |
| 83 | + id: 'legacy-polyfills.js', |
| 84 | + content: POLYFILLS, |
| 85 | + type: 'text/javascript', |
| 86 | +}; |
| 87 | + |
| 88 | +// Adds the polyfills to the test iframe page |
| 89 | +export function legacyPolyfillsPlugin(): Plugin { |
| 90 | + return { |
| 91 | + name: 'legacy-polyfills', |
| 92 | + transformIndexHtml() { |
| 93 | + return [ |
| 94 | + { |
| 95 | + tag: 'script', |
| 96 | + children: POLYFILLS, |
| 97 | + injectTo: 'head-prepend', |
| 98 | + }, |
| 99 | + ]; |
| 100 | + }, |
| 101 | + }; |
| 102 | +} |
0 commit comments