1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.fetchThenEvalAsync = void 0;
4/**
5 * Copyright © 2022 650 Industries.
6 *
7 * This source code is licensed under the MIT license found in the
8 * LICENSE file in the root directory of this source tree.
9 */
10const fetchAsync_1 = require("./fetchAsync");
11/**
12 * Load a bundle for a URL using fetch + eval on native and script tag injection on web.
13 *
14 * @param bundlePath Given a statement like `import('./Bacon')` `bundlePath` would be `Bacon`.
15 */
16function fetchThenEvalAsync(url) {
17    return (0, fetchAsync_1.fetchAsync)(url).then(({ body, headers }) => {
18        if (headers?.has?.('Content-Type') != null &&
19            headers.get('Content-Type').includes('application/json')) {
20            // Errors are returned as JSON.
21            throw new Error(JSON.parse(body).message || `Unknown error fetching '${url}'`);
22        }
23        // NOTE(EvanBacon): All of this code is ignored in development mode at the root.
24        // Some engines do not support `sourceURL` as a comment. We expose a
25        // `globalEvalWithSourceUrl` function to handle updates in that case.
26        if (global.globalEvalWithSourceUrl) {
27            global.globalEvalWithSourceUrl(body, url);
28        }
29        else {
30            // eslint-disable-next-line no-eval
31            eval(body);
32        }
33    });
34}
35exports.fetchThenEvalAsync = fetchThenEvalAsync;
36//# sourceMappingURL=fetchThenEvalJs.js.map