compress-shader-literals 1.3.6 → 1.3.8
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 +30 -23
- package/dist/README.md +145 -0
- package/dist/defaults-BdBaB3Yd.cjs +2 -0
- package/dist/defaults-CxdkREQu.js +26 -0
- package/dist/index.cjs +6 -6
- package/dist/index.d.ts +7 -0
- package/dist/index.js +52 -49
- package/dist/validate-Cp5hJk0B.js +29 -0
- package/dist/validate-vYVHSZBv.cjs +1 -0
- package/package.json +18 -2
package/README.md
CHANGED
|
@@ -66,6 +66,13 @@ const frag = /* wgsl */ `
|
|
|
66
66
|
| `outputRatio` | `false` | Print a bytes-saved summary after build |
|
|
67
67
|
| `transform` | built-in `minifyShader` | Custom minifier — `(shader: string) => string` |
|
|
68
68
|
| `debug` | `false` | Log each file's discovered literals to the console |
|
|
69
|
+
| `validate` | `false` | Re-parse each changed shader and warn on build if one stops parsing |
|
|
70
|
+
|
|
71
|
+
**`validate: true`** — re-parses every shader the plugin touches, before and after, and warns (via the bundler's normal warning channel) if one stopped parsing. Needs [`@shaderfrog/glsl-parser`](https://www.npmjs.com/package/@shaderfrog/glsl-parser) (GLSL) and/or [`wgsl_reflect`](https://www.npmjs.com/package/wgsl_reflect) (WGSL) installed — both are optional peer dependencies, loaded lazily so builds that don't opt in never pay for them:
|
|
72
|
+
|
|
73
|
+
```js
|
|
74
|
+
compressShaderLiterals.vite({ validate: true });
|
|
75
|
+
```
|
|
69
76
|
|
|
70
77
|
**`scan: 'loose'`** — for files Babel can't parse (anything that isn't plain JS/TS: Svelte components, Astro components, etc). Instead of a whole-file AST parse, it matches the same tagged/comment-prefixed literal shapes by regex. Opt in and point `include` at the files yourself — there's no whole-file syntax guarantee, so a match is only touched once its content also looks like a real shader:
|
|
71
78
|
|
|
@@ -95,29 +102,29 @@ Real shaders shipped by popular libraries, run through the built-in minifier:
|
|
|
95
102
|
|
|
96
103
|
| Package | Shaders | Before | After | Saved | Net after Brotli |
|
|
97
104
|
| ----------------------------- | ------: | ----------: | ----------: | --------: | ---------------: |
|
|
98
|
-
| `vtk.js` | 142 | 276,617 B |
|
|
99
|
-
| `three-stdlib` | 370 | 429,350 B |
|
|
100
|
-
| `curtainsjs` | 7 | 3,406 B | 2,
|
|
101
|
-
| `hydra-synth` | 15 | 3,852 B | 2,
|
|
102
|
-
| `cesium` | 546 | 951,897 B |
|
|
103
|
-
| `troika-three-utils` | 4 | 168 B | 120 B | **28.6%** |
|
|
104
|
-
| `
|
|
105
|
-
| `
|
|
106
|
-
| `@luma.gl/shadertools` | 24 | 149,192 B |
|
|
107
|
-
| `three` | 281 | 240,906 B |
|
|
108
|
-
| `
|
|
109
|
-
| `
|
|
110
|
-
| `deck.gl` | 132 | 242,113 B |
|
|
111
|
-
| `@paper-design/shaders` | 30 | 142,466 B |
|
|
112
|
-
| `@deck.gl/layers` | 104 | 223,902 B |
|
|
113
|
-
| `@deck.gl/core` | 40 | 17,746 B | 15,
|
|
114
|
-
| `@deck.gl/aggregation-layers` | 56 | 43,713 B |
|
|
115
|
-
| `@luma.gl/engine` | 29 | 11,357 B | 10,
|
|
116
|
-
| `@babylonjs/core` | 349 | 669,740 B |
|
|
117
|
-
| `postprocessing` | 136 | 179,705 B | 179,705 B | **0.0%** |
|
|
118
|
-
| **Total** | 3323 | 4,976,645 B | 3,
|
|
119
|
-
|
|
120
|
-
|
|
105
|
+
| `vtk.js` | 142 | 276,617 B | 157,780 B | **43.0%** | +39.5% |
|
|
106
|
+
| `three-stdlib` | 370 | 429,350 B | 266,774 B | **37.9%** | +35.5% |
|
|
107
|
+
| `curtainsjs` | 7 | 3,406 B | 2,290 B | **32.8%** | +10.4% |
|
|
108
|
+
| `hydra-synth` | 15 | 3,852 B | 2,591 B | **32.7%** | +17.9% |
|
|
109
|
+
| `cesium` | 546 | 951,897 B | 649,851 B | **31.7%** | +32.6% |
|
|
110
|
+
| `troika-three-utils` | 4 | 168 B | 120 B | **28.6%** | +32.3% |
|
|
111
|
+
| `shader-park-core` | 18 | 10,794 B | 7,863 B | **27.2%** | +19.7% |
|
|
112
|
+
| `pixi.js` | 162 | 75,768 B | 55,260 B | **27.1%** | +16.3% |
|
|
113
|
+
| `@luma.gl/shadertools` | 24 | 149,192 B | 109,615 B | **26.5%** | +29.9% |
|
|
114
|
+
| `three` | 281 | 240,906 B | 181,942 B | **24.5%** | +25.5% |
|
|
115
|
+
| `ogl` | 22 | 6,109 B | 4,821 B | **21.1%** | +9.1% |
|
|
116
|
+
| `playcanvas` | 856 | 1,297,844 B | 1,025,421 B | **21.0%** | +49.1% |
|
|
117
|
+
| `deck.gl` | 132 | 242,113 B | 195,406 B | **19.3%** | +31.1% |
|
|
118
|
+
| `@paper-design/shaders` | 30 | 142,466 B | 118,204 B | **17.0%** | +8.9% |
|
|
119
|
+
| `@deck.gl/layers` | 104 | 223,902 B | 192,483 B | **14.0%** | +32.4% |
|
|
120
|
+
| `@deck.gl/core` | 40 | 17,746 B | 15,494 B | **12.7%** | +9.1% |
|
|
121
|
+
| `@deck.gl/aggregation-layers` | 56 | 43,713 B | 38,382 B | **12.2%** | +25.3% |
|
|
122
|
+
| `@luma.gl/engine` | 29 | 11,357 B | 10,206 B | **10.1%** | +3.1% |
|
|
123
|
+
| `@babylonjs/core` | 349 | 669,740 B | 660,843 B | **1.3%** | +2.1% |
|
|
124
|
+
| `postprocessing` | 136 | 179,705 B | 179,705 B | **0.0%** | +0.0% |
|
|
125
|
+
| **Total** | 3323 | 4,976,645 B | 3,875,051 B | **22.1%** | — |
|
|
126
|
+
|
|
127
|
+
_2477/3323 parseable shaders. ✅ Verified 2026-07-13. [How this is measured](docs/stats.md)_
|
|
121
128
|
|
|
122
129
|
<!-- STATS:END -->
|
|
123
130
|
|
package/dist/README.md
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
# compress-shader-literals
|
|
2
|
+
|
|
3
|
+
> **Size matters.** Strip everything strippable from your GLSL & WGSL shaders at build time.
|
|
4
|
+
|
|
5
|
+
> ⭐ Star this repository to support development and help others discover it.
|
|
6
|
+
|
|
7
|
+
[](https://www.npmjs.com/package/compress-shader-literals)
|
|
8
|
+
[](https://bundlephobia.com/package/compress-shader-literals)
|
|
9
|
+
[](https://www.npmjs.com/package/compress-shader-literals)
|
|
10
|
+
[](./LICENSE)
|
|
11
|
+
|
|
12
|
+
A tiny build-time minifier for GLSL & WGSL shaders written as template literals in your JS/TS. Strips comments + whitespace — any bundler, no renaming, no toolchain, no runtime cost.
|
|
13
|
+
|
|
14
|
+

|
|
15
|
+
|
|
16
|
+
## Install
|
|
17
|
+
|
|
18
|
+
<!-- [](https://www.npmjs.com/package/compress-shader-literals) -->
|
|
19
|
+
|
|
20
|
+
```sh
|
|
21
|
+
bun add -d compress-shader-literals
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## About
|
|
25
|
+
|
|
26
|
+
- Removes comments, collapses whitespace, joins statements, and trims space around delimiters. Whitespace and comments only.
|
|
27
|
+
- No renaming, no dead-code removal, no operator-space removal — output stays valid.
|
|
28
|
+
- Runs before your bundler's own minifier.
|
|
29
|
+
|
|
30
|
+
## Usage
|
|
31
|
+
|
|
32
|
+
One [unplugin](https://github.com/unjs/unplugin) plugin, same API for every bundler:
|
|
33
|
+
|
|
34
|
+
```js
|
|
35
|
+
import { compressShaderLiterals } from 'compress-shader-literals';
|
|
36
|
+
|
|
37
|
+
// vite.config.js
|
|
38
|
+
export default { plugins: [compressShaderLiterals.vite({ outputRatio: true })] };
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
For Rollup, webpack, esbuild, Rspack, Rolldown or Farm, call the matching method — `.rollup()`, `.webpack()`, `.esbuild()`, `.rspack()`, `.rolldown()`, `.farm()` — with the same options.
|
|
42
|
+
|
|
43
|
+
Finds tagged and comment-prefixed literals. The comment form keeps editor syntax highlighting:
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
const vert = glsl`
|
|
47
|
+
// vertex shader ← stripped
|
|
48
|
+
precision highp float;
|
|
49
|
+
void main() { gl_Position = vec4(0.0); }
|
|
50
|
+
`;
|
|
51
|
+
|
|
52
|
+
const frag = /* wgsl */ `
|
|
53
|
+
/* fragment */
|
|
54
|
+
fn main() {}
|
|
55
|
+
`;
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
**Options**
|
|
59
|
+
|
|
60
|
+
| Option | Default | Description |
|
|
61
|
+
| ------------- | ---------------------------- | -------------------------------------------------------------------------- |
|
|
62
|
+
| `tags` | `['glsl', 'wgsl', 'shader']` | Tag names / comment markers to match |
|
|
63
|
+
| `scan` | `'ast'` | `'ast'` parses the file with Babel; `'loose'` matches by regex — see below |
|
|
64
|
+
| `include` | `[/\.[mc]?[jt]sx?$/]` | Files to process — the JS/TS family Babel can parse |
|
|
65
|
+
| `exclude` | `[/node_modules/, /dist/]` | Files to skip — dependencies are skipped by default |
|
|
66
|
+
| `outputRatio` | `false` | Print a bytes-saved summary after build |
|
|
67
|
+
| `transform` | built-in `minifyShader` | Custom minifier — `(shader: string) => string` |
|
|
68
|
+
| `debug` | `false` | Log each file's discovered literals to the console |
|
|
69
|
+
| `validate` | `false` | Re-parse each changed shader and warn on build if one stops parsing |
|
|
70
|
+
|
|
71
|
+
**`validate: true`** — re-parses every shader the plugin touches, before and after, and warns (via the bundler's normal warning channel) if one stopped parsing. Needs [`@shaderfrog/glsl-parser`](https://www.npmjs.com/package/@shaderfrog/glsl-parser) (GLSL) and/or [`wgsl_reflect`](https://www.npmjs.com/package/wgsl_reflect) (WGSL) installed — both are optional peer dependencies, loaded lazily so builds that don't opt in never pay for them:
|
|
72
|
+
|
|
73
|
+
```js
|
|
74
|
+
compressShaderLiterals.vite({ validate: true });
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
**`scan: 'loose'`** — for files Babel can't parse (anything that isn't plain JS/TS: Svelte components, Astro components, etc). Instead of a whole-file AST parse, it matches the same tagged/comment-prefixed literal shapes by regex. Opt in and point `include` at the files yourself — there's no whole-file syntax guarantee, so a match is only touched once its content also looks like a real shader:
|
|
78
|
+
|
|
79
|
+
```js
|
|
80
|
+
compressShaderLiterals.vite({ scan: 'loose', include: [/\.svelte$/] });
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
**Programmatic API** — the core helpers are exported for tooling authors (validators, ESLint rules, CLIs), no plugin required:
|
|
84
|
+
|
|
85
|
+
```js
|
|
86
|
+
import { extractShaderLiterals, extractShaderLiteralsLoose, minifyShader } from 'compress-shader-literals';
|
|
87
|
+
|
|
88
|
+
extractShaderLiterals('const v = glsl`void main() {}`');
|
|
89
|
+
// → [{ tag: 'glsl', value: 'void main() {}', start: 14, end: 30 }]
|
|
90
|
+
|
|
91
|
+
extractShaderLiteralsLoose('<script>const v = glsl`void main() {}`</script>');
|
|
92
|
+
// → same shape, found by regex instead of a Babel parse
|
|
93
|
+
|
|
94
|
+
minifyShader('// comment\nvoid main() {}'); // → 'void main() {}'
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## Stats
|
|
98
|
+
|
|
99
|
+
Real shaders shipped by popular libraries, run through the built-in minifier:
|
|
100
|
+
|
|
101
|
+
<!-- STATS:START -->
|
|
102
|
+
|
|
103
|
+
| Package | Shaders | Before | After | Saved | Net after Brotli |
|
|
104
|
+
| ----------------------------- | ------: | ----------: | ----------: | --------: | ---------------: |
|
|
105
|
+
| `vtk.js` | 142 | 276,617 B | 157,780 B | **43.0%** | +39.5% |
|
|
106
|
+
| `three-stdlib` | 370 | 429,350 B | 266,774 B | **37.9%** | +35.5% |
|
|
107
|
+
| `curtainsjs` | 7 | 3,406 B | 2,290 B | **32.8%** | +10.4% |
|
|
108
|
+
| `hydra-synth` | 15 | 3,852 B | 2,591 B | **32.7%** | +17.9% |
|
|
109
|
+
| `cesium` | 546 | 951,897 B | 649,851 B | **31.7%** | +32.6% |
|
|
110
|
+
| `troika-three-utils` | 4 | 168 B | 120 B | **28.6%** | +32.3% |
|
|
111
|
+
| `shader-park-core` | 18 | 10,794 B | 7,863 B | **27.2%** | +19.7% |
|
|
112
|
+
| `pixi.js` | 162 | 75,768 B | 55,260 B | **27.1%** | +16.3% |
|
|
113
|
+
| `@luma.gl/shadertools` | 24 | 149,192 B | 109,615 B | **26.5%** | +29.9% |
|
|
114
|
+
| `three` | 281 | 240,906 B | 181,942 B | **24.5%** | +25.5% |
|
|
115
|
+
| `ogl` | 22 | 6,109 B | 4,821 B | **21.1%** | +9.1% |
|
|
116
|
+
| `playcanvas` | 856 | 1,297,844 B | 1,025,421 B | **21.0%** | +49.1% |
|
|
117
|
+
| `deck.gl` | 132 | 242,113 B | 195,406 B | **19.3%** | +31.1% |
|
|
118
|
+
| `@paper-design/shaders` | 30 | 142,466 B | 118,204 B | **17.0%** | +8.9% |
|
|
119
|
+
| `@deck.gl/layers` | 104 | 223,902 B | 192,483 B | **14.0%** | +32.4% |
|
|
120
|
+
| `@deck.gl/core` | 40 | 17,746 B | 15,494 B | **12.7%** | +9.1% |
|
|
121
|
+
| `@deck.gl/aggregation-layers` | 56 | 43,713 B | 38,382 B | **12.2%** | +25.3% |
|
|
122
|
+
| `@luma.gl/engine` | 29 | 11,357 B | 10,206 B | **10.1%** | +3.1% |
|
|
123
|
+
| `@babylonjs/core` | 349 | 669,740 B | 660,843 B | **1.3%** | +2.1% |
|
|
124
|
+
| `postprocessing` | 136 | 179,705 B | 179,705 B | **0.0%** | +0.0% |
|
|
125
|
+
| **Total** | 3323 | 4,976,645 B | 3,875,051 B | **22.1%** | — |
|
|
126
|
+
|
|
127
|
+
_2477/3323 parseable shaders. ✅ Verified 2026-07-13. [How this is measured](docs/stats.md)_
|
|
128
|
+
|
|
129
|
+
<!-- STATS:END -->
|
|
130
|
+
|
|
131
|
+
## How it works
|
|
132
|
+
|
|
133
|
+
1. Parses each matched file with Babel — `.js`, `.jsx`, `.ts`, `.tsx`, `.mjs`, `.cjs`, `.mts`, `.cts` by default. `scan: 'loose'` skips the parse and matches by regex instead, for files Babel can't parse.
|
|
134
|
+
2. Finds tagged (`` glsl`…` ``) and comment-prefixed (`/* glsl */ \`…\``) literals, skipping any with `${…}` interpolation.
|
|
135
|
+
3. Strips comments, collapses whitespace, and joins statements onto one line — keeping real newlines around `#` preprocessor directives and `\` line-continuations (which are newline-sensitive). Whitespace hugging a delimiter (`( ) { } ; ,`) is removed entirely; whitespace around operators (and `=`, to stay WGSL-generic-safe) is preserved, so adjacent tokens never merge.
|
|
136
|
+
4. Rewrites the literal in place with [magic-string](https://github.com/Rich-Harris/magic-string), so sourcemaps stay intact.
|
|
137
|
+
5. Runs as a `pre` transform, so your bundler's own minifier still sees the result.
|
|
138
|
+
|
|
139
|
+
## Issues & compatibility
|
|
140
|
+
|
|
141
|
+
If you run into any problems — wrong output, a shader pattern that isn't picked up, or a library that doesn't play nicely — please [open an issue](https://github.com/jayf0x/compress-shader-literals/issues) or just mention which library you're using and I'll add it to the test suite.
|
|
142
|
+
|
|
143
|
+
## License
|
|
144
|
+
|
|
145
|
+
[MIT](./LICENSE) © [jayF0x](https://github.com/jayf0x)
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var e=[`glsl`,`wgsl`,`shader`],t=[/\.[mc]?[jt]sx?$/],n=[/node_modules/,/dist/],r=e=>RegExp(`^\\s*(${e.join(`|`)})\\s*$`),i=/\b(gl_FragColor|gl_Position|void\s+main|precision\s+(highp|mediump|lowp)|fn\s+main)\b/,a=/\r\n/g,o=/\/\*[\s\S]*?\*\//g,s=/\/\/.*$/gm,c=/[ \t]+/g,l=/\s*([(){};,])\s*/g,u=/@vertex|@fragment|@group|var<|@compute/,d=e=>u.test(e)&&!/void\s+main/.test(e),f=/(?<!>)\s+=\s+/g,p=/\\(?!\n)/,m=e=>!p.test(e);function h(e,t){let n=e.replace(a,`
|
|
2
|
+
`).replace(o,``).replace(s,``),r=e=>{let t=[0,0,0,0,0,0];for(let n of e){let e=`()[]{}`.indexOf(n);e>=0&&t[e]++}return t.join(`,`)};return r(n)===r(t)}Object.defineProperty(exports,"a",{enumerable:!0,get:function(){return a}}),Object.defineProperty(exports,"c",{enumerable:!0,get:function(){return c}}),Object.defineProperty(exports,"d",{enumerable:!0,get:function(){return h}}),Object.defineProperty(exports,"f",{enumerable:!0,get:function(){return m}}),Object.defineProperty(exports,"i",{enumerable:!0,get:function(){return o}}),Object.defineProperty(exports,"l",{enumerable:!0,get:function(){return s}}),Object.defineProperty(exports,"m",{enumerable:!0,get:function(){return r}}),Object.defineProperty(exports,"n",{enumerable:!0,get:function(){return t}}),Object.defineProperty(exports,"o",{enumerable:!0,get:function(){return l}}),Object.defineProperty(exports,"p",{enumerable:!0,get:function(){return d}}),Object.defineProperty(exports,"r",{enumerable:!0,get:function(){return e}}),Object.defineProperty(exports,"s",{enumerable:!0,get:function(){return f}}),Object.defineProperty(exports,"t",{enumerable:!0,get:function(){return n}}),Object.defineProperty(exports,"u",{enumerable:!0,get:function(){return i}});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
//#region src/defaults.js
|
|
2
|
+
var e = [
|
|
3
|
+
"glsl",
|
|
4
|
+
"wgsl",
|
|
5
|
+
"shader"
|
|
6
|
+
], t = [/\.[mc]?[jt]sx?$/], n = [/node_modules/, /dist/], r = (e) => RegExp(`^\\s*(${e.join("|")})\\s*$`), i = /\b(gl_FragColor|gl_Position|void\s+main|precision\s+(highp|mediump|lowp)|fn\s+main)\b/, a = /\r\n/g, o = /\/\*[\s\S]*?\*\//g, s = /\/\/.*$/gm, c = /[ \t]+/g, l = /\s*([(){};,])\s*/g, u = /@vertex|@fragment|@group|var<|@compute/, d = (e) => u.test(e) && !/void\s+main/.test(e), f = /(?<!>)\s+=\s+/g, p = /\\(?!\n)/, m = (e) => !p.test(e);
|
|
7
|
+
function h(e, t) {
|
|
8
|
+
let n = e.replace(a, "\n").replace(o, "").replace(s, ""), r = (e) => {
|
|
9
|
+
let t = [
|
|
10
|
+
0,
|
|
11
|
+
0,
|
|
12
|
+
0,
|
|
13
|
+
0,
|
|
14
|
+
0,
|
|
15
|
+
0
|
|
16
|
+
];
|
|
17
|
+
for (let n of e) {
|
|
18
|
+
let e = "()[]{}".indexOf(n);
|
|
19
|
+
e >= 0 && t[e]++;
|
|
20
|
+
}
|
|
21
|
+
return t.join(",");
|
|
22
|
+
};
|
|
23
|
+
return r(n) === r(t);
|
|
24
|
+
}
|
|
25
|
+
//#endregion
|
|
26
|
+
export { a, c, h as d, m as f, o as i, s as l, r as m, t as n, l as o, d as p, e as r, f as s, n as t, i as u };
|
package/dist/index.cjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},s=(n,r,a)=>(a=n==null?{}:e(i(n)),o(r||!n||!n.__esModule?t(a,`default`,{value:n,enumerable:!0}):a,n));
|
|
2
|
-
`).replace(
|
|
3
|
-
`).map(e=>e.replace(
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},s=(n,r,a)=>(a=n==null?{}:e(i(n)),o(r||!n||!n.__esModule?t(a,`default`,{value:n,enumerable:!0}):a,n));const c=require("./defaults-BdBaB3Yd.cjs");let l=require("@rollup/pluginutils"),u=require("byte-snap"),d=require("magic-string");d=s(d,1);let f=require("unplugin"),p=require("@babel/parser"),m=require("@babel/traverse");m=s(m,1);var h=m.default.default||m.default,g=(e,t=c.r)=>{let n=new Set(t),r=c.m(t),i=[];try{h((0,p.parse)(e,{sourceType:`module`,plugins:[`typescript`,`jsx`,`decorators-legacy`],allowReturnOutsideFunction:!0}),{TaggedTemplateExpression(e){let{tag:t,quasi:r}=e.node,a=null;t.type===`Identifier`&&n.has(t.name)?a=t.name:t.type===`MemberExpression`&&t.property.type===`Identifier`&&n.has(t.property.name)&&(a=t.property.name),a&&r.expressions.length===0&&i.push({tag:a,value:r.quasis[0].value.raw,start:r.start,end:r.end})},TemplateLiteral(e){let t=e.node;if(t.expressions.length>0)return;let n=t.leadingComments||e.parentPath?.node?.leadingComments||[];for(let e of n){if(!e||e.type!==`CommentBlock`)continue;let n=e.value.match(r);if(n){i.push({tag:n[1],value:t.quasis[0].value.raw,start:t.start,end:t.end});break}}}})}catch(e){if(e.name!==`SyntaxError`)throw e}return i},_=(e,t)=>{for(let n=t;n<e.length;n++)if(e[n]===`\\`)n++;else if(e[n]==="`")return n;return-1},v=(e,t=c.r)=>{let n=t.map(e=>e.replace(/[.*+?^${}()|[\]\\]/g,`\\$&`)).join(`|`),r=RegExp(`\\b(${n})\\s*\`|/\\*\\s*(${n})\\s*\\*/\\s*\``,`g`),i=[],a;for(;a=r.exec(e);){let t=a.index+a[0].length,n=_(e,t);if(n===-1)continue;let o=e.slice(t,n);!o.includes("${")&&c.u.test(o)&&i.push({tag:a[1]??a[2],value:o,start:t-1,end:n+1}),r.lastIndex=n+1}return i},y=e=>{let t=e.replace(c.a,`
|
|
2
|
+
`).replace(c.i,``).replace(c.l,``).split(`
|
|
3
|
+
`).map(e=>e.replace(c.c,` `).trim()).filter(Boolean),n=``,r=!1;for(let e of t)e.startsWith(`#`)||r?n+=(n&&!n.endsWith(`
|
|
4
4
|
`)?`
|
|
5
5
|
`:``)+e+`
|
|
6
6
|
`:n+=n===``||n.endsWith(`
|
|
7
|
-
`)?e:` `+e,r=e.endsWith(`\\`);return r=!1,n=n.split(`
|
|
8
|
-
`).map(e=>{
|
|
9
|
-
`),n.trim()},
|
|
7
|
+
`)?e:` `+e,r=e.endsWith(`\\`);let i=c.p(n);return r=!1,n=n.split(`
|
|
8
|
+
`).map(e=>{if(e.startsWith(`#`)||r)return r=e.endsWith(`\\`),e;let t=e.replace(c.o,`$1`);return i||(t=t.replace(c.s,`=`)),r=e.endsWith(`\\`),t}).join(`
|
|
9
|
+
`),n.trim()},b,x=(0,f.createUnplugin)((e={})=>{let t=e.tags||c.r,n=e.transform||y,r=e.scan===`loose`?v:g,i=(0,l.createFilter)(e.include||c.n,e.exclude||c.t),a=``,o=``,s=0;return{name:`compress-shader-literals`,enforce:`pre`,transform(c,l){if(!i(l)||!t.some(e=>c.includes(e)))return null;let u=r(c,t);if(u.length===0)return null;e.debug&&console.log(`[compress-shader-literals] ${l}: ${u.length} literal(s) — ${u.map(e=>e.tag).join(`, `)}`);let f=new d.default(c),p=!1,m=[];for(let t of u){let r=n(t.value);t.value!==r&&(f.overwrite(t.start,t.end,`\`${r}\``),p=!0,e.validate&&(b??=Promise.resolve().then(()=>require("./validate-vYVHSZBv.cjs")),m.push(b.then(({validateShader:e})=>e(t.value,r)).then(e=>{e===`broken`&&this.warn(`compress-shader-literals: minified ${t.tag}\`...\` in ${l} stopped parsing`)})))),e.outputRatio&&(a+=t.value,o+=r,s++)}if(!p)return null;let h={code:f.toString(),map:f.generateMap({hires:!0,source:l})};return m.length?Promise.all(m).then(()=>h):h},buildEnd(){if(e.outputRatio&&a){let e=`compress-shader-literals: ${s} shader literal${s===1?``:`s`}`;(0,u.diff)(u.snap.text(a),u.snap.text(o)).print(e)}}}});exports.compressShaderLiterals=x,exports.extractShaderLiterals=g,exports.minifyShader=y;
|
package/dist/index.d.ts
CHANGED
|
@@ -24,6 +24,13 @@ export interface CompressShaderLiteralsOptions {
|
|
|
24
24
|
transform?: (shader: string) => string;
|
|
25
25
|
/** Log each file's discovered literals to the console. Default: `false` */
|
|
26
26
|
debug?: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* Re-parse each changed shader before/after minifying and warn on build if
|
|
29
|
+
* one stops parsing. Needs `@shaderfrog/glsl-parser` (GLSL) and/or
|
|
30
|
+
* `wgsl_reflect` (WGSL) installed — both are optional peer dependencies,
|
|
31
|
+
* loaded lazily so non-validating builds never pay for them. Default: `false`
|
|
32
|
+
*/
|
|
33
|
+
validate?: boolean;
|
|
27
34
|
}
|
|
28
35
|
|
|
29
36
|
/** A shader literal discovered in source. */
|
package/dist/index.js
CHANGED
|
@@ -1,18 +1,15 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import {
|
|
6
|
-
import
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
"shader"
|
|
12
|
-
], c = [/\.[mc]?[jt]sx?$/], l = [/node_modules/, /dist/], u = (e) => RegExp(`^\\s*(${e.join("|")})\\s*$`), d = /\b(gl_FragColor|gl_Position|void\s+main|precision\s+(highp|mediump|lowp)|fn\s+main)\b/, f = /\r\n/g, p = /\/\*[\s\S]*?\*\//g, m = /\/\/.*$/gm, h = /[ \t]+/g, g = /\s*([(){};,])\s*/g, _ = o.default || o, v = (e, t = s) => {
|
|
13
|
-
let n = new Set(t), r = u(t), i = [];
|
|
1
|
+
import { a as e, c as t, i as n, l as r, m as i, n as a, o, p as s, r as c, s as l, t as u, u as d } from "./defaults-CxdkREQu.js";
|
|
2
|
+
import { createFilter as f } from "@rollup/pluginutils";
|
|
3
|
+
import { diff as p, snap as m } from "byte-snap";
|
|
4
|
+
import h from "magic-string";
|
|
5
|
+
import { createUnplugin as g } from "unplugin";
|
|
6
|
+
import { parse as _ } from "@babel/parser";
|
|
7
|
+
import v from "@babel/traverse";
|
|
8
|
+
//#region src/core.js
|
|
9
|
+
var y = v.default || v, b = (e, t = c) => {
|
|
10
|
+
let n = new Set(t), r = i(t), a = [];
|
|
14
11
|
try {
|
|
15
|
-
_(
|
|
12
|
+
y(_(e, {
|
|
16
13
|
sourceType: "module",
|
|
17
14
|
plugins: [
|
|
18
15
|
"typescript",
|
|
@@ -22,9 +19,9 @@ var s = [
|
|
|
22
19
|
allowReturnOutsideFunction: !0
|
|
23
20
|
}), {
|
|
24
21
|
TaggedTemplateExpression(e) {
|
|
25
|
-
let { tag: t, quasi: r } = e.node,
|
|
26
|
-
t.type === "Identifier" && n.has(t.name) ?
|
|
27
|
-
tag:
|
|
22
|
+
let { tag: t, quasi: r } = e.node, i = null;
|
|
23
|
+
t.type === "Identifier" && n.has(t.name) ? i = t.name : t.type === "MemberExpression" && t.property.type === "Identifier" && n.has(t.property.name) && (i = t.property.name), i && r.expressions.length === 0 && a.push({
|
|
24
|
+
tag: i,
|
|
28
25
|
value: r.quasis[0].value.raw,
|
|
29
26
|
start: r.start,
|
|
30
27
|
end: r.end
|
|
@@ -38,7 +35,7 @@ var s = [
|
|
|
38
35
|
if (!e || e.type !== "CommentBlock") continue;
|
|
39
36
|
let n = e.value.match(r);
|
|
40
37
|
if (n) {
|
|
41
|
-
|
|
38
|
+
a.push({
|
|
42
39
|
tag: n[1],
|
|
43
40
|
value: t.quasis[0].value.raw,
|
|
44
41
|
start: t.start,
|
|
@@ -52,15 +49,15 @@ var s = [
|
|
|
52
49
|
} catch (e) {
|
|
53
50
|
if (e.name !== "SyntaxError") throw e;
|
|
54
51
|
}
|
|
55
|
-
return
|
|
56
|
-
},
|
|
52
|
+
return a;
|
|
53
|
+
}, x = (e, t) => {
|
|
57
54
|
for (let n = t; n < e.length; n++) if (e[n] === "\\") n++;
|
|
58
55
|
else if (e[n] === "`") return n;
|
|
59
56
|
return -1;
|
|
60
|
-
},
|
|
57
|
+
}, S = (e, t = c) => {
|
|
61
58
|
let n = t.map((e) => e.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("|"), r = RegExp(`\\b(${n})\\s*\`|/\\*\\s*(${n})\\s*\\*/\\s*\``, "g"), i = [], a;
|
|
62
59
|
for (; a = r.exec(e);) {
|
|
63
|
-
let t = a.index + a[0].length, n =
|
|
60
|
+
let t = a.index + a[0].length, n = x(e, t);
|
|
64
61
|
if (n === -1) continue;
|
|
65
62
|
let o = e.slice(t, n);
|
|
66
63
|
!o.includes("${") && d.test(o) && i.push({
|
|
@@ -71,43 +68,49 @@ var s = [
|
|
|
71
68
|
}), r.lastIndex = n + 1;
|
|
72
69
|
}
|
|
73
70
|
return i;
|
|
74
|
-
},
|
|
75
|
-
let
|
|
76
|
-
for (let e of
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
return
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
71
|
+
}, C = (i) => {
|
|
72
|
+
let a = i.replace(e, "\n").replace(n, "").replace(r, "").split("\n").map((e) => e.replace(t, " ").trim()).filter(Boolean), c = "", u = !1;
|
|
73
|
+
for (let e of a) e.startsWith("#") || u ? c += (c && !c.endsWith("\n") ? "\n" : "") + e + "\n" : c += c === "" || c.endsWith("\n") ? e : " " + e, u = e.endsWith("\\");
|
|
74
|
+
let d = s(c);
|
|
75
|
+
return u = !1, c = c.split("\n").map((e) => {
|
|
76
|
+
if (e.startsWith("#") || u) return u = e.endsWith("\\"), e;
|
|
77
|
+
let t = e.replace(o, "$1");
|
|
78
|
+
return d || (t = t.replace(l, "=")), u = e.endsWith("\\"), t;
|
|
79
|
+
}).join("\n"), c.trim();
|
|
80
|
+
}, w, T = g((e = {}) => {
|
|
81
|
+
let t = e.tags || c, n = e.transform || C, r = e.scan === "loose" ? S : b, i = f(e.include || a, e.exclude || u), o = "", s = "", l = 0;
|
|
83
82
|
return {
|
|
84
83
|
name: "compress-shader-literals",
|
|
85
84
|
enforce: "pre",
|
|
86
|
-
transform(
|
|
87
|
-
if (!
|
|
88
|
-
let
|
|
89
|
-
if (
|
|
90
|
-
|
|
91
|
-
let
|
|
92
|
-
for (let
|
|
93
|
-
let
|
|
94
|
-
|
|
85
|
+
transform(a, c) {
|
|
86
|
+
if (!i(c) || !t.some((e) => a.includes(e))) return null;
|
|
87
|
+
let u = r(a, t);
|
|
88
|
+
if (u.length === 0) return null;
|
|
89
|
+
e.debug && console.log(`[compress-shader-literals] ${c}: ${u.length} literal(s) — ${u.map((e) => e.tag).join(", ")}`);
|
|
90
|
+
let d = new h(a), f = !1, p = [];
|
|
91
|
+
for (let t of u) {
|
|
92
|
+
let r = n(t.value);
|
|
93
|
+
t.value !== r && (d.overwrite(t.start, t.end, `\`${r}\``), f = !0, e.validate && (w ??= import("./validate-Cp5hJk0B.js"), p.push(w.then(({ validateShader: e }) => e(t.value, r)).then((e) => {
|
|
94
|
+
e === "broken" && this.warn(`compress-shader-literals: minified ${t.tag}\`...\` in ${c} stopped parsing`);
|
|
95
|
+
})))), e.outputRatio && (o += t.value, s += r, l++);
|
|
95
96
|
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
97
|
+
if (!f) return null;
|
|
98
|
+
let m = {
|
|
99
|
+
code: d.toString(),
|
|
100
|
+
map: d.generateMap({
|
|
99
101
|
hires: !0,
|
|
100
|
-
source:
|
|
102
|
+
source: c
|
|
101
103
|
})
|
|
102
|
-
}
|
|
104
|
+
};
|
|
105
|
+
return p.length ? Promise.all(p).then(() => m) : m;
|
|
103
106
|
},
|
|
104
107
|
buildEnd() {
|
|
105
|
-
if (
|
|
106
|
-
let e = `compress-shader-literals: ${
|
|
107
|
-
|
|
108
|
+
if (e.outputRatio && o) {
|
|
109
|
+
let e = `compress-shader-literals: ${l} shader literal${l === 1 ? "" : "s"}`;
|
|
110
|
+
p(m.text(o), m.text(s)).print(e);
|
|
108
111
|
}
|
|
109
112
|
}
|
|
110
113
|
};
|
|
111
114
|
});
|
|
112
115
|
//#endregion
|
|
113
|
-
export {
|
|
116
|
+
export { T as compressShaderLiterals, b as extractShaderLiterals, C as minifyShader };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { d as e, f as t, p as n } from "./defaults-CxdkREQu.js";
|
|
2
|
+
//#region src/validate.js
|
|
3
|
+
function r(e) {
|
|
4
|
+
console.warn(`compress-shader-literals: validate:true needs "${e}" installed (it's an optional peer dependency) — skipping validation for that dialect.`);
|
|
5
|
+
}
|
|
6
|
+
var i, a, o = import("@shaderfrog/glsl-parser").then((e) => {
|
|
7
|
+
i = (t) => e.default.parser.parse(t, { quiet: !0 });
|
|
8
|
+
}).catch(() => r("@shaderfrog/glsl-parser")), s = import("wgsl_reflect/wgsl_reflect.module.js").then((e) => {
|
|
9
|
+
a = (t) => new e.WgslParser().parse(t);
|
|
10
|
+
}).catch(() => r("wgsl_reflect"));
|
|
11
|
+
async function c(r, c) {
|
|
12
|
+
if (t(r) && !t(c) || !e(r, c)) return "broken";
|
|
13
|
+
let l = n(r);
|
|
14
|
+
await (l ? s : o);
|
|
15
|
+
let u = l ? a : i;
|
|
16
|
+
if (!u) throw Error("compress-shader-literals: validate:true needs its parser peer dependency installed");
|
|
17
|
+
try {
|
|
18
|
+
u(r);
|
|
19
|
+
} catch {
|
|
20
|
+
return "fragment";
|
|
21
|
+
}
|
|
22
|
+
try {
|
|
23
|
+
return u(c), "ok";
|
|
24
|
+
} catch {
|
|
25
|
+
return "broken";
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
//#endregion
|
|
29
|
+
export { c as validateShader };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const e=require("./defaults-BdBaB3Yd.cjs");function t(e){console.warn(`compress-shader-literals: validate:true needs "${e}" installed (it's an optional peer dependency) — skipping validation for that dialect.`)}var n,r,i=import(`@shaderfrog/glsl-parser`).then(e=>{n=t=>e.default.parser.parse(t,{quiet:!0})}).catch(()=>t(`@shaderfrog/glsl-parser`)),a=import(`wgsl_reflect/wgsl_reflect.module.js`).then(e=>{r=t=>new e.WgslParser().parse(t)}).catch(()=>t(`wgsl_reflect`));async function o(t,o){if(e.f(t)&&!e.f(o)||!e.d(t,o))return`broken`;let s=e.p(t);await(s?a:i);let c=s?r:n;if(!c)throw Error(`compress-shader-literals: validate:true needs its parser peer dependency installed`);try{c(t)}catch{return`fragment`}try{return c(o),`ok`}catch{return`broken`}}exports.validateShader=o;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "compress-shader-literals",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.8",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "✂️ Strip comments & whitespace from GLSL/WGSL shader literals in your JS/TS at build time — any bundler, no toolchain, no runtime cost.",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -42,13 +42,29 @@
|
|
|
42
42
|
"magic-string": "^0.30.21",
|
|
43
43
|
"unplugin": "^3.0.0"
|
|
44
44
|
},
|
|
45
|
+
"peerDependencies": {
|
|
46
|
+
"@shaderfrog/glsl-parser": "^0.2.4",
|
|
47
|
+
"wgsl_reflect": "^1.5.0"
|
|
48
|
+
},
|
|
49
|
+
"peerDependenciesMeta": {
|
|
50
|
+
"@shaderfrog/glsl-parser": {
|
|
51
|
+
"optional": true
|
|
52
|
+
},
|
|
53
|
+
"wgsl_reflect": {
|
|
54
|
+
"optional": true
|
|
55
|
+
}
|
|
56
|
+
},
|
|
45
57
|
"devDependencies": {
|
|
58
|
+
"@shaderfrog/glsl-parser": "^0.2.4",
|
|
46
59
|
"@size-limit/file": "^11.0.0",
|
|
47
60
|
"@trivago/prettier-plugin-sort-imports": "^5.0.0",
|
|
61
|
+
"plugin-include": "^1.0.2",
|
|
48
62
|
"prettier": "^3.0.0",
|
|
49
63
|
"size-limit": "^11.0.0",
|
|
64
|
+
"taglify": "^1.0.5",
|
|
50
65
|
"typescript": "^5.6.0",
|
|
51
|
-
"vite": "^8.1.0"
|
|
66
|
+
"vite": "^8.1.0",
|
|
67
|
+
"wgsl_reflect": "^1.5.0"
|
|
52
68
|
},
|
|
53
69
|
"engines": {
|
|
54
70
|
"node": ">=18"
|