Skip to content

feat(cli): support ESM binding#2284

Merged
Brooooooklyn merged 4 commits intonapi-rs:mainfrom
ChainSafe:feat/esm
Oct 15, 2024
Merged

feat(cli): support ESM binding#2284
Brooooooklyn merged 4 commits intonapi-rs:mainfrom
ChainSafe:feat/esm

Conversation

@wemeetagain
Copy link
Copy Markdown
Contributor

  • Split createCjsBinding into reusable code chunks createCommonBinding and bindingHeader
  • Add createEsmBinding
  • Add a --esm option to build command to select esm vs cjs binding
  • Tested the ESM build locally
  • resolves Support native ESM codegen #2244

Copy link
Copy Markdown
Member

@Brooooooklyn Brooooooklyn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about keep the cjs format (for cjs projects), and create the esm binding from cjs file:

import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);
const __dirname = new URL('.', import.meta.url).pathname;

export const { Foo, Bar, Baz } = require('./index.js') 

@wemeetagain
Copy link
Copy Markdown
Contributor Author

and create the esm binding from cjs file

That could work. Although, the DX is annoying in that it would be another file to configure. Eg: --js bindingFile.cjs --esm otherBindingFile.mjs
I think its better to simply emit the proper kind of format straightaway.

@Brooooooklyn Brooooooklyn changed the title feat: support ESM binding feat(cli): support ESM binding Oct 15, 2024
@Brooooooklyn Brooooooklyn merged commit 960d07e into napi-rs:main Oct 15, 2024
richerfu pushed a commit to richerfu/napi-rs that referenced this pull request Oct 23, 2024
* feat: support ESM binding

* chore: consistent use of semicolons

* chore: apply suggestion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support native ESM codegen

2 participants