---
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`, lexically 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.
* `entryPoints` contains the names of top-level `fn` declarations carrying an exact `@vertex`, `@fragment`, or `@compute` attribute, in source order. Other declaration attributes may appear before or after the stage attribute. Comments and function bodies are ignored, and Unicode XID names are preserved without normalization.
* Entry-point detection is intentionally lexical rather than semantic. It does not validate stage signatures, workgroup sizes, duplicate attributes, or any other WGSL rule. In incomplete source, an unterminated block comment or attribute argument ends extraction without throwing; names confirmed earlier are retained.
* `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.
* `compile()` output is not a renderer input: a `ResolvedShader` has no artifact `version`, so `draw`, `effect`, and `compute` reject it with `VGPU-SHADER-SOURCE-INVALID`. Pass `prepareShader(resolved.wgsl)` from `@vgpu/wgsl/prepare` instead, or import the file through a loader.
* **See also:** `ResolvedShader`, `ShaderSource`, `prepareShader` (`@vgpu/wgsl/prepare`), `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)