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