scurker / currency.js

A javascript library for handling currencies
https://currency.js.org
MIT License
3.15k stars 142 forks source link

Use of ECMAScript modules to prevent optimization bailouts in Angular applications #449

Closed sebastianhaberey closed 1 year ago

sebastianhaberey commented 1 year ago

Angular complains about not being able to optimize currency.js:

Warning: /Users/user/[...]/currency.functions.ts depends on 'currency.js'. CommonJS or AMD dependencies can cause optimization bailouts.
For more info see: https://angular.io/guide/build#configuring-commonjs-dependencies

I wonder if it would be difficult to move this library to ECMAScript modules?

scurker commented 1 year ago

We already publish both cjs and esm so this should already be supported. Can you check to see why your project might be pulling in the cjs version?

sebastianhaberey commented 1 year ago

Awesome! Turns out I had to enable ESM support in my TypeScript project. Thanks for your help!

sebastianhaberey commented 1 year ago

So I thought I was able to resolve this by setting my TypeScript config to:

  "compilerOptions": {
    "module": "NodeNext",

and it seemed to work fine. It did compile and the tests were passing. But I am facing an error when building in production mode:

Error: libs/neo/data-access/src/lib/ressourcen/money/money.ts:28:10 - error TS2349: This expression is not callable.
  Type '{ default: Constructor; }' has no call signatures.

28   return currency(money.betragInCent, {
            ~~~~~~~~

  libs/neo/data-access/src/lib/ressourcen/money/money.ts:1:1
    1 import * as currency from 'currency.js';
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Type originates

I am sure this isn't something in currency.js but rather in my Angular / nx configuration. So I'm just leaving this here in case anyone has seen this before.