1/**
2 * Copyright © 2022 650 Industries.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7// Basically `__webpack_require__.l`.
8export function fetchThenEvalAsync(url, { scriptType, nonce, crossOrigin, } = {}) {
9    if (typeof document === 'undefined') {
10        throw new Error('Cannot use fetchThenEvalAsync in a non-browser environment.');
11    }
12    return new Promise((resolve, reject) => {
13        const script = document.createElement('script');
14        if (scriptType)
15            script.type = scriptType;
16        if (nonce)
17            script.setAttribute('nonce', nonce);
18        // script.setAttribute('data-expo-metro', ...);
19        script.src = url;
20        if (crossOrigin && script.src.indexOf(window.location.origin + '/') !== 0) {
21            script.crossOrigin = crossOrigin;
22        }
23        script.onload = () => {
24            script.parentNode && script.parentNode.removeChild(script);
25            resolve();
26        };
27        // Create a new error object to preserve the original stack trace.
28        const error = new AsyncRequireError();
29        // Server error or network error.
30        script.onerror = (ev) => {
31            let event;
32            if (typeof ev === 'string') {
33                event = {
34                    type: 'error',
35                    target: {
36                        // @ts-expect-error
37                        src: event,
38                    },
39                };
40            }
41            else {
42                event = ev;
43            }
44            const errorType = event && (event.type === 'load' ? 'missing' : event.type);
45            // @ts-expect-error
46            const realSrc = event?.target?.src;
47            error.message = 'Loading module ' + url + ' failed.\n(' + errorType + ': ' + realSrc + ')';
48            error.type = errorType;
49            error.request = realSrc;
50            script.parentNode && script.parentNode.removeChild(script);
51            reject(error);
52        };
53        document.head.appendChild(script);
54    });
55}
56class AsyncRequireError extends Error {
57    name = 'AsyncRequireError';
58    type;
59    request;
60}
61//# sourceMappingURL=fetchThenEval.web.js.map