Skip to content

feat: convert package to es module#58

Closed
dnalborczyk wants to merge 3 commits into
sindresorhus:mainfrom
dnalborczyk:module
Closed

feat: convert package to es module#58
dnalborczyk wants to merge 3 commits into
sindresorhus:mainfrom
dnalborczyk:module

Conversation

@dnalborczyk

@dnalborczyk dnalborczyk commented Oct 20, 2021

Copy link
Copy Markdown

also bumped all dev dependencies, fixed linting.

small memory improvement (lazy init): https://github.com/sindresorhus/cli-spinners/pull/58/files#diff-e727e4bdf3657fd1d798edcd6b099d6e092f8573cba266154583a746bba0f346R11

depends on: #59

@sindresorhus

Copy link
Copy Markdown
Owner

I appreciate the pull request, but I don't really see the point of doing this now. Keeping it as-is will achieve the same, since it's still using require now. I prefer to wait for import assertions, which will bring JSON support. However, since you already did the work, I'm going to merge and not release this for now. Should be easy to remove createRequire when the time comes.

@sindresorhus

Copy link
Copy Markdown
Owner

Can you include #59 in this PR? It's related.

@dnalborczyk

dnalborczyk commented Oct 21, 2021

Copy link
Copy Markdown
Author

I appreciate the pull request, but I don't really see the point of doing this now.

how is this "the same"? with his PR this module is an ES module, not cjs.

Keeping it as-is will achieve the same,

it would achieve the same as your other cjs to esm package conversions achieve. you want to move the eco-system as a whole forward. that was at least my impression: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c

since it's still using require now.

the "global" require does not exist in an ES module. the imported createRequire function is returning a function which acts the same as require does, but it's not the "global" require. it's only being used to import the json file. you might as well use readFile[Sync] + JSON.parse. nonetheless, it doesn't matter, it's just an implementation detail.

I prefer to wait for import assertions, which will bring JSON support.

me too, but import assertions is very likely not ported back to older LTS versions, because of lacking V8 support.

@dnalborczyk

Copy link
Copy Markdown
Author

Can you include #59 in this PR? It's related.

Done

@sindresorhus

Copy link
Copy Markdown
Owner

how is this "the same"? with his PR this module is an ES module, not cjs.

But you're using createRequire, so the CommonJS loader is still being used in this package.

@sindresorhus

Copy link
Copy Markdown
Owner

What I'm afraid of is that createRequire will not work with bundlers. It took bundlers years to add support for JSON required through normal require. And I don't want to deal with the support if it ends up breaking a bundler (I already have enough support to do with just the ESM migration). What I decide to do here is what I will do with all my other packages, I'll need to think this over. I'll get back to you.

@dnalborczyk

dnalborczyk commented Oct 22, 2021

Copy link
Copy Markdown
Author

What I'm afraid of is that createRequire will not work with bundlers.

good point. I'll have a look myself with webpack and rollup.

It took bundlers years to add support for JSON required through normal require. And I don't want to deal with the support if it ends up breaking a bundler (I already have enough support to do with just the ESM migration. What I decide to do here is what I will do with all my other packages, I'll need to think this over.

gotcha, makes sense. 👍

I'll get back to you.

cool, thank you! I'll report back about bundler support as well.


thank you for taking the lead in converting your packages to esm, I think it will help to keep the whole ecosystem moving in the right direction and wipe cjs out of existence.

ps: no hard feelings about closing this PR. it was just a drive-by coming from using ink and going through some ink plugins code, ending up here, and being surprised that this module was still cjs. 😃

@sindresorhus

Copy link
Copy Markdown
Owner

ps: no hard feelings about closing this PR.

I'm not going to close it. I just need to decide whether to release it or wait.

@dnalborczyk

dnalborczyk commented Oct 24, 2021

Copy link
Copy Markdown
Author

alright, here is what I found:

createRequire

support in bundlers is pretty much non-existent at this point. (there's an issue and an [incomplete?] PR for webpack with not much traction. nothing for rollup as far as I can tell.)

json import assertions

still at stage 3 at ECMA.

support has been added to webpack, but I believe it is syntax support only so far. nothing for rollup, although there's an issue for that.

Chrome support has been released (I assume therefore for Edge as well), Mozilla is working on it.

there is an active PR for node.js, which will likely be released in v17, and possibly back-borted to v16, but not for any earlier LTS releases, as there is no V8 support for the new assert syntax. unfortunately it also looks like it's landing behind a flag first, not sure what the policy is for removing flags (if there is any), meaning who knows when that's gonna happen. nonetheless, flags are usually not required nor being used by 3rd party modules.

assuming you want to support the last supported LTS versions, starting with v16 (under the assumption it's being back-ported), you would be looking at April 2023, that's when v14 LTS reaches end-of-life. https://nodejs.org/en/about/releases/

@sindresorhus

Copy link
Copy Markdown
Owner

Thanks for investigating this. You should definitely comment this on https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c

@sindresorhus

Copy link
Copy Markdown
Owner

I think it's smart to wait a few months and see how things develop before doing anything more here.

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.

3 participants