UNPKG

@bacons/expo-metro-runtime

Version:

Tools for making experimental Metro bundler features work

50 lines 2.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.buildAsyncRequire = void 0; const loadBundle_1 = require("./loadBundle"); /** Create an `asyncRequire` function in the expected shape for Metro bundler. */ function buildAsyncRequire(metroRequire) { const importBundleNames = Object.create(null); const importBundlePromises = Object.create(null); // This is basically `__webpack_require__.u` -> returns the bundle path for a numeric moduleID function getBundlePath(moduleID) { return importBundleNames[moduleID]; } function asyncRequire(moduleID, moduleName = "", options = { isPrefetchOnly: false }) { if (options.isPrefetchOnly) { return Promise.resolve(); } const stringModuleID = String(moduleID); const bundlePath = getBundlePath(stringModuleID); if (bundlePath) { // Prevent loading the same module more than once. if (!importBundlePromises[stringModuleID]) { importBundlePromises[stringModuleID] = (0, loadBundle_1.loadBundleAsync)(bundlePath).then(() => metroRequire(moduleID)); } // Return for the user to resolve. return importBundlePromises[stringModuleID]; } return metroRequire.importAll(moduleID); } asyncRequire.prefetch = function (moduleID, moduleName) { const result = asyncRequire(moduleID, moduleName, { isPrefetchOnly: true }); if (result instanceof Promise) { result.then(() => { }, () => { }); } }; asyncRequire.resource = function (moduleID, moduleName) { throw new Error("Unimplemented Metro runtime feature"); }; /** * Register modules that can be loaded async. * Key is a numeric string and value is a string denoting the bundle path. * * @example { '1': 'Second' } */ asyncRequire.addImportBundleNames = function (names) { Object.assign(importBundleNames, names); }; return asyncRequire; } exports.buildAsyncRequire = buildAsyncRequire; //# sourceMappingURL=buildAsyncRequire.js.map