Native ESM Support (no new build dependencies required)
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.
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?
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.
@PaulKiddle You seem to already have a good handle on how this could be implemented. Would you be interested in creating a PR?
Yep, if you're happy to go down this route I'll set up a PR when I get some free time