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(javascript): use exports field to pick correct bundle #947

Merged
merged 4 commits into from
Aug 23, 2022

Conversation

francoischalifour
Copy link
Member

@francoischalifour francoischalifour commented Aug 22, 2022

🧭 What and Why

🎟 JIRA Ticket: https://algolia.atlassian.net/browse/PRED-530 https://algolia.atlassian.net/browse/APIC-625
Slack thread: https://algolia.slack.com/archives/C0341QDM3EG/p1660811425419539

This updates the clients exports to use the exports field.

There were issues in how clients were exported because the module assumed that it would get executed in a Node environment. This threw in multiple scenarios: Vite, Rollup, standard <script type="module">, etc.

We now rely on exports with two branches: "node" to load the Node ESM and "default" to load the browser ESM.

When using environment branches, always include a "default" condition where possible. Providing a "default" condition ensures that any unknown JS environments are able to use this universal implementation, which helps avoid these JS environments from having to pretend to be existing environments in order to support packages with conditional exports. For this reason, using "node" and "default" condition branches is usually preferable to using "node" and "browser" condition branches.

— From Conditional exports in Node.js

Using "browser" instead of "default" also fails with Rollup and @rollup/plugin-node-resolve:

(!) Plugin node-resolve: Could not resolve import "@algolia/predict" in /packages/predict-tools/dist/esm/PredictToolsSettings.js using exports defined in /node_modules/@algolia/predict/package.json.

For consistency, and to follow the Node recommendation, I also updated the algoliasearch browser export to use "default".

Changes included:

  • Rely on exports with "node" and "default" for all API clients
  • Rely on "default" instead of "browser" for algoliasearch

🧪 Test

No tests added, I believe the CI will do the work?

@netlify
Copy link

netlify bot commented Aug 22, 2022

Deploy Preview for api-clients-automation canceled.

Name Link
🔨 Latest commit d097f1e
🔍 Latest deploy log https://app.netlify.com/sites/api-clients-automation/deploys/63048366f3431900093cd653

@algolia-bot
Copy link
Collaborator

algolia-bot commented Aug 22, 2022

✗ The generated branch has been deleted.

If the PR has been merged, you can check the generated code on the main branch.
You can still access the code generated on main via this commit.

@shortcuts shortcuts changed the title fix: use exports field to pick correct bundle fix(javascript): use exports field to pick correct bundle Aug 22, 2022
Copy link
Member

@shortcuts shortcuts left a comment

Choose a reason for hiding this comment

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

Thanks! Small question :D

templates/javascript/clients/package.mustache Outdated Show resolved Hide resolved
Copy link
Member

@shortcuts shortcuts left a comment

Choose a reason for hiding this comment

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

Should be good after this fix!

@shortcuts shortcuts merged commit 3b85c76 into main Aug 23, 2022
@shortcuts shortcuts deleted the fix/exports branch August 23, 2022 07:48
"default": "./dist/{{packageName}}.umd.js"
}
},
"./src/*": "./src/*.ts",
Copy link
Member Author

@francoischalifour francoischalifour Aug 23, 2022

Choose a reason for hiding this comment

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

I think this should rather be one of those:

{
    "./src/*": "./src/*",
    "./src/*.ts": "./src/*.ts",
}

Otherwise if there's another extension in the folder, it would incorrectly get imported as TypeScript.

Copy link
Member

Choose a reason for hiding this comment

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

Those clients all follow the same pattern with a single API file in their src folder, we could be more precise on the name instead of using globs but it shouldn't change for now at least

Copy link
Member Author

Choose a reason for hiding this comment

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

I see, but it's less error prone and reduce the public API if we went for "./src/*.ts": "./src/*.ts". It would get tricky to debug if you encounter this bug. So we better address it now imo.

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