EmitPlugin
Interface in Emit
A transform that runs inside module emit.
import type { EmitPlugin } from 'typeshade'
The signature, the description and the examples come from the compiler's own source at commit 26de7be8.
Syntax
interface EmitPlugin { readonly name: string; readonly identity?: string; readonly transformIR?: (lowered: ModuleDecl) => ModuleDecl; readonly transformText?: (code: string) => string;}Description
Plugins are passed to emitModule and
emitGlslModule through EmitOptions as { plugins: [...] }. The core emit knows
nothing about what a plugin does. The plugins shipped with the package (mangle, minify,
prune, obfuscate and others) live on the typeshade/emit-prod subpath, so an
application that emits at runtime and never imports them does not bundle them.
A plugin has two hooks, both optional. transformIR receives the module after every
lowering and optimisation pass has run, and returns a module the backend can spell into
source text. transformText receives the assembled source string and returns the string
to use in its place.
The hooks fire in stages across all plugins: every plugin’s transformIR runs, in plugins
order, before the module is assembled; then every plugin’s transformText runs, in
plugins order, on the assembled string.
Examples
Example
const banner: EmitPlugin = { name: 'banner', transformText: (code) => `// generated\n${code}`,}const wgsl = emitModule(MODULE, { plugins: [banner] })Instance properties
nameread onlystringThe plugin’s name, used in error messages and diagnostics.
identityoptionalread onlystringHow this plugin appears in the identity string that
emitIdentitycomputes for an emit configuration. When it is unset,nameis used. Set it when the plugin’s own options change the emitted bytes, so that two configurations of the same plugin produce different identities:minify({a})andminify({b})would otherwise be the same string. None of the plugins shipped with the package set it.transformIRoptionalread only(lowered: ModuleDecl) => ModuleDeclRewrites the module after every lowering and optimisation pass. The result must be deterministic for a given module: the GLSL vertex and fragment stages are emitted by separate calls that must agree on every shared name.
transformTextoptionalread only(code: string) => stringRewrites the assembled source string.
In the guide
See also
Source
src/core/emit.ts, line 664, at commit 26de7be8