wesl-plugin 0.6.2 → 0.6.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/dist/PluginExtension-B9OlwcsY.d.ts +511 -0
  2. package/dist/WeslPlugin-DjY8zodn.js +6137 -0
  3. package/dist/pluginIndex.d.ts +7 -4
  4. package/dist/pluginIndex.js +71 -62
  5. package/dist/plugins/astro.d.ts +4 -4
  6. package/dist/plugins/astro.js +10 -15
  7. package/dist/plugins/esbuild.d.ts +6 -6
  8. package/dist/plugins/esbuild.js +6 -9
  9. package/dist/plugins/farm.d.ts +6 -6
  10. package/dist/plugins/farm.js +6 -9
  11. package/dist/plugins/nuxt.d.ts +7 -8
  12. package/dist/plugins/nuxt.js +17 -24
  13. package/dist/plugins/rollup.d.ts +6 -6
  14. package/dist/plugins/rollup.js +6 -9
  15. package/dist/plugins/rspack.d.ts +5 -5
  16. package/dist/plugins/rspack.js +6 -9
  17. package/dist/plugins/vite.d.ts +6 -6
  18. package/dist/plugins/vite.js +4 -8
  19. package/dist/plugins/webpack.d.ts +6 -6
  20. package/dist/plugins/webpack.js +4 -8
  21. package/dist/vite-Bx_gff8v.js +8 -0
  22. package/dist/webpack-Bq0QiCAt.js +8 -0
  23. package/package.json +11 -10
  24. package/src/BindingLayoutExtension.ts +4 -4
  25. package/src/PluginApi.ts +201 -0
  26. package/src/PluginExtension.ts +3 -2
  27. package/src/WeslPlugin.ts +17 -198
  28. package/src/WeslPluginOptions.ts +1 -1
  29. package/src/defaultSuffixTypes.d.ts +1 -1
  30. package/src/extensions/LinkExtension.ts +9 -6
  31. package/src/extensions/StaticExtension.ts +9 -5
  32. package/src/plugins/nuxt.ts +1 -1
  33. package/dist/PluginExtension-B_oge2T3.d.ts +0 -48
  34. package/dist/WeslPluginOptions-ZzGgegv3.d.ts +0 -8
  35. package/dist/chunk-3YZCYXZ7.js +0 -11
  36. package/dist/chunk-JSBRDJBE.js +0 -30
  37. package/dist/chunk-LHNI4JI3.js +0 -6820
  38. package/dist/chunk-ZK6IKQOJ.js +0 -11
@@ -0,0 +1,511 @@
1
+ import "unplugin";
2
+ import { Span } from "mini-parse";
3
+
4
+ //#region ../wesl/src/LiveDeclarations.d.ts
5
+ /** decls currently visible in this scope */
6
+ /** decls currently visible in this scope */
7
+ interface LiveDecls {
8
+ /** decls currently visible in this scope */
9
+ decls: Map<string, DeclIdent>;
10
+ /** live decls in the parent scope. null for the modue root scope */
11
+ parent?: LiveDecls | null;
12
+ }
13
+
14
+ //#endregion
15
+ //#region ../wesl/src/ParseWESL.d.ts
16
+ /** create a LiveDecls */
17
+
18
+ /** result of a parse for one wesl module (e.g. one .wesl file)
19
+ *
20
+ * The parser constructs the AST constructed into three sections
21
+ * for convenient access by the binding stage.
22
+ * - import statements
23
+ * - language elements (fn, struct, etc)
24
+ * - scopes
25
+ *
26
+ */
27
+ interface WeslAST {
28
+ /** source text for this module */
29
+ srcModule: SrcModule;
30
+ /** root module element */
31
+ moduleElem: ModuleElem;
32
+ /** root scope for this module */
33
+ rootScope: Scope;
34
+ /** imports found in this module */
35
+ imports: ImportStatement[];
36
+ /** module level const_assert statements */
37
+ moduleAsserts?: ConstAssertElem[];
38
+ }
39
+
40
+ //#endregion
41
+ //#region ../wesl/src/Scope.d.ts
42
+ /** an extended version of the AST */
43
+ interface SrcModule {
44
+ /** module path "rand_pkg::sub::foo", or "package::main" */
45
+ modulePath: string;
46
+ /** file path to the module for user error reporting e.g "rand_pkg:sub/foo.wesl", or "./sub/foo.wesl" */
47
+ debugFilePath: string;
48
+ /** original src for module */
49
+ src: string;
50
+ }
51
+ /** a src declaration or reference to an ident */
52
+ type Ident = DeclIdent | RefIdent;
53
+ /** LATER change this to a Map, so that `toString` isn't accidentally a condition */
54
+
55
+ interface IdentBase {
56
+ originalName: string;
57
+ id?: number;
58
+ }
59
+ interface RefIdent extends IdentBase {
60
+ kind: "ref";
61
+ refersTo?: Ident;
62
+ std?: true;
63
+ ast: WeslAST;
64
+ refIdentElem: RefIdentElem;
65
+ }
66
+ interface DeclIdent extends IdentBase {
67
+ kind: "decl";
68
+ /** name in the output code */
69
+ mangledName?: string;
70
+ /** link to AST so that we can traverse scopes and know what elems to emit */
71
+ declElem?: DeclarationElem;
72
+ /** scope in which this declaration is found */
73
+ containingScope: Scope;
74
+ /** scope for the references within this declaration
75
+ * (only needed for global decls.)
76
+ * if this decl is included in the link, dependentScope holds other refIdents that should be included too */
77
+ dependentScope?: Scope;
78
+ /** true if this is a global declaration (e.g. not a local variable) */
79
+ isGlobal: boolean;
80
+ /** To figure out which module this declaration is from. */
81
+ srcModule: SrcModule;
82
+ }
83
+ /** tree of ident references, organized by lexical scope and partialScope . */
84
+ type Scope = LexicalScope | PartialScope;
85
+ /** A wgsl scope */
86
+ interface LexicalScope extends ScopeBase {
87
+ kind: "scope";
88
+ /** @if condition for conditionally translating this scope */
89
+ ifAttribute?: IfAttribute;
90
+ /**
91
+ * Efficient access to declarations in this scope.
92
+ * constructed on demand, for module root scopes only */
93
+ _scopeDecls?: LiveDecls;
94
+ }
95
+ /** A synthetic partial scope to contain @if conditioned idents.
96
+ * PartialScope idents are considered to be in the wgsl lexical scope of their parent. */
97
+ interface PartialScope extends ScopeBase {
98
+ kind: "partial";
99
+ /** @if condition for conditionally translating this scope */
100
+ ifAttribute?: IfAttribute;
101
+ }
102
+ /** common scope elements */
103
+ interface ScopeBase {
104
+ /** id for debugging */
105
+ id: number;
106
+ /** null for root scope in a module */
107
+ parent: Scope | null;
108
+ contents: (Ident | Scope)[];
109
+ /** @if conditions for conditionally translating this scope */
110
+ ifAttribute?: IfAttribute;
111
+ }
112
+
113
+ //#endregion
114
+ //#region ../wesl/src/AbstractElems.d.ts
115
+ /** Combine two scope siblings.
116
+ * The first scope is mutated to append the contents of the second. */
117
+ /**
118
+ * Structures to describe the 'interesting' parts of a WESL source file.
119
+ *
120
+ * The parts of the source that need to analyze further in the linker
121
+ * are pulled out into these structures.
122
+ *
123
+ * The parts that are uninteresting the the linker are recorded
124
+ * as 'TextElem' nodes, which are generally just copied to the output WGSL
125
+ * along with their containing element.
126
+ */
127
+ type AbstractElem = GrammarElem | SyntheticElem;
128
+ type GrammarElem = ContainerElem | TerminalElem;
129
+ type ContainerElem = AttributeElem | AliasElem | ConstAssertElem | ConstElem | UnknownExpressionElem | SimpleMemberRef | FnElem | TypedDeclElem | GlobalVarElem | LetElem | ModuleElem | OverrideElem | FnParamElem | StructElem | StructMemberElem | StuffElem | TypeRefElem | VarElem | StatementElem | SwitchClauseElem;
130
+ /** Inspired by https://github.com/wgsl-tooling-wg/wesl-rs/blob/3b2434eac1b2ebda9eb8bfb25f43d8600d819872/crates/wgsl-parse/src/syntax.rs#L364 */
131
+ type ExpressionElem = Literal | TranslateTimeFeature | RefIdentElem | ParenthesizedExpression | ComponentExpression | ComponentMemberExpression | UnaryExpression | BinaryExpression | FunctionCallExpression;
132
+ type TerminalElem = DirectiveElem | DeclIdentElem | NameElem | RefIdentElem | TextElem | ImportElem;
133
+ type GlobalDeclarationElem = AliasElem | ConstElem | FnElem | GlobalVarElem | OverrideElem | StructElem;
134
+ type DeclarationElem = GlobalDeclarationElem | FnParamElem | VarElem;
135
+ interface AbstractElemBase {
136
+ kind: AbstractElem["kind"];
137
+ start: number;
138
+ end: number;
139
+ }
140
+ interface ElemWithContentsBase extends AbstractElemBase {
141
+ contents: AbstractElem[];
142
+ }
143
+ interface HasAttributes {
144
+ attributes?: AttributeElem[];
145
+ }
146
+ /**
147
+ * a raw bit of text in WESL source that's typically copied to the linked WGSL.
148
+ * e.g. a keyword like 'var'
149
+ * or a phrase we needn't analyze further like '@diagnostic(off,derivative_uniformity)'
150
+ */
151
+ interface TextElem extends AbstractElemBase {
152
+ kind: "text";
153
+ srcModule: SrcModule;
154
+ }
155
+ /** a name that doesn't need to be an Ident
156
+ * e.g.
157
+ * - a struct member name
158
+ * - a diagnostic rule name
159
+ * - an enable-extension name
160
+ * - an interpolation sampling name
161
+ */
162
+ interface NameElem extends AbstractElemBase {
163
+ kind: "name";
164
+ name: string;
165
+ }
166
+ /** an identifier that 'refers to' a declaration (aka a symbol reference) */
167
+ interface RefIdentElem extends AbstractElemBase {
168
+ kind: RefIdent["kind"];
169
+ ident: RefIdent;
170
+ srcModule: SrcModule;
171
+ }
172
+ /** a declaration identifier (aka a symbol declaration) */
173
+ interface DeclIdentElem extends AbstractElemBase {
174
+ kind: DeclIdent["kind"];
175
+ ident: DeclIdent;
176
+ srcModule: SrcModule;
177
+ }
178
+ /** Holds an import statement, and has a span */
179
+ interface ImportElem extends AbstractElemBase {
180
+ kind: "import";
181
+ imports: ImportStatement;
182
+ }
183
+ /**
184
+ * An import statement, which is tree shaped.
185
+ * `import foo::bar::{baz, cat as neko};
186
+ */
187
+ interface ImportStatement {
188
+ kind: "import-statement";
189
+ segments: ImportSegment[];
190
+ finalSegment: ImportCollection | ImportItem;
191
+ }
192
+ /**
193
+ * A collection of import trees.
194
+ * `{baz, cat as neko}`
195
+ */
196
+ interface ImportSegment {
197
+ kind: "import-segment";
198
+ name: string;
199
+ }
200
+ /**
201
+ * A primitive segment in an import statement.
202
+ * `foo`
203
+ */
204
+ interface ImportCollection {
205
+ kind: "import-collection";
206
+ subtrees: ImportStatement[];
207
+ }
208
+ /**
209
+ * A renamed item at the end of an import statement.
210
+ * `cat as neko`
211
+ */
212
+ interface ImportItem {
213
+ kind: "import-item";
214
+ name: string;
215
+ as?: string;
216
+ }
217
+ /** generated element, produced after parsing and binding */
218
+ interface SyntheticElem {
219
+ kind: "synthetic";
220
+ text: string;
221
+ }
222
+ /** a declaration identifer with a possible type */
223
+ interface TypedDeclElem extends ElemWithContentsBase {
224
+ kind: "typeDecl";
225
+ decl: DeclIdentElem;
226
+ typeRef?: TypeRefElem;
227
+ typeScope?: Scope;
228
+ }
229
+ /** an alias statement */
230
+ interface AliasElem extends ElemWithContentsBase, HasAttributes {
231
+ kind: "alias";
232
+ name: DeclIdentElem;
233
+ typeRef: TypeRefElem;
234
+ }
235
+ /** an attribute like '@compute' or '@binding(0)' */
236
+ interface AttributeElem extends ElemWithContentsBase {
237
+ kind: "attribute";
238
+ attribute: Attribute;
239
+ }
240
+ type Attribute = StandardAttribute | InterpolateAttribute | BuiltinAttribute | DiagnosticAttribute | IfAttribute;
241
+ interface StandardAttribute {
242
+ kind: "@attribute";
243
+ name: string;
244
+ params?: UnknownExpressionElem[];
245
+ }
246
+ interface InterpolateAttribute {
247
+ kind: "@interpolate";
248
+ params: NameElem[];
249
+ }
250
+ interface BuiltinAttribute {
251
+ kind: "@builtin";
252
+ param: NameElem;
253
+ }
254
+ interface DiagnosticAttribute {
255
+ kind: "@diagnostic";
256
+ severity: NameElem;
257
+ rule: [NameElem, NameElem | null];
258
+ }
259
+ interface IfAttribute {
260
+ kind: "@if";
261
+ param: TranslateTimeExpressionElem;
262
+ }
263
+ /** a const_assert statement */
264
+ interface ConstAssertElem extends ElemWithContentsBase, HasAttributes {
265
+ kind: "assert";
266
+ }
267
+ /** a const declaration */
268
+ interface ConstElem extends ElemWithContentsBase, HasAttributes {
269
+ kind: "const";
270
+ name: TypedDeclElem;
271
+ }
272
+ /** an expression w/o special handling, used inside attribute parameters */
273
+ interface UnknownExpressionElem extends ElemWithContentsBase {
274
+ kind: "expression";
275
+ }
276
+ /** an expression that can be safely evaluated at compile time */
277
+ interface TranslateTimeExpressionElem {
278
+ kind: "translate-time-expression";
279
+ expression: ExpressionElem;
280
+ span: Span;
281
+ }
282
+ /** A literal value in WESL source. A boolean or a number. */
283
+ interface Literal {
284
+ kind: "literal";
285
+ value: string;
286
+ span: Span;
287
+ }
288
+ /** `words`s inside `@if` */
289
+ interface TranslateTimeFeature {
290
+ kind: "translate-time-feature";
291
+ name: string;
292
+ span: Span;
293
+ }
294
+ /** (expr) */
295
+ interface ParenthesizedExpression {
296
+ kind: "parenthesized-expression";
297
+ expression: ExpressionElem;
298
+ }
299
+ /** `foo[expr]` */
300
+ interface ComponentExpression {
301
+ kind: "component-expression";
302
+ base: ExpressionElem;
303
+ access: ExpressionElem;
304
+ }
305
+ /** `foo.member` */
306
+ interface ComponentMemberExpression {
307
+ kind: "component-member-expression";
308
+ base: ExpressionElem;
309
+ access: NameElem;
310
+ }
311
+ /** `+foo` */
312
+ interface UnaryExpression {
313
+ kind: "unary-expression";
314
+ operator: UnaryOperator;
315
+ expression: ExpressionElem;
316
+ }
317
+ /** `foo + bar` */
318
+ interface BinaryExpression {
319
+ kind: "binary-expression";
320
+ operator: BinaryOperator;
321
+ left: ExpressionElem;
322
+ right: ExpressionElem;
323
+ }
324
+ /** `foo(arg, arg)` */
325
+ interface FunctionCallExpression {
326
+ kind: "call-expression";
327
+ function: RefIdentElem;
328
+ arguments: ExpressionElem[];
329
+ }
330
+ interface UnaryOperator {
331
+ value: "!" | "&" | "*" | "-" | "~";
332
+ span: Span;
333
+ }
334
+ interface BinaryOperator {
335
+ value: ("||" | "&&" | "+" | "-" | "*" | "/" | "%" | "==") | ("!=" | "<" | "<=" | ">" | ">=" | "|" | "&" | "^") | ("<<" | ">>");
336
+ span: Span;
337
+ }
338
+ type DirectiveVariant = DiagnosticDirective | EnableDirective | RequiresDirective;
339
+ interface DirectiveElem extends AbstractElemBase, HasAttributes {
340
+ kind: "directive";
341
+ directive: DirectiveVariant;
342
+ }
343
+ interface DiagnosticDirective {
344
+ kind: "diagnostic";
345
+ severity: NameElem;
346
+ rule: [NameElem, NameElem | null];
347
+ }
348
+ interface EnableDirective {
349
+ kind: "enable";
350
+ extensions: NameElem[];
351
+ }
352
+ interface RequiresDirective {
353
+ kind: "requires";
354
+ extensions: NameElem[];
355
+ }
356
+ /** a function declaration */
357
+ interface FnElem extends ElemWithContentsBase, HasAttributes {
358
+ kind: "fn";
359
+ name: DeclIdentElem;
360
+ params: FnParamElem[];
361
+ body: StatementElem;
362
+ returnAttributes?: AttributeElem[];
363
+ returnType?: TypeRefElem;
364
+ }
365
+ /** a global variable declaration (at the root level) */
366
+ interface GlobalVarElem extends ElemWithContentsBase, HasAttributes {
367
+ kind: "gvar";
368
+ name: TypedDeclElem;
369
+ }
370
+ /** an entire file */
371
+ interface ModuleElem extends ElemWithContentsBase {
372
+ kind: "module";
373
+ }
374
+ /** an override declaration */
375
+ interface OverrideElem extends ElemWithContentsBase, HasAttributes {
376
+ kind: "override";
377
+ name: TypedDeclElem;
378
+ }
379
+ /** a parameter in a function declaration */
380
+ interface FnParamElem extends ElemWithContentsBase, HasAttributes {
381
+ kind: "param";
382
+ name: TypedDeclElem;
383
+ }
384
+ /** simple references to structures, like myStruct.bar
385
+ * (used for transforming refs to binding structs) */
386
+ interface SimpleMemberRef extends ElemWithContentsBase {
387
+ kind: "memberRef";
388
+ name: RefIdentElem;
389
+ member: NameElem;
390
+ extraComponents?: StuffElem;
391
+ }
392
+ /** a struct declaration */
393
+ interface StructElem extends ElemWithContentsBase, HasAttributes {
394
+ kind: "struct";
395
+ name: DeclIdentElem;
396
+ members: StructMemberElem[];
397
+ bindingStruct?: true;
398
+ }
399
+ /** generic container of other elements */
400
+ interface StuffElem extends ElemWithContentsBase {
401
+ kind: "stuff";
402
+ }
403
+ /** a struct declaration that's been marked as a bindingStruct */
404
+
405
+ /** a member of a struct declaration */
406
+ interface StructMemberElem extends ElemWithContentsBase, HasAttributes {
407
+ kind: "member";
408
+ name: NameElem;
409
+ typeRef: TypeRefElem;
410
+ mangledVarName?: string;
411
+ }
412
+ type TypeTemplateParameter = TypeRefElem | UnknownExpressionElem;
413
+ /** a reference to a type, like 'f32', or 'MyStruct', or 'ptr<storage, array<f32>, read_only>' */
414
+ interface TypeRefElem extends ElemWithContentsBase {
415
+ kind: "type";
416
+ name: RefIdent;
417
+ templateParams?: TypeTemplateParameter[];
418
+ }
419
+ /** a variable declaration */
420
+ interface VarElem extends ElemWithContentsBase, HasAttributes {
421
+ kind: "var";
422
+ name: TypedDeclElem;
423
+ }
424
+ interface LetElem extends ElemWithContentsBase, HasAttributes {
425
+ kind: "let";
426
+ name: TypedDeclElem;
427
+ }
428
+ interface StatementElem extends ElemWithContentsBase, HasAttributes {
429
+ kind: "statement";
430
+ }
431
+ interface SwitchClauseElem extends ElemWithContentsBase, HasAttributes {
432
+ kind: "switch-clause";
433
+ }
434
+
435
+ //#endregion
436
+ //#region ../wesl/src/ParsedRegistry.d.ts
437
+ interface ParsedRegistry {
438
+ modules: Record<string, WeslAST>;
439
+ }
440
+
441
+ //#endregion
442
+ //#region ../wesl/src/Linker.d.ts
443
+ type LinkerTransform = (boundAST: TransformedAST) => TransformedAST;
444
+ interface WeslJsPlugin {
445
+ transform?: LinkerTransform;
446
+ }
447
+ interface TransformedAST extends Pick<WeslAST, "srcModule" | "moduleElem"> {
448
+ globalNames: Set<string>;
449
+ notableElems: Record<string, AbstractElem[]>;
450
+ }
451
+
452
+ //#endregion
453
+ //#region src/WeslPluginOptions.d.ts
454
+ interface WeslPluginOptions {
455
+ weslToml?: string;
456
+ extensions?: PluginExtension[];
457
+ }
458
+
459
+ //#endregion
460
+ //#region src/WeslPlugin.d.ts
461
+ /** loaded (or synthesized) info from .toml */
462
+ interface WeslToml {
463
+ /** glob search strings to find .wesl/.wgsl files. Relative to the toml directory. */
464
+ weslFiles: string[];
465
+ /** base directory for wesl files. Relative to the toml directory. */
466
+ weslRoot: string;
467
+ /** names of directly referenced wesl shader packages (e.g. npm package names) */
468
+ dependencies?: string[];
469
+ }
470
+ interface WeslTomlInfo {
471
+ /** The path to the toml file, relative to the cwd, undefined if no toml file */
472
+ tomlFile: string | undefined;
473
+ /** The absolute path to the directory that contains the toml.
474
+ * Paths inside the toml are relative to this. */
475
+ tomlDir: string;
476
+ /** The wesl root, relative to the cwd.
477
+ * This lets us correctly do `path.resolve(resolvedWeslRoot, someShaderFile)` */
478
+ resolvedWeslRoot: string;
479
+ /** The underlying toml file */
480
+ toml: WeslToml;
481
+ }
482
+
483
+ //#endregion
484
+ //#region src/PluginExtension.d.ts
485
+ /** internal cache used by the plugin to avoid reloading files
486
+ * The assumption is that the plugin is used for a single wesl.toml and set of shaders
487
+ * (a plugin instance supports only one shader project)
488
+ */
489
+ /** function type required for for emit extensions */
490
+ type ExtensionEmitFn = (/** absolute path to the shader to which the extension is attached */
491
+ shaderPath: string, /** support functions available to plugin extensions */
492
+ pluginApi: PluginExtensionApi, /** static conditions specified on the js import */conditions?: Record<string, boolean>) => Promise<string>;
493
+ /** an extension that runs inside the wesl-js build plugin */
494
+ interface PluginExtension extends WeslJsPlugin {
495
+ /** javascript imports with this suffix will trigger the plugin */
496
+ extensionName: string;
497
+ /** generate javascript text for js/ts importers to use.
498
+ * e.g. import myPluginJs from "./foo.wesl?myPlugin"; */
499
+ emitFn: ExtensionEmitFn;
500
+ }
501
+ /** api supplied to plugin extensions */
502
+ interface PluginExtensionApi {
503
+ weslToml: () => Promise<WeslTomlInfo>;
504
+ weslSrc: () => Promise<Record<string, string>>;
505
+ weslRegistry: () => Promise<ParsedRegistry>;
506
+ weslMain: (baseId: string) => Promise<string>;
507
+ weslDependencies: () => Promise<string[]>;
508
+ }
509
+
510
+ //#endregion
511
+ export { ExtensionEmitFn, PluginExtension, PluginExtensionApi, WeslPluginOptions };