Skip to content

Commit d735e36

Browse files
docs: demonstrate import map shimming workaround (#240)
1 parent f92d664 commit d735e36

1 file changed

Lines changed: 75 additions & 0 deletions

File tree

src/pages/docs/introduction/web-standards.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,81 @@ Some known issues / examples observed so far include:
143143
- `ERR_MODULE_NOT_FOUND` - Observed with packages like [**@types/trusted-types**](https://github.com/DefinitelyTyped/DefinitelyTyped) which has an [empty string](https://unpkg.com/browse/@types/[email protected]/package.json) for the **main** field, or [**font-awesome**](https://fontawesome.com/), which has [no entry point](https://unpkg.com/browse/[email protected]/package.json) at all, at least as of `v4.x`. This is also a fairly common issue with packages that primarily deal with shipping types since they will likely only define a `types` field in their _package.json_.
144144
- `ERR_PACKAGE_PATH_NOT_EXPORTED` - Encountered with packages like [**geist-font**](https://github.com/vercel/geist-font/issues/150) or [**@libsql/core**](https://github.com/thescientist13/import-meta-resolve-demo?tab=readme-ov-file#no-main-exports-map-entry-point-err_package_path_not_exported), which has [no default export](https://github.com/vercel/geist-font/issues/150) in their exports map, which is assumed by the NodeJS resolver algorithm.
145145

146+
---
147+
148+
In cases where this issu may prevent scripts from resolving in the browser, you can create an import map programmatically and use a [Resource plugin](/docs/reference/plugins-api/#resource) to add it to your page HTML.
149+
150+
The below example demonstrates adding supplemental custom import map entries for [**heroicons**](https://heroicons.com/) SVG icons.
151+
152+
```js
153+
import { mergeImportMap } from "@greenwood/cli/src/lib/node-modules-utils.js";
154+
import fs from "node:fs";
155+
156+
const NODE_MODULES_LOCATION = new URL("./node_modules/", import.meta.url);
157+
const PACKAGE_NAME = "heroicons";
158+
159+
let importMap;
160+
161+
function generateImportMap() {
162+
if (!importMap) {
163+
importMap = new Map();
164+
165+
fs.globSync("**/**/*.svg", {
166+
cwd: `${NODE_MODULES_LOCATION.pathname}/${PACKAGE_NAME}/`,
167+
}).forEach((file) => {
168+
const resolved = new URL(`./${PACKAGE_NAME}/${file}`, NODE_MODULES_LOCATION);
169+
170+
// add the query param for import raw plugin support
171+
importMap.set(`${PACKAGE_NAME}/${file}?type=raw`, `/~${resolved.pathname}?type=raw`);
172+
});
173+
}
174+
175+
return Object.fromEntries(importMap);
176+
}
177+
178+
class HeroiconsImportMap {
179+
constructor(compilation) {
180+
this.compilation = compilation;
181+
}
182+
183+
async shouldIntercept(url, request) {
184+
const { protocol, pathname } = url;
185+
const hasMatchingPageRoute = this.compilation.graph.find((node) => node.route === pathname);
186+
187+
return (
188+
process.env.__GWD_COMMAND__ === "develop" &&
189+
protocol.startsWith("http") &&
190+
hasMatchingPageRoute &&
191+
request.headers.get("Accept").indexOf("text/html") >= 0
192+
);
193+
}
194+
195+
async intercept(url, request, response) {
196+
const body = await response.text();
197+
const newBody = mergeImportMap(body, generateImportMap());
198+
199+
return new Response(newBody);
200+
}
201+
}
202+
203+
// make sure to include this into your Greenwood config file
204+
export function heroiconsImportMapPlugin() {
205+
return {
206+
type: "resource",
207+
name: "heroicons-import-map-plugin",
208+
provider: (compilation) => new HeroiconsImportMap(compilation),
209+
};
210+
}
211+
```
212+
213+
This will now allow usage of bare specifiers from a client-side JavaScript file:
214+
215+
```js
216+
import pauseSvg from "heroicons/24/solid/pause.svg?type=raw";
217+
218+
console.log({ pauseSvg });
219+
```
220+
146221
> In almost all of our observed diagnostic cases, they would all go away if [this feature](https://github.com/nodejs/node/issues/49445) was added to NodeJS, so please add an up-vote to that issue! 👍
147222
>
148223
> If you have any issues or questions, please reach out in our [discussion on the topic](https://github.com/ProjectEvergreen/greenwood/discussions/1396).

0 commit comments

Comments
 (0)