Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add browser entrypoint #124

Merged
merged 1 commit into from
Mar 15, 2024
Merged

Add browser entrypoint #124

merged 1 commit into from
Mar 15, 2024

Conversation

ehmicky
Copy link
Collaborator

@ehmicky ehmicky commented Mar 15, 2024

Fixes #123.

@@ -13,6 +13,7 @@
"type": "module",
"exports": {
"types": "./source/index.d.ts",
"browser": "./source/exports.js",
"default": "./source/index.js"
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

There is also the browser top-level field, which is older and predates exports.browser. I am not sure which style do you prefer, and whether I am missing some other field?

Copy link
Owner

Choose a reason for hiding this comment

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

No, this is all we need. The browser field is underspecified and legacy.

@@ -142,6 +142,16 @@ try {
}
```

## Browser support
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Documenting it to be pro-active since users are most likely going to open issues with some unique build configuration where this might not work. :)

@@ -1,7 +1,7 @@
import {isReadableStream} from 'is-stream';

export const getAsyncIterable = stream => {
if (isReadableStream(stream, {checkOpen: false})) {
if (isReadableStream(stream, {checkOpen: false}) && nodeImports.on !== undefined) {
Copy link
Collaborator Author

@ehmicky ehmicky Mar 15, 2024

Choose a reason for hiding this comment

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

In principle, browsers do not support Node streams, so if stream is a Node Readable, then nodeImports.on should be defined.

However, there might be some exotic environment which would load the browser entrypoint and have Node streams support. For example, maybe Deno.

So I added an extra check there.

const error = new Error('test');
stream.emit('error', error);
t.is(await t.throwsAsync(promise), error);
});
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The above was not working when using dynamic imports, since the error listeners would be set too late. This test makes sure this now works.

import {finished} from 'node:stream/promises';
import {nodeImports} from './stream.js';

Object.assign(nodeImports, {on, finished});
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

An alternative would be to bind those with each exported method.
However, using a top-level variable results in simpler code, since those "Node imports" do not need to be passed around through the codebase.

@ehmicky
Copy link
Collaborator Author

ehmicky commented Mar 15, 2024

Merge conflict fixed. 👍

Also, I don't have any other pending PR for this repo, so we're ready for a major release after it.

@sindresorhus sindresorhus merged commit f489cb3 into main Mar 15, 2024
6 checks passed
@sindresorhus sindresorhus deleted the node-static-imports branch March 15, 2024 07:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use a separate entrypoint for browsers
2 participants