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