@eik/esbuild-plugin 2.0.0-next.1 → 2.0.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/README.md CHANGED
@@ -1,48 +1,43 @@
1
1
  # @eik/esbuild-plugin
2
2
 
3
- ESBuild plugin for loading import maps from a [Eik server](https://eik.dev/) and applying the mapping to ECMAScript modules in preparation for upload to the same server.
3
+ [esbuild](https://esbuild.github.io/) plugin for [build-time import mapping with Eik](https://eik.dev/docs/guides/esbuild).
4
4
 
5
5
  ## Installation
6
6
 
7
7
  ```bash
8
- $ npm install @eik/esbuild-plugin
8
+ npm install --save-dev esbuild @eik/esbuild-plugin
9
9
  ```
10
10
 
11
11
  ## Usage
12
12
 
13
13
  ```js
14
- import * as eik from '@eik/esbuild-plugin';
15
- import esbuild from 'esbuild';
16
-
17
- await eik.load();
18
-
19
- await esbuild.build({
20
- entryPoints: ['./src/app.js'],
21
- bundle: true,
22
- format: 'esm',
23
- target: ['esnext'],
24
- plugins: [eik.plugin()],
14
+ import * as eik from "@eik/esbuild-plugin";
15
+ import esbuild from "esbuild";
16
+
17
+ const options = /** @type {esbuild.BuildOptions}*/ ({
18
+ entryPoints: ["./src/index.js"],
19
+ outdir: "./public",
20
+ format: "esm",
21
+ platform: "browser",
22
+ target: ["es2017"],
23
+ bundle: true,
24
+ sourcemap: true,
25
25
  });
26
- ```
27
-
28
- ## Description
29
26
 
30
- This plugin will read a local Eik config file (`eik.json`) and download the import maps from the Eik server defined in the config. The downloaded import maps will then be applied to the ECMAScript modules being processed.
31
-
32
- ### Plugin result
33
-
34
- Bundles will have bare imports mapped to absolute URLs.
35
-
36
- Ie. Something like this...
37
-
38
- ```js
39
- import { LitElement, html, css } from "lit-element";
40
- ```
41
-
42
- Will be mapped to something like this...
43
-
44
- ```js
45
- import { LitElement, html, css } from "https://cdn.eik.dev/lit-element/v2";
27
+ const watch = process.argv.includes("--dev");
28
+ if (watch) {
29
+ let ctx = await esbuild.context(options);
30
+ await ctx.watch();
31
+ console.log("[esbuild] watching...");
32
+ } else {
33
+ // Use the Eik plugin to to import mapping for the production build
34
+ // Load the import maps listed in eik.json from the Eik server
35
+ await eik.load();
36
+ await esbuild.build({
37
+ ...options,
38
+ plugins: [eik.plugin()],
39
+ });
40
+ }
46
41
  ```
47
42
 
48
43
  ## API
@@ -53,11 +48,11 @@ This module has the following API:
53
48
 
54
49
  Loads an Eik configuration or import maps directly for the plugn to use to apply.
55
50
 
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. |
51
+ | option | default | type | required | details |
52
+ | ------ | -------------- | -------- | -------- | --------------------------------------- |
53
+ | path | `cwd/eik.json` | `string` | `false` | Path to eik.json file. |
54
+ | urls | `[]` | `array` | `false` | Array of import map URLs to fetch from. |
55
+ | maps | `[]` | `array` | `false` | Array of import map as objects. |
61
56
 
62
57
  The plugin will attempt to read a `eik.json` from the current working directory of the Node.js process.
63
58
 
@@ -69,7 +64,7 @@ The path to the location of an `eik.json` file can be specified with the `path`
69
64
 
70
65
  ```js
71
66
  await eik.load({
72
- path: '/path/to/eik.json',
67
+ path: "/path/to/eik.json",
73
68
  });
74
69
  ```
75
70
 
@@ -77,7 +72,7 @@ The plugin can also load import maps directly from one or multiple URLs using th
77
72
 
78
73
  ```js
79
74
  await eik.load({
80
- urls: ['http://myserver/import-maps/map.json'],
75
+ urls: ["http://myserver/import-maps/map.json"],
81
76
  });
82
77
  ```
83
78
 
@@ -85,11 +80,13 @@ Additionally, individual import maps can be specified using the `maps` option.
85
80
 
86
81
  ```js
87
82
  await eik.load({
88
- maps: [{
89
- imports: {
90
- 'lit-element': 'https://cdn.eik.dev/lit-element/v2',
91
- }
92
- }],
83
+ maps: [
84
+ {
85
+ imports: {
86
+ "lit-element": "https://cdn.eik.dev/lit-element/v2",
87
+ },
88
+ },
89
+ ],
93
90
  });
94
91
  ```
95
92
 
@@ -99,13 +96,15 @@ ie. in the following example
99
96
 
100
97
  ```js
101
98
  await eik.load({
102
- path: '/path/to/eik.json',
103
- urls: ['http://myserver/import-maps/map.json'],
104
- maps: [{
105
- imports: {
106
- 'lit-element': 'https://cdn.eik.dev/lit-element/v2',
107
- }
108
- }],
99
+ path: "/path/to/eik.json",
100
+ urls: ["http://myserver/import-maps/map.json"],
101
+ maps: [
102
+ {
103
+ imports: {
104
+ "lit-element": "https://cdn.eik.dev/lit-element/v2",
105
+ },
106
+ },
107
+ ],
109
108
  });
110
109
  ```
111
110
 
@@ -113,48 +112,28 @@ Any import map URLs in `eik.json` will be loaded first, then merged with (and ov
113
112
 
114
113
  ### .plugin()
115
114
 
116
- Returns the plugin which will apply the loaded import maps during build. The returned plugin should be appended to the ESBuild plugin array.
115
+ Returns the plugin which will apply the loaded import maps during build. The returned plugin should be appended to the esbuild plugin array.
117
116
 
118
117
  ```js
119
- import * as eik from '@eik/esbuild-plugin';
120
- import esbuild from 'esbuild';
118
+ import * as eik from "@eik/esbuild-plugin";
119
+ import esbuild from "esbuild";
121
120
 
122
121
  await eik.load();
123
122
 
124
- esbuild.build({
125
- entryPoints: ['src/main.js'],
126
- bundle: true,
127
- format: 'esm',
128
- minify: false,
129
- sourcemap: false,
130
- target: ['chrome58', 'firefox57', 'safari11', 'edge16'],
131
- plugins: [eik.plugin()],
132
- outfile: 'out.js',
133
- }).catch(() => process.exit(1))
123
+ esbuild
124
+ .build({
125
+ entryPoints: ["src/main.js"],
126
+ bundle: true,
127
+ format: "esm",
128
+ minify: false,
129
+ sourcemap: false,
130
+ target: ["es2017"],
131
+ plugins: [eik.plugin()],
132
+ outfile: "out.js",
133
+ })
134
+ .catch(() => process.exit(1));
134
135
  ```
135
136
 
136
137
  ### .clear()
137
138
 
138
- Clears the loaded import maps from the plugins internal cache.
139
-
140
- ## License
141
-
142
- Copyright (c) 2020 Finn.no
143
-
144
- Permission is hereby granted, free of charge, to any person obtaining a copy
145
- of this software and associated documentation files (the "Software"), to deal
146
- in the Software without restriction, including without limitation the rights
147
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
148
- copies of the Software, and to permit persons to whom the Software is
149
- furnished to do so, subject to the following conditions:
150
-
151
- The above copyright notice and this permission notice shall be included in all
152
- copies or substantial portions of the Software.
153
-
154
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
155
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
156
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
157
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
158
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
159
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
160
- SOFTWARE.
139
+ Clears the loaded import maps from the plugins internal cache.
package/package.json CHANGED
@@ -1,17 +1,22 @@
1
1
  {
2
2
  "name": "@eik/esbuild-plugin",
3
- "version": "2.0.0-next.1",
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.",
3
+ "version": "2.0.0",
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 --no-coverage",
12
- "test:snapshot": "TAP_SNAPSHOT=1 tap test/*.js --no-coverage",
13
- "lint": "eslint . --ext=js",
14
- "lint:fix": "eslint . --fix --ext=js"
13
+ "clean": "rimraf .tap node_modules types",
14
+ "test": "tap --disable-coverage --allow-empty-coverage",
15
+ "lint": "eslint .",
16
+ "lint:fix": "npm run lint -- --fix",
17
+ "types": "run-s types:module types:test",
18
+ "types:module": "tsc",
19
+ "types:test": "tsc --project tsconfig.test.json"
15
20
  },
16
21
  "repository": {
17
22
  "type": "git",
@@ -35,19 +40,26 @@
35
40
  },
36
41
  "homepage": "https://github.com/eik-lib/esbuild-plugin#readme",
37
42
  "devDependencies": {
38
- "@semantic-release/changelog": "6.0.1",
39
- "@semantic-release/git": "10.0.1",
40
- "esbuild": "0.14.43",
41
- "eslint": "8.16.0",
42
- "eslint-config-airbnb-base": "15.0.0",
43
- "eslint-plugin-import": "2.26.0",
44
- "fastify": "3.29.0",
45
- "semantic-release": "19.0.2",
46
- "tap": "16.2.0"
43
+ "@eik/eslint-config": "1.0.13",
44
+ "@eik/prettier-config": "1.0.1",
45
+ "@eik/semantic-release-config": "1.0.2",
46
+ "@eik/typescript-config": "1.0.0",
47
+ "@types/esbuild-plugin-import-map": "0.0.3",
48
+ "esbuild": "0.25.3",
49
+ "eslint": "9.16.0",
50
+ "fastify": "4.28.1",
51
+ "npm-run-all2": "5.0.2",
52
+ "prettier": "3.4.1",
53
+ "rimraf": "6.0.1",
54
+ "semantic-release": "24.2.3",
55
+ "tap": "20.0.3",
56
+ "typescript": "5.7.2"
47
57
  },
48
58
  "dependencies": {
49
- "@eik/common": "3.0.1",
50
- "esbuild-plugin-import-map": "3.0.0-next.1",
51
- "undici": "5.5.1"
59
+ "@eik/common": "5.1.0",
60
+ "esbuild-plugin-import-map": "2.1.0"
61
+ },
62
+ "engines": {
63
+ "node": ">=20"
52
64
  }
53
65
  }
package/src/plugin.js CHANGED
@@ -1,56 +1,83 @@
1
- /* eslint-disable no-restricted-syntax */
2
-
3
- import * as importMapPlugin from 'esbuild-plugin-import-map';
4
- import { helpers } from '@eik/common';
5
- import { request } from 'undici';
6
-
7
- const fetchImportMaps = async (urls = []) => {
8
- try {
9
- const maps = urls.map(async (map) => {
10
- const {
11
- statusCode,
12
- body,
13
- } = await request(map, { maxRedirections: 2 });
14
-
15
- if (statusCode === 404) {
16
- throw new Error('Import map could not be found on server');
17
- } else if (statusCode >= 400 && statusCode < 500) {
18
- throw new Error('Server rejected client request');
19
- } else if (statusCode >= 500) {
20
- throw new Error('Server error');
21
- }
22
- return body.json();
23
- });
24
- return await Promise.all(maps);
25
- } catch (err) {
26
- throw new Error(
27
- `Unable to load import map file from server: ${err.message}`,
28
- );
29
- }
30
- };
1
+ import * as importMapPlugin from "esbuild-plugin-import-map";
2
+ import { helpers } from "@eik/common";
31
3
 
4
+ /**
5
+ * @typedef {object} ImportMap
6
+ * @property {Record<string, string>} imports
7
+ */
8
+
9
+ /**
10
+ * @typedef {object} LoadOptions
11
+ * @property {string} [path=process.cwd()]
12
+ * @property {string[]} [urls=[]] URLs to import maps hosted on an Eik server. Takes precedence over `eik.json`.
13
+ * @property {ImportMap[]} [maps=[]] Inline import maps that should be used. Takes precedence over `urls` and `eik.json`.
14
+ */
15
+
16
+ /**
17
+ * Loads an Eik configuration or import maps directly for the plugn to use.
18
+ * Call before building.
19
+ *
20
+ * @param {LoadOptions} options
21
+ * @returns {Promise<void>}
22
+ * @example
23
+ * ```js
24
+ * import * as eik from "@eik/esbuild-plugin";
25
+ * import esbuild from "esbuild";
26
+ *
27
+ * await eik.load();
28
+ * await esbuild.build({
29
+ * plugins: [eik.plugin()],
30
+ * });
31
+ * ```
32
+ */
32
33
  export async function load({
33
- path = process.cwd(),
34
- maps = [],
35
- urls = [],
34
+ path = process.cwd(),
35
+ maps = [],
36
+ urls = [],
36
37
  } = {}) {
37
- const pMaps = Array.isArray(maps) ? maps : [maps];
38
- const pUrls = Array.isArray(urls) ? urls : [urls];
38
+ const pMaps = Array.isArray(maps) ? maps : [maps];
39
+ const pUrls = Array.isArray(urls) ? urls : [urls];
39
40
 
40
- const config = await helpers.getDefaults(path);
41
+ const config = helpers.getDefaults(path);
41
42
 
42
- const fetched = await fetchImportMaps([...config.map, ...pUrls]);
43
- const mappings = pMaps.concat(fetched);
43
+ const fetched = await helpers.fetchImportMaps([...config.map, ...pUrls]);
44
+ const mappings = fetched.concat(pMaps);
44
45
 
45
- await importMapPlugin.load(mappings);
46
+ await importMapPlugin.load(mappings);
46
47
  }
47
48
 
49
+ /**
50
+ * Clear the loaded import maps from the plugins internal cache.
51
+ *
52
+ * @returns {void}
53
+ */
48
54
  export function clear() {
49
- importMapPlugin.clear();
55
+ importMapPlugin.clear();
50
56
  }
51
57
 
58
+ /**
59
+ * @typedef {object} Plugin
60
+ * @property {string} name
61
+ * @property {(build: any) => void} setup
62
+ */
63
+
64
+ /**
65
+ * Returns the plugin which will apply the loaded import maps during build.
66
+ *
67
+ * @returns {Plugin}
68
+ * @example
69
+ * ```js
70
+ * import * as eik from "@eik/esbuild-plugin";
71
+ * import esbuild from "esbuild";
72
+ *
73
+ * await eik.load();
74
+ * await esbuild.build({
75
+ * plugins: [eik.plugin()],
76
+ * });
77
+ * ```
78
+ */
52
79
  export function plugin() {
53
- const obj = importMapPlugin.plugin();
54
- obj.name = '@eik/esbuild-plugin';
55
- return obj;
80
+ const obj = importMapPlugin.plugin();
81
+ obj.name = "@eik/esbuild-plugin";
82
+ return obj;
56
83
  }
@@ -0,0 +1,73 @@
1
+ /**
2
+ * @typedef {object} ImportMap
3
+ * @property {Record<string, string>} imports
4
+ */
5
+ /**
6
+ * @typedef {object} LoadOptions
7
+ * @property {string} [path=process.cwd()]
8
+ * @property {string[]} [urls=[]] URLs to import maps hosted on an Eik server. Takes precedence over `eik.json`.
9
+ * @property {ImportMap[]} [maps=[]] Inline import maps that should be used. Takes precedence over `urls` and `eik.json`.
10
+ */
11
+ /**
12
+ * Loads an Eik configuration or import maps directly for the plugn to use.
13
+ * Call before building.
14
+ *
15
+ * @param {LoadOptions} options
16
+ * @returns {Promise<void>}
17
+ * @example
18
+ * ```js
19
+ * import * as eik from "@eik/esbuild-plugin";
20
+ * import esbuild from "esbuild";
21
+ *
22
+ * await eik.load();
23
+ * await esbuild.build({
24
+ * plugins: [eik.plugin()],
25
+ * });
26
+ * ```
27
+ */
28
+ export function load({ path, maps, urls, }?: LoadOptions): Promise<void>;
29
+ /**
30
+ * Clear the loaded import maps from the plugins internal cache.
31
+ *
32
+ * @returns {void}
33
+ */
34
+ export function clear(): void;
35
+ /**
36
+ * @typedef {object} Plugin
37
+ * @property {string} name
38
+ * @property {(build: any) => void} setup
39
+ */
40
+ /**
41
+ * Returns the plugin which will apply the loaded import maps during build.
42
+ *
43
+ * @returns {Plugin}
44
+ * @example
45
+ * ```js
46
+ * import * as eik from "@eik/esbuild-plugin";
47
+ * import esbuild from "esbuild";
48
+ *
49
+ * await eik.load();
50
+ * await esbuild.build({
51
+ * plugins: [eik.plugin()],
52
+ * });
53
+ * ```
54
+ */
55
+ export function plugin(): Plugin;
56
+ export type ImportMap = {
57
+ imports: Record<string, string>;
58
+ };
59
+ export type LoadOptions = {
60
+ path?: string;
61
+ /**
62
+ * URLs to import maps hosted on an Eik server. Takes precedence over `eik.json`.
63
+ */
64
+ urls?: string[];
65
+ /**
66
+ * Inline import maps that should be used. Takes precedence over `urls` and `eik.json`.
67
+ */
68
+ maps?: ImportMap[];
69
+ };
70
+ export type Plugin = {
71
+ name: string;
72
+ setup: (build: any) => void;
73
+ };