@wordpress/interactivity-router
Version:
Package that exposes state and actions from the `core/router` store, part of the Interactivity API.
72 lines (69 loc) • 2.14 kB
JavaScript
/**
* This code is derived from the following projects:
*
* 1. dynamic-importmap (https://github.com/keller-mark/dynamic-importmap)
* 2. es-module-shims (https://github.com/guybedford/es-module-shims)
*
* The original implementation was created by Guy Bedford in es-module-shims,
* then adapted by Mark Keller in dynamic-importmap, and further modified
* for use in this project.
*
* Both projects are licensed under the MIT license.
*
* MIT License: https://opensource.org/licenses/MIT
*/
/**
* Internal dependencies
*/
import { addImportMap, resolve } from './resolver';
import { initPromise, topLevelLoad, preloadModule } from './loader';
// TODO: check if this baseURI should change per document, and so
// it need to be passed as a parameter to methods like `importWithMap`
// and `preloadWithMap`.
const baseUrl = document.baseURI;
const pageBaseUrl = baseUrl;
Object.defineProperty(self, 'wpInteractivityRouterImport', {
value: importShim,
writable: false,
enumerable: false,
configurable: false
});
async function importShim(id) {
await initPromise;
return topLevelLoad(resolve(id, pageBaseUrl), {
credentials: 'same-origin'
});
}
/**
* Imports the module with the passed ID.
*
* The module is resolved against the internal dynamic import map,
* extended with the passed import map.
*
* @param id Module ID.
* @param importMapIn Import map.
* @return Resolved module.
*/
export async function importWithMap(id, importMapIn) {
addImportMap(importMapIn);
return importShim(id);
}
/**
* Preloads the module with the passed ID along with its dependencies.
*
* The module is resolved against the internal dynamic import map,
* extended with the passed import map.
*
* @param id Module ID.
* @param importMapIn Import map.
* @return Resolved `ModuleLoad` instance.
*/
export async function preloadWithMap(id, importMapIn) {
addImportMap(importMapIn);
await initPromise;
return preloadModule(resolve(id, pageBaseUrl), {
credentials: 'same-origin'
});
}
export { initialImportMap, importPreloadedModule } from './loader';
//# sourceMappingURL=index.js.map