-
Notifications
You must be signed in to change notification settings - Fork 70
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
fix: allow overriding entry point in browser field #101
base: master
Are you sure you want to change the base?
fix: allow overriding entry point in browser field #101
Conversation
Some modules are [published](https://unpkg.com/cborg@1.1.2/package.json) with no `main` field in `package.json` and no `/index.js`, instead using `exports` to define the entry point for the module. They sometimes specify an entry point for the browser using a `"."` or `"./"` key in the `browser` field, but currently these are broken with browserify as it tries to resolve `/index.js` when no `main` field is present. The change here is to allow define the entry point for the module using the `browser` field: ```json { "browser": { ".": "path/to/index.js" } } ```
Can you elaborate on this? Currently, The "browser" field explicitly can't work with the "exports" field, so for this package - which doesn't actually provide a proper "browser" implementation - there's really no solution. The package links to |
In other words, the browser field spec does not permit a key of "." in this case, because that would node-resolve to the "main" or "index.js", both of which are missing here. The package you linked simply isn't compatible (yet) with the browserify ecosystem. |
welp, you may as well join this party @ljharb mikeal/ipjs#9 |
I've subscribed to that issue, and I agree with mikeal's thoughts in the thread. In my perspective, the only path to success is maintaining backwards compatibility. This means, either publishing packages as "only CJS" forever (which is fine, CJS and ESM are both first-class module systems in node, CJS is never going away, and isn't "legacy" in any way), or, publishing a dual package using one of these techniques:
In all these examples, a top-level "browser" field should point to CJS files that, when bundled by a non-broken module bundler, work in a browser. The "exports" field's "browser" condition should hold the same. If in the future a tool exists that can benefit from pointing to native ESM, and "import" is not sufficient, then a different "exports" condition would need to be invented to point to that - but since no such tool exists yet that I'm aware of, this isn't yet a use case that needs addressing. |
> a top-level "browser" field should point to CJS files that, when bundled by a non-broken module bundler, work in a browser browserify/browser-resolve#101 (comment) I've changed the `browser` field to point to the `cjs` version of this module in line with the above comment because this module is currently broken when used with browserify. I've also added the [util](https://www.npmjs.com/package/util) module as a dep since it's used in the code. It would be ignored in most environments so the only cost is a slightly larger bundle but it's likely to be included in any non-trivial bundle somewhere anyway so there's likely to be no real-world impact. Also: - Adds typescript dep to tests that test typescript, otherwise tests fail with 'cannot determine executable to run' - unless you have tsc installed globally I guess? - Fixes a typo in a comment - Simplifies .gitignore Fixes #19
> a top-level "browser" field should point to CJS files that, when bundled by a non-broken module bundler, work in a browser browserify/browser-resolve#101 (comment) I've changed the `browser` field to point to the `cjs` version of this module in line with the above comment because this module is currently broken when used with browserify. I've also added the [util](https://www.npmjs.com/package/util) module as a dep since it's used in the code. It would be ignored in most environments so the only cost is a slightly larger bundle but it's likely to be included in any non-trivial bundle somewhere anyway so there's likely to be no real-world impact. Also: - Adds typescript dep to tests that test typescript, otherwise tests fail with 'cannot determine executable to run' - unless you have tsc installed globally I guess? - Fixes a typo in a comment - Simplifies .gitignore Fixes #19
Some modules are published with no
main
field inpackage.json
and no/index.js
, instead usingexports
to define the entry point for the module.They sometimes specify an entry point for the browser using a
"."
or"./"
key in thebrowser
field, but currently these are broken with browserify as it tries to resolve/index.js
when nomain
field is present.The change here is to allow define the entry point for the module using the
browser
field: