Skip to content

Feature Request: Add Minification Support for WGSL Output #182

Description

@hectoreccles

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.01).

Motivation

Automated WGSL minification can significantly reduce bundle size and improve shader load times, particularly for projects with many shaders or large shader libraries.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    Status
    Unprioritized

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions