diff --git a/src/index.js b/src/index.js index fa191cf..de95e56 100644 --- a/src/index.js +++ b/src/index.js @@ -294,4 +294,35 @@ export default class Eik { 'Eik config was not loaded or "loadMaps" is "false" when calling .maps()', ); } + + /** + * Function that generates and returns an import map script tag for use in an document head. + * + * Currently only a single import map is supported in the browser so we need to merge together import map objects into a single object + * by using a JS Map object. Last in wins so order of import maps defined in eik.json is important if multiple maps share the same entries. + * + * @example + * ``` + * const importMap = eik.toHTML(); + * + *
+ * ... + * ${importMap} + * ... + * + * ``` + * + * @returns {string} + */ + toHTML() { + const allImportMapKeyValuePairs = this.maps().flatMap((map) => + Object.entries(map.imports), + ); + const mergedAndDedupedImportMapObject = Object.fromEntries( + new Map(allImportMapKeyValuePairs).entries(), + ); + return ``; + } } diff --git a/test/index.test.js b/test/index.test.js index ca1601b..e6f2aed 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -342,3 +342,23 @@ tap.test( t.end(); }, ); + +tap.test( + "Client - toHTML - import maps merged and script tag created", + async (t) => { + const client = new Eik({ + loadMaps: true, + path: t.context.fixture, + }); + await client.load(); + + const resolved = client.toHTML(); + + t.same( + resolved, + ``, + "Should return an import map script tag", + ); + t.end(); + }, +);