@eik/esbuild-plugin 1.1.47 → 1.1.48
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/README.md +46 -40
- package/package.json +23 -15
- package/src/plugin.js +93 -39
- package/types/plugin.d.ts +69 -0
package/README.md
CHANGED
|
@@ -11,17 +11,17 @@ $ npm install @eik/esbuild-plugin
|
|
|
11
11
|
## Usage
|
|
12
12
|
|
|
13
13
|
```js
|
|
14
|
-
import * as eik from
|
|
15
|
-
import esbuild from
|
|
14
|
+
import * as eik from "@eik/esbuild-plugin";
|
|
15
|
+
import esbuild from "esbuild";
|
|
16
16
|
|
|
17
17
|
await eik.load();
|
|
18
18
|
|
|
19
19
|
await esbuild.build({
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
20
|
+
entryPoints: ["./src/app.js"],
|
|
21
|
+
bundle: true,
|
|
22
|
+
format: "esm",
|
|
23
|
+
target: ["esnext"],
|
|
24
|
+
plugins: [eik.plugin()],
|
|
25
25
|
});
|
|
26
26
|
```
|
|
27
27
|
|
|
@@ -31,7 +31,7 @@ This plugin will read a local Eik config file (`eik.json`) and download the impo
|
|
|
31
31
|
|
|
32
32
|
### Plugin result
|
|
33
33
|
|
|
34
|
-
Bundles will have bare imports mapped to absolute URLs.
|
|
34
|
+
Bundles will have bare imports mapped to absolute URLs.
|
|
35
35
|
|
|
36
36
|
Ie. Something like this...
|
|
37
37
|
|
|
@@ -53,11 +53,11 @@ This module has the following API:
|
|
|
53
53
|
|
|
54
54
|
Loads an Eik configuration or import maps directly for the plugn to use to apply.
|
|
55
55
|
|
|
56
|
-
| option
|
|
57
|
-
|
|
|
58
|
-
| path
|
|
59
|
-
| urls
|
|
60
|
-
| maps
|
|
56
|
+
| option | default | type | required | details |
|
|
57
|
+
| ------ | -------------- | -------- | -------- | --------------------------------------- |
|
|
58
|
+
| path | `cwd/eik.json` | `string` | `false` | Path to eik.json file. |
|
|
59
|
+
| urls | `[]` | `array` | `false` | Array of import map URLs to fetch from. |
|
|
60
|
+
| maps | `[]` | `array` | `false` | Array of import map as objects. |
|
|
61
61
|
|
|
62
62
|
The plugin will attempt to read a `eik.json` from the current working directory of the Node.js process.
|
|
63
63
|
|
|
@@ -69,7 +69,7 @@ The path to the location of an `eik.json` file can be specified with the `path`
|
|
|
69
69
|
|
|
70
70
|
```js
|
|
71
71
|
await eik.load({
|
|
72
|
-
|
|
72
|
+
path: "/path/to/eik.json",
|
|
73
73
|
});
|
|
74
74
|
```
|
|
75
75
|
|
|
@@ -77,7 +77,7 @@ The plugin can also load import maps directly from one or multiple URLs using th
|
|
|
77
77
|
|
|
78
78
|
```js
|
|
79
79
|
await eik.load({
|
|
80
|
-
|
|
80
|
+
urls: ["http://myserver/import-maps/map.json"],
|
|
81
81
|
});
|
|
82
82
|
```
|
|
83
83
|
|
|
@@ -85,11 +85,13 @@ Additionally, individual import maps can be specified using the `maps` option.
|
|
|
85
85
|
|
|
86
86
|
```js
|
|
87
87
|
await eik.load({
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
88
|
+
maps: [
|
|
89
|
+
{
|
|
90
|
+
imports: {
|
|
91
|
+
"lit-element": "https://cdn.eik.dev/lit-element/v2",
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
],
|
|
93
95
|
});
|
|
94
96
|
```
|
|
95
97
|
|
|
@@ -99,13 +101,15 @@ ie. in the following example
|
|
|
99
101
|
|
|
100
102
|
```js
|
|
101
103
|
await eik.load({
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
104
|
+
path: "/path/to/eik.json",
|
|
105
|
+
urls: ["http://myserver/import-maps/map.json"],
|
|
106
|
+
maps: [
|
|
107
|
+
{
|
|
108
|
+
imports: {
|
|
109
|
+
"lit-element": "https://cdn.eik.dev/lit-element/v2",
|
|
110
|
+
},
|
|
111
|
+
},
|
|
112
|
+
],
|
|
109
113
|
});
|
|
110
114
|
```
|
|
111
115
|
|
|
@@ -116,26 +120,28 @@ Any import map URLs in `eik.json` will be loaded first, then merged with (and ov
|
|
|
116
120
|
Returns the plugin which will apply the loaded import maps during build. The returned plugin should be appended to the ESBuild plugin array.
|
|
117
121
|
|
|
118
122
|
```js
|
|
119
|
-
import * as eik from
|
|
120
|
-
import esbuild from
|
|
123
|
+
import * as eik from "@eik/esbuild-plugin";
|
|
124
|
+
import esbuild from "esbuild";
|
|
121
125
|
|
|
122
126
|
await eik.load();
|
|
123
127
|
|
|
124
|
-
esbuild
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
128
|
+
esbuild
|
|
129
|
+
.build({
|
|
130
|
+
entryPoints: ["src/main.js"],
|
|
131
|
+
bundle: true,
|
|
132
|
+
format: "esm",
|
|
133
|
+
minify: false,
|
|
134
|
+
sourcemap: false,
|
|
135
|
+
target: ["chrome58", "firefox57", "safari11", "edge16"],
|
|
136
|
+
plugins: [eik.plugin()],
|
|
137
|
+
outfile: "out.js",
|
|
138
|
+
})
|
|
139
|
+
.catch(() => process.exit(1));
|
|
134
140
|
```
|
|
135
141
|
|
|
136
142
|
### .clear()
|
|
137
143
|
|
|
138
|
-
Clears the loaded import maps from the plugins internal cache.
|
|
144
|
+
Clears the loaded import maps from the plugins internal cache.
|
|
139
145
|
|
|
140
146
|
## License
|
|
141
147
|
|
package/package.json
CHANGED
|
@@ -1,17 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eik/esbuild-plugin",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.48",
|
|
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",
|
|
7
|
+
"types": "./types/plugin.d.ts",
|
|
7
8
|
"files": [
|
|
8
|
-
"src"
|
|
9
|
+
"src",
|
|
10
|
+
"types"
|
|
9
11
|
],
|
|
10
12
|
"scripts": {
|
|
11
|
-
"test": "tap --
|
|
12
|
-
"
|
|
13
|
-
"lint": "
|
|
14
|
-
"
|
|
13
|
+
"test": "tap --disable-coverage --allow-empty-coverage",
|
|
14
|
+
"lint": "eslint .",
|
|
15
|
+
"lint:fix": "npm run lint -- --fix",
|
|
16
|
+
"types": "run-s types:module types:test",
|
|
17
|
+
"types:module": "tsc",
|
|
18
|
+
"types:test": "tsc --project tsconfig.test.json"
|
|
15
19
|
},
|
|
16
20
|
"repository": {
|
|
17
21
|
"type": "git",
|
|
@@ -35,15 +39,19 @@
|
|
|
35
39
|
},
|
|
36
40
|
"homepage": "https://github.com/eik-lib/esbuild-plugin#readme",
|
|
37
41
|
"devDependencies": {
|
|
38
|
-
"@
|
|
39
|
-
"@
|
|
40
|
-
"
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
"
|
|
45
|
-
"
|
|
46
|
-
"
|
|
42
|
+
"@eik/eslint-config": "1.0.2",
|
|
43
|
+
"@eik/prettier-config": "1.0.1",
|
|
44
|
+
"@eik/semantic-release-config": "1.0.0",
|
|
45
|
+
"@eik/typescript-config": "1.0.0",
|
|
46
|
+
"@types/esbuild-plugin-import-map": "0.0.3",
|
|
47
|
+
"esbuild": "0.23.0",
|
|
48
|
+
"eslint": "9.8.0",
|
|
49
|
+
"fastify": "4.28.1",
|
|
50
|
+
"npm-run-all": "4.1.5",
|
|
51
|
+
"prettier": "3.3.3",
|
|
52
|
+
"semantic-release": "24.0.0",
|
|
53
|
+
"tap": "20.0.3",
|
|
54
|
+
"typescript": "5.5.4"
|
|
47
55
|
},
|
|
48
56
|
"dependencies": {
|
|
49
57
|
"@eik/common": "3.0.1",
|
package/src/plugin.js
CHANGED
|
@@ -1,56 +1,110 @@
|
|
|
1
|
-
|
|
1
|
+
import * as importMapPlugin from "esbuild-plugin-import-map";
|
|
2
|
+
import { helpers } from "@eik/common";
|
|
3
|
+
import { request } from "undici";
|
|
2
4
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
5
|
+
/**
|
|
6
|
+
* @typedef {object} ImportMap
|
|
7
|
+
* @property {Record<string, string>} imports
|
|
8
|
+
*/
|
|
6
9
|
|
|
10
|
+
/**
|
|
11
|
+
* @param {string[]} urls
|
|
12
|
+
* @returns {Promise<ImportMap[]>}
|
|
13
|
+
*/
|
|
7
14
|
const fetchImportMaps = async (urls = []) => {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
`Unable to load import map file from server: ${err.message}`,
|
|
28
|
-
);
|
|
29
|
-
}
|
|
15
|
+
try {
|
|
16
|
+
const maps = urls.map(async (map) => {
|
|
17
|
+
const { statusCode, body } = await request(map, { maxRedirections: 2 });
|
|
18
|
+
|
|
19
|
+
if (statusCode === 404) {
|
|
20
|
+
throw new Error("Import map could not be found on server");
|
|
21
|
+
} else if (statusCode >= 400 && statusCode < 500) {
|
|
22
|
+
throw new Error("Server rejected client request");
|
|
23
|
+
} else if (statusCode >= 500) {
|
|
24
|
+
throw new Error("Server error");
|
|
25
|
+
}
|
|
26
|
+
return /** @type {Promise<ImportMap>} */ (body.json());
|
|
27
|
+
});
|
|
28
|
+
return await Promise.all(maps);
|
|
29
|
+
} catch (err) {
|
|
30
|
+
throw new Error(
|
|
31
|
+
`Unable to load import map file from server: ${err.message}`,
|
|
32
|
+
);
|
|
33
|
+
}
|
|
30
34
|
};
|
|
31
35
|
|
|
36
|
+
/**
|
|
37
|
+
* @typedef {object} LoadOptions
|
|
38
|
+
* @property {string} [path=process.cwd()]
|
|
39
|
+
* @property {string[]} [urls=[]] URLs to import maps hosted on an Eik server. Takes precedence over `eik.json`.
|
|
40
|
+
* @property {ImportMap[]} [maps=[]] Inline import maps that should be used. Takes precedence over `urls` and `eik.json`.
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Loads an Eik configuration or import maps directly for the plugn to use.
|
|
45
|
+
* Call before building.
|
|
46
|
+
*
|
|
47
|
+
* @param {LoadOptions} options
|
|
48
|
+
* @returns {Promise<void>}
|
|
49
|
+
* @example
|
|
50
|
+
* ```js
|
|
51
|
+
* import * as eik from "@eik/esbuild-plugin";
|
|
52
|
+
* import esbuild from "esbuild";
|
|
53
|
+
*
|
|
54
|
+
* await eik.load();
|
|
55
|
+
* await esbuild.build({
|
|
56
|
+
* plugins: [eik.plugin()],
|
|
57
|
+
* });
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
32
60
|
export async function load({
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
61
|
+
path = process.cwd(),
|
|
62
|
+
maps = [],
|
|
63
|
+
urls = [],
|
|
36
64
|
} = {}) {
|
|
37
|
-
|
|
38
|
-
|
|
65
|
+
const pMaps = Array.isArray(maps) ? maps : [maps];
|
|
66
|
+
const pUrls = Array.isArray(urls) ? urls : [urls];
|
|
39
67
|
|
|
40
|
-
|
|
68
|
+
const config = await helpers.getDefaults(path);
|
|
41
69
|
|
|
42
|
-
|
|
43
|
-
|
|
70
|
+
const fetched = await fetchImportMaps([...config.map, ...pUrls]);
|
|
71
|
+
const mappings = pMaps.concat(fetched);
|
|
44
72
|
|
|
45
|
-
|
|
73
|
+
await importMapPlugin.load(mappings);
|
|
46
74
|
}
|
|
47
75
|
|
|
76
|
+
/**
|
|
77
|
+
* Clear the loaded import maps from the plugins internal cache.
|
|
78
|
+
*
|
|
79
|
+
* @returns {void}
|
|
80
|
+
*/
|
|
48
81
|
export function clear() {
|
|
49
|
-
|
|
82
|
+
importMapPlugin.clear();
|
|
50
83
|
}
|
|
51
84
|
|
|
85
|
+
/**
|
|
86
|
+
* @typedef {object} Plugin
|
|
87
|
+
* @property {string} name
|
|
88
|
+
* @property {(build: any) => void} setup
|
|
89
|
+
*/
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Returns the plugin which will apply the loaded import maps during build.
|
|
93
|
+
*
|
|
94
|
+
* @returns {Plugin}
|
|
95
|
+
* @example
|
|
96
|
+
* ```js
|
|
97
|
+
* import * as eik from "@eik/esbuild-plugin";
|
|
98
|
+
* import esbuild from "esbuild";
|
|
99
|
+
*
|
|
100
|
+
* await eik.load();
|
|
101
|
+
* await esbuild.build({
|
|
102
|
+
* plugins: [eik.plugin()],
|
|
103
|
+
* });
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
52
106
|
export function plugin() {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
107
|
+
const obj = importMapPlugin.plugin();
|
|
108
|
+
obj.name = "@eik/esbuild-plugin";
|
|
109
|
+
return obj;
|
|
56
110
|
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {object} LoadOptions
|
|
3
|
+
* @property {string} [path=process.cwd()]
|
|
4
|
+
* @property {string[]} [urls=[]] URLs to import maps hosted on an Eik server. Takes precedence over `eik.json`.
|
|
5
|
+
* @property {ImportMap[]} [maps=[]] Inline import maps that should be used. Takes precedence over `urls` and `eik.json`.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Loads an Eik configuration or import maps directly for the plugn to use.
|
|
9
|
+
* Call before building.
|
|
10
|
+
*
|
|
11
|
+
* @param {LoadOptions} options
|
|
12
|
+
* @returns {Promise<void>}
|
|
13
|
+
* @example
|
|
14
|
+
* ```js
|
|
15
|
+
* import * as eik from "@eik/esbuild-plugin";
|
|
16
|
+
* import esbuild from "esbuild";
|
|
17
|
+
*
|
|
18
|
+
* await eik.load();
|
|
19
|
+
* await esbuild.build({
|
|
20
|
+
* plugins: [eik.plugin()],
|
|
21
|
+
* });
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export function load({ path, maps, urls, }?: LoadOptions): Promise<void>;
|
|
25
|
+
/**
|
|
26
|
+
* Clear the loaded import maps from the plugins internal cache.
|
|
27
|
+
*
|
|
28
|
+
* @returns {void}
|
|
29
|
+
*/
|
|
30
|
+
export function clear(): void;
|
|
31
|
+
/**
|
|
32
|
+
* @typedef {object} Plugin
|
|
33
|
+
* @property {string} name
|
|
34
|
+
* @property {(build: any) => void} setup
|
|
35
|
+
*/
|
|
36
|
+
/**
|
|
37
|
+
* Returns the plugin which will apply the loaded import maps during build.
|
|
38
|
+
*
|
|
39
|
+
* @returns {Plugin}
|
|
40
|
+
* @example
|
|
41
|
+
* ```js
|
|
42
|
+
* import * as eik from "@eik/esbuild-plugin";
|
|
43
|
+
* import esbuild from "esbuild";
|
|
44
|
+
*
|
|
45
|
+
* await eik.load();
|
|
46
|
+
* await esbuild.build({
|
|
47
|
+
* plugins: [eik.plugin()],
|
|
48
|
+
* });
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export function plugin(): Plugin;
|
|
52
|
+
export type LoadOptions = {
|
|
53
|
+
path?: string;
|
|
54
|
+
/**
|
|
55
|
+
* URLs to import maps hosted on an Eik server. Takes precedence over `eik.json`.
|
|
56
|
+
*/
|
|
57
|
+
urls?: string[];
|
|
58
|
+
/**
|
|
59
|
+
* Inline import maps that should be used. Takes precedence over `urls` and `eik.json`.
|
|
60
|
+
*/
|
|
61
|
+
maps?: ImportMap[];
|
|
62
|
+
};
|
|
63
|
+
export type Plugin = {
|
|
64
|
+
name: string;
|
|
65
|
+
setup: (build: any) => void;
|
|
66
|
+
};
|
|
67
|
+
export type ImportMap = {
|
|
68
|
+
imports: Record<string, string>;
|
|
69
|
+
};
|