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

Add UMD build #1067

Merged
merged 2 commits into from Apr 7, 2022
Merged

Add UMD build #1067

merged 2 commits into from Apr 7, 2022

Conversation

rinsuki
Copy link
Contributor

@rinsuki rinsuki commented Apr 4, 2022

Add UMD build to zod.

Example

with UMD build, you can use zod on web browser without bundler/ESM support, like this:

<script src="https://some.cdn.example/npm/zod/lib/index.umd.js"></script>
<script>
// @ts-check
const { z } = Zod

z.object({hello: z.literal("world")})
</script>

Background

I want to use zod in UserScript, but I have some issues:

  • Zod is pretty big to bundle it to UserScript (>2000 lines if you wont minify it)
    • some hosting sites are restricting file size, like Greasy Fork
  • If bundle libraries to UserScript, makes hard to:
    • read/audit source codes (because lines of source code will increase)
    • determine this source code comes from which version of libraries

so I added a UMD build to Zod, which allows UserScript to use @require to load zod, like // @require https://some.cdn.like.jsdelivr.example/npm/zod@x.x.x/lib/index.umd.js.

package.json Outdated
"build:deno": "node ./deno/build.mjs",
"build:esm": "rollup --config rollup.config.js",
"build:cjs": "tsc --p tsconfig.cjs.json",
"build:types": "tsc --p tsconfig.types.json",
"build:tweak-dts": "echo 'export as namespace Zod;' >> lib/index.d.ts",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there something we can do in the source to get this same effect without having to patch the artifact?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find way to do it without patch to generated dts! can you review it?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely better! Can you give me a little context as to why we need to export a namespace for the UMD target? Is that because it adds a global binding to the library?

Copy link
Contributor Author

@rinsuki rinsuki Apr 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that because it adds a global binding to the library?

Yes!

FYI: If project uses module (like import ...), global binding will be disabled on tsc (tsc says 'Zod' refers to a UMD global, but the current file is a module. Consider adding an import instead.ts(2686))

@scotttrinh scotttrinh merged commit c7792de into colinhacks:master Apr 7, 2022
@colinhacks
Copy link
Owner

@rinsuki I'd rather the name of the global namespace be z as this is the recommended way to consume Zod's API. Is there a reason this wouldn't work?

@rinsuki
Copy link
Contributor Author

rinsuki commented Apr 9, 2022

  1. z are too short and might be overwritten by other scripts (some dumbs script writes temporary variables to global)
  2. if we uses z for UMD name, and if users .ts file doesn't using import, z will be automatically available on global. then users may forget to import, and causes runtime error. it would be good to force user to write const { z } = Zod, i think.

MrAwesome pushed a commit to MrAwesome/zod that referenced this pull request May 20, 2022
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

3 participants