Skip to content
Merged
Prev Previous commit
Next Next commit
Update doc/api/esm.md to improve "import map" loader
Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com>
  • Loading branch information
giltayar and aduh95 authored May 4, 2023
commit 88a4ce9bc531db7c924085d38c82886fc3313240
2 changes: 1 addition & 1 deletion doc/api/esm.md
Original file line number Diff line number Diff line change
Expand Up @@ -1192,7 +1192,7 @@ import fs from 'node:fs/promises';
const { imports } = JSON.parse(await fs.readFile('import-map.json'));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const { imports } = JSON.parse(await fs.readFile('import-map.json'));
const { imports } = JSON.parse(await fs.readFile(new URL('./import-map.json', import.meta.url)));

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The import-map.json should be given by the user of the loader, so it should be taken from current working directory and not next to the source code of the loader. So I would prefer leaving it like this and not accepting your suggestion. Having said that, I don't feel strongly about it, as this is demo code, so just say the word and I'll commit the suggestion.

(obviously, cwd is naive and there should be an env variable or some such, but this is naive code for demo purpose only)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just make the example the esm.sh one then? That’s much simpler and involves only one file, so the overall example would also be shorter.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you wrap it in path.resolve and/or add a comment? Without that, it’s a bit confusing IMO.

Why not just make the example the esm.sh one then? That’s much simpler and involves only one file, so the overall example would also be shorter.

We would have trouble with Windows compatibility, right?

@GeoffreyBooth GeoffreyBooth May 4, 2023 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just make the example the esm.sh one then? That’s much simpler and involves only one file, so the overall example would also be shorter.

We would have trouble with Windows compatibility, right?

That example I had in mind was to rewrite all bare specifiers like lodash to https://esm.sh/lodash, and that’s it. Add a comment that load would fail without --experimental-network-imports, or without chaining the example HTTPS loader, and call it a day. It should be like a five-line example I’d think.

It’s still a bit contrived as presumably you wouldn’t always want to load the latest version of every dependency, but for an example I think it gets the idea across and then real-world implementations can fill in details.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems unwise for node docs to bless an arbitrary site in the ecosystem by including it in examples.


export async function resolve(specifier, context, nextResolve) {
if (specifier in imports) {
if (Object.hasOwn(imports, specifier)) {
return nextResolve(imports[specifier], context);
}

Expand Down