algoliasearch-client-javascript icon indicating copy to clipboard operation
algoliasearch-client-javascript copied to clipboard

Consider using ECMAScript Modules

Open thiboot opened this issue 2 years ago • 3 comments

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.

thiboot avatar Jan 20 '23 19:01 thiboot

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 :

  • The field module is not supporting multi-entry (as far as I know) and using it wouldn't be standard because the module field is not official
  • The browser field is already in use for umd. Switching from umd to esm files would be a breaking change

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 avatar Feb 15 '23 15:02 GabrielDelepine

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

GabrielDelepine avatar Feb 15 '23 15:02 GabrielDelepine

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

GabrielDelepine avatar Feb 16 '23 10:02 GabrielDelepine