---
title: "compile(): function in Authoring, TypeShade API reference"
description: "compile, a function in Authoring. Compile TypeShade TypeScript source into the module and generated shader outputs."
url: https://typeshade.dev/api/compile/
language: en
---

# `compile()`

Function in Authoring

Compile TypeShade TypeScript source into the module and generated shader outputs.

`import { compile } from 'typeshade'`

The signature, the description and the examples come from the compiler's own source at commit af54ddad5.

## Syntax

```ts
function compile(source: string, options: CompileOptions = {}): CompileResult
```

## Parameters

**`source` `string`**

**`options` optional `CompileOptions`**

## Return value

`CompileResult`

## Description

The source must carry the `"use typeshade"` directive (`compileTsSource`’s `requireDirective` default): without it the result has one `MISSING_DIRECTIVE` error and nothing else. When the front end reports an error the backends are not run at all, so `wgsl` and `glsl` are `undefined` and `eval` throws; a partial module is never packed into shader text. When the front end reports no error, a WGSL emitter that throws anyway (a literal the target cannot spell, an IR the emitter rejects) is reported as one `BACKEND` error diagnostic rather than an exception, and `wgsl` and `glsl` are again `undefined`. GLSL is attempted for every module whose WGSL exists; when the GLSL emitter throws on a module with a render entry (a binding whose GLSL lowering is not supported yet, [Rule 10.5](https://typeshade.dev/reference/rules/10-5/)) the module has still compiled: `wgsl` stays, `glsl` is `undefined`, and the throw is one `BACKEND` diagnostic with category `warning`. A compute entry beside a render entry runs on WebGL2 as its pass program (change 0054, [Rule 10.3](https://typeshade.dev/reference/rules/10-3/)), so `glsl` holds what the render entries reach, and a compute entry with no pass program is one more `BACKEND` warning. A compute-only module gets `glsl: undefined` with no diagnostic, since it has no vertex or fragment program to miss. See `CompileResult` for each field.

`options.fileName` names the source; see [`CompileOptions.fileName`](https://typeshade.dev/api/compileoptions/) for when the default placeholder is not good enough.

## In the guide

- [Language guide](https://typeshade.dev/guide/internals/)
- [Your first shader](https://typeshade.dev/guide/internals/your-first-shader/)
- [Layouts and resources](https://typeshade.dev/guide/internals/layouts-and-resources/)
- [Emitting and reflection](https://typeshade.dev/guide/internals/emitting-and-reflection/)
- [The CPU oracle](https://typeshade.dev/guide/internals/the-cpu-oracle/)
- [Diagnostics](https://typeshade.dev/guide/internals/diagnostics/)
- [Production emit](https://typeshade.dev/guide/internals/production-emit/)
- [Migrating a GLSL shader](https://typeshade.dev/guide/internals/migrating-a-glsl-shader/)

## See also

- [`CompileOptions`](https://typeshade.dev/api/compileoptions/)

## Source

[`src/compiler/ts/compile.ts`, line 185](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/compile.ts#L185), at commit af54ddad5
