In T294993#8624960, @Sgs wrote:Currently the codex packages have "main" and "module" entries in their package.json:
"main": "dist/codex.umd.js", "module": "dist/codex.es.js"I don't know what's a valid mechanism to import components from the es module in a non-MW environment. For instance when trying to build a VitePress project which imports Codex components as import { CdxButton } from '@wikimedia/codex'; the following error is yield:
import { CdxDialog, CdxButton } from "@wikimedia/codex"; ^^^^^^^^^ SyntaxError: Named export 'CdxButton' not found. The requested module '@wikimedia/codex' is a CommonJS module, which may not support all module.exports as named exports. CommonJS modules can always be imported via the default export, for example using: import pkg from '@wikimedia/codex'; const { CdxDialog, CdxButton } = pkg;Have you considered using package.json "exports" entry? I was able to import the es Codex components and icons by adding an "exports" entry as follows:
"main": "dist/codex.umd.js", "module": "dist/codex.es.mjs", "exports": { "import": "./dist/codex.es.mjs", "require": "./dist/codex.umd.js" }(Note in my tests I had to use the .mjs extension to get it working, not sure why). Before that I had also tried "type":"module" in the project package.json but didn't work. I think for that we would need to change the "main" entry. Is there a preferred mechanism to import from the es module that I'm missing?
We need to rename codex.es.js to have an .mjs extension, and explicitly expose it using the "exports" property in package.json