Summary
When bundling, it would be useful to automatically generate minified WGSL shader code to reduce bundle size.
Proposed Functionality
Minification could be implemented in multiple levels of complexity:
1. Basic Minification
- Strip whitespace, newlines, and comments.
2. Variable Renaming
- Rename variables to shorter (e.g., single-character) names.
3. Function Renaming
- Rename functions while ensuring that corresponding entry points in JavaScript are also updated.
4. Advanced Minification
- Remove redundant syntax such as verbose type constructors and unnecessary parentheses.
Examples
Before
const TRIANGLE_VERTICES = array<vec2<f32>, 3>(
vec2<f32>(1.0, 0.0),
vec2<f32>(-0.5, 0.3),
vec2<f32>(-0.5, -0.3)
);
After
const TRIANGLE_VERTICES = array(
vec2(1.0, 0.0),
vec2(-0.5, 0.3),
vec2(-0.5, -0.3)
);
Additional Considerations
- Redundant parentheses: Can be safely removed in most expressions.
- Floating-point literals:
- In all my test cases,
.1 appears to work the same as 0.1.
- Based on testing, appending
.0 to integers isn't necessary (e.g., 1.0 → 1).
Motivation
Automated WGSL minification can significantly reduce bundle size and improve shader load times, particularly for projects with many shaders or large shader libraries.
Summary
When bundling, it would be useful to automatically generate minified WGSL shader code to reduce bundle size.
Proposed Functionality
Minification could be implemented in multiple levels of complexity:
1. Basic Minification
2. Variable Renaming
3. Function Renaming
4. Advanced Minification
Examples
Before
After
Additional Considerations
.1appears to work the same as0.1..0to integers isn't necessary (e.g.,1.0→1).Motivation
Automated WGSL minification can significantly reduce bundle size and improve shader load times, particularly for projects with many shaders or large shader libraries.