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

ESM export is seen as default.default #80

Closed
silverwind opened this issue Jun 22, 2023 · 10 comments
Closed

ESM export is seen as default.default #80

silverwind opened this issue Jun 22, 2023 · 10 comments
Milestone

Comments

@silverwind
Copy link

Because of the AMD wrapper, the AnsiUp export is seen as default.default.AnsiUp when the module is loaded in ESM context:

$ node
> await import("ansi_up")
[Module: null prototype] {
  __esModule: true,
  default: { default: [Function: AnsiUp] }
}

Ideally convert the module to ESM with export default AnsiUp which can be loaded direcly in a browser via import AnsiUp from "./ansi_up.js" without any extra <script> tag needed.

@drudru
Copy link
Owner

drudru commented Jun 23, 2023

Hi, this is interesting.
I have not been the modules space.
Is there now a single standard way of using code in a browser and node?

@silverwind
Copy link
Author

silverwind commented Jun 23, 2023

Yes, it's called ECMAScript Modules (ESM), or alternatively JavaScript modules. Some resources:

It is possible to load such a module directly in a browser via import ... from 'url' syntax, removing the need for a extra script tag. Browser support this since around 5 years now, and the bundler ecosystem (webpack, vite, parcel) has more or less full support for it since probably around 2-3 years now.

@drudru
Copy link
Owner

drudru commented Jun 23, 2023

@silverwind

Excellent, do you have a lot of experience with this?
Do I need to rename the resulting file with a .mjs suffix?

I am excited to finally get rid of the hack I had to implement.

@silverwind
Copy link
Author

silverwind commented Jun 26, 2023

No, .mjs can be avoided with a "type": "module" in package.json. I can likely file a pull request to get this repo modernized. Afterwards, a 6.0.0 should be pushed as this change will require existing users to change their imports.

@Shaun-Regenbaum
Copy link

I did the required changes if you want and tested with a couple environments to make sure everything was backwards compatible. Hope it was helpful :) #83

@drudru
Copy link
Owner

drudru commented Jul 31, 2023

@silverwind - please try the v6.0.0 branch

@silverwind
Copy link
Author

silverwind commented Jul 31, 2023

Yes, looks good there:

$ node
> await import("ansi_up")
[Module: null prototype] { AnsiUp: [class AnsiUp] }

For the record, I installed via

npm i 'git+https://github.com/drudru/ansi_up.git#v6.0.0'

@drudru
Copy link
Owner

drudru commented Jul 31, 2023

@silverwind - excellent, I want to get a few more things in and then I will create a release

@silverwind
Copy link
Author

silverwind commented Jul 31, 2023

One caveat when going ESM is that some older code bases that are still using CommonJS (CJS) may not be able to import the module. Advice to them should be pretty much this.

There is a possibilty to dual-publish ESM and CJS, but I think it's generally not worth the trouble. You should definitely publish a new major version with this change.

@drudru
Copy link
Owner

drudru commented Aug 15, 2023

Released

@drudru drudru closed this as completed Aug 15, 2023
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

No branches or pull requests

3 participants