@eik/esbuild-plugin 1.0.5 → 1.1.3

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.
Files changed (2) hide show
  1. package/package.json +13 -16
  2. package/src/plugin.js +17 -27
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eik/esbuild-plugin",
3
- "version": "1.0.5",
3
+ "version": "1.1.3",
4
4
  "description": "ESBuild plugin for loading import maps from a Eik server and applying the mapping to ECMAScript modules in preparation for upload to the same server.",
5
5
  "type": "module",
6
6
  "main": "./src/plugin.js",
@@ -9,7 +9,7 @@
9
9
  ],
10
10
  "scripts": {
11
11
  "test": "tap --no-coverage",
12
- "test:snapshot": "TAP_SNAPSHOT=1 tap --no-esm test/*.js --no-coverage",
12
+ "test:snapshot": "TAP_SNAPSHOT=1 tap test/*.js --no-coverage",
13
13
  "lint": "eslint . --ext=js",
14
14
  "lint:fix": "eslint . --fix --ext=js"
15
15
  },
@@ -35,22 +35,19 @@
35
35
  },
36
36
  "homepage": "https://github.com/eik-lib/esbuild-plugin#readme",
37
37
  "devDependencies": {
38
- "@semantic-release/changelog": "6.0.0",
39
- "@semantic-release/commit-analyzer": "9.0.1",
40
- "@semantic-release/git": "10.0.0",
41
- "@semantic-release/github": "8.0.1",
42
- "@semantic-release/npm": "8.0.0",
43
- "@semantic-release/release-notes-generator": "10.0.2",
44
- "esbuild": "0.9.0",
45
- "eslint": "7.32.0",
46
- "eslint-config-airbnb-base": "14.2.1",
47
- "eslint-plugin-import": "2.24.0",
48
- "fastify": "3.13.0",
49
- "semantic-release": "18.0.0",
50
- "tap": "15.0.5"
38
+ "@semantic-release/changelog": "6.0.1",
39
+ "@semantic-release/git": "10.0.1",
40
+ "esbuild": "0.14.13",
41
+ "eslint": "8.7.0",
42
+ "eslint-config-airbnb-base": "15.0.0",
43
+ "eslint-plugin-import": "2.25.4",
44
+ "fastify": "3.27.0",
45
+ "semantic-release": "19.0.2",
46
+ "tap": "15.1.6"
51
47
  },
52
48
  "dependencies": {
49
+ "@eik/common": "3.0.0",
53
50
  "esbuild-plugin-import-map": "2.1.0",
54
- "node-fetch": "2.6.6"
51
+ "undici": "4.13.0"
55
52
  }
56
53
  }
package/src/plugin.js CHANGED
@@ -1,55 +1,45 @@
1
1
  /* eslint-disable no-restricted-syntax */
2
2
 
3
3
  import * as importMapPlugin from 'esbuild-plugin-import-map';
4
- import { join } from 'path';
5
- import fetch from 'node-fetch';
6
- import fs from 'fs';
4
+ import { helpers } from '@eik/common';
5
+ import { request } from 'undici';
7
6
 
8
- async function readEikJSONMaps(eikJSONPath) {
7
+ const fetchImportMaps = async (urls = []) => {
9
8
  try {
10
- const contents = await fs.promises.readFile(eikJSONPath);
11
- const eikJSON = JSON.parse(contents);
12
- if (typeof eikJSON['import-map'] === 'string') return [eikJSON['import-map']];
13
- return eikJSON['import-map'] || [];
14
- } catch (err) {
15
- return [];
16
- }
17
- }
9
+ const maps = urls.map(async (map) => {
10
+ const {
11
+ statusCode,
12
+ body,
13
+ } = await request(map, { maxRedirections: 2 });
18
14
 
19
- async function fetchImportMaps(urls = []) {
20
- try {
21
- const maps = urls.map((map) => fetch(map).then((result) => {
22
- if (result.status === 404) {
15
+ if (statusCode === 404) {
23
16
  throw new Error('Import map could not be found on server');
24
- } else if (result.status >= 400 && result.status < 500) {
17
+ } else if (statusCode >= 400 && statusCode < 500) {
25
18
  throw new Error('Server rejected client request');
26
- } else if (result.status >= 500) {
19
+ } else if (statusCode >= 500) {
27
20
  throw new Error('Server error');
28
21
  }
29
- return result.json();
30
- }));
22
+ return body.json();
23
+ });
31
24
  return await Promise.all(maps);
32
25
  } catch (err) {
33
26
  throw new Error(
34
27
  `Unable to load import map file from server: ${err.message}`,
35
28
  );
36
29
  }
37
- }
30
+ };
38
31
 
39
32
  export async function load({
40
- path = join(process.cwd(), 'eik.json'),
33
+ path = process.cwd(),
41
34
  maps = [],
42
35
  urls = [],
43
36
  } = {}) {
44
37
  const pMaps = Array.isArray(maps) ? maps : [maps];
45
38
  const pUrls = Array.isArray(urls) ? urls : [urls];
46
39
 
47
- const importMapUrls = await readEikJSONMaps(path);
48
- for (const map of importMapUrls) {
49
- pUrls.push(map);
50
- }
40
+ const config = await helpers.getDefaults(path);
51
41
 
52
- const fetched = await fetchImportMaps(pUrls);
42
+ const fetched = await fetchImportMaps([...config.map, ...pUrls]);
53
43
  const mappings = pMaps.concat(fetched);
54
44
 
55
45
  await importMapPlugin.load(mappings);