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: bundlers not using the browser export #299

Merged
merged 1 commit into from
Aug 12, 2024
Merged

fix: bundlers not using the browser export #299

merged 1 commit into from
Aug 12, 2024

Conversation

kobim
Copy link
Contributor

@kobim kobim commented Aug 9, 2024

Bundlers use conditions to decide which file to import. The logic, explained by esbuild emphasizes it would read the exports field in-order and try matching it to any of the conditions.

When bundling for browser, eslint will end up adding default and import to the list of conditions. When iterating eta's exports list, it ended up catching import instead of browser. With this change, it will first catch browser.

How to test?

Create a new file named index.mjs with the following content:

import { Eta } from 'eta';

const eta = new Eta();

Install esbuild and eta:

npm i --save-dev esbuild eta

Try to bundle the file:

npx esbuild index.mjs --bundle --conditions=browser

With the current published version (3.4.1) it will emit an error:

✘ [ERROR] Could not resolve "node:path"

    node_modules/eta/dist/eta.module.mjs:1:22:
      1 │ import * as path from 'node:path';
        ╵                       ~~~~~~~~~~~

  The package "node:path" wasn't found on the file system but is built into node. Are you trying to
  bundle for node? You can use "--platform=node" to do that, which will remove this error.

When trying with this branch's build, it passes successfully.

resolves #283

@nebrelbug
Copy link
Collaborator

@kobim thanks for looking into this! The JS imports/exports field is sadly very murky sometimes so I appreciate the contribution.

Just to check, this will only affect builds targeting the browser, right?

Bundlers use _conditions_ to decide which file to import. The logic,
[explained by esbuild](https://esbuild.github.io/api/#how-conditions-work)
emphasizes it would read the `exports` field *in-order* and try matching
it to any of the conditions.

When bundling for `browser`, eslint will end up adding `default` and
`import` to the list of _conditions_. When iterating `eta`'s `exports` list,
it ended up catching `import` instead of `browser`.
With this change, it will first catch `browser`.

How to test?
---
Create a new file named `index.mjs` with the following content:

```javascript
import { Eta } from 'eta';

const eta = new Eta();
```

Install `esbuild` and `eta`:
```sh
npm i --save-dev esbuild eta
```

Try to bundle the file:
```sh
npx esbuild index.mjs --bundle --conditions=browser
```

With the current published version (3.4.1) it will emit an error:
```
✘ [ERROR] Could not resolve "node:path"

    node_modules/eta/dist/eta.module.mjs:1:22:
      1 │ import * as path from 'node:path';
        ╵                       ~~~~~~~~~~~

  The package "node:path" wasn't found on the file system but is built into node. Are you trying
  to bundle for node? You can use "--platform=node" to do that, which will remove this error.
```

When trying with this branch's build, it passes successfully.

resolves #283
@kobim
Copy link
Contributor Author

kobim commented Aug 12, 2024

that's correct - only builds with a browser condition will pick this up, while the rest will continue and catch either the import/require lines.

@nebrelbug
Copy link
Collaborator

@kobim great. I'll merge this now and release -- thanks again!

@nebrelbug nebrelbug merged commit 54f2f0d into eta-dev:main Aug 12, 2024
3 checks passed
@nebrelbug
Copy link
Collaborator

Just released in 3.5.0

@kobim kobim deleted the fix/browser-condition branch August 13, 2024 03:04
@nfarina
Copy link

nfarina commented Aug 16, 2024

Thanks so much! Cleaned up my build logs tremendously :)

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.

Cannot import "browser" version from ESM package
3 participants