Closed hmaurer closed 2 years ago
Sure I'm down - looking forward to the PR 🤙
ESM modules are now available in zod@2.0.0-beta.20+
. Big thanks to @cbenz for the PR!
Hm the ESM module is breaking things https://github.com/vriad/zod/issues/215
It seems to be related to some tricky circular dependency issues that are doing to take some time to figure out, so I'm removing the ESM module in zod@2.0.0-beta.21
. You can stay on zod@2.0.0-beta.20
if you need the ESM module but I suspect it won't actually work at runtime due to the issues described in #215.
@cbenz Can you try building your project and running the bundled version? It appears these issues often don't exist in a development environment then crop up in built/bundled versions.
Actually the Snowpack dev tool I use, heavily focused on ESModules, has been upgraded in the last couple of days to enhance support of CommonJS modules, so I can now use zod without those native ESModules. Of course in the end ESModules should preferably be shipped for the browser platform rather than CommonJS, but in the meanwhile I'm not blocked anymore, so no problem for me if you remove them.
Re-opening because the ES module was removed again in zod@2.0.0-beta.21.
We seem to have an error related to this (and to #215).
Currently experiencing this runtime error:
TypeError: Object prototype may only be an Object or null: undefined
This happens with zod@2.0.0-beta.20
, but not zod@2.0.0-beta.21
Yeah I took the ESM module out in beta21: https://github.com/colinhacks/zod/issues/215#issuecomment-724317509
Gonna revisit the ESM bundling once I have time to debug these issues. 👍
We're kicking off a project using Snowpack, which compiles zod to ESM and has this circular dependency issue.. has anyone managed to work around it for now using Snowpack?
I've run into the circular dependencies problem with Rollup as well, which makes sense b/c Snowpack is built on top of it.
My use case is mainly for the check
method as a type guard, which doesn't throw btw, it just always returns false
.
FWIW, it's being worked on: https://github.com/rollup/plugins/pull/658
@colinhacks FYI using the ESM built off the v2 branch with tsc --p tsconfig.esm.json
appears to work in our app, even w/ the circular dependency, but we haven't fully exercised it.
@colinhacks : @aaronjensen and @lukastaegert are using zod as a use-case for this PR. In this comment there is a custom Rollup plugin that may be able to create an ESM bundle for zod:
So it would need to be changed by a code transform e.g. via a plugin. But if this is a viable approach for you, why not? I actually gave it a shot with this REALLY simple version and it worked for zod, even making the output slightly smaller
plugins: [
{
transform(code) {
return code.replace('var __importStar = (this && this.__importStar) || function (mod) {\n' +
' if (mod && mod.__esModule) return mod;\n' +
' var result = {};\n' +
' if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);\n' +
' __setModuleDefault(result, mod);\n' +
' return result;\n' +
'};','var __importStar = function (mod) {\n' +
' return mod;\n' +
'};')
},
}
]
Note that it still needs the commonjs plugin from the branch + manually enabling module side effects. To get rid of the latter issue, I will need to make a change in Rollup which will require some more time.
@jacksteamdev @aaronjensen @lukastaegert @cbenz I'm having trouble reproducing these runtime issues with the ESM module like the ones described in https://github.com/colinhacks/zod/issues/215. I'd love to see a PR for this, or at least a minimal repro. tbh all these module resolution subtleties are a little over my head.
@colinhacks I'm not sure how to reproduce those issues either. They likely require a React Native build or, one person indicated a Next.js build. They likely have to do with the way that TS is converting to ES modules combined with the way that they're bundling combined with the particular circular dependencies that are in this repo triggering some edge case. It's messy stuff. The best bet is probably to get rid of the circular dependencies (likely easier said than done, I don't have my head wrapped around their cause in this repo) or implement the init
pattern reference in one of these issues.
@mmeylan I'm having trouble reproducing the runtime issues with the ESM module you're describing. What's your build process? If you could throw together a reproduction repo that would be awesome. I'm trying to fix the issues with the Zod ES module over the weekend.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Hello! Would you be open to a pull request adding an ESM bundle? This would require roughly two changes:
1) add a build script
build:esm
, building the project using a slightly modifiedtsconfig.json
and outputting todist/esm
(or similar). 2) add amodule
field topackage.json
pointing todist/esm/index.js
.If you are keen I am happy to PR :)
Thanks for the library!