Skip to content

Better ESM/CJS support #648

Closed
Closed
@vladmoroz

Description

@vladmoroz

Current issues

  • Defaults to CJS even for modern Node
    • node folder for CJS is misleading and not discoverable
    • View source action in VS Code goes to the (hard to read) CJS version
  • package.json doesn’t specify the type field
  • ESM code is missing extensions
    • ESM version can't be used without a bunlder
    • Makes it incompatible with moduleResolution: "Node" | "NodeNext"

Suggestions

My preferred implementation would be to do what we learned from doing the dual package in Radix Themes (see on unpkg):

  • type: "commonjs" in the root package.json
  • type: "module" in the esm folder package.json
  • exports field with require and import declarations
  • Explicit cjs and esm folders in the built package
    • Makes it easy for consumers to find the right version if they need to explicitly point their tooling to the ESM/CJS version
  • Extensions in source code
    • No build tooling would be needed to transform extensions
    • ESLint can enforce this and auto add them, VSC support is good too

publint can be a good way to check if all looks good:
https://publint.dev/@base_ui/react@1.0.0-alpha.2
https://publint.dev/@radix-ui/themes@3.1.4

Some details on the possible implementations:
https://www.sensedeep.com/blog/posts/2021/how-to-create-single-source-npm-module.html

Metadata

Metadata

Assignees

Labels

bug 🐛Something isn't workingcoreDO NOT USE. This label has been deprecated and replaced by `all components` and `internal`.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions