dundalek / latinize

Simple library to convert accents (diacritics) from strings to latin characters.
BSD 2-Clause "Simplified" License
148 stars 33 forks source link

warning when used in ESM context: "CommonJS or AMD dependencies can cause optimization bailouts" #21

Closed mpellerin42 closed 1 year ago

mpellerin42 commented 1 year ago

In our project, we're using latinize to slugify some text (like here). We are in an ESM context, and we import latinize accordingly:

import latinize from 'latinize';

and still, when building an angular application using this utility function, we get the following warning:

Warning: […]/libs/core/src/lib/utils/utils.ts depends on 'latinize'. CommonJS or AMD dependencies can cause optimization bailouts.
For more info see: https://angular.io/guide/build#configuring-commonjs-dependencies

I guess the problem comes from latinize having only one latinize.js output, probably containing ESM, commonJS and AMD version all together? Would it be possible to package latinize in different files supporting the different modules to avoid this problem and get the most out for ESM usage (like proper tree shaking)?

dundalek commented 1 year ago

Hi, this library was created before ESM existed and uses CommonJS. Since this module exports only a single function, there are no significant size savings to be made with tree shaking, so I don't intend to spend the time to upgrade it to ESM.

Bundlers should be able to translate appropriately. If there are issues one option can be to use a modern bundler like Bun which handles ESM and CJS together in a compatible way.

If anyone wants to create ESM variant I suggest to fork and publish a new package.

dundalek commented 1 year ago

I've been thinking I could transition to ESM-only and bump the major version and CJS would still be available as a previous version for compatibility reasons. I will see what I can do.

dundalek commented 1 year ago

I published the 2.0.0 version that uses ESM.