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

Native ESM Support (no new build dependencies required) #152

Open
PaulKiddle opened this issue Jun 22, 2023 · 4 comments
Open

Native ESM Support (no new build dependencies required) #152

PaulKiddle opened this issue Jun 22, 2023 · 4 comments

Comments

@PaulKiddle
Copy link

Hi thanks for this great project.

I know this has been discussed a couple of times before but not resolved. I'm using Rollup for my project and cookie is currently the only dependency that doesn't have ESM support, meaning I have to install the CJS plugin just for this package. It's fine, but ideally I'd like to simplify my build process even more by removing it.

I'm aware one of the reasons for not adding ESM support is that you don't want to add any build dependencies, which is fair enough. But there is a very simple way to add ESM support to this package just by concatenating extra lines at the start and end, which would require no build steps.

Since the only CJS-specific feature you're using is assigning properties to the exports object, basic ESM support would require creating a file like this:

const exports = {};

/* ...CJS code goes here... */

export default exports;

This is easy enough to create using a common shell command like cat.

@dougwilson
Copy link
Contributor

Thank you, that is interesting. So you're saying I can just add those lines to the top and bottom of the index.js file and that is enough?

@PaulKiddle
Copy link
Author

PaulKiddle commented Oct 16, 2023

You'd have to create a separate .mjs file if you still want to support CJS implementations (since the export keyword is not supported outside of a module context), but essentially yes,
that's correct - consumers would then be able to import the library using import cookie from 'cookie';

If you want to support importing the individual functions too you'd need to explicitly export those:

export const parse = exports.parse;
export const serialize = exports.serialize;

The consumer would then be able to use import { parse, serialize } from 'cookie';.

I believe there's also a config you'd need to add to the package.json that points to this esm version of the library.

@Sammy-T
Copy link

Sammy-T commented Oct 16, 2023

@PaulKiddle You seem to already have a good handle on how this could be implemented. Would you be interested in creating a PR?

@PaulKiddle
Copy link
Author

Yep, if you're happy to go down this route I'll set up a PR when I get some free time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants