---
title: compile
description: Converts one raw runtime WGSL string into the data-only `ResolvedShader` shape used by `@vgpu/wgsl`. Use it when the shader source is already a complete WGSL entry module and does not need import resolution.
---

# compile



## Import

```ts
import { compile } from "@vgpu/wgsl";
```

## Signature

```ts
import type { ResolvedShader } from "@vgpu/wgsl";

declare function compile(wgsl: string): ResolvedShader;
```

## Parameters

| Param | Type   | Required | Default | Notes                                                                                                                                                                                                 |
| ----- | ------ | -------- | ------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| wgsl  | string | ✔        | —       | Complete WGSL source for one runtime shader. It is copied byte-for-byte to `resolved.wgsl`. Top-level `import` syntax is rejected; use `resolveShader` or a build-time loader for WGSL import graphs. |

**Returns:** `ResolvedShader` — a data object with `kind: "wgsl"`, the original `wgsl`, passthrough source/AST/source-map metadata, deterministic `cacheKey`, detected `entryPoints`, and source `stats`.

**Throws:** `VGPU-WGSL-RUNTIME-IMPORT` when the trimmed source starts with a top-level `import` statement after comments are stripped — remove the import, pre-resolve with `resolveShader`, or use the Vite/webpack loader.

## Examples

```ts
import { compile } from "@vgpu/wgsl";

const shader = compile(`
@compute @workgroup_size(1)
fn main() {
}
`);

shader.kind satisfies "wgsl";
shader.entryPoints satisfies readonly string[];
console.log(shader.entryPoints.includes("main"));
```

```ts
import { compile } from "@vgpu/wgsl";

try {
  compile(`import { helper } from "./helper.wgsl";`);
} catch (error) {
  if (error instanceof Error && "code" in error) {
    console.log(error.code === "VGPU-WGSL-RUNTIME-IMPORT");
  }
}
```

## Notes

* `compile()` does not read files, resolve packages, mangle imported symbols, reflect binding layouts, or validate WGSL semantics.
* The only syntax check is import rejection. Invalid WGSL without `import` still returns a `ResolvedShader`; WebGPU validation happens later when a shader module is created.
* `stats.bindGroups` is `0` in this runtime passthrough shape. Do not use it as reflection for resource bindings.
* Runtime WGSL modules with resources are allowed because there is no import graph. For imported WGSL modules, keep modules pure and declare every `@group/@binding` resource in the entry module.
* **See also:** `ResolvedShader`, `ShaderSource`, `resolveShader`.


---

For a semantic overview of all documentation, see [/sitemap.md](/sitemap.md)

For an index of all available documentation, see [/llms.txt](/llms.txt)

For agent-facing discovery, including API and MCP surfaces, see [/agents.md](/agents.md)