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

feat: add ESM support #252

Merged
merged 9 commits into from
Nov 28, 2022
Merged

feat: add ESM support #252

merged 9 commits into from
Nov 28, 2022

Conversation

unional
Copy link
Contributor

@unional unional commented Nov 23, 2022

I kept the existing code unchanged and only add the new index.mjs file copied from index.js.

Adjusted var => const and let,
and update package.json to with the exports field and added test:esm script.

Updated ci.yml to run npm run test:esm.

Fixes #251 #248

@lpinca
Copy link
Member

lpinca commented Nov 23, 2022

Can't we use a wrapper as documented here https://nodejs.org/api/packages.html#approach-1-use-an-es-module-wrapper instead of duplicating everything?

@unional
Copy link
Contributor Author

unional commented Nov 23, 2022

AFAIK, that doesn't work in practice.
For example, if you try to run test using jest, you will end up with error "exports is not defined" because the CJS code using module.exports.

I can give it a try to see how it works out.

@unional
Copy link
Contributor Author

unional commented Nov 23, 2022

Good news. I have tried it and it works.

The reason it works is the index.js did the check of if ('undefined' === typeof module) so the code is not hit in ESM pipeline.

Also updated the index.mjs and index.d.ts to export EventEmitter as named export, to match the usage of import { EventEmitter } from 'eventemitter3'

index.d.ts Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
test/test.mjs Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
@lpinca
Copy link
Member

lpinca commented Nov 23, 2022

Another issue I see is that this works in Node.js but not in the browser. We only have the UMD for the browser.

index.mjs Outdated Show resolved Hide resolved
@unional
Copy link
Contributor Author

unional commented Nov 23, 2022

Another issue I see is that this works in Node.js but not in the browser. We only have the UMD for the browser.

I think UMD is ok, people who use UMD are not using ESM. So the code can remain as is.

@lpinca
Copy link
Member

lpinca commented Nov 23, 2022

I think UMD is ok, people who use UMD are not using ESM. So the code can remain as is.

Yes I was referring to the fact that is not possible to use this as ESM in the browser. For example using a CDN like https://www.jsdelivr.com/. I wonder if it makes sense to create both an ESM and UMD module renaming the umd folder to dist and using rollup instead of browserify.

@unional
Copy link
Contributor Author

unional commented Nov 23, 2022

Yes I was referring to the fact that is not possible to use this as ESM in the browser.

I see, so you want to have two bundles, one for umd and one for esm.

create both an ESM and UMD module renaming the umd folder to dist

That will be a braking change for require('.../umd/eventemitter3.js') unless we hack it in the exports map. 😛

@lpinca
Copy link
Member

lpinca commented Nov 23, 2022

I see, so you want to have two bundles, one for umd and one for esm.

Ideally yes, that is what I would expect from "ESM support".

@lpinca
Copy link
Member

lpinca commented Nov 23, 2022

That will be a braking change for require('.../umd/eventemitter3.js') unless we hack it in the exports map.

There are already breaking changes.

@unional
Copy link
Contributor Author

unional commented Nov 23, 2022

Ideally, yes, that is what I would expect from "ESM support".

Ok. let me see if I can cook up one.

@unional
Copy link
Contributor Author

unional commented Nov 23, 2022

There are already breaking changes.

If that's the case, do you still want to support the use case of require('../dist/eventemitter3.js')?

I don't see why people would do deep linking in NodeJS env.

@unional
Copy link
Contributor Author

unional commented Nov 23, 2022

One more question. Do you want to also replace uglifyjs? And do you want to keep the minified code in ES5 or we can use terser instead?

UPDATE: I have updated to use terser. Please let me know if you have any concerns

@unional
Copy link
Contributor Author

unional commented Nov 23, 2022

There is some problem with the adjustment of:

import EventEmitter from './index.js'
// to
import * as EventEmitter from './index.js`

in the index.mjs.

First one works, second doesn't. But rollup complain about the first one. Need to fix that

UPDATE: done

@lpinca
Copy link
Member

lpinca commented Nov 24, 2022

Thanks. I will review properly in the next few days.

.gitignore Outdated Show resolved Hide resolved
.github/workflows/ci.yml Outdated Show resolved Hide resolved
index.mjs Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
rollup.config.mjs Outdated Show resolved Hide resolved
test/test.mjs Outdated Show resolved Hide resolved
test/test.mjs Outdated Show resolved Hide resolved
test/test.mjs Outdated Show resolved Hide resolved
test/test.mjs Outdated Show resolved Hide resolved
bundle using rollup
@unional
Copy link
Contributor Author

unional commented Nov 24, 2022

All review changes updated and squashed for easier reading.

.github/workflows/ci.yml Outdated Show resolved Hide resolved
Co-authored-by: Luigi Pinca <luigipinca@gmail.com>
package.json Outdated Show resolved Hide resolved
@lpinca lpinca merged commit dd44977 into primus:master Nov 28, 2022
@lpinca
Copy link
Member

lpinca commented Nov 28, 2022

Thank you.

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.

ESM support
2 participants