{"id":14990836,"url":"https://github.com/pluvial/vite-plugin-zig","last_synced_at":"2025-04-12T03:24:22.977Z","repository":{"id":57393244,"uuid":"454411051","full_name":"pluvial/vite-plugin-zig","owner":"pluvial","description":"Import Wasm modules compiled from Zig files","archived":false,"fork":false,"pushed_at":"2024-06-18T11:57:49.000Z","size":99,"stargazers_count":24,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-25T23:01:39.571Z","etag":null,"topics":["rollup","rollup-plugin","vite-plugin","vitejs","wasm","zig","ziglang"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pluvial.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-02-01T14:05:44.000Z","updated_at":"2024-11-29T00:44:28.000Z","dependencies_parsed_at":"2024-06-18T13:38:21.569Z","dependency_job_id":null,"html_url":"https://github.com/pluvial/vite-plugin-zig","commit_stats":{"total_commits":20,"total_committers":2,"mean_commits":10.0,"dds":"0.050000000000000044","last_synced_commit":"7e4f25f76895305ed8b7dbdfd981134804125b33"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pluvial%2Fvite-plugin-zig","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pluvial%2Fvite-plugin-zig/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pluvial%2Fvite-plugin-zig/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pluvial%2Fvite-plugin-zig/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pluvial","download_url":"https://codeload.github.com/pluvial/vite-plugin-zig/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248236300,"owners_count":21070001,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["rollup","rollup-plugin","vite-plugin","vitejs","wasm","zig","ziglang"],"created_at":"2024-09-24T14:20:56.476Z","updated_at":"2025-04-12T03:24:22.954Z","avatar_url":"https://github.com/pluvial.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# vite-plugin-zig\n\nImport WebAssembly modules compiled from Zig files.\n\n## Prerequisites\n\n- Install the [Zig compiler](https://ziglang.org): the binary can be downloaded from [downloads page](https://ziglang.org/download), or built from source by following the [GitHub Wiki instructions](https://github.com/ziglang/zig/wiki/Building-Zig-From-Source), or using the [zig-bootstrap](https://github.com/ziglang/zig-bootstrap) scripts. As an alternative, the [`@ziglang/cli`](https://github.com/pluvial/node-zig/tree/main/packages/cli) npm package can be added as a dependency, useful in a CI environment for instance.\n\n## Usage\n\nInstall with `npm i -D vite-plugin-zig` (or `pnpm i -D` or `yarn i -D`), then add the plugin to your `vite.config.js`:\n\n```js\n// vite.config.js\nimport zig from 'vite-plugin-zig';\n\n/** @type {import('vite').UserConfig} */\nexport default {\n  plugins: [zig()],\n  build: { target: 'esnext' },\n};\n```\n\nWrite your Zig code and `export` any symbol to be used in JS code:\n\n```zig\n// src/main.zig\nexport fn add(a: i32, b: i32) i32 {\n    return a + b;\n}\n```\n\nIf available, top-level await can be used so that importing the module feels similar to importing a regular JS module:\n\n```js\n// example.js\nimport { instantiate } from './src/main.zig';\n\n// pass any custom importObject here, functions should be declared\n// as extern in the Zig file\nconst importObject = {\n  // ...\n};\n// instantiate the compiled WebAssembly module, can also be moved\n// to a Worker for instantiation in another thread\nconst { exports, instance } = await instantiate(importObject);\n// call exported functions from the exports object\nconsole.log(exports.add(5, 37)); // 42\n```\n\nAs a shorthand to avoid having to manually call `await instantiate()`, the `?instantiate` query parameter can be specified in the module import to both compile and instantiate the module at import time, allowing access to `instance` and `exports`:\n\n```js\nimport { exports, instance, module } from './src/main.zig?instantiate';\n\n// call exported functions from the exports object\nconsole.log(exports.add(5, 37)); // 42\n```\n\nIf your Vite config does not allow for top-level await (by setting `build: { target: 'esnext' }`, e.g. if the framework you're using enforces a specific target value), an alternative API is provided which instead exposes Promises (`compiled` and `instantiated` respectively depending on whether `?instantiate` is used) which resolve when the compilation or instantiation of the module are complete:\n\n```js\n// example.js\nimport { compiled, instantiate, module } from './src/main.zig';\n\n(async () =\u003e {\n  // `await compiled` can be used to populate the `module` import\n  // manually before instantiation if necessary\n\n  // pass any custom importObject here, functions should be declared\n  // as extern in the Zig file\n  const importObject = {\n    // ...\n  };\n  // instantiate the compiled WebAssembly module, can also be moved\n  // to a Worker for instantiation in another thread\n  const { exports, instance } = await instantiate(importObject);\n  // call exported functions from the exports object\n  console.log(exports.add(5, 37)); // 42\n})();\n```\n\n```js\n// example.js\nimport {\n  exports,\n  instance,\n  instantiated,\n  module,\n} from './src/main.zig?instantiate';\n\n(async () =\u003e {\n  // manually await to populate the imports\n  await instantiated;\n  // call exported functions from the exports object\n  console.log(exports.add(5, 37)); // 42\n})();\n```\n\nTo integrate with SSR frameworks such as SvelteKit, use a dynamic import:\n\n```svelte\n\u003cscript\u003e\n  import { onMount } from 'svelte';\n\n  onMount(async () =\u003e {\n    const wasm = await import('$lib/main.zig?instantiate');\n    await wasm.instantiated;\n    console.log(wasm.exports.add(5, 37)); // 42\n  });\n\u003c/script\u003e\n```\n\n## Notes and TODOs\n\n- It would be great to have something similar to Rust's `wasm-bindgen` to generate JS glue code and type definitions\n\n## License\n\n[MIT](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpluvial%2Fvite-plugin-zig","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpluvial%2Fvite-plugin-zig","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpluvial%2Fvite-plugin-zig/lists"}