Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ES Modules with type:module #192

Closed
fregante opened this issue Jul 26, 2020 · 11 comments · Fixed by #315
Closed

Use ES Modules with type:module #192

fregante opened this issue Jul 26, 2020 · 11 comments · Fixed by #315
Assignees

Comments

@fregante
Copy link

fregante commented Jul 26, 2020

It'd be great to have just be a Node-compatible ES Module with type: "module" in the package.json

I don't remember if there's a backwards-compatible way to have both CJS and type:module in the same import, but this is definitely possible:

- index.js // cjs file
- esm/index.js // esm file
- esm/package.json // Content: `{"type": "module"}`

This would enable both:

const something = require('just-something');
import something from 'just-something/esm'; // Notice the `/esm`
@angus-c
Copy link
Owner

angus-c commented Aug 26, 2020

yes I should look into this, thanks

@angus-c angus-c self-assigned this Aug 26, 2020
@boogerlad
Copy link

any update on this?

@angus-c
Copy link
Owner

angus-c commented Dec 18, 2020

Sorry I haven't had the time to get to this. In theory it should be fairly simple (if repetitive) but I wanted to take the time to verify a POC before making updates. Will try to look at this over the holidays.

@antony
Copy link
Contributor

antony commented Jul 8, 2021

one option which can save time with this is to add a small 3-4 line rollup file which compiles an es6 version of the library into dist which can just be done at packaging time.

There's an example of this here: mcollina/fastq#48

@raulfdm
Copy link
Contributor

raulfdm commented Jul 21, 2021

@angus-c want some help on this task?

@angus-c
Copy link
Owner

angus-c commented Aug 8, 2021

@angus-c want some help on this task?

sure!

@PCOffline
Copy link

Hey, any updates with this?

@raulfdm
Copy link
Contributor

raulfdm commented Oct 9, 2021

@angus-c I was about start but I think, before any work, we should discuss how we want to do that.

I believe there are some strategies to make it work.

Copy strategy

If we don't want to change anything, we could simply:

  1. make a index.js copy and name index.esm.js
  2. replace the module.exports with export default
  3. Add the property type: module to inform that ESM should take precedence if supported;
  4. Point the index.esm.js file as module ("module": "index.esm.js)

The problem with that is we'll end up having duplicated code. If something needs to be fixed, it'll need to be done in 2 places.

Single source of truth

Since every package is self contained and written in CJS way, we could use a transpiler tool like SWC, ESbuild, or Rollup and then:

  1. write a script to transform index.js into index.esm.js targeting esm;
  2. Add in the package.json the module and type;
  3. include that new file to published

Since you're using a monorepo, it won't be much work. The only thing we should be aware is to run the build process for every new publish.


I only can see these 2 approaches, let me know what you want to do so I can help in a more assertive way.

Cheers.

@PuruVJ
Copy link
Contributor

PuruVJ commented Oct 20, 2021

I second this and would love to help with transition. I'm already building a library on top of just, and this move will resolve tree-shaking issues in that

@angus-c
Copy link
Owner

angus-c commented Oct 23, 2021

@raulfdm Yeah I prefer the second option, for maintainability. Running the new build process on publish should be no problem

@raulfdm
Copy link
Contributor

raulfdm commented Oct 23, 2021

@angus-c I've just opened a PR with a proposal.

I did for one single package to align with you the direction. If you agreed I'll do for all packages. Otherwise we can refine the approach until we get in strategy I can following for all other packages.

@angus-c angus-c assigned raulfdm and unassigned angus-c Oct 23, 2021
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 a pull request may close this issue.

7 participants