anchorme.js icon indicating copy to clipboard operation
anchorme.js copied to clipboard

Support native esm import

Open sebamarynissen opened this issue 4 years ago • 0 comments

Now that Node supports native esm imports, it makes sense that this library supports it as well. However

import anchorme from 'anchorme';

produces an unexpected result as the anchorme variable will look like

const anchorme = {
  default: {
    list() {},
    validate() {},
  },
};

in Node because of the way esm is transpiled to commonjs. This means that you have to resort to hacks like

import _anchorme from 'anchorme';
const anchorme = _anchorme.default || _anchorme;

if you want your code to run both in the browser and in Node.

This PR circumvents this by leveraging Node's conditional exports and providing a wrapper.mjs file while maintaining full backwards compatibility for older Node versions.

sebamarynissen avatar Feb 08 '21 15:02 sebamarynissen