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

Generate both CommonJS and ES modules #208

Merged
merged 1 commit into from
Nov 3, 2020
Merged

Generate both CommonJS and ES modules #208

merged 1 commit into from
Nov 3, 2020

Conversation

cbenz
Copy link
Contributor

@cbenz cbenz commented Oct 31, 2020

Related to #175

Based on this article: https://blog.logrocket.com/publishing-node-modules-typescript-es-modules/

I added a new tsconfig file to generate a new ESM bundle. I changed the output directory from lib/src to lib/{cjs,esm}/src. I added a command to npm run build (that could have been parallelized with concurrently but maybe it's overkill).

I tested npm run build and the ES modules worked with npm link from a local application that uses ES modules with Snowpack (and I confirm that the CommonJS build did not work because of some error with those generated lines: Object.defineProperty(exports, "__esModule", { value: true });).

Obviously I did not tested npm publish, so I hope I did not break the publication process.

Note: I did not understand the need to include tsconfig.package.json in the lib/src directory, so I kept it as-is in the lib/{cjs,esm}/src directories.

@cbenz cbenz changed the base branch from master to v2 October 31, 2020 08:21
colinhacks pushed a commit that referenced this pull request Nov 3, 2020
@colinhacks colinhacks merged commit b71f177 into colinhacks:v2 Nov 3, 2020
@colinhacks
Copy link
Owner

Thanks for this wonderful PR! I made some small changes overriding and cleaned up all the tsconfigs — there were several redundant settings. Hopefully I didn't break anything.

Turns out there's no reason why the tsconfig.package.json was getting copied into the lib directory 🤷‍♂️ So I got rid of that.

npm publish worked fine. Please me know if the ESM module is working as expected! Once I get confirmation, I'll update the README.

Is there any specific instructions for how to consume the ES6 module? Do you have to use import * as z from 'zod/lib/esm'? Or does it work automatically in a system that supports ES6 modules?

@cbenz
Copy link
Contributor Author

cbenz commented Nov 8, 2020

I can confirm that it worked for me now, doing import * as z from "zod". No need to reference zod/lib/esm thanks to the "module": "./lib/esm/src" key in package.json.

@colinhacks
Copy link
Owner

Excellent! Thanks Christophe!

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.

None yet

2 participants