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 an ESM bundle #175

Closed
hmaurer opened this issue Sep 28, 2020 · 17 comments
Closed

Generate an ESM bundle #175

hmaurer opened this issue Sep 28, 2020 · 17 comments
Labels
wontfix This will not be worked on

Comments

@hmaurer
Copy link

hmaurer commented Sep 28, 2020

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 modified tsconfig.json and outputting to dist/esm (or similar).
  2. add a module field to package.json pointing to dist/esm/index.js.

If you are keen I am happy to PR :)

Thanks for the library!

@colinhacks
Copy link
Owner

Sure I'm down - looking forward to the PR 🤙

@colinhacks
Copy link
Owner

ESM modules are now available in zod@2.0.0-beta.20+. Big thanks to @cbenz for the PR!

@colinhacks
Copy link
Owner

colinhacks commented Nov 9, 2020

Hm the ESM module is breaking things #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.

@cbenz
Copy link
Contributor

cbenz commented Nov 10, 2020

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.

@colinhacks
Copy link
Owner

Re-opening because the ES module was removed again in zod@2.0.0-beta.21.

@mmeylan
Copy link

mmeylan commented Nov 13, 2020

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

@colinhacks
Copy link
Owner

Yeah I took the ESM module out in beta21: #215 (comment)

Gonna revisit the ESM bundling once I have time to debug these issues. 👍

@aaronjensen
Copy link
Contributor

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?

@jacksteamdev
Copy link

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.

@aaronjensen
Copy link
Contributor

FWIW, it's being worked on: rollup/plugins#658

@aaronjensen
Copy link
Contributor

@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.

@jacksteamdev
Copy link

jacksteamdev commented Dec 8, 2020

@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' +
          '};')
    },
  }
]

@lukastaegert
Copy link

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.

@colinhacks
Copy link
Owner

colinhacks commented Dec 10, 2020

@jacksteamdev @aaronjensen @lukastaegert @cbenz I'm having trouble reproducing these runtime issues with the ESM module like the ones described in #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.

@aaronjensen
Copy link
Contributor

@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.

@colinhacks
Copy link
Owner

@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.

@stale
Copy link

stale bot commented Mar 2, 2022

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.

@stale stale bot added the wontfix This will not be worked on label Mar 2, 2022
@stale stale bot closed this as completed Mar 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

7 participants