Skip to content

Commit

Permalink
feat: add esm build
Browse files Browse the repository at this point in the history
Also:
- docs: adds demo
  • Loading branch information
brettz9 committed Sep 5, 2024
1 parent 7048838 commit 0b0bf8d
Show file tree
Hide file tree
Showing 15 changed files with 136 additions and 43 deletions.
3 changes: 0 additions & 3 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npx commitlint --edit ${1}
5 changes: 1 addition & 4 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

pnpm lint-staged
pnpm install --frozen-lockfile
pnpm install --frozen-lockfile
File renamed without changes.
26 changes: 26 additions & 0 deletions demo/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Zodex Demo</title>
<style>
textarea { width: 300px; height: 400px; }
</style>
<link rel="shortcut icon" href="data:image/x-icon;," type="image/x-icon" />
<script type="importmap">
{
"imports": {
"zod": "../node_modules/zod/lib/index.mjs"
}
}
</script>
<script type="module" src="index.js"></script>
</head>
<body>
<textarea id="js" placeholder="Put arbitrary JavaScript object here (e.g., `{a: 5}`)">{a: 5}</textarea>
<textarea id="zod" placeholder="Put Zod here (e.g., `z.object({a: z.number()})`">z.object({a: z.number()})</textarea>
<button id="zerialize">Zerialize</button>
<textarea id="zodexJSON" placeholder="Put Zodex JSON here (or zerialize a Zod expression)"></textarea>
<button id="dezerialize">Dezerialize and validate</button>
</body>
</html>
66 changes: 66 additions & 0 deletions demo/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { z } from "zod";
import { zerialize, dezerialize } from "../dist/esm/index.js";

const $ = (sel) => {
return document.querySelector(sel);
};

$("#dezerialize").addEventListener("click", () => {
let js;
try {
js = eval(`(${$("#js").value})`);
} catch (err) {
alert("Error evaluating JavaScript");
return;
}

let json;
try {
json = JSON.parse($("#zodexJSON").value);
} catch (err) {
alert("Error evaluating JSON");
return;
}

let dez;
try {
dez = dezerialize(json);
} catch (err) {
alert("Error dezerializing JSON");
return;
}

let parseObj;
try {
parseObj = dez.safeParse(js);
} catch (err) {
alert("Error parsing JavaScript");
return;
}

alert(JSON.stringify(parseObj, null, 2));
});

$("#zerialize").addEventListener("click", () => {
let zod;
try {
zod = eval(`(${$("#zod").value})`);
} catch (err) {
alert("Error evaluating Zod");
return;
}
let zer;
try {
zer = zerialize(zod);
} catch (err) {
alert("Error zerializing Zod");
return;
}

try {
$("#zodexJSON").value = JSON.stringify(zer, null, 2);
} catch (err) {
alert("Error stringifying Zod JSON object");
return;
}
});
15 changes: 2 additions & 13 deletions eslint.config.mjs → eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,11 @@ import eslintConfigPrettier from "eslint-config-prettier";

export default tseslint.config(
{
ignores: [
"src/schema.zodex.json",
"dist/",
".idea",
"coverage",
],
ignores: ["src/schema.zodex.json", "dist/", ".idea", "coverage"],
},
eslint.configs.recommended,
...tseslint.configs.recommended,
eslintConfigPrettier,
{
files: ['*.cjs'],
languageOptions: {
globals: globals.node
}
},
{
files: ["demo/**/*.js"],
languageOptions: {
Expand All @@ -40,5 +29,5 @@ export default tseslint.config(
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-unused-vars": "off",
},
},
}
);
12 changes: 11 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@
"name": "zodex",
"version": "0.0.0-dev",
"description": "Type-safe (de)serialization for Zod",
"type": "module",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
".": {
"import": "./dist/esm/index.js",
"require": "./dist/index.js"
},
"./dist/schema.zodex.json": "./dist/schema.zodex.json"
},
"keywords": [
"zod",
"serialization",
Expand All @@ -13,11 +21,12 @@
"author": "Gregor Weber<[email protected]>",
"license": "MIT",
"scripts": {
"start": "vite serve demo",
"prepare": "husky",
"check-style": "prettier --check src",
"lint": "eslint --no-warn-ignored .",
"test": "vitest --coverage --silent=false --reporter=basic",
"build": "rm -rf dist && pnpm tsc && cp src/schema.zodex.json dist/schema.zodex.json",
"build": "rm -rf dist && pnpm tsc --module esnext --moduleResolution bundler --outDir dist/esm && pnpm tsc && echo '{\"type\": \"commonjs\"}' > dist/package.json && echo '{\"type\": \"module\"}' > dist/esm/package.json && cp src/schema.zodex.json dist/schema.zodex.json",
"prepublish": "pnpm run build"
},
"peerDependencies": {
Expand All @@ -40,6 +49,7 @@
"prettier": "2.8.8",
"typescript": "5.5.4",
"typescript-eslint": "8.4.0",
"vite": "^5.4.3",
"vitest": "2.0.5"
},
"packageManager": "[email protected]",
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/dezerialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ import {
SzUnknown,
SzVoid,
SzRef,
} from "./types";
import { ZodTypes } from "./zod-types";
} from "./types.js";
import { ZodTypes } from "./zod-types.js";

type DezerializerOptions = {
superRefinements?: {
Expand Down
2 changes: 1 addition & 1 deletion src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { expect, test } from "vitest";
import { z } from "zod";
import { SzCatch } from "./types";

import { dezerialize, SzType, zerialize, Zerialize } from "./index";
import { dezerialize, SzType, zerialize, Zerialize } from "./index.js";

const zodexSchemaJSON = JSON.parse(
fs.readFileSync("./src/schema.zodex.json", "utf-8")
Expand Down
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { SzType } from "./types";
import { SzType } from "./types.js";

export * from "./dezerialize";
export * from "./zerialize";
export * from "./dezerialize.js";
export * from "./zerialize.js";

export * from "./types";
export * from "./types.js";

type KeysOfUnion<T> = T extends T ? keyof T : never;
export type SzPropertyKeysOf<T extends SzType> = KeysOfUnion<
Expand Down
4 changes: 2 additions & 2 deletions src/zerialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import {
SzUnknown,
STRING_KINDS,
SzRef,
} from "./types";
import { ZodTypes, ZTypeName } from "./zod-types";
} from "./types.js";
import { ZodTypes, ZTypeName } from "./zod-types.js";

export const PRIMITIVES = {
ZodString: "string",
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
"declaration": true,
"outDir": "dist"
},
"exclude": ["vite.config.mts", "dist", "node_modules", "**/*.test.ts"]
"exclude": ["vite.config.ts", "dist", "node_modules", "**/*.test.ts"]
}
12 changes: 0 additions & 12 deletions vite.config.mts

This file was deleted.

17 changes: 17 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { defineConfig } from "vitest/config";

export default defineConfig({
test: {
coverage: {
exclude: [
"demo",
".idea",
"dist",
"*.config.js",
"*.config.ts",
"node_modules",
"src/zod-types.ts",
],
},
},
});

0 comments on commit 0b0bf8d

Please sign in to comment.