@eik/node-client 1.1.64 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ # [1.2.0](https://github.com/eik-lib/node-client/compare/v1.1.64...v1.2.0) (2024-10-15)
2
+
3
+
4
+ ### Features
5
+
6
+ * add toHTML function ([4576ddd](https://github.com/eik-lib/node-client/commit/4576dddc88afb5722b80574438b03dc41956af72))
7
+
1
8
  ## [1.1.64](https://github.com/eik-lib/node-client/compare/v1.1.63...v1.1.64) (2024-08-30)
2
9
 
3
10
 
package/README.md CHANGED
@@ -200,13 +200,6 @@ const client = new Eik({
200
200
  await client.load();
201
201
 
202
202
  const maps = client.maps();
203
- const combined = maps.reduce((map, acc) => ({ ...acc, ...map }), {});
204
-
205
- const html = `
206
- <script type="importmap">
207
- ${JSON.stringify(combined, null, 2)}
208
- </script>
209
- `;
210
203
  ```
211
204
 
212
205
  #### returns
@@ -257,6 +250,33 @@ await client.load();
257
250
  client.base(); // http://localhost:8080/assets
258
251
  ```
259
252
 
253
+ ### .toHTML()
254
+
255
+ Constructs an HTML import map script tag for use in the document head when doing import mapping.
256
+
257
+ ```js
258
+ const client = new Eik({
259
+ loadMaps: true,
260
+ ...
261
+ });
262
+ await client.load();
263
+
264
+ const html = `
265
+ <html>
266
+ <head>
267
+ ...
268
+ ${client.toHTML()}
269
+ ...
270
+ </head>
271
+ <body>
272
+ ...
273
+ </body>
274
+ </html>
275
+ `;
276
+ ```
277
+
278
+ Due to browsers being restricted to a single import map, all import maps registered in eik.json or package.json will be merged down into a single import map with last in winning in case of duplicate keys.
279
+
260
280
  ## License
261
281
 
262
282
  Copyright (c) 2021 FINN.no
package/dist/index.cjs CHANGED
@@ -321,23 +321,12 @@ class Eik {
321
321
  *
322
322
  * @example
323
323
  * ```js
324
- * // generate a <script type="importmap">
325
- * // for import mapping in the browser
326
324
  * const client = new Eik({
327
325
  * loadMaps: true,
328
326
  * });
329
327
  * await client.load();
330
328
  *
331
329
  * const maps = client.maps();
332
- * const combined = maps
333
- * .map((map) => map.imports)
334
- * .reduce((map, acc) => ({ ...acc, ...map }), {});
335
- *
336
- * const html = `
337
- * <script type="importmap">
338
- * ${JSON.stringify(combined, null, 2)}
339
- * </script>
340
- * `;
341
330
  * ```
342
331
  */
343
332
  maps() {
@@ -346,6 +335,38 @@ class Eik {
346
335
  'Eik config was not loaded or "loadMaps" is "false" when calling .maps()',
347
336
  );
348
337
  }
338
+
339
+ /**
340
+ * Function that generates and returns an import map script tag for use in an document head.
341
+ *
342
+ * Only a single import map is allowed per HTML document.
343
+ * A key (ex. `react`) can only be defined once.
344
+ * If multiple import maps defined in `eik.json` use the same key, the last key wins.
345
+ *
346
+ * @example
347
+ * ```
348
+ * const importMap = eik.toHTML();
349
+ *
350
+ * <head>
351
+ * ...
352
+ * ${importMap}
353
+ * ...
354
+ * </head>
355
+ * ```
356
+ *
357
+ * @returns {string}
358
+ */
359
+ toHTML() {
360
+ const allImportMapKeyValuePairs = this.maps().flatMap((map) =>
361
+ Object.entries(map.imports),
362
+ );
363
+ const mergedAndDedupedImportMapObject = Object.fromEntries(
364
+ new Map(allImportMapKeyValuePairs).entries(),
365
+ );
366
+ return `<script type="importmap">${JSON.stringify({
367
+ imports: mergedAndDedupedImportMapObject,
368
+ })}</script>`;
369
+ }
349
370
  }
350
371
 
351
372
  module.exports = Eik;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eik/node-client",
3
- "version": "1.1.64",
3
+ "version": "1.2.0",
4
4
  "description": "Utilities for working with assets and import maps on an Eik server",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -52,13 +52,13 @@
52
52
  "@eik/prettier-config": "1.0.1",
53
53
  "@eik/semantic-release-config": "1.0.0",
54
54
  "@eik/typescript-config": "1.0.0",
55
- "eslint": "9.8.0",
56
- "npm-run-all": "4.1.5",
55
+ "eslint": "9.11.1",
56
+ "npm-run-all2": "5.0.2",
57
57
  "prettier": "3.3.3",
58
58
  "rimraf": "6.0.1",
59
- "rollup": "4.20.0",
59
+ "rollup": "4.22.5",
60
60
  "semantic-release": "24.0.0",
61
- "tap": "21.0.0",
61
+ "tap": "21.0.1",
62
62
  "typescript": "5.5.4"
63
63
  }
64
64
  }
package/src/index.js CHANGED
@@ -269,23 +269,12 @@ export default class Eik {
269
269
  *
270
270
  * @example
271
271
  * ```js
272
- * // generate a <script type="importmap">
273
- * // for import mapping in the browser
274
272
  * const client = new Eik({
275
273
  * loadMaps: true,
276
274
  * });
277
275
  * await client.load();
278
276
  *
279
277
  * const maps = client.maps();
280
- * const combined = maps
281
- * .map((map) => map.imports)
282
- * .reduce((map, acc) => ({ ...acc, ...map }), {});
283
- *
284
- * const html = `
285
- * <script type="importmap">
286
- * ${JSON.stringify(combined, null, 2)}
287
- * </script>
288
- * `;
289
278
  * ```
290
279
  */
291
280
  maps() {
@@ -294,4 +283,36 @@ export default class Eik {
294
283
  'Eik config was not loaded or "loadMaps" is "false" when calling .maps()',
295
284
  );
296
285
  }
286
+
287
+ /**
288
+ * Function that generates and returns an import map script tag for use in an document head.
289
+ *
290
+ * Only a single import map is allowed per HTML document.
291
+ * A key (ex. `react`) can only be defined once.
292
+ * If multiple import maps defined in `eik.json` use the same key, the last key wins.
293
+ *
294
+ * @example
295
+ * ```
296
+ * const importMap = eik.toHTML();
297
+ *
298
+ * <head>
299
+ * ...
300
+ * ${importMap}
301
+ * ...
302
+ * </head>
303
+ * ```
304
+ *
305
+ * @returns {string}
306
+ */
307
+ toHTML() {
308
+ const allImportMapKeyValuePairs = this.maps().flatMap((map) =>
309
+ Object.entries(map.imports),
310
+ );
311
+ const mergedAndDedupedImportMapObject = Object.fromEntries(
312
+ new Map(allImportMapKeyValuePairs).entries(),
313
+ );
314
+ return `<script type="importmap">${JSON.stringify({
315
+ imports: mergedAndDedupedImportMapObject,
316
+ })}</script>`;
317
+ }
297
318
  }
package/types/index.d.ts CHANGED
@@ -175,26 +175,36 @@ export default class Eik {
175
175
  *
176
176
  * @example
177
177
  * ```js
178
- * // generate a <script type="importmap">
179
- * // for import mapping in the browser
180
178
  * const client = new Eik({
181
179
  * loadMaps: true,
182
180
  * });
183
181
  * await client.load();
184
182
  *
185
183
  * const maps = client.maps();
186
- * const combined = maps
187
- * .map((map) => map.imports)
188
- * .reduce((map, acc) => ({ ...acc, ...map }), {});
189
- *
190
- * const html = `
191
- * <script type="importmap">
192
- * ${JSON.stringify(combined, null, 2)}
193
- * </script>
194
- * `;
195
184
  * ```
196
185
  */
197
186
  maps(): ImportMap[];
187
+ /**
188
+ * Function that generates and returns an import map script tag for use in an document head.
189
+ *
190
+ * Only a single import map is allowed per HTML document.
191
+ * A key (ex. `react`) can only be defined once.
192
+ * If multiple import maps defined in `eik.json` use the same key, the last key wins.
193
+ *
194
+ * @example
195
+ * ```
196
+ * const importMap = eik.toHTML();
197
+ *
198
+ * <head>
199
+ * ...
200
+ * ${importMap}
201
+ * ...
202
+ * </head>
203
+ * ```
204
+ *
205
+ * @returns {string}
206
+ */
207
+ toHTML(): string;
198
208
  #private;
199
209
  }
200
210
  export type Options = {