shaderkit 0.0.2 → 0.1.1
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.
- package/README.md +183 -1
- package/dist/index.js +563 -122
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +563 -122
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -3
- package/src/constants.ts +496 -19
- package/src/minifier.ts +39 -72
- package/src/tokenizer.ts +53 -44
package/dist/index.mjs
CHANGED
|
@@ -1,3 +1,457 @@
|
|
|
1
|
+
const WGSL_KEYWORDS = [
|
|
2
|
+
// 5.2.6 Vector Types
|
|
3
|
+
"vec2",
|
|
4
|
+
"vec2i",
|
|
5
|
+
"vec2u",
|
|
6
|
+
"vec2f",
|
|
7
|
+
"vec2h",
|
|
8
|
+
"vec3",
|
|
9
|
+
"vec3i",
|
|
10
|
+
"vec3u",
|
|
11
|
+
"vec3f",
|
|
12
|
+
"vec3h",
|
|
13
|
+
"vec4",
|
|
14
|
+
"vec4i",
|
|
15
|
+
"vec4u",
|
|
16
|
+
"vec4f",
|
|
17
|
+
"vec4h",
|
|
18
|
+
// 5.2.7 Matrix Types
|
|
19
|
+
"mat2x2",
|
|
20
|
+
"mat2x2f",
|
|
21
|
+
"mat2x2h",
|
|
22
|
+
"mat2x3",
|
|
23
|
+
"mat2x3f",
|
|
24
|
+
"mat2x3h",
|
|
25
|
+
"mat2x4",
|
|
26
|
+
"mat2x4f",
|
|
27
|
+
"mat2x4h",
|
|
28
|
+
"mat3x2",
|
|
29
|
+
"mat3x2f",
|
|
30
|
+
"mat3x2h",
|
|
31
|
+
"mat3x3",
|
|
32
|
+
"mat3x3f",
|
|
33
|
+
"mat3x3h",
|
|
34
|
+
"mat3x4",
|
|
35
|
+
"mat3x4f",
|
|
36
|
+
"mat3x4h",
|
|
37
|
+
"mat4x2",
|
|
38
|
+
"mat4x2f",
|
|
39
|
+
"mat4x2h",
|
|
40
|
+
"mat4x3",
|
|
41
|
+
"mat4x3f",
|
|
42
|
+
"mat4x3h",
|
|
43
|
+
"mat4x4",
|
|
44
|
+
"mat4x4f",
|
|
45
|
+
"mat4x4h",
|
|
46
|
+
// 15.1.1 Type-defining Keywords
|
|
47
|
+
"array",
|
|
48
|
+
"atomic",
|
|
49
|
+
"bool",
|
|
50
|
+
"f32",
|
|
51
|
+
"f16",
|
|
52
|
+
"i32",
|
|
53
|
+
// 'mat2x2',
|
|
54
|
+
// 'mat2x3',
|
|
55
|
+
// 'mat2x4',
|
|
56
|
+
// 'mat3x2',
|
|
57
|
+
// 'mat3x3',
|
|
58
|
+
// 'mat3x4',
|
|
59
|
+
// 'mat4x2',
|
|
60
|
+
// 'mat4x3',
|
|
61
|
+
// 'mat4x4',
|
|
62
|
+
"ptr",
|
|
63
|
+
"sampler",
|
|
64
|
+
"sampler_comparison",
|
|
65
|
+
"texture_1d",
|
|
66
|
+
"texture_2d",
|
|
67
|
+
"texture_2d_array",
|
|
68
|
+
"texture_3d",
|
|
69
|
+
"texture_cube",
|
|
70
|
+
"texture_cube_array",
|
|
71
|
+
"texture_multisampled_2d",
|
|
72
|
+
"texture_storage_1d",
|
|
73
|
+
"texture_storage_2d",
|
|
74
|
+
"texture_storage_2d_array",
|
|
75
|
+
"texture_storage_3d",
|
|
76
|
+
"texture_depth_2d",
|
|
77
|
+
"texture_depth_2d_array",
|
|
78
|
+
"texture_depth_cube",
|
|
79
|
+
"texture_depth_cube_array",
|
|
80
|
+
"texture_depth_multisampled_2d",
|
|
81
|
+
"u32",
|
|
82
|
+
// 'vec2',
|
|
83
|
+
// 'vec3',
|
|
84
|
+
// 'vec4',
|
|
85
|
+
// 15.1.2 Other Keywords
|
|
86
|
+
"alias",
|
|
87
|
+
"bitcast",
|
|
88
|
+
"break",
|
|
89
|
+
"case",
|
|
90
|
+
"const",
|
|
91
|
+
"const_assert",
|
|
92
|
+
"continue",
|
|
93
|
+
"continuing",
|
|
94
|
+
"default",
|
|
95
|
+
"discard",
|
|
96
|
+
"else",
|
|
97
|
+
"enable",
|
|
98
|
+
"false",
|
|
99
|
+
"fn",
|
|
100
|
+
"for",
|
|
101
|
+
"if",
|
|
102
|
+
"let",
|
|
103
|
+
"loop",
|
|
104
|
+
"override",
|
|
105
|
+
"return",
|
|
106
|
+
"struct",
|
|
107
|
+
"switch",
|
|
108
|
+
"true",
|
|
109
|
+
"var",
|
|
110
|
+
"while",
|
|
111
|
+
// 15.2 Reserved Words
|
|
112
|
+
"Hullshader",
|
|
113
|
+
"NULL",
|
|
114
|
+
"Self",
|
|
115
|
+
"abstract",
|
|
116
|
+
"active",
|
|
117
|
+
"alignas",
|
|
118
|
+
"alignof",
|
|
119
|
+
"as",
|
|
120
|
+
"asm",
|
|
121
|
+
"asm_fragment",
|
|
122
|
+
"async",
|
|
123
|
+
"attribute",
|
|
124
|
+
"auto",
|
|
125
|
+
"await",
|
|
126
|
+
"become",
|
|
127
|
+
"bf16",
|
|
128
|
+
"binding_array",
|
|
129
|
+
"cast",
|
|
130
|
+
"catch",
|
|
131
|
+
"class",
|
|
132
|
+
"co_await",
|
|
133
|
+
"co_return",
|
|
134
|
+
"co_yield",
|
|
135
|
+
"coherent",
|
|
136
|
+
"column_major",
|
|
137
|
+
"common",
|
|
138
|
+
"compile",
|
|
139
|
+
"compile_fragment",
|
|
140
|
+
"concept",
|
|
141
|
+
"const_cast",
|
|
142
|
+
"consteval",
|
|
143
|
+
"constexpr",
|
|
144
|
+
"constinit",
|
|
145
|
+
"crate",
|
|
146
|
+
"debugger",
|
|
147
|
+
"decltype",
|
|
148
|
+
"delete",
|
|
149
|
+
"demote",
|
|
150
|
+
"demote_to_helper",
|
|
151
|
+
"do",
|
|
152
|
+
"dynamic_cast",
|
|
153
|
+
"enum",
|
|
154
|
+
"explicit",
|
|
155
|
+
"export",
|
|
156
|
+
"extends",
|
|
157
|
+
"extern",
|
|
158
|
+
"external",
|
|
159
|
+
"f64",
|
|
160
|
+
"fallthrough",
|
|
161
|
+
"filter",
|
|
162
|
+
"final",
|
|
163
|
+
"finally",
|
|
164
|
+
"friend",
|
|
165
|
+
"from",
|
|
166
|
+
"fxgroup",
|
|
167
|
+
"get",
|
|
168
|
+
"goto",
|
|
169
|
+
"groupshared",
|
|
170
|
+
"handle",
|
|
171
|
+
"highp",
|
|
172
|
+
"i16",
|
|
173
|
+
"i64",
|
|
174
|
+
"i8",
|
|
175
|
+
"impl",
|
|
176
|
+
"implements",
|
|
177
|
+
"import",
|
|
178
|
+
"inline",
|
|
179
|
+
"instanceof",
|
|
180
|
+
"interface",
|
|
181
|
+
"layout",
|
|
182
|
+
"lowp",
|
|
183
|
+
"macro",
|
|
184
|
+
"macro_rules",
|
|
185
|
+
"match",
|
|
186
|
+
"mediump",
|
|
187
|
+
"meta",
|
|
188
|
+
"mod",
|
|
189
|
+
"module",
|
|
190
|
+
"move",
|
|
191
|
+
"mut",
|
|
192
|
+
"mutable",
|
|
193
|
+
"namespace",
|
|
194
|
+
"new",
|
|
195
|
+
"nil",
|
|
196
|
+
"noexcept",
|
|
197
|
+
"noinline",
|
|
198
|
+
"nointerpolation",
|
|
199
|
+
"noperspective",
|
|
200
|
+
"null",
|
|
201
|
+
"nullptr",
|
|
202
|
+
"of",
|
|
203
|
+
"operator",
|
|
204
|
+
"package",
|
|
205
|
+
"packoffset",
|
|
206
|
+
"partition",
|
|
207
|
+
"pass",
|
|
208
|
+
"patch",
|
|
209
|
+
"pixelfragment",
|
|
210
|
+
"precise",
|
|
211
|
+
"precision",
|
|
212
|
+
"premerge",
|
|
213
|
+
"priv",
|
|
214
|
+
"protected",
|
|
215
|
+
"pub",
|
|
216
|
+
"public",
|
|
217
|
+
"quat",
|
|
218
|
+
"readonly",
|
|
219
|
+
"ref",
|
|
220
|
+
"regardless",
|
|
221
|
+
"register",
|
|
222
|
+
"reinterpret_cast",
|
|
223
|
+
"requires",
|
|
224
|
+
"resource",
|
|
225
|
+
"restrict",
|
|
226
|
+
"self",
|
|
227
|
+
"set",
|
|
228
|
+
"shared",
|
|
229
|
+
"sizeof",
|
|
230
|
+
"smooth",
|
|
231
|
+
"snorm",
|
|
232
|
+
"static",
|
|
233
|
+
"static_assert",
|
|
234
|
+
"static_cast",
|
|
235
|
+
"std",
|
|
236
|
+
"subroutine",
|
|
237
|
+
"super",
|
|
238
|
+
"target",
|
|
239
|
+
"template",
|
|
240
|
+
"this",
|
|
241
|
+
"thread_local",
|
|
242
|
+
"throw",
|
|
243
|
+
"trait",
|
|
244
|
+
"try",
|
|
245
|
+
"type",
|
|
246
|
+
"typedef",
|
|
247
|
+
"typeid",
|
|
248
|
+
"typename",
|
|
249
|
+
"typeof",
|
|
250
|
+
"u16",
|
|
251
|
+
"u64",
|
|
252
|
+
"u8",
|
|
253
|
+
"union",
|
|
254
|
+
"unless",
|
|
255
|
+
"unorm",
|
|
256
|
+
"unsafe",
|
|
257
|
+
"unsized",
|
|
258
|
+
"use",
|
|
259
|
+
"using",
|
|
260
|
+
"varying",
|
|
261
|
+
"virtual",
|
|
262
|
+
"volatile",
|
|
263
|
+
"wgsl",
|
|
264
|
+
"where",
|
|
265
|
+
"with",
|
|
266
|
+
"writeonly",
|
|
267
|
+
"yield",
|
|
268
|
+
// 15.4 Context-Dependent Name Tokens - Attributes
|
|
269
|
+
"align",
|
|
270
|
+
"binding",
|
|
271
|
+
"builtin",
|
|
272
|
+
"compute",
|
|
273
|
+
"const",
|
|
274
|
+
"fragment",
|
|
275
|
+
"group",
|
|
276
|
+
"id",
|
|
277
|
+
"interpolate",
|
|
278
|
+
"invariant",
|
|
279
|
+
"location",
|
|
280
|
+
"size",
|
|
281
|
+
"vertex",
|
|
282
|
+
"workgroup_size",
|
|
283
|
+
// 15.4 Context-Dependent Name Tokens - Interpolation Types
|
|
284
|
+
"perspective",
|
|
285
|
+
"linear",
|
|
286
|
+
"flat",
|
|
287
|
+
// 15.4 Context-Dependent Name Tokens - Interpolation Sampling
|
|
288
|
+
"center",
|
|
289
|
+
"centroid",
|
|
290
|
+
"sample",
|
|
291
|
+
// 15.4 Context-Dependent Name Tokens - Built-in Values
|
|
292
|
+
"vertex_index",
|
|
293
|
+
"instance_index",
|
|
294
|
+
"position",
|
|
295
|
+
"front_facing",
|
|
296
|
+
"frag_depth",
|
|
297
|
+
"local_invocation_id",
|
|
298
|
+
"local_invocation_index",
|
|
299
|
+
"global_invocation_id",
|
|
300
|
+
"workgroup_id",
|
|
301
|
+
"num_workgroups",
|
|
302
|
+
"sample_index",
|
|
303
|
+
"sample_mask",
|
|
304
|
+
// 15.4 Context-Dependent Name Tokens - Access Modes
|
|
305
|
+
"read",
|
|
306
|
+
"write",
|
|
307
|
+
"read_write",
|
|
308
|
+
// 15.4 Context-Dependent Name Tokens - Address Spaces
|
|
309
|
+
"function",
|
|
310
|
+
"private",
|
|
311
|
+
"workgroup",
|
|
312
|
+
"uniform",
|
|
313
|
+
"storage",
|
|
314
|
+
// 15.4 Context-Dependent Name Tokens - Texel Formats
|
|
315
|
+
"rgba8unorm",
|
|
316
|
+
"rgba8snorm",
|
|
317
|
+
"rgba8uint",
|
|
318
|
+
"rgba8sint",
|
|
319
|
+
"rgba16uint",
|
|
320
|
+
"rgba16sint",
|
|
321
|
+
"rgba16float",
|
|
322
|
+
"r32uint",
|
|
323
|
+
"r32sint",
|
|
324
|
+
"r32float",
|
|
325
|
+
"rg32uint",
|
|
326
|
+
"rg32sint",
|
|
327
|
+
"rg32float",
|
|
328
|
+
"rgba32uint",
|
|
329
|
+
"rgba32sint",
|
|
330
|
+
"rgba32float",
|
|
331
|
+
"bgra8unorm",
|
|
332
|
+
// 15.4 Context-Dependent Name Tokens - Extensions
|
|
333
|
+
"f16",
|
|
334
|
+
// v1 optional
|
|
335
|
+
// 17.1 Logical Built-in Functions
|
|
336
|
+
"all",
|
|
337
|
+
"any",
|
|
338
|
+
"select",
|
|
339
|
+
// 17.2 Array Built-in Functions
|
|
340
|
+
"arrayLength",
|
|
341
|
+
// 17.3 Numeric Built-in Functions
|
|
342
|
+
"abs",
|
|
343
|
+
"acos",
|
|
344
|
+
"acosh",
|
|
345
|
+
"asin",
|
|
346
|
+
"asinh",
|
|
347
|
+
"atan",
|
|
348
|
+
"atanh",
|
|
349
|
+
"atan2",
|
|
350
|
+
"ceil",
|
|
351
|
+
"clamp",
|
|
352
|
+
"cos",
|
|
353
|
+
"cosh",
|
|
354
|
+
"countLeadingZeros",
|
|
355
|
+
"countOneBits",
|
|
356
|
+
"countTrailingZeros",
|
|
357
|
+
"cross",
|
|
358
|
+
"degrees",
|
|
359
|
+
"determinant",
|
|
360
|
+
"distance",
|
|
361
|
+
"dot",
|
|
362
|
+
"exp",
|
|
363
|
+
"exp2",
|
|
364
|
+
"extractBits",
|
|
365
|
+
"faceForward",
|
|
366
|
+
"firstLeadingBit",
|
|
367
|
+
"firstTrailingBit",
|
|
368
|
+
"floor",
|
|
369
|
+
"fma",
|
|
370
|
+
"fract",
|
|
371
|
+
"frexp",
|
|
372
|
+
"insertBits",
|
|
373
|
+
"inverseSqrt",
|
|
374
|
+
"ldexp",
|
|
375
|
+
"length",
|
|
376
|
+
"log",
|
|
377
|
+
"log2",
|
|
378
|
+
"max",
|
|
379
|
+
"min",
|
|
380
|
+
"mix",
|
|
381
|
+
"modf",
|
|
382
|
+
"normalize",
|
|
383
|
+
"pow",
|
|
384
|
+
"quantizeToF16",
|
|
385
|
+
"radians",
|
|
386
|
+
"reflect",
|
|
387
|
+
"refract",
|
|
388
|
+
"reverseBits",
|
|
389
|
+
"round",
|
|
390
|
+
"saturate",
|
|
391
|
+
"sign",
|
|
392
|
+
"sin",
|
|
393
|
+
"sinh",
|
|
394
|
+
"smoothstep",
|
|
395
|
+
"sqrt",
|
|
396
|
+
"step",
|
|
397
|
+
"tan",
|
|
398
|
+
"tanh",
|
|
399
|
+
"transpose",
|
|
400
|
+
"trunc",
|
|
401
|
+
// 17.4 Derivative Built-in Functions
|
|
402
|
+
"dpdx",
|
|
403
|
+
"dpdxCoarse",
|
|
404
|
+
"dpdxFine",
|
|
405
|
+
"dpdy",
|
|
406
|
+
"dpdyCoarse",
|
|
407
|
+
"dpdyFine",
|
|
408
|
+
"fwidth",
|
|
409
|
+
"fwidthCoarse",
|
|
410
|
+
"fwidthFine",
|
|
411
|
+
// 17.5 Texture Built-in Functions
|
|
412
|
+
"textureDimensions",
|
|
413
|
+
"textureGather",
|
|
414
|
+
"textureGatherCompare",
|
|
415
|
+
"textureLoad",
|
|
416
|
+
"textureNumLayers",
|
|
417
|
+
"textureNumLevels",
|
|
418
|
+
"textureNumSamples",
|
|
419
|
+
"textureSample",
|
|
420
|
+
"textureSampleBias",
|
|
421
|
+
"textureSampleCompare",
|
|
422
|
+
"textureSampleCompareLevel",
|
|
423
|
+
"textureSampleGrad",
|
|
424
|
+
"textureSampleLevel",
|
|
425
|
+
"textureSampleBaseClampToEdge",
|
|
426
|
+
// 17.6 Atomic Built-in Functions
|
|
427
|
+
"atomicLoad",
|
|
428
|
+
"atomicStore",
|
|
429
|
+
"atomicAdd",
|
|
430
|
+
"atomicSub",
|
|
431
|
+
"atomicMax",
|
|
432
|
+
"atomicMin",
|
|
433
|
+
"atomicAnd",
|
|
434
|
+
"atomicOr",
|
|
435
|
+
"atomicXor",
|
|
436
|
+
"atomicExchange",
|
|
437
|
+
"atomicCompareExchangeWeak",
|
|
438
|
+
// 17.7 Data Packing Built-in Functions
|
|
439
|
+
"pack4x8snorm",
|
|
440
|
+
"pack4x8unorm",
|
|
441
|
+
"pack2x16snorm",
|
|
442
|
+
"pack2x16unorm",
|
|
443
|
+
"pack2x16float",
|
|
444
|
+
// 17.8 Data Unpacking Built-in Functions
|
|
445
|
+
"unpack4x8snorm",
|
|
446
|
+
"unpack4x8unorm",
|
|
447
|
+
"unpack2x16snorm",
|
|
448
|
+
"unpack2x16unorm",
|
|
449
|
+
"unpack2x16float",
|
|
450
|
+
// 17.9 Synchronization Built-in Functions
|
|
451
|
+
"storageBarrier",
|
|
452
|
+
"workgroupBarrier",
|
|
453
|
+
"workgroupUniformLoad"
|
|
454
|
+
];
|
|
1
455
|
const GLSL_KEYWORDS = [
|
|
2
456
|
// 3.8 Keywords
|
|
3
457
|
"const",
|
|
@@ -173,19 +627,19 @@ const GLSL_KEYWORDS = [
|
|
|
173
627
|
"namespace",
|
|
174
628
|
"using",
|
|
175
629
|
// 3.5 Preprocessor
|
|
176
|
-
"
|
|
177
|
-
"
|
|
178
|
-
"
|
|
179
|
-
"
|
|
180
|
-
"
|
|
181
|
-
"
|
|
182
|
-
"
|
|
183
|
-
"
|
|
184
|
-
"
|
|
185
|
-
"
|
|
186
|
-
"
|
|
187
|
-
"
|
|
188
|
-
"
|
|
630
|
+
"define",
|
|
631
|
+
"undef",
|
|
632
|
+
"if",
|
|
633
|
+
"ifdef",
|
|
634
|
+
"ifndef",
|
|
635
|
+
"else",
|
|
636
|
+
"elif",
|
|
637
|
+
"endif",
|
|
638
|
+
"error",
|
|
639
|
+
"pragma",
|
|
640
|
+
"extension",
|
|
641
|
+
"version",
|
|
642
|
+
"line",
|
|
189
643
|
// 3.5 Preprocessor - Operators
|
|
190
644
|
"defined",
|
|
191
645
|
// 3.5 Preprocessor - Macros
|
|
@@ -316,7 +770,7 @@ const GLSL_KEYWORDS = [
|
|
|
316
770
|
"dFdy",
|
|
317
771
|
"fwidth",
|
|
318
772
|
// Additional directives
|
|
319
|
-
"
|
|
773
|
+
"include",
|
|
320
774
|
// WEBGL_multi_draw https://registry.khronos.org/webgl/extensions/WEBGL_multi_draw
|
|
321
775
|
"gl_DrawID",
|
|
322
776
|
// vertex only
|
|
@@ -325,11 +779,11 @@ const GLSL_KEYWORDS = [
|
|
|
325
779
|
"gl_ViewID_OVR",
|
|
326
780
|
"GL_OVR_multiview2"
|
|
327
781
|
];
|
|
328
|
-
const
|
|
329
|
-
// Preprocessor
|
|
782
|
+
const SYMBOLS = [
|
|
783
|
+
// Preprocessor (GLSL)
|
|
330
784
|
"#",
|
|
331
|
-
//
|
|
332
|
-
"
|
|
785
|
+
// Function arrow (WGSL)
|
|
786
|
+
"->",
|
|
333
787
|
// Comments
|
|
334
788
|
"//",
|
|
335
789
|
"/*",
|
|
@@ -381,142 +835,129 @@ const GLSL_SYMBOLS = [
|
|
|
381
835
|
"<<=",
|
|
382
836
|
">>="
|
|
383
837
|
];
|
|
838
|
+
const isWGSL = RegExp.prototype.test.bind(/\bfn\s+\w+\s*\(|\b(var|let)\b/);
|
|
839
|
+
const isFloat = RegExp.prototype.test.bind(/\.|[eEpP][-+]?\d|[fFhH]$/);
|
|
840
|
+
const isBool = RegExp.prototype.test.bind(/^(true|false)$/);
|
|
841
|
+
const ZERO = 48;
|
|
842
|
+
const NINE = 57;
|
|
843
|
+
const A = 65;
|
|
844
|
+
const Z = 90;
|
|
845
|
+
const LF = 10;
|
|
846
|
+
const CR = 13;
|
|
847
|
+
const TAB = 9;
|
|
848
|
+
const SPACE = 32;
|
|
849
|
+
const PLUS = 43;
|
|
850
|
+
const MINUS = 45;
|
|
851
|
+
const DOT = 46;
|
|
852
|
+
const UNDERSCORE = 95;
|
|
853
|
+
const SLASH = 47;
|
|
854
|
+
const STAR = 42;
|
|
855
|
+
const isDigit = (c) => ZERO <= c && c <= NINE;
|
|
856
|
+
const isAlpha = (c) => (c &= ~32, A <= c && c <= Z);
|
|
857
|
+
const isLine = (c) => c === LF || c === CR;
|
|
858
|
+
const isSpace = (c) => isLine(c) || c === TAB || c === SPACE;
|
|
859
|
+
const isNumber = (c) => isAlpha(c) || isDigit(c) || c === PLUS || c === MINUS || c === DOT;
|
|
860
|
+
const isIdent = (c) => isAlpha(c) || isDigit(c) || c === UNDERSCORE;
|
|
384
861
|
function tokenize(code, index = 0) {
|
|
385
|
-
var _a;
|
|
386
862
|
const tokens = [];
|
|
863
|
+
const KEYWORDS = isWGSL(code) ? WGSL_KEYWORDS : GLSL_KEYWORDS;
|
|
387
864
|
while (index < code.length) {
|
|
388
|
-
let
|
|
389
|
-
|
|
390
|
-
if (
|
|
391
|
-
index
|
|
392
|
-
continue;
|
|
393
|
-
}
|
|
394
|
-
if (char === "#") {
|
|
395
|
-
let j = index;
|
|
396
|
-
while (code[j] !== "\n" && !/\/[\/\*]/.test(code[j] + code[j + 1]))
|
|
397
|
-
j++;
|
|
398
|
-
code = code.substring(0, j) + "\\" + code.substring(j);
|
|
399
|
-
}
|
|
400
|
-
while ((/\d/.test(char) ? /[\d\.\-\w]/ : /\w/).test(code[index]))
|
|
401
|
-
value += code[index++];
|
|
402
|
-
if (!value) {
|
|
403
|
-
value = char;
|
|
404
|
-
for (const symbol of GLSL_SYMBOLS) {
|
|
405
|
-
if (symbol.length > value.length && code.startsWith(symbol, index))
|
|
406
|
-
value = symbol;
|
|
407
|
-
}
|
|
408
|
-
index += value.length;
|
|
409
|
-
}
|
|
410
|
-
if (/\/[\/\*]/.test(value)) {
|
|
411
|
-
const isMultiline = value === "/*";
|
|
412
|
-
while (!value.endsWith(isMultiline ? "*/" : "\n"))
|
|
865
|
+
let value = code[index];
|
|
866
|
+
const char = code.charCodeAt(index++);
|
|
867
|
+
if (isSpace(char)) {
|
|
868
|
+
while (isSpace(code.charCodeAt(index)))
|
|
413
869
|
value += code[index++];
|
|
414
|
-
tokens.push({ type: "
|
|
415
|
-
} else if (
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
if (
|
|
419
|
-
tokens.push({ type: "uint", value });
|
|
420
|
-
else if (/\.|[eE]-?\d/.test(value))
|
|
870
|
+
tokens.push({ type: "whitespace", value });
|
|
871
|
+
} else if (isDigit(char)) {
|
|
872
|
+
while (isNumber(code.charCodeAt(index)))
|
|
873
|
+
value += code[index++];
|
|
874
|
+
if (isFloat(value))
|
|
421
875
|
tokens.push({ type: "float", value });
|
|
422
876
|
else
|
|
423
877
|
tokens.push({ type: "int", value });
|
|
424
|
-
} else if (
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
878
|
+
} else if (isAlpha(char)) {
|
|
879
|
+
while (isIdent(code.charCodeAt(index)))
|
|
880
|
+
value += code[index++];
|
|
881
|
+
if (isBool(value))
|
|
882
|
+
tokens.push({ type: "bool", value });
|
|
883
|
+
else if (KEYWORDS.includes(value))
|
|
884
|
+
tokens.push({ type: "keyword", value });
|
|
885
|
+
else
|
|
886
|
+
tokens.push({ type: "identifier", value });
|
|
887
|
+
} else if (char === SLASH && (code.charCodeAt(index) === SLASH || code.charCodeAt(index) === STAR)) {
|
|
888
|
+
const isMultiline = code.charCodeAt(index) === STAR;
|
|
889
|
+
while (!value.endsWith(isMultiline ? "*/" : "\n"))
|
|
890
|
+
value += code[index++];
|
|
891
|
+
tokens.push({ type: "comment", value });
|
|
428
892
|
} else {
|
|
429
|
-
|
|
893
|
+
for (const symbol of SYMBOLS) {
|
|
894
|
+
if (symbol.length > value.length && code.startsWith(symbol, index - 1))
|
|
895
|
+
value = symbol;
|
|
896
|
+
}
|
|
897
|
+
index += value.length - 1;
|
|
898
|
+
tokens.push({ type: "symbol", value });
|
|
430
899
|
}
|
|
431
900
|
}
|
|
432
901
|
return tokens;
|
|
433
902
|
}
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
|
|
903
|
+
const isWord = RegExp.prototype.test.bind(/^\w/);
|
|
904
|
+
const isName = RegExp.prototype.test.bind(/^[A-Za-z]/);
|
|
905
|
+
const isStorage = RegExp.prototype.test.bind(/^(uniform|in|out|attribute|varying|,)$/);
|
|
906
|
+
function minify(code, { mangle = false, mangleMap = /* @__PURE__ */ new Map(), mangleExternals = false } = {}) {
|
|
907
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
908
|
+
code = code.replace(/(^\s*#[^\\]*?)(\n|\/[\/\*])/gm, "$1\\$2");
|
|
441
909
|
const exclude = new Set(mangleMap.values());
|
|
442
|
-
const tokens = tokenize(code).filter((token) => token.type !== "comment");
|
|
910
|
+
const tokens = tokenize(code).filter((token) => token.type !== "whitespace" && token.type !== "comment");
|
|
443
911
|
let mangleIndex = 0;
|
|
444
912
|
let blockIndex = null;
|
|
445
|
-
let prefix = null;
|
|
446
913
|
let minified = "";
|
|
447
914
|
for (let i = 0; i < tokens.length; i++) {
|
|
448
915
|
const token = tokens[i];
|
|
449
|
-
if (
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
}
|
|
458
|
-
let renamed = mangleMap.get(key);
|
|
916
|
+
if (isWord(token.value) && isWord((_a = tokens[i - 1]) == null ? void 0 : _a.value))
|
|
917
|
+
minified += " ";
|
|
918
|
+
if (token.value === "{" && isName((_b = tokens[i - 1]) == null ? void 0 : _b.value))
|
|
919
|
+
blockIndex = i - 1;
|
|
920
|
+
else if (token.value === "}")
|
|
921
|
+
blockIndex = null;
|
|
922
|
+
if (isName(token.value)) {
|
|
923
|
+
let renamed = mangleMap.get(token.value);
|
|
459
924
|
if (
|
|
460
925
|
// no-op
|
|
461
|
-
!renamed && //
|
|
462
|
-
|
|
463
|
-
token.
|
|
926
|
+
!renamed && // Skip struct properties
|
|
927
|
+
blockIndex == null && // Filter variable names
|
|
928
|
+
token.value !== "main" && (typeof mangle === "boolean" ? mangle : mangle(token, i, tokens)) && // Is declaration, reference, namespace, or comma-separated list
|
|
929
|
+
token.type === "identifier" && (isName((_c = tokens[i - 1]) == null ? void 0 : _c.value) || /}|,/.test((_d = tokens[i - 1]) == null ? void 0 : _d.value) || ((_e = tokens[i + 1]) == null ? void 0 : _e.value) === ":") && // Skip shader externals when disabled
|
|
930
|
+
(mangleExternals || !isStorage((_f = tokens[i - 1]) == null ? void 0 : _f.value) && !isStorage((_g = tokens[i - 2]) == null ? void 0 : _g.value))
|
|
464
931
|
) {
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
// Struct header, fully specified uniform, or comma-separated list
|
|
473
|
-
mangleExternals || !/(uniform|in|out|attribute|varying|,)/.test((_h = tokens[i - 1]) == null ? void 0 : _h.value) && !/(uniform|in|out|attribute|varying)/.test((_i = tokens[i - 2]) == null ? void 0 : _i.value)
|
|
474
|
-
))
|
|
475
|
-
) {
|
|
476
|
-
while (!renamed || exclude.has(renamed)) {
|
|
477
|
-
renamed = "";
|
|
478
|
-
mangleIndex++;
|
|
479
|
-
let j = mangleIndex;
|
|
480
|
-
while (j > 0) {
|
|
481
|
-
renamed = String.fromCharCode(97 + (j % 26 - 1)) + renamed;
|
|
482
|
-
j = Math.floor(j / 26);
|
|
483
|
-
}
|
|
932
|
+
while (!renamed || exclude.has(renamed)) {
|
|
933
|
+
renamed = "";
|
|
934
|
+
mangleIndex++;
|
|
935
|
+
let j = mangleIndex;
|
|
936
|
+
while (j > 0) {
|
|
937
|
+
renamed = String.fromCharCode(97 + (j % 26 - 1)) + renamed;
|
|
938
|
+
j = Math.floor(j / 26);
|
|
484
939
|
}
|
|
485
|
-
mangleMap.set(key, renamed);
|
|
486
|
-
}
|
|
487
|
-
if (((_j = tokens[i + 1]) == null ? void 0 : _j.value) === "{") {
|
|
488
|
-
let j = i;
|
|
489
|
-
while (tokens[j].value !== "}")
|
|
490
|
-
j++;
|
|
491
|
-
const suffix = tokens[j + 1].value;
|
|
492
|
-
if (suffix !== ";")
|
|
493
|
-
prefix = suffix;
|
|
494
|
-
blockIndex = i;
|
|
495
|
-
} else if (((_k = tokens[i + 2]) == null ? void 0 : _k.value) === "}") {
|
|
496
|
-
prefix = null;
|
|
497
|
-
blockIndex = null;
|
|
498
940
|
}
|
|
941
|
+
mangleMap.set(token.value, renamed);
|
|
499
942
|
}
|
|
500
943
|
minified += renamed != null ? renamed : token.value;
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
minified += "\n";
|
|
504
|
-
while (tokens[i].value !== "\\") {
|
|
505
|
-
if ((tokens[i].type !== "symbol" || ((_m = tokens[i - 2]) == null ? void 0 : _m.value) === "#") && ((_n = tokens[i - 1]) == null ? void 0 : _n.type) !== "symbol")
|
|
506
|
-
minified += " ";
|
|
507
|
-
minified += tokens[i].value;
|
|
508
|
-
i++;
|
|
509
|
-
}
|
|
510
|
-
minified += "\n";
|
|
944
|
+
if (token.value === "include")
|
|
945
|
+
minified += " ";
|
|
511
946
|
} else {
|
|
512
|
-
|
|
947
|
+
if (token.value === "#" && ((_h = tokens[i - 1]) == null ? void 0 : _h.value) !== "\\")
|
|
948
|
+
minified += "\n#";
|
|
949
|
+
else if (token.value === "\\")
|
|
950
|
+
minified += "\n";
|
|
951
|
+
else
|
|
952
|
+
minified += token.value;
|
|
513
953
|
}
|
|
514
954
|
}
|
|
515
955
|
return minified.trim();
|
|
516
956
|
}
|
|
517
957
|
export {
|
|
518
958
|
GLSL_KEYWORDS,
|
|
519
|
-
|
|
959
|
+
SYMBOLS,
|
|
960
|
+
WGSL_KEYWORDS,
|
|
520
961
|
minify,
|
|
521
962
|
tokenize
|
|
522
963
|
};
|