kentcdodds / mdx-bundler

šŸ¦¤ Give me MDX/TSX strings and I'll give you back a component you can render. Supports imports!

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Importing packages with `exports` in `package.json`

pomber opened this issue Ā· comments

  • mdx-bundler version: 8.0.1
  • node version: 16.13.2
  • npm version: 8.1.2

When the source mdx imports something from node_modules:

import Ipsum from "lorem/ipsum"

# Hello

<Ipsum />

and the lorem package.json uses the exports field:

{
  "name": "lorem",
  "exports": {
    "./ipsum": "./dist/whatever.js"
  }
}

mdx-bundler throws an error:

error: Could not resolve "lorem/ipsum" (mark it as external to exclude it from the bundle)

I think the problem is the @esbuild-plugins/node-resolve plugin, which uses the resolve package, which has this issue: browserify/resolve#222

I'm not very familiar with the esbuild ecosystem, but apparently esbuild already handles the exports field . Maybe it could be possible to reuse esbuild path resolution before throwing?

I'm experiencing the same problem, non-ESM packages resolve fine but when I attempt to import an ESM package which should resolve from exports.default in its package.json, I get a similar "Could not resolve" error.

I have a fork with failing test: https://github.com/revelt/mdx-bundler/tree/pr/support-esm-packages that's a good start