nagami-rs 2026.4.11

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 ekarad1ium
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,110 @@
1
+ # Nagami[n]
2
+
3
+ Naga + Minify. Shrinks your WGSL shaders — not by squishing text, but by
4
+ understanding them.
5
+
6
+ Nagami lowers WGSL into [Naga](https://github.com/gfx-rs/naga) IR, optimizes
7
+ the IR in multiple passes, and emits the smallest valid WGSL it can.
8
+
9
+ **What it does that grep-and-replace can't:**
10
+
11
+ - Dead code elimination — unused functions, variables, constants vanish
12
+ - Constant folding — `1.0 + 2.0` becomes `3.0` at compile time
13
+ - Function inlining — small helpers get absorbed into callers
14
+ - Load deduplication — redundant reads from the same pointer merge
15
+ - Variable coalescing — non-overlapping locals share one slot
16
+ - Identifier mangling — `myLongVariableName` -> `a`
17
+ - Literal extraction — repeated magic numbers become a shared `const`
18
+ - Precedence-aware parens — only the parentheses that matter survive
19
+ - Float precision trimming — optionally truncate decimal places (lossy, opt-in)
20
+ - Compound assignment — `x = x + 1` -> `x += 1`
21
+ - Type elision — redundant type annotations on `var` / `const` stripped
22
+ - Empty branch flipping — `if c {} else { x; }` -> `if !c { x; }`
23
+
24
+ Runs passes in fixed-point sweeps until the output stops shrinking. Typically
25
+ converges in 3 sweeps.
26
+
27
+ ## CLI
28
+
29
+ Install with cargo:
30
+
31
+ ```sh
32
+ cargo install nagami --features cli
33
+ ```
34
+
35
+ Example usage:
36
+
37
+ ```sh
38
+ nagami shader.wgsl -o shader.min.wgsl # minify (max profile by default)
39
+ nagami shader.wgsl --in-place --stats # in-place, print savings
40
+ nagami shader.wgsl -o out.wgsl -p baseline # lighter touch, no mangle
41
+ cat shader.wgsl | nagami - > out.wgsl # stdin -> stdout
42
+ nagami shader.wgsl --check # exit 1 if not minified
43
+ ```
44
+
45
+ ## Profiles
46
+
47
+ | Profile | What it does | Mangle |
48
+ |---|---|---|
49
+ | `baseline` | DCE, fold, merge, rename | No |
50
+ | `aggressive` | Inline, dedup, coalesce | No |
51
+ | **`max`** (default) | Aggressive cranked up | Yes |
52
+
53
+ ## Use in Rust
54
+
55
+ Install with cargo:
56
+
57
+ ```sh
58
+ cargo add nagami
59
+ ```
60
+
61
+ Run with default config:
62
+
63
+ ```rust
64
+ let output = nagami::run(src, &nagami::config::Config::default())?;
65
+ println!("{}", output.source); // smol shader
66
+ ```
67
+
68
+ ## Use in JavaScript / TypeScript
69
+
70
+ Install with npm:
71
+
72
+ ```sh
73
+ npm install nagami-rs
74
+ ```
75
+
76
+ For JavaScript / TypeScript use:
77
+
78
+ ```js
79
+ import init, { minify } from 'nagami-rs';
80
+ await init(); // load the WASM module once
81
+ const output = minify(shader);
82
+ ```
83
+
84
+ With options:
85
+
86
+ ```js
87
+ const output = minify(shader, {
88
+ profile: 'max', // "baseline" | "aggressive" | "max" (default)
89
+ mangle: true, // rename identifiers
90
+ preserveSymbols: ['main'], // keep these names untouched
91
+ beautify: false, // compact output
92
+ maxPrecision: 6, // truncate float decimal places (lossy)
93
+ });
94
+ ```
95
+
96
+ Node.js (synchronous init):
97
+
98
+ ```js
99
+ import { readFileSync } from 'node:fs';
100
+ import { initSync, minify } from 'nagami-rs';
101
+ const wasm = readFileSync(new URL('nagami_bg.wasm', import.meta.resolve('nagami')));
102
+ initSync({ module: wasm });
103
+ const output = minify(shader);
104
+ ```
105
+
106
+ ## License
107
+
108
+ ```
109
+ MIT License - Copyright (c) 2026 ekarad1ium
110
+ ```
package/nagami.d.ts ADDED
@@ -0,0 +1,53 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+
4
+ export interface Options {
5
+ profile?: "baseline" | "aggressive" | "max";
6
+ preserveSymbols?: string[];
7
+ mangle?: boolean;
8
+ beautify?: boolean;
9
+ indent?: number;
10
+ maxPrecision?: number;
11
+ maxInlineNodeCount?: number;
12
+ maxInlineCallSites?: number;
13
+ }
14
+
15
+ export function minify(source: string, options?: Options): string;
16
+ export function version(): string;
17
+
18
+
19
+
20
+ export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
21
+
22
+ export interface InitOutput {
23
+ readonly memory: WebAssembly.Memory;
24
+ readonly minify: (a: number, b: number, c: number, d: number) => void;
25
+ readonly version: (a: number) => void;
26
+ readonly __wbindgen_export: (a: number, b: number) => number;
27
+ readonly __wbindgen_export2: (a: number, b: number, c: number, d: number) => number;
28
+ readonly __wbindgen_export3: (a: number) => void;
29
+ readonly __wbindgen_add_to_stack_pointer: (a: number) => number;
30
+ readonly __wbindgen_export4: (a: number, b: number, c: number) => void;
31
+ }
32
+
33
+ export type SyncInitInput = BufferSource | WebAssembly.Module;
34
+
35
+ /**
36
+ * Instantiates the given `module`, which can either be bytes or
37
+ * a precompiled `WebAssembly.Module`.
38
+ *
39
+ * @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated.
40
+ *
41
+ * @returns {InitOutput}
42
+ */
43
+ export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput;
44
+
45
+ /**
46
+ * If `module_or_path` is {RequestInfo} or {URL}, makes a request and
47
+ * for everything else, calls `WebAssembly.instantiate` directly.
48
+ *
49
+ * @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
50
+ *
51
+ * @returns {Promise<InitOutput>}
52
+ */
53
+ export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;
package/nagami.js ADDED
@@ -0,0 +1,340 @@
1
+ /* @ts-self-types="./nagami.d.ts" */
2
+
3
+ /**
4
+ * @param {string} source
5
+ * @param {any} options
6
+ * @returns {string}
7
+ */
8
+ export function minify(source, options) {
9
+ let deferred3_0;
10
+ let deferred3_1;
11
+ try {
12
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
13
+ const ptr0 = passStringToWasm0(source, wasm.__wbindgen_export, wasm.__wbindgen_export2);
14
+ const len0 = WASM_VECTOR_LEN;
15
+ wasm.minify(retptr, ptr0, len0, addHeapObject(options));
16
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
17
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
18
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
19
+ var r3 = getDataViewMemory0().getInt32(retptr + 4 * 3, true);
20
+ var ptr2 = r0;
21
+ var len2 = r1;
22
+ if (r3) {
23
+ ptr2 = 0; len2 = 0;
24
+ throw takeObject(r2);
25
+ }
26
+ deferred3_0 = ptr2;
27
+ deferred3_1 = len2;
28
+ return getStringFromWasm0(ptr2, len2);
29
+ } finally {
30
+ wasm.__wbindgen_add_to_stack_pointer(16);
31
+ wasm.__wbindgen_export4(deferred3_0, deferred3_1, 1);
32
+ }
33
+ }
34
+
35
+ /**
36
+ * @returns {string}
37
+ */
38
+ export function version() {
39
+ let deferred1_0;
40
+ let deferred1_1;
41
+ try {
42
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
43
+ wasm.version(retptr);
44
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
45
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
46
+ deferred1_0 = r0;
47
+ deferred1_1 = r1;
48
+ return getStringFromWasm0(r0, r1);
49
+ } finally {
50
+ wasm.__wbindgen_add_to_stack_pointer(16);
51
+ wasm.__wbindgen_export4(deferred1_0, deferred1_1, 1);
52
+ }
53
+ }
54
+
55
+ function __wbg_get_imports() {
56
+ const import0 = {
57
+ __proto__: null,
58
+ __wbg_Error_2e59b1b37a9a34c3: function(arg0, arg1) {
59
+ const ret = Error(getStringFromWasm0(arg0, arg1));
60
+ return addHeapObject(ret);
61
+ },
62
+ __wbg___wbindgen_boolean_get_a86c216575a75c30: function(arg0) {
63
+ const v = getObject(arg0);
64
+ const ret = typeof(v) === 'boolean' ? v : undefined;
65
+ return isLikeNone(ret) ? 0xFFFFFF : ret ? 1 : 0;
66
+ },
67
+ __wbg___wbindgen_is_null_344c8750a8525473: function(arg0) {
68
+ const ret = getObject(arg0) === null;
69
+ return ret;
70
+ },
71
+ __wbg___wbindgen_is_undefined_c0cca72b82b86f4d: function(arg0) {
72
+ const ret = getObject(arg0) === undefined;
73
+ return ret;
74
+ },
75
+ __wbg___wbindgen_number_get_7579aab02a8a620c: function(arg0, arg1) {
76
+ const obj = getObject(arg1);
77
+ const ret = typeof(obj) === 'number' ? obj : undefined;
78
+ getDataViewMemory0().setFloat64(arg0 + 8 * 1, isLikeNone(ret) ? 0 : ret, true);
79
+ getDataViewMemory0().setInt32(arg0 + 4 * 0, !isLikeNone(ret), true);
80
+ },
81
+ __wbg___wbindgen_string_get_914df97fcfa788f2: function(arg0, arg1) {
82
+ const obj = getObject(arg1);
83
+ const ret = typeof(obj) === 'string' ? obj : undefined;
84
+ var ptr1 = isLikeNone(ret) ? 0 : passStringToWasm0(ret, wasm.__wbindgen_export, wasm.__wbindgen_export2);
85
+ var len1 = WASM_VECTOR_LEN;
86
+ getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
87
+ getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
88
+ },
89
+ __wbg___wbindgen_throw_81fc77679af83bc6: function(arg0, arg1) {
90
+ throw new Error(getStringFromWasm0(arg0, arg1));
91
+ },
92
+ __wbg_get_f96702c6245e4ef9: function() { return handleError(function (arg0, arg1) {
93
+ const ret = Reflect.get(getObject(arg0), getObject(arg1));
94
+ return addHeapObject(ret);
95
+ }, arguments); },
96
+ __wbg_get_unchecked_7d7babe32e9e6a54: function(arg0, arg1) {
97
+ const ret = getObject(arg0)[arg1 >>> 0];
98
+ return addHeapObject(ret);
99
+ },
100
+ __wbg_isArray_db61795ad004c139: function(arg0) {
101
+ const ret = Array.isArray(getObject(arg0));
102
+ return ret;
103
+ },
104
+ __wbg_length_6e821edde497a532: function(arg0) {
105
+ const ret = getObject(arg0).length;
106
+ return ret;
107
+ },
108
+ __wbindgen_cast_0000000000000001: function(arg0, arg1) {
109
+ // Cast intrinsic for `Ref(String) -> Externref`.
110
+ const ret = getStringFromWasm0(arg0, arg1);
111
+ return addHeapObject(ret);
112
+ },
113
+ __wbindgen_object_drop_ref: function(arg0) {
114
+ takeObject(arg0);
115
+ },
116
+ };
117
+ return {
118
+ __proto__: null,
119
+ "./nagami_bg.js": import0,
120
+ };
121
+ }
122
+
123
+ function addHeapObject(obj) {
124
+ if (heap_next === heap.length) heap.push(heap.length + 1);
125
+ const idx = heap_next;
126
+ heap_next = heap[idx];
127
+
128
+ heap[idx] = obj;
129
+ return idx;
130
+ }
131
+
132
+ function dropObject(idx) {
133
+ if (idx < 1028) return;
134
+ heap[idx] = heap_next;
135
+ heap_next = idx;
136
+ }
137
+
138
+ let cachedDataViewMemory0 = null;
139
+ function getDataViewMemory0() {
140
+ if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) {
141
+ cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
142
+ }
143
+ return cachedDataViewMemory0;
144
+ }
145
+
146
+ function getStringFromWasm0(ptr, len) {
147
+ ptr = ptr >>> 0;
148
+ return decodeText(ptr, len);
149
+ }
150
+
151
+ let cachedUint8ArrayMemory0 = null;
152
+ function getUint8ArrayMemory0() {
153
+ if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
154
+ cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
155
+ }
156
+ return cachedUint8ArrayMemory0;
157
+ }
158
+
159
+ function getObject(idx) { return heap[idx]; }
160
+
161
+ function handleError(f, args) {
162
+ try {
163
+ return f.apply(this, args);
164
+ } catch (e) {
165
+ wasm.__wbindgen_export3(addHeapObject(e));
166
+ }
167
+ }
168
+
169
+ let heap = new Array(1024).fill(undefined);
170
+ heap.push(undefined, null, true, false);
171
+
172
+ let heap_next = heap.length;
173
+
174
+ function isLikeNone(x) {
175
+ return x === undefined || x === null;
176
+ }
177
+
178
+ function passStringToWasm0(arg, malloc, realloc) {
179
+ if (realloc === undefined) {
180
+ const buf = cachedTextEncoder.encode(arg);
181
+ const ptr = malloc(buf.length, 1) >>> 0;
182
+ getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
183
+ WASM_VECTOR_LEN = buf.length;
184
+ return ptr;
185
+ }
186
+
187
+ let len = arg.length;
188
+ let ptr = malloc(len, 1) >>> 0;
189
+
190
+ const mem = getUint8ArrayMemory0();
191
+
192
+ let offset = 0;
193
+
194
+ for (; offset < len; offset++) {
195
+ const code = arg.charCodeAt(offset);
196
+ if (code > 0x7F) break;
197
+ mem[ptr + offset] = code;
198
+ }
199
+ if (offset !== len) {
200
+ if (offset !== 0) {
201
+ arg = arg.slice(offset);
202
+ }
203
+ ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
204
+ const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
205
+ const ret = cachedTextEncoder.encodeInto(arg, view);
206
+
207
+ offset += ret.written;
208
+ ptr = realloc(ptr, len, offset, 1) >>> 0;
209
+ }
210
+
211
+ WASM_VECTOR_LEN = offset;
212
+ return ptr;
213
+ }
214
+
215
+ function takeObject(idx) {
216
+ const ret = getObject(idx);
217
+ dropObject(idx);
218
+ return ret;
219
+ }
220
+
221
+ let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
222
+ cachedTextDecoder.decode();
223
+ const MAX_SAFARI_DECODE_BYTES = 2146435072;
224
+ let numBytesDecoded = 0;
225
+ function decodeText(ptr, len) {
226
+ numBytesDecoded += len;
227
+ if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
228
+ cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
229
+ cachedTextDecoder.decode();
230
+ numBytesDecoded = len;
231
+ }
232
+ return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
233
+ }
234
+
235
+ const cachedTextEncoder = new TextEncoder();
236
+
237
+ if (!('encodeInto' in cachedTextEncoder)) {
238
+ cachedTextEncoder.encodeInto = function (arg, view) {
239
+ const buf = cachedTextEncoder.encode(arg);
240
+ view.set(buf);
241
+ return {
242
+ read: arg.length,
243
+ written: buf.length
244
+ };
245
+ };
246
+ }
247
+
248
+ let WASM_VECTOR_LEN = 0;
249
+
250
+ let wasmModule, wasm;
251
+ function __wbg_finalize_init(instance, module) {
252
+ wasm = instance.exports;
253
+ wasmModule = module;
254
+ cachedDataViewMemory0 = null;
255
+ cachedUint8ArrayMemory0 = null;
256
+ return wasm;
257
+ }
258
+
259
+ async function __wbg_load(module, imports) {
260
+ if (typeof Response === 'function' && module instanceof Response) {
261
+ if (typeof WebAssembly.instantiateStreaming === 'function') {
262
+ try {
263
+ return await WebAssembly.instantiateStreaming(module, imports);
264
+ } catch (e) {
265
+ const validResponse = module.ok && expectedResponseType(module.type);
266
+
267
+ if (validResponse && module.headers.get('Content-Type') !== 'application/wasm') {
268
+ console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);
269
+
270
+ } else { throw e; }
271
+ }
272
+ }
273
+
274
+ const bytes = await module.arrayBuffer();
275
+ return await WebAssembly.instantiate(bytes, imports);
276
+ } else {
277
+ const instance = await WebAssembly.instantiate(module, imports);
278
+
279
+ if (instance instanceof WebAssembly.Instance) {
280
+ return { instance, module };
281
+ } else {
282
+ return instance;
283
+ }
284
+ }
285
+
286
+ function expectedResponseType(type) {
287
+ switch (type) {
288
+ case 'basic': case 'cors': case 'default': return true;
289
+ }
290
+ return false;
291
+ }
292
+ }
293
+
294
+ function initSync(module) {
295
+ if (wasm !== undefined) return wasm;
296
+
297
+
298
+ if (module !== undefined) {
299
+ if (Object.getPrototypeOf(module) === Object.prototype) {
300
+ ({module} = module)
301
+ } else {
302
+ console.warn('using deprecated parameters for `initSync()`; pass a single object instead')
303
+ }
304
+ }
305
+
306
+ const imports = __wbg_get_imports();
307
+ if (!(module instanceof WebAssembly.Module)) {
308
+ module = new WebAssembly.Module(module);
309
+ }
310
+ const instance = new WebAssembly.Instance(module, imports);
311
+ return __wbg_finalize_init(instance, module);
312
+ }
313
+
314
+ async function __wbg_init(module_or_path) {
315
+ if (wasm !== undefined) return wasm;
316
+
317
+
318
+ if (module_or_path !== undefined) {
319
+ if (Object.getPrototypeOf(module_or_path) === Object.prototype) {
320
+ ({module_or_path} = module_or_path)
321
+ } else {
322
+ console.warn('using deprecated parameters for the initialization function; pass a single object instead')
323
+ }
324
+ }
325
+
326
+ if (module_or_path === undefined) {
327
+ module_or_path = new URL('nagami_bg.wasm', import.meta.url);
328
+ }
329
+ const imports = __wbg_get_imports();
330
+
331
+ if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) {
332
+ module_or_path = fetch(module_or_path);
333
+ }
334
+
335
+ const { instance, module } = await __wbg_load(await module_or_path, imports);
336
+
337
+ return __wbg_finalize_init(instance, module);
338
+ }
339
+
340
+ export { initSync, __wbg_init as default };
package/nagami_bg.wasm ADDED
Binary file
@@ -0,0 +1,10 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+ export const memory: WebAssembly.Memory;
4
+ export const minify: (a: number, b: number, c: number, d: number) => void;
5
+ export const version: (a: number) => void;
6
+ export const __wbindgen_export: (a: number, b: number) => number;
7
+ export const __wbindgen_export2: (a: number, b: number, c: number, d: number) => number;
8
+ export const __wbindgen_export3: (a: number) => void;
9
+ export const __wbindgen_add_to_stack_pointer: (a: number) => number;
10
+ export const __wbindgen_export4: (a: number, b: number, c: number) => void;
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "nagami-rs",
3
+ "type": "module",
4
+ "description": "Shrinks WGSL shaders via Naga IR optimization passes",
5
+ "version": "2026.4.11",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/ekarad1um/Nagami"
10
+ },
11
+ "files": [
12
+ "nagami_bg.wasm",
13
+ "nagami_bg.wasm.d.ts",
14
+ "nagami.js",
15
+ "nagami.d.ts"
16
+ ],
17
+ "main": "nagami.js",
18
+ "types": "nagami.d.ts",
19
+ "sideEffects": false,
20
+ "keywords": [
21
+ "wgsl",
22
+ "shader",
23
+ "minify",
24
+ "minifier",
25
+ "webgpu",
26
+ "naga",
27
+ "optimizer"
28
+ ],
29
+ "exports": {
30
+ ".": {
31
+ "types": "./nagami.d.ts",
32
+ "import": "./nagami.js"
33
+ }
34
+ },
35
+ "engines": {
36
+ "node": ">=16"
37
+ }
38
+ }