import foo from 'bar/module.mjs';).
Obviously nobody wants to write these out manually, but this presents some new challenges for transpilers, which unfortunately no transpilers have yet tried to solve.
So what can we do about this?
babel-plugin-esm-resolver, a Babel plugin I wrote to simplify this process without the need to manually rewrite your imports for CommonJS and ESM modules. It features 3 options for transforming your import statements.
sourceSource modules (
import foo from './bar').
moduleModule main entry points (
import foo from 'bar').
submoduleModule submodules (
import foo from 'bar/foo').
You can read the documentation on these options on the GitHub repo, but here’s an example Babel configuration.
This example works with:
modulefield for modules with
./moduledeep-import entry file for modules with
- Resolves module deep-imports to
There we go, everything we need to be able to write and publish ESM modules TODAY!
The need to resolve Node module entry points in dual-packages to a deep-import to use them as ESM modules is kinda ugly. Hopefully we’ll see some cleaner options for that added to Node before the ESM loader is unflagged.