compress-shader-literals 0.0.3 → 0.0.4
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 +87 -23
- package/package.json +9 -1
package/README.md
CHANGED
|
@@ -1,43 +1,107 @@
|
|
|
1
1
|
# compress-shader-literals
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
> **Size matters.** Minify your GLSL & WGSL shader strings at build time — automatically, in any bundler.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
[](https://www.npmjs.com/package/compress-shader-literals)
|
|
6
|
+
[](https://bundlephobia.com/package/compress-shader-literals)
|
|
7
|
+
[](https://www.npmjs.com/package/compress-shader-literals)
|
|
8
|
+
[](./LICENSE)
|
|
9
|
+
|
|
10
|
+
Your shaders ship with every comment, indent, and blank line you wrote them with. This strips all of it — block comments, line comments, redundant whitespace — from `glsl` / `wgsl` template literals, **before** your bundler minifies the rest. Smaller bundles, zero source changes, full sourcemaps.
|
|
11
|
+
|
|
12
|
+
Built on [unplugin](https://github.com/unjs/unplugin), so it runs in **Vite, Rollup, webpack, esbuild, Rspack, Rolldown & Farm** from one package.
|
|
13
|
+
|
|
14
|
+
## Install
|
|
15
|
+
|
|
16
|
+
```sh
|
|
17
|
+
npm i -D compress-shader-literals
|
|
18
|
+
# or: bun add -d compress-shader-literals
|
|
19
|
+
```
|
|
6
20
|
|
|
7
21
|
## Usage
|
|
8
22
|
|
|
23
|
+
Pick your bundler — same plugin, same options:
|
|
24
|
+
|
|
9
25
|
```js
|
|
10
|
-
|
|
11
|
-
import { compressShaderLiterals } from './compress-shader-literals/plugin.js';
|
|
26
|
+
import { compressShaderLiterals } from 'compress-shader-literals';
|
|
12
27
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
};
|
|
16
|
-
```
|
|
28
|
+
// Vite vite.config.js
|
|
29
|
+
export default { plugins: [compressShaderLiterals.vite({ outputRatio: true })] };
|
|
17
30
|
|
|
18
|
-
|
|
31
|
+
// Rollup rollup.config.js → compressShaderLiterals.rollup({ ... })
|
|
32
|
+
// webpack webpack.config.js → compressShaderLiterals.webpack({ ... })
|
|
33
|
+
// esbuild build script → compressShaderLiterals.esbuild({ ... })
|
|
34
|
+
// Rspack / Rolldown / Farm → .rspack() / .rolldown() / .farm()
|
|
35
|
+
```
|
|
19
36
|
|
|
20
|
-
|
|
21
|
-
| ------------- | ---------------------------- | ------------------------------ |
|
|
22
|
-
| `tags` | `['glsl', 'wgsl', 'shader']` | Tag names to match |
|
|
23
|
-
| `include` | `/\.[jt]sx?$/` | Files to process |
|
|
24
|
-
| `exclude` | `/node_modules/`, `/dist/` | Files to skip |
|
|
25
|
-
| `outputRatio` | `false` | Print size summary after build |
|
|
37
|
+
That's it. Shaders are compressed on the way through; everything else is untouched.
|
|
26
38
|
|
|
27
|
-
##
|
|
39
|
+
## What it compresses
|
|
28
40
|
|
|
29
41
|
```ts
|
|
30
42
|
// Tagged template literal
|
|
31
|
-
const vert =
|
|
43
|
+
const vert = glsl`
|
|
44
|
+
// vertex shader ← stripped
|
|
45
|
+
precision highp float;
|
|
46
|
+
void main() { gl_Position = vec4(0.0); }
|
|
47
|
+
`;
|
|
32
48
|
|
|
33
|
-
// Comment-prefixed template literal
|
|
34
|
-
const frag = /*
|
|
49
|
+
// Comment-prefixed template literal (great for editor syntax highlighting)
|
|
50
|
+
const frag = /* wgsl */ `
|
|
51
|
+
/* fragment */
|
|
52
|
+
fn main() {}
|
|
53
|
+
`;
|
|
35
54
|
```
|
|
36
55
|
|
|
37
|
-
|
|
56
|
+
→ both become a single tight line, no comments, no padding.
|
|
57
|
+
|
|
58
|
+
## Options
|
|
59
|
+
|
|
60
|
+
| Option | Default | Description |
|
|
61
|
+
| ------------- | ---------------------------- | --------------------------------------- |
|
|
62
|
+
| `tags` | `['glsl', 'wgsl', 'shader']` | Tag names / comment markers to match |
|
|
63
|
+
| `include` | `[/\.[jt]sx?$/]` | Files to process |
|
|
64
|
+
| `exclude` | `[/node_modules/, /dist/]` | Files to skip |
|
|
65
|
+
| `outputRatio` | `false` | Print a bytes-saved summary after build |
|
|
66
|
+
|
|
67
|
+
With `outputRatio: true`:
|
|
38
68
|
|
|
39
|
-
```sh
|
|
40
|
-
node compress-shader-literals/run-test.js
|
|
41
69
|
```
|
|
70
|
+
compress-shader-literals
|
|
71
|
+
────────────────────────
|
|
72
|
+
215.00 B → 134.00 B
|
|
73
|
+
saved: 81.00 B (37.67% smaller)
|
|
74
|
+
shaders: 1
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## Real-world stats
|
|
78
|
+
|
|
79
|
+
Run against the actual shaders shipped in **[three.js](https://www.npmjs.com/package/three)**, **[ogl](https://www.npmjs.com/package/ogl)**, **[shader-park-core](https://www.npmjs.com/package/shader-park-core)**, **[curtains.js](https://www.npmjs.com/package/curtainsjs)** and more — measured by this plugin's own engine ([`tests/e2e.js`](tests/e2e.js)):
|
|
80
|
+
|
|
81
|
+
<!-- STATS:START -->
|
|
82
|
+
|
|
83
|
+
| Package | Shaders | Before | After | Saved |
|
|
84
|
+
| --------------------- | ------: | --------: | --------: | --------: |
|
|
85
|
+
| `three` | 281 | 240,772 B | 203,428 B | **15.5%** |
|
|
86
|
+
| `@jayf0x/fluidity-js` | 9 | 9,524 B | 7,133 B | **25.1%** |
|
|
87
|
+
| `ogl` | 22 | 6,109 B | 5,335 B | **12.7%** |
|
|
88
|
+
| `shader-park-core` | 18 | 10,794 B | 9,033 B | **16.3%** |
|
|
89
|
+
| `curtainsjs` | 7 | 3,406 B | 2,563 B | **24.8%** |
|
|
90
|
+
| **Total** | 337 | 270,605 B | 227,492 B | **15.9%** |
|
|
91
|
+
|
|
92
|
+
_337 real shaders from 5 package(s), compressed by this plugin's engine._
|
|
93
|
+
|
|
94
|
+
<!-- STATS:END -->
|
|
95
|
+
|
|
96
|
+
Free bytes off code you already ship — no source changes. Marginal per shader, but it compounds across every shader in every bundle, and at infra scale even 1% is worth shipping.
|
|
97
|
+
|
|
98
|
+
## How it works
|
|
99
|
+
|
|
100
|
+
1. Parses each matched file with Babel (TS/JSX aware).
|
|
101
|
+
2. Finds tagged and comment-prefixed shader literals (see [What it compresses](#what-it-compresses)).
|
|
102
|
+
3. Strips comments + collapses whitespace, rewriting via [`magic-string`](https://github.com/Rich-Harris/magic-string) so **sourcemaps stay intact**.
|
|
103
|
+
4. Runs as a `pre` transform, so your bundler's own minifier still sees the result.
|
|
104
|
+
|
|
105
|
+
## License
|
|
42
106
|
|
|
43
|
-
|
|
107
|
+
[MIT](./LICENSE) © jayF0x
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "compress-shader-literals",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Size matters. Minify GLSL/WGSL shader template literals at build time — universal plugin for Vite, Rollup, webpack, esbuild & more.",
|
|
6
6
|
"keywords": [
|
|
@@ -11,6 +11,13 @@
|
|
|
11
11
|
"compress",
|
|
12
12
|
"webgl",
|
|
13
13
|
"webgpu",
|
|
14
|
+
"three",
|
|
15
|
+
"threejs",
|
|
16
|
+
"ogl",
|
|
17
|
+
"react-three-fiber",
|
|
18
|
+
"shader-park",
|
|
19
|
+
"curtainsjs",
|
|
20
|
+
"shadertoy",
|
|
14
21
|
"vite-plugin",
|
|
15
22
|
"rollup-plugin",
|
|
16
23
|
"webpack-plugin",
|
|
@@ -42,6 +49,7 @@
|
|
|
42
49
|
"build": "bun build src/plugin.js --outdir dist --entry-naming index.js --target node --format esm --external @babel/parser --external @babel/traverse --external @rollup/pluginutils --external magic-string --external unplugin && node scripts/compress-dist.js",
|
|
43
50
|
"test": "bun test",
|
|
44
51
|
"test:run": "bun test",
|
|
52
|
+
"test:e2e": "cd tests && bun install && node e2e.js",
|
|
45
53
|
"format": "prettier --write .",
|
|
46
54
|
"format:check": "prettier --check .",
|
|
47
55
|
"npm:deploy": "bash ./scripts/publish-npm.sh",
|