Skip to content

Commit

Permalink
feat: sdk build with wasm (#421)
Browse files Browse the repository at this point in the history
  • Loading branch information
zifeo authored Oct 2, 2023
1 parent 44a73a0 commit cda19ec
Show file tree
Hide file tree
Showing 26 changed files with 396 additions and 411 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,8 @@ jobs:
npm install --global @bytecodealliance/jco@$JCO_VERSION
cargo build -p typegraph_core --target wasm32-unknown-unknown -F wasm
wasm-tools component new target/wasm32-unknown-unknown/debug/typegraph_core.wasm -o $WASM_FILE
rm -rf typegraph/deno/gen
jco transpile $WASM_FILE -o typegraph/deno/gen --no-nodejs-compat --map "*=../src/imports.ts"
rm -rf typegraph/deno/src/gen
jco transpile $WASM_FILE -o typegraph/deno/src/gen --map "*=../imports.ts"
deno run -A typegraph/deno/dev/fix-declarations.ts
rm -rf typegraph/python_next/typegraph_next/gen
poetry run python -m wasmtime.bindgen $WASM_FILE --out-dir typegraph/python_next/typegraph_next/gen
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ tmp/
coverage*

typegraph/node
typegraph/deno/gen
typegraph/deno/src/gen
typegraph/python_next/typegraph_next/gen

*.egg-info/
53 changes: 44 additions & 9 deletions dev/publish.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,54 @@
// Copyright Metatype OÜ, licensed under the Elastic License 2.0.
// SPDX-License-Identifier: Elastic-2.0

import { dnt, resolve } from "./deps.ts";
import { dnt, expandGlobSync, resolve } from "./deps.ts";
import { getLockfile, projectDir } from "./utils.ts";

// Direct node users need to use module
// module = Node16 or moduleResolution = Node16 inside tsconfig.json seems to be enough
// https://github.com/denoland/dnt/issues/205

const lockfile = await getLockfile();

const srcDir = resolve(projectDir, "./typegraph/deno/src");
const outDir = resolve(projectDir, "./typegraph/node");
await dnt.emptyDir(outDir);

const entryPoints: dnt.BuildOptions["entryPoints"] = ["./mod.ts"];
for (
const { name, path } of expandGlobSync("./**/*.ts", {
root: srcDir,
includeDirs: false,
globstar: true,
})
) {
const relPath = path.replace(srcDir, ".");
if (
!relPath.startsWith("./gen") && !name.startsWith("_") && name !== "mod.ts"
) {
entryPoints.push({
name: relPath.slice(0, -3),
path: relPath,
});
}
}

// runs in cwd: https://github.com/denoland/dnt/issues/337
await dnt.build({
entryPoints: [resolve(projectDir, "./typegraph/deno/src/mod.ts")],
entryPoints,
outDir,
shims: {
deno: true,
},
scriptModule: false,
typeCheck: false,
compilerOptions: {
target: "ES2020",
},
test: false,
scriptModule: false, // only generate ESM
typeCheck: "single",
packageManager: "pnpm",
package: {
name: "@metatypedev/typegraph",
name: "@typegraph/sdk",
version: "0.0.1", //lockfile.dev.lock.METATYPE_VERSION,
description: lockfile.dev.lock.TAGLINE,
license: "MPL-2.0",
Expand All @@ -32,9 +61,15 @@ await dnt.build({
},
},
postBuild() {
Deno.copyFileSync(
resolve(projectDir, "./dev/LICENSE-MPL-2.0.md"),
resolve(outDir, "./LICENSE.md"),
);
const wasm = "./typegraph/deno/src/gen/typegraph_core.core.wasm";
const files = [
["./dev/LICENSE-MPL-2.0.md", "./LICENSE.md"],
[wasm, "./esm/gen/typegraph_core.core.wasm"],
];
for (const [src, dest] of files) {
Deno.copyFileSync(resolve(projectDir, src), resolve(outDir, dest));
}
// remove source (comment when debugging)
Deno.removeSync(resolve(outDir, "src"), { recursive: true });
},
});
34 changes: 15 additions & 19 deletions examples/templates/node/api.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
import * as meta from "@metatypedev/typegraph/esm/src/mod.ts";

console.log(meta);

/*
import { Policy, t, typegraph } from "@typegraph/deno/src/mod.ts";
import { DenoRuntime } from "@metatypedev/typegraph/deno/src/runtimes/deno.ts";
import { PythonRuntime } from "@typegraph/deno/src/runtimes/python.ts";
import { Policy, t, typegraph } from "@metatypedev/typegraph";
import { DenoRuntime } from "@metatypedev/typegraph/runtimes/deno";
import { PythonRuntime } from "@metatypedev/typegraph/runtimes/python";

typegraph("test-multiple-runtimes", (g) => {
const pub = Policy.public();
const deno = new DenoRuntime();
const python = new PythonRuntime();

g.expose({
add: t.func(
t.struct({ "first": t.float(), "second": t.float() }),
t.float(),
python.fromLambda("lambda x: x['first'] + x['second']"),
).withPolicy(pub),
multiply: deno.func(
t.struct({ "first": t.float(), "second": t.float() }),
t.float(),
{ code: "({first, second}) => first * second" },
).withPolicy(pub),
add: t
.func(
t.struct({ first: t.float(), second: t.float() }),
t.float(),
python.fromLambda("lambda x: x['first'] + x['second']")
)
.withPolicy(pub),
multiply: deno
.func(t.struct({ first: t.float(), second: t.float() }), t.float(), {
code: "({first, second}) => first * second",
})
.withPolicy(pub),
});
});
*/
13 changes: 5 additions & 8 deletions examples/templates/node/package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
{
"name": "example",
"type": "module",
"version": "1.0.0",
"description": "",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "ts-node api.ts"
"start": "tsx api.ts"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@metatypedev/typegraph": "^0.0.1"
"@metatypedev/typegraph": "^0.0.3"
},
"devDependencies": {
"ts-node": "^10.9.1"
"tsx": "^3.13.0",
"typescript": "^5.2.2"
}
}
Loading

0 comments on commit cda19ec

Please sign in to comment.