Skip to content

Can’t use @formatjs/fast-memoize in isomorphic code #3999

@remcohaszing

Description

@remcohaszing

Which package?

@formatjs/fast-memoize

Describe the bug

The main property of @formatjs/fast-memoize is a CommonJS module that specifies module.exports.default. There is no (real) ESM support. That means that usage in CommonJS is as follows:

const memoize = require('@formatjs/fast-memoize')

memoize.default(/* … */)

and usage in ESM is:

import memoize from '@formatjs/fast-memoize'

memoize.default(/* … */)

IMO this is not pretty, but it works.

The problem is that @formatjs/fast-memoize does specify the module field. This points to a faux ESM module. Many bundlers use this field, so correct use with a bundler is:

import memoize from '@formatjs/fast-memoize'

memoize(/* … */)

This means it’s not possible to use @formatjs/fast-memoize in isomorphic code.

To Reproduce

Codesandbox URL

N/A

Reproducible Steps/Repo

  1. Create the following script named test.mjs:
    import memoize from '@formatjs/fast-memoize';
    
    console.log(memoize);
    ``
  2. Run the following script using Node.js:
    node test.mjs

Notice that memoize is a module with the property default. memoize itself is not a function.

Expected behavior

I would expect at least one of the following:

  1. @formatjs/fast-memoize supports proper dual packaging using package exports.
  2. @formatjs/fast-memoize removes support for CommonJS.
  3. @formatjs/fast-memoize removes support for faux ESM.
  4. @formatjs/fast-memoize uses a named export instead of a default export.
  5. @formatjs/fast-memoize gets deprecated in favour of fast-memoize.

A clear and concise description of what you expected to happen.

Screenshots

N/A

Desktop (please complete the following information):

  • OS: Pop!_OS
  • Browser: Node.js
  • Version: 18

Smartphone (please complete the following information):

  • Device: N/A
  • OS: N/A
  • Browser N/A
  • Version N/A

Additional context

@formatjs/fast-memoize was created because of a very niche issue (#1913). fast-memoize uses CommonJS (not UMD, as is claimed in that issue). Node.js native ESM and bundlers can consume CJS just fine. A lot of ESM packages have CJS dependencies.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions