algolia / algoliasearch-client-javascript

⚡️ A fully-featured and blazing-fast JavaScript API client to interact with Algolia.
https://www.algolia.com/doc/api-client/javascript/getting-started/
MIT License
1.33k stars 222 forks source link

Consider using ECMAScript Modules #1437

Open thiboot opened 1 year ago

thiboot commented 1 year ago

Could you consider using ECMAScript Modules ?

Warning is raised when compiling app with angular-instantsearch

Screenshot 2023-01-21 at 14 07 47

From angular documentation :

It is recommended that you avoid depending on CommonJS modules in your Angular applications. Depending on CommonJS modules can prevent bundlers and minifiers from optimizing your application, which results in larger bundle sizes. Instead, it is recommended that you use ECMAScript modules in your entire application. For more information, see How CommonJS is making your bundles larger.

GabrielDelepine commented 1 year ago

the NPM package does contain ECMAScript Modules files

dist/algoliasearch-lite.esm.browser.js
algoliasearch.esm.browser.js

But these files are not referenced in the package.json. I see different reasons for that :

To fix this issue, an exports field listing all files present in the npm package need to be added to the package.json (not listing all files, including package.json itself, would be a breaking change, requiring bumping the major version)

If you need a fix right now, just import the ESM files directly in your app (side effect: Typescript won't be able to type the default export)

Screenshot 2023-02-15 at 16 19 39

Screenshot 2023-02-15 at 16 23 39

GabrielDelepine commented 1 year ago

exports has already been discussed here https://github.com/algolia/algoliasearch-client-javascript/issues/1381

GabrielDelepine commented 1 year ago

An easy fix for the missing TS definition is to create this d.ts file anywhere in your project

Screenshot 2023-02-16 at 12 04 09

declare module 'algoliasearch/dist/algoliasearch-lite.esm.browser.js' {
  export * from 'algoliasearch/dist/algoliasearch-lite';
  export { default } from 'algoliasearch/dist/algoliasearch-lite';
}

PS: In order to create a PR, I had a look at the scripts/build.js and rollup.config.js files in this repo but there are simply too many packages generated. It will be safer if a regular maintainer had a look at it

PS2: The name of the d.ts file can be anything. Only the .d.ts extension is mandatory