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

fix: work with browserify #13

Merged
merged 1 commit into from Jul 15, 2021

Conversation

achingbrain
Copy link
Collaborator

Browserify does not support "exports" in a package.json, and nor can you use "browser" in package.json as a hint to Browserify to look in a certain place for a file.

This means the output of ipjs is not compatible with Browserify since it expects the runtime/bundler to use the "exports" field to look up files paths.

If Browserify finds a file path, it will then use the "browser" values to apply any overrides, which ipjs uses to direct it to the /cjs folder.

The problem is if it can't find the file in the first place it won't use the "browser" map to get the overrides.

Handily we're generating the "browser" field values from the "exports" values so we know we have the complete set of files that the user wants to expose to the outside world, and the paths we want people to use to access them.

The change in this PR is to use the "browser" field values to mimc the "exports" structure in a CJS-compatible directory structure as per @ljharb's suggestion.

For any file that we are overriding with "browser" values, we create an empty file (where a resolvable file does not already exist) a the path Browserify expects it to be at, then it'll dutifully use the "browser" field to pull the actual file in.

Browserify [does not support](browserify/resolve#224) `"exports"`
in a `package.json`, and nor can you use `"browser"` in `package.json`
[as a hint to Browserify to look in a certain place for a file](browserify/resolve#250 (comment)).

This means the output of `ipjs` is not compatible with Browserify since
it expects the runtime/bundler to use the `"exports"` field to look up
files paths.

If Browserify finds a file path, it will then use the `"browser"` values
to apply any overrides, which `ipjs` uses to direct it to the `/cjs` folder.

The problem is if it can't find the file in the first place it won't use
the `"browser"` map to get the overrides.

Handily we're generating the `"browser"` field values from the `"exports"`
values so we know we have the complete set of files that the user wants to
expose to the outside world, and the paths we want people to use to access
them.

The change in this PR is to use the `"browser"` field values to [mimc the `"exports"` structure in a CJS-compatible directory structure](browserify/resolve#250 (comment))
as per @ljharb's suggestion.

For any file that we are overriding with `"browser"` values, we create an
empty file (where a resolvable file does not already exist) a the path
Browserify expects it to be at, then it'll dutifully use the `"browser"`
field to pull the actual file in.
@achingbrain
Copy link
Collaborator Author

I find this somewhat unpalatable, but it does work, so I'll understand if you'd prefer it to be behind a flag.

At any rate we should be able to pull it out if browserify/resolve#224 ever lands.

@ljharb
Copy link

ljharb commented Jul 14, 2021

Even when that lands, you'd still want this structure present for backwards compatibility with non-ESM node versions.

@achingbrain
Copy link
Collaborator Author

I think we are more ok with dropping support for node versions that are out of LTS than we are dropping support for bundlers.

@ljharb
Copy link

ljharb commented Jul 14, 2021

I'm sure; but why bother dropping support for anyone if it's so easy to keep it.

@mikeal mikeal merged commit c101763 into mikeal:master Jul 15, 2021
@achingbrain achingbrain deleted the fix/work-with-browserify branch July 15, 2021 06:42
achingbrain added a commit to achingbrain/ipjs that referenced this pull request Jul 15, 2021
Jest [doesn't support package exports](jestjs/jest#9771),
nor does it support `browser` overrides out of the box (though it
[can be configured](https://jestjs.io/docs/configuration#resolver-string)).

This means it parses the stubbed files introduced in mikeal#13 as javascript,
so let's just require and export the file that the stub is stubbing.

This has the added bonus of also supporting old nodes that don't
understand package exports.

Fixes achingbrain/uint8arrays#21
@achingbrain achingbrain mentioned this pull request Jul 15, 2021
mikeal pushed a commit that referenced this pull request Jul 15, 2021
Jest [doesn't support package exports](jestjs/jest#9771),
nor does it support `browser` overrides out of the box (though it
[can be configured](https://jestjs.io/docs/configuration#resolver-string)).

This means it parses the stubbed files introduced in #13 as javascript,
so let's just require and export the file that the stub is stubbing.

This has the added bonus of also supporting old nodes that don't
understand package exports.

Fixes achingbrain/uint8arrays#21
vasco-santos pushed a commit to vasco-santos/ipjs that referenced this pull request Jul 29, 2021
Jest [doesn't support package exports](jestjs/jest#9771),
nor does it support `browser` overrides out of the box (though it
[can be configured](https://jestjs.io/docs/configuration#resolver-string)).

This means it parses the stubbed files introduced in mikeal#13 as javascript,
so let's just require and export the file that the stub is stubbing.

This has the added bonus of also supporting old nodes that don't
understand package exports.

Fixes achingbrain/uint8arrays#21
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.

None yet

3 participants