Skip to content

Replace CommonJS-only dependencies with inline ESM code #3649

@andreialecu

Description

@andreialecu

Problem

When using mathjs in an Angular project (and likely other modern ESM-first bundlers), the following warnings are emitted during the build:

▲ [WARNING] Module 'seedrandom' used by 'node_modules/mathjs/lib/esm/function/probability/util/seededRNG.js' is not ESM

▲ [WARNING] Module 'escape-latex' used by 'node_modules/mathjs/lib/esm/utils/latex.js' is not ESM

▲ [WARNING] Module 'javascript-natural-sort' used by 'node_modules/mathjs/lib/esm/function/relational/compareNatural.js' is not ESM

▲ [WARNING] Module 'tiny-emitter' used by 'node_modules/mathjs/lib/esm/utils/emitter.js' is not ESM

CommonJS or AMD dependencies can cause optimization bailouts.
For more information see: https://angular.dev/tools/cli/build#configuring-commonjs-dependencies

Proposal

All four of these dependencies are tiny libraries that haven't been updated in 7+ years:

Package Last publish Size
seedrandom 7 years ago small
escape-latex 7 years ago small
javascript-natural-sort 9 years ago small
tiny-emitter 7 years ago small

Since they are unmaintained and unlikely to ever ship ESM builds, I'd suggest inlining their code directly into mathjs as proper ESM modules. This would:

  1. Eliminate the CommonJS warnings for Angular/esbuild/Vite users
  2. Allow tree-shaking to work properly on this code
  3. Remove external dependencies that are effectively abandoned

I'll follow up with PRs to inline each of these.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions