miniray 0.1.1 → 0.1.3
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 +123 -5
- package/lib/main.d.ts +14 -0
- package/miniray.wasm +0 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -75,11 +75,15 @@ Minify WGSL source code.
|
|
|
75
75
|
|
|
76
76
|
```typescript
|
|
77
77
|
interface MinifyOptions {
|
|
78
|
-
minifyWhitespace?: boolean;
|
|
79
|
-
minifyIdentifiers?: boolean;
|
|
80
|
-
minifySyntax?: boolean;
|
|
81
|
-
mangleExternalBindings?: boolean;
|
|
82
|
-
|
|
78
|
+
minifyWhitespace?: boolean; // Remove whitespace (default: true)
|
|
79
|
+
minifyIdentifiers?: boolean; // Rename identifiers (default: true)
|
|
80
|
+
minifySyntax?: boolean; // Optimize syntax (default: true)
|
|
81
|
+
mangleExternalBindings?: boolean; // Mangle uniform/storage names (default: false)
|
|
82
|
+
treeShaking?: boolean; // Remove unused declarations (default: true)
|
|
83
|
+
preserveUniformStructTypes?: boolean; // Keep struct types used in uniforms (default: false)
|
|
84
|
+
keepNames?: string[]; // Names to preserve from renaming
|
|
85
|
+
sourceMap?: boolean; // Generate source map (default: false)
|
|
86
|
+
sourceMapSources?: boolean; // Include source in sourcesContent (default: false)
|
|
83
87
|
}
|
|
84
88
|
|
|
85
89
|
interface MinifyResult {
|
|
@@ -87,6 +91,7 @@ interface MinifyResult {
|
|
|
87
91
|
errors: MinifyError[]; // Parse/minification errors
|
|
88
92
|
originalSize: number; // Input size in bytes
|
|
89
93
|
minifiedSize: number; // Output size in bytes
|
|
94
|
+
sourceMap?: string; // Source map JSON (if sourceMap: true)
|
|
90
95
|
}
|
|
91
96
|
```
|
|
92
97
|
|
|
@@ -133,6 +138,40 @@ fn getValue() -> f32 { return uniforms * 2.0; }
|
|
|
133
138
|
// Output: "@group(0) @binding(0) var<uniform> a:f32;fn b()->f32{return a*2.0;}"
|
|
134
139
|
```
|
|
135
140
|
|
|
141
|
+
### `treeShaking`
|
|
142
|
+
|
|
143
|
+
Enable dead code elimination to remove unused declarations (default: `true`):
|
|
144
|
+
|
|
145
|
+
```javascript
|
|
146
|
+
minify(source, {
|
|
147
|
+
treeShaking: true, // Remove unreachable code
|
|
148
|
+
});
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
### `preserveUniformStructTypes`
|
|
152
|
+
|
|
153
|
+
Automatically preserve struct type names that are used in `var<uniform>` or `var<storage>` declarations (default: `false`):
|
|
154
|
+
|
|
155
|
+
```javascript
|
|
156
|
+
// Input
|
|
157
|
+
const shader = `
|
|
158
|
+
struct MyUniforms { time: f32 }
|
|
159
|
+
@group(0) @binding(0) var<uniform> u: MyUniforms;
|
|
160
|
+
@fragment fn main() -> @location(0) vec4f { return vec4f(u.time); }
|
|
161
|
+
`;
|
|
162
|
+
|
|
163
|
+
// With preserveUniformStructTypes: false (default)
|
|
164
|
+
// struct MyUniforms -> struct a
|
|
165
|
+
|
|
166
|
+
// With preserveUniformStructTypes: true
|
|
167
|
+
// struct MyUniforms preserved
|
|
168
|
+
minify(source, {
|
|
169
|
+
preserveUniformStructTypes: true,
|
|
170
|
+
});
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
This is particularly useful for frameworks like PNGine that detect builtin uniforms by struct type name.
|
|
174
|
+
|
|
136
175
|
### `keepNames`
|
|
137
176
|
|
|
138
177
|
Array of identifier names that should not be renamed:
|
|
@@ -144,6 +183,85 @@ minify(source, {
|
|
|
144
183
|
});
|
|
145
184
|
```
|
|
146
185
|
|
|
186
|
+
### `sourceMap`
|
|
187
|
+
|
|
188
|
+
Generate a source map to debug minified shaders by mapping back to original source:
|
|
189
|
+
|
|
190
|
+
```javascript
|
|
191
|
+
const result = minify(source, {
|
|
192
|
+
minifyIdentifiers: true,
|
|
193
|
+
sourceMap: true,
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
console.log(result.code);
|
|
197
|
+
// "const a=42;fn b()->i32{return a;}"
|
|
198
|
+
|
|
199
|
+
console.log(result.sourceMap);
|
|
200
|
+
// '{"version":3,"sources":[],"names":["longVariable","myFunction"],"mappings":"MAAAA,..."}'
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
The source map follows the [Source Map v3 specification](https://sourcemaps.info/spec.html) and includes:
|
|
204
|
+
- `version`: Always 3
|
|
205
|
+
- `names`: Original names of renamed identifiers
|
|
206
|
+
- `mappings`: VLQ-encoded position mappings
|
|
207
|
+
|
|
208
|
+
### `sourceMapSources`
|
|
209
|
+
|
|
210
|
+
Include the original source code in the source map's `sourcesContent` field for self-contained debugging:
|
|
211
|
+
|
|
212
|
+
```javascript
|
|
213
|
+
const result = minify(source, {
|
|
214
|
+
sourceMap: true,
|
|
215
|
+
sourceMapSources: true, // Embed original source
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
const map = JSON.parse(result.sourceMap);
|
|
219
|
+
console.log(map.sourcesContent);
|
|
220
|
+
// ["const longVariable = 42;\nfn myFunction() -> i32 { return longVariable; }"]
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
### Source Map Example: Complete Workflow
|
|
224
|
+
|
|
225
|
+
```javascript
|
|
226
|
+
import { initialize, minify } from 'miniray';
|
|
227
|
+
|
|
228
|
+
await initialize({ wasmURL: '/miniray.wasm' });
|
|
229
|
+
|
|
230
|
+
const source = `
|
|
231
|
+
const longVariableName = 42;
|
|
232
|
+
|
|
233
|
+
fn helperFunction(value: i32) -> i32 {
|
|
234
|
+
return value * 2;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
@compute @workgroup_size(1)
|
|
238
|
+
fn main() {
|
|
239
|
+
let result = helperFunction(longVariableName);
|
|
240
|
+
}
|
|
241
|
+
`;
|
|
242
|
+
|
|
243
|
+
const result = minify(source, {
|
|
244
|
+
minifyWhitespace: true,
|
|
245
|
+
minifyIdentifiers: true,
|
|
246
|
+
sourceMap: true,
|
|
247
|
+
sourceMapSources: true,
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
console.log('Minified code:');
|
|
251
|
+
console.log(result.code);
|
|
252
|
+
// "const a=42;fn b(c:i32)->i32{return c*2;}@compute @workgroup_size(1) fn main(){let d=b(a);}"
|
|
253
|
+
|
|
254
|
+
console.log('\nSource map:');
|
|
255
|
+
const map = JSON.parse(result.sourceMap);
|
|
256
|
+
console.log('Names:', map.names);
|
|
257
|
+
// Names: ["longVariableName", "helperFunction", "value", "result"]
|
|
258
|
+
|
|
259
|
+
// To use the source map inline:
|
|
260
|
+
const codeWithSourceMap = result.code +
|
|
261
|
+
'\n//# sourceMappingURL=data:application/json;base64,' +
|
|
262
|
+
btoa(result.sourceMap);
|
|
263
|
+
```
|
|
264
|
+
|
|
147
265
|
## Using with Bundlers
|
|
148
266
|
|
|
149
267
|
### Vite
|
package/lib/main.d.ts
CHANGED
|
@@ -29,6 +29,20 @@ export interface MinifyOptions {
|
|
|
29
29
|
*/
|
|
30
30
|
mangleExternalBindings?: boolean;
|
|
31
31
|
|
|
32
|
+
/**
|
|
33
|
+
* Enable dead code elimination to remove unused declarations.
|
|
34
|
+
* @default true
|
|
35
|
+
*/
|
|
36
|
+
treeShaking?: boolean;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Automatically preserve struct type names that are used in
|
|
40
|
+
* var<uniform> or var<storage> declarations.
|
|
41
|
+
* Useful for frameworks that detect uniforms by struct type name.
|
|
42
|
+
* @default false
|
|
43
|
+
*/
|
|
44
|
+
preserveUniformStructTypes?: boolean;
|
|
45
|
+
|
|
32
46
|
/**
|
|
33
47
|
* Identifier names that should not be renamed.
|
|
34
48
|
*/
|
package/miniray.wasm
CHANGED
|
Binary file
|