compress-shader-literals 1.3.5 → 1.3.7

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 CHANGED
@@ -57,22 +57,32 @@ const frag = /* wgsl */ `
57
57
 
58
58
  **Options**
59
59
 
60
- | Option | Default | Description |
61
- | ------------- | ---------------------------- | --------------------------------------------------- |
62
- | `tags` | `['glsl', 'wgsl', 'shader']` | Tag names / comment markers to match |
63
- | `include` | `[/\.[mc]?[jt]sx?$/]` | Files to process — the JS/TS family Babel can parse |
64
- | `exclude` | `[/node_modules/, /dist/]` | Files to skipdependencies are skipped by default |
65
- | `outputRatio` | `false` | Print a bytes-saved summary after build |
66
- | `transform` | built-in `minifyShader` | Custom minifier `(shader: string) => string` |
67
- | `debug` | `false` | Log each file's discovered literals to the console |
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 processthe 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
+
70
+ **`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:
68
71
 
69
- **Programmatic API** — the two core helpers are exported for tooling authors (validators, ESLint rules, CLIs), no plugin required:
72
+ ```js
73
+ compressShaderLiterals.vite({ scan: 'loose', include: [/\.svelte$/] });
74
+ ```
75
+
76
+ **Programmatic API** — the core helpers are exported for tooling authors (validators, ESLint rules, CLIs), no plugin required:
70
77
 
71
78
  ```js
72
- import { extractShaderLiterals, minifyShader } from 'compress-shader-literals';
79
+ import { extractShaderLiterals, extractShaderLiteralsLoose, minifyShader } from 'compress-shader-literals';
73
80
 
74
81
  extractShaderLiterals('const v = glsl`void main() {}`');
75
- // → [{ tag: 'glsl', value: 'void main() {}', start: 10, end: 26 }]
82
+ // → [{ tag: 'glsl', value: 'void main() {}', start: 14, end: 30 }]
83
+
84
+ extractShaderLiteralsLoose('<script>const v = glsl`void main() {}`</script>');
85
+ // → same shape, found by regex instead of a Babel parse
76
86
 
77
87
  minifyShader('// comment\nvoid main() {}'); // → 'void main() {}'
78
88
  ```
@@ -85,35 +95,35 @@ Real shaders shipped by popular libraries, run through the built-in minifier:
85
95
 
86
96
  | Package | Shaders | Before | After | Saved | Net after Brotli |
87
97
  | ----------------------------- | ------: | ----------: | ----------: | --------: | ---------------: |
88
- | `vtk.js` | 142 | 276,617 B | 159,634 B | **42.3%** | +39.6% |
89
- | `three-stdlib` | 370 | 429,350 B | 272,486 B | **36.5%** | +35.5% |
90
- | `curtainsjs` | 7 | 3,406 B | 2,352 B | **30.9%** | +10.1% |
91
- | `hydra-synth` | 15 | 3,852 B | 2,675 B | **30.6%** | +19.4% |
92
- | `cesium` | 546 | 951,897 B | 664,581 B | **30.2%** | +32.6% |
93
- | `troika-three-utils` | 4 | 168 B | 120 B | **28.6%** | |
94
- | `pixi.js` | 162 | 75,768 B | 56,152 B | **25.9%** | |
95
- | `shader-park-core` | 18 | 10,794 B | 8,007 B | **25.8%** | |
96
- | `@luma.gl/shadertools` | 24 | 149,192 B | 111,095 B | **25.5%** | |
97
- | `three` | 281 | 240,906 B | 185,520 B | **23.0%** | |
98
- | `playcanvas` | 856 | 1,297,844 B | 1,039,365 B | **19.9%** | |
99
- | `ogl` | 22 | 6,109 B | 4,925 B | **19.4%** | |
100
- | `deck.gl` | 132 | 242,113 B | 198,706 B | **17.9%** | |
101
- | `@paper-design/shaders` | 30 | 142,466 B | 122,036 B | **14.3%** | |
102
- | `@deck.gl/layers` | 104 | 223,902 B | 195,827 B | **12.5%** | |
103
- | `@deck.gl/core` | 40 | 17,746 B | 15,738 B | **11.3%** | |
104
- | `@deck.gl/aggregation-layers` | 56 | 43,713 B | 39,094 B | **10.6%** | |
105
- | `@luma.gl/engine` | 29 | 11,357 B | 10,384 B | **8.6%** | |
106
- | `@babylonjs/core` | 349 | 669,740 B | 661,013 B | **1.3%** | |
107
- | `postprocessing` | 136 | 179,705 B | 179,705 B | **0.0%** | |
108
- | **Total** | 3323 | 4,976,645 B | 3,929,415 B | **21.0%** | — |
109
-
110
- _3323 shaders · 2477/3323 parseable shaders (GLSL + WGSL) verified valid after minify · [how this is measured](docs/stats.md) · 2026-07-09_
98
+ | `vtk.js` | 142 | 276,617 B | 157,780 B | **43.0%** | +39.5% |
99
+ | `three-stdlib` | 370 | 429,350 B | 266,774 B | **37.9%** | +35.5% |
100
+ | `curtainsjs` | 7 | 3,406 B | 2,290 B | **32.8%** | +10.4% |
101
+ | `hydra-synth` | 15 | 3,852 B | 2,591 B | **32.7%** | +17.9% |
102
+ | `cesium` | 546 | 951,897 B | 649,851 B | **31.7%** | +32.6% |
103
+ | `troika-three-utils` | 4 | 168 B | 120 B | **28.6%** | +32.3% |
104
+ | `shader-park-core` | 18 | 10,794 B | 7,863 B | **27.2%** | +19.7% |
105
+ | `pixi.js` | 162 | 75,768 B | 55,260 B | **27.1%** | +16.3% |
106
+ | `@luma.gl/shadertools` | 24 | 149,192 B | 109,615 B | **26.5%** | +29.9% |
107
+ | `three` | 281 | 240,906 B | 181,942 B | **24.5%** | +25.5% |
108
+ | `ogl` | 22 | 6,109 B | 4,821 B | **21.1%** | +9.1% |
109
+ | `playcanvas` | 856 | 1,297,844 B | 1,025,421 B | **21.0%** | +49.1% |
110
+ | `deck.gl` | 132 | 242,113 B | 195,406 B | **19.3%** | +31.1% |
111
+ | `@paper-design/shaders` | 30 | 142,466 B | 118,204 B | **17.0%** | +8.9% |
112
+ | `@deck.gl/layers` | 104 | 223,902 B | 192,483 B | **14.0%** | +32.4% |
113
+ | `@deck.gl/core` | 40 | 17,746 B | 15,494 B | **12.7%** | +9.1% |
114
+ | `@deck.gl/aggregation-layers` | 56 | 43,713 B | 38,382 B | **12.2%** | +25.3% |
115
+ | `@luma.gl/engine` | 29 | 11,357 B | 10,206 B | **10.1%** | +3.1% |
116
+ | `@babylonjs/core` | 349 | 669,740 B | 660,843 B | **1.3%** | +2.1% |
117
+ | `postprocessing` | 136 | 179,705 B | 179,705 B | **0.0%** | +0.0% |
118
+ | **Total** | 3323 | 4,976,645 B | 3,875,051 B | **22.1%** | — |
119
+
120
+ _2477/3323 parseable shaders. Verified 2026-07-11. [How this is measured](docs/stats.md)_
111
121
 
112
122
  <!-- STATS:END -->
113
123
 
114
124
  ## How it works
115
125
 
116
- 1. Parses each matched file with Babel — `.js`, `.jsx`, `.ts`, `.tsx`, `.mjs`, `.cjs`, `.mts`, `.cts` (not `.vue`/`.svelte`/`.glsl`, which aren't whole-file JS).
126
+ 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.
117
127
  2. Finds tagged (`` glsl`…` ``) and comment-prefixed (`/* glsl */ \`…\``) literals, skipping any with `${…}` interpolation.
118
128
  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.
119
129
  4. Rewrites the literal in place with [magic-string](https://github.com/Rich-Harris/magic-string), so sourcemaps stay intact.
package/dist/README.md ADDED
@@ -0,0 +1,138 @@
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
+ [![npm version](https://img.shields.io/npm/v/compress-shader-literals?color=cb3837&logo=npm)](https://www.npmjs.com/package/compress-shader-literals)
8
+ [![minzipped size](https://img.shields.io/bundlephobia/minzip/compress-shader-literals?color=success)](https://bundlephobia.com/package/compress-shader-literals)
9
+ [![npm downloads](https://img.shields.io/npm/dm/compress-shader-literals?color=success)](https://www.npmjs.com/package/compress-shader-literals)
10
+ [![license](https://img.shields.io/npm/l/compress-shader-literals?color=blue)](./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
+ ![Alt Preview](./assets/preview.png)
15
+
16
+ ## Install
17
+
18
+ <!-- [![NPM](https://nodei.co/npm/compress-shader-literals.png)](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
+
70
+ **`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
+
72
+ ```js
73
+ compressShaderLiterals.vite({ scan: 'loose', include: [/\.svelte$/] });
74
+ ```
75
+
76
+ **Programmatic API** — the core helpers are exported for tooling authors (validators, ESLint rules, CLIs), no plugin required:
77
+
78
+ ```js
79
+ import { extractShaderLiterals, extractShaderLiteralsLoose, minifyShader } from 'compress-shader-literals';
80
+
81
+ extractShaderLiterals('const v = glsl`void main() {}`');
82
+ // → [{ tag: 'glsl', value: 'void main() {}', start: 14, end: 30 }]
83
+
84
+ extractShaderLiteralsLoose('<script>const v = glsl`void main() {}`</script>');
85
+ // → same shape, found by regex instead of a Babel parse
86
+
87
+ minifyShader('// comment\nvoid main() {}'); // → 'void main() {}'
88
+ ```
89
+
90
+ ## Stats
91
+
92
+ Real shaders shipped by popular libraries, run through the built-in minifier:
93
+
94
+ <!-- STATS:START -->
95
+
96
+ | Package | Shaders | Before | After | Saved | Net after Brotli |
97
+ | ----------------------------- | ------: | ----------: | ----------: | --------: | ---------------: |
98
+ | `vtk.js` | 142 | 276,617 B | 157,780 B | **43.0%** | +39.5% |
99
+ | `three-stdlib` | 370 | 429,350 B | 266,774 B | **37.9%** | +35.5% |
100
+ | `curtainsjs` | 7 | 3,406 B | 2,290 B | **32.8%** | +10.4% |
101
+ | `hydra-synth` | 15 | 3,852 B | 2,591 B | **32.7%** | +17.9% |
102
+ | `cesium` | 546 | 951,897 B | 649,851 B | **31.7%** | +32.6% |
103
+ | `troika-three-utils` | 4 | 168 B | 120 B | **28.6%** | +32.3% |
104
+ | `shader-park-core` | 18 | 10,794 B | 7,863 B | **27.2%** | +19.7% |
105
+ | `pixi.js` | 162 | 75,768 B | 55,260 B | **27.1%** | +16.3% |
106
+ | `@luma.gl/shadertools` | 24 | 149,192 B | 109,615 B | **26.5%** | +29.9% |
107
+ | `three` | 281 | 240,906 B | 181,942 B | **24.5%** | +25.5% |
108
+ | `ogl` | 22 | 6,109 B | 4,821 B | **21.1%** | +9.1% |
109
+ | `playcanvas` | 856 | 1,297,844 B | 1,025,421 B | **21.0%** | +49.1% |
110
+ | `deck.gl` | 132 | 242,113 B | 195,406 B | **19.3%** | +31.1% |
111
+ | `@paper-design/shaders` | 30 | 142,466 B | 118,204 B | **17.0%** | +8.9% |
112
+ | `@deck.gl/layers` | 104 | 223,902 B | 192,483 B | **14.0%** | +32.4% |
113
+ | `@deck.gl/core` | 40 | 17,746 B | 15,494 B | **12.7%** | +9.1% |
114
+ | `@deck.gl/aggregation-layers` | 56 | 43,713 B | 38,382 B | **12.2%** | +25.3% |
115
+ | `@luma.gl/engine` | 29 | 11,357 B | 10,206 B | **10.1%** | +3.1% |
116
+ | `@babylonjs/core` | 349 | 669,740 B | 660,843 B | **1.3%** | +2.1% |
117
+ | `postprocessing` | 136 | 179,705 B | 179,705 B | **0.0%** | +0.0% |
118
+ | **Total** | 3323 | 4,976,645 B | 3,875,051 B | **22.1%** | — |
119
+
120
+ _2477/3323 parseable shaders. ✅ Verified 2026-07-11. [How this is measured](docs/stats.md)_
121
+
122
+ <!-- STATS:END -->
123
+
124
+ ## How it works
125
+
126
+ 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.
127
+ 2. Finds tagged (`` glsl`…` ``) and comment-prefixed (`/* glsl */ \`…\``) literals, skipping any with `${…}` interpolation.
128
+ 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.
129
+ 4. Rewrites the literal in place with [magic-string](https://github.com/Rich-Harris/magic-string), so sourcemaps stay intact.
130
+ 5. Runs as a `pre` transform, so your bundler's own minifier still sees the result.
131
+
132
+ ## Issues & compatibility
133
+
134
+ 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.
135
+
136
+ ## License
137
+
138
+ [MIT](./LICENSE) © [jayF0x](https://github.com/jayf0x)
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));let c=require("@rollup/pluginutils"),l=require("byte-snap"),u=require("magic-string");u=s(u,1);let d=require("unplugin"),f=require("@babel/parser"),p=require("@babel/traverse");p=s(p,1);var m=[`glsl`,`wgsl`,`shader`],h=[/\.[mc]?[jt]sx?$/],g=[/node_modules/,/dist/],_=e=>RegExp(`^\\s*(${e.join(`|`)})\\s*$`),v=/\r\n/g,y=/\/\*[\s\S]*?\*\//g,b=/\/\/.*$/gm,x=/[ \t]+/g,S=/\s*([(){};,])\s*/g,C=p.default.default||p.default,w=(e,t=m)=>{let n=new Set(t),r=_(t),i=[];try{C((0,f.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},T=e=>{let t=e.replace(v,`
2
- `).replace(y,``).replace(b,``).split(`
3
- `).map(e=>e.replace(x,` `).trim()).filter(Boolean),n=``,r=!1;for(let e of t)e.startsWith(`#`)||r?n+=(n&&!n.endsWith(`
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));let c=require("@rollup/pluginutils"),l=require("byte-snap"),u=require("magic-string");u=s(u,1);let d=require("unplugin"),f=require("@babel/parser"),p=require("@babel/traverse");p=s(p,1);var m=[`glsl`,`wgsl`,`shader`],h=[/\.[mc]?[jt]sx?$/],g=[/node_modules/,/dist/],_=e=>RegExp(`^\\s*(${e.join(`|`)})\\s*$`),v=/\b(gl_FragColor|gl_Position|void\s+main|precision\s+(highp|mediump|lowp)|fn\s+main)\b/,y=/\r\n/g,b=/\/\*[\s\S]*?\*\//g,x=/\/\/.*$/gm,S=/[ \t]+/g,C=/\s*([(){};,])\s*/g,w=/@vertex|@fragment|@group|var<|@compute/,T=e=>w.test(e)&&!/void\s+main/.test(e),E=/(?<!>)\s+=\s+/g,D=p.default.default||p.default,O=(e,t=m)=>{let n=new Set(t),r=_(t),i=[];try{D((0,f.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},k=(e,t)=>{for(let n=t;n<e.length;n++)if(e[n]===`\\`)n++;else if(e[n]==="`")return n;return-1},A=(e,t=m)=>{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=k(e,t);if(n===-1)continue;let o=e.slice(t,n);!o.includes("${")&&v.test(o)&&i.push({tag:a[1]??a[2],value:o,start:t-1,end:n+1}),r.lastIndex=n+1}return i},j=e=>{let t=e.replace(y,`
2
+ `).replace(b,``).replace(x,``).split(`
3
+ `).map(e=>e.replace(S,` `).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=>{let t=e.startsWith(`#`)||r?e:e.replace(S,`$1`);return r=e.endsWith(`\\`),t}).join(`
9
- `),n.trim()},E=(0,d.createUnplugin)((e={})=>{let t=e.tags||m,n=e.transform||T,r=(0,c.createFilter)(e.include||h,e.exclude||g),i=``,a=``,o=0;return{name:`compress-shader-literals`,enforce:`pre`,transform(s,c){if(!r(c)||!t.some(e=>s.includes(e)))return null;let l=w(s,t);if(l.length===0)return null;e.debug&&console.log(`[compress-shader-literals] ${c}: ${l.length} literal(s) — ${l.map(e=>e.tag).join(`, `)}`);let d=new u.default(s),f=!1;for(let t of l){let r=n(t.value);t.value!==r&&(d.overwrite(t.start,t.end,`\`${r}\``),f=!0),e.outputRatio&&(i+=t.value,a+=r,o++)}return f?{code:d.toString(),map:d.generateMap({hires:!0,source:c})}:null},buildEnd(){if(e.outputRatio&&i){let e=`compress-shader-literals: ${o} shader literal${o===1?``:`s`}`;(0,l.diff)(l.snap.text(i),l.snap.text(a)).print(e)}}}});exports.compressShaderLiterals=E,exports.extractShaderLiterals=w,exports.minifyShader=T;
7
+ `)?e:` `+e,r=e.endsWith(`\\`);let i=T(n);return r=!1,n=n.split(`
8
+ `).map(e=>{if(e.startsWith(`#`)||r)return r=e.endsWith(`\\`),e;let t=e.replace(C,`$1`);return i||(t=t.replace(E,`=`)),r=e.endsWith(`\\`),t}).join(`
9
+ `),n.trim()},M=(0,d.createUnplugin)((e={})=>{let t=e.tags||m,n=e.transform||j,r=e.scan===`loose`?A:O,i=(0,c.createFilter)(e.include||h,e.exclude||g),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 d=r(c,t);if(d.length===0)return null;e.debug&&console.log(`[compress-shader-literals] ${l}: ${d.length} literal(s) — ${d.map(e=>e.tag).join(`, `)}`);let f=new u.default(c),p=!1;for(let t of d){let r=n(t.value);t.value!==r&&(f.overwrite(t.start,t.end,`\`${r}\``),p=!0),e.outputRatio&&(a+=t.value,o+=r,s++)}return p?{code:f.toString(),map:f.generateMap({hires:!0,source:l})}:null},buildEnd(){if(e.outputRatio&&a){let e=`compress-shader-literals: ${s} shader literal${s===1?``:`s`}`;(0,l.diff)(l.snap.text(a),l.snap.text(o)).print(e)}}}});exports.compressShaderLiterals=M,exports.extractShaderLiterals=O,exports.minifyShader=j;
package/dist/index.d.ts CHANGED
@@ -6,6 +6,14 @@ type FilterPattern = string | RegExp | ReadonlyArray<string | RegExp> | null;
6
6
  export interface CompressShaderLiteralsOptions {
7
7
  /** Tag names / comment markers to match. Default: `['glsl', 'wgsl', 'shader']` */
8
8
  tags?: string[];
9
+ /**
10
+ * Extraction method. `'ast'` parses the whole file with Babel (JS/TS only).
11
+ * `'loose'` finds the same tagged/comment-prefixed literal shapes by regex
12
+ * instead, for files Babel can't parse — point `include` at them yourself.
13
+ * No whole-file parse means no syntax guarantee; only a match confirmed by a
14
+ * shader-content check is touched. Default: `'ast'`
15
+ */
16
+ scan?: 'ast' | 'loose';
9
17
  /** Files to process. Default: `[/\.[mc]?[jt]sx?$/]` (the JS/TS family Babel can parse). */
10
18
  include?: FilterPattern;
11
19
  /** Files to skip. Default: `[/node_modules/, /dist/]` */
@@ -37,5 +45,8 @@ export declare const compressShaderLiterals: UnpluginInstance<CompressShaderLite
37
45
  /** Find tagged (`glsl\`...\``) and comment-prefixed (`/* wgsl *\/ \`...\``) shader literals in source. */
38
46
  export declare function extractShaderLiterals(code: string, tags?: string[]): ShaderLiteral[];
39
47
 
48
+ /** Same as `extractShaderLiterals`, but by regex instead of a Babel parse — for source Babel can't parse. */
49
+ export declare function extractShaderLiteralsLoose(code: string, tags?: string[]): ShaderLiteral[];
50
+
40
51
  /** Strip comments and collapse whitespace in a shader source string. */
41
52
  export declare function minifyShader(src: string): string;
package/dist/index.js CHANGED
@@ -9,10 +9,10 @@ var s = [
9
9
  "glsl",
10
10
  "wgsl",
11
11
  "shader"
12
- ], c = [/\.[mc]?[jt]sx?$/], l = [/node_modules/, /dist/], u = (e) => RegExp(`^\\s*(${e.join("|")})\\s*$`), d = /\r\n/g, f = /\/\*[\s\S]*?\*\//g, p = /\/\/.*$/gm, m = /[ \t]+/g, h = /\s*([(){};,])\s*/g, g = o.default || o, _ = (e, t = s) => {
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, _ = /@vertex|@fragment|@group|var<|@compute/, v = (e) => _.test(e) && !/void\s+main/.test(e), y = /(?<!>)\s+=\s+/g, b = o.default || o, x = (e, t = s) => {
13
13
  let n = new Set(t), r = u(t), i = [];
14
14
  try {
15
- g(a(e, {
15
+ b(a(e, {
16
16
  sourceType: "module",
17
17
  plugins: [
18
18
  "typescript",
@@ -53,27 +53,47 @@ var s = [
53
53
  if (e.name !== "SyntaxError") throw e;
54
54
  }
55
55
  return i;
56
- }, v = (e) => {
57
- let t = e.replace(d, "\n").replace(f, "").replace(p, "").split("\n").map((e) => e.replace(m, " ").trim()).filter(Boolean), n = "", r = !1;
56
+ }, S = (e, t) => {
57
+ for (let n = t; n < e.length; n++) if (e[n] === "\\") n++;
58
+ else if (e[n] === "`") return n;
59
+ return -1;
60
+ }, C = (e, t = s) => {
61
+ let n = t.map((e) => e.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("|"), r = RegExp(`\\b(${n})\\s*\`|/\\*\\s*(${n})\\s*\\*/\\s*\``, "g"), i = [], a;
62
+ for (; a = r.exec(e);) {
63
+ let t = a.index + a[0].length, n = S(e, t);
64
+ if (n === -1) continue;
65
+ let o = e.slice(t, n);
66
+ !o.includes("${") && d.test(o) && i.push({
67
+ tag: a[1] ?? a[2],
68
+ value: o,
69
+ start: t - 1,
70
+ end: n + 1
71
+ }), r.lastIndex = n + 1;
72
+ }
73
+ return i;
74
+ }, w = (e) => {
75
+ let t = e.replace(f, "\n").replace(p, "").replace(m, "").split("\n").map((e) => e.replace(h, " ").trim()).filter(Boolean), n = "", r = !1;
58
76
  for (let e of t) e.startsWith("#") || r ? n += (n && !n.endsWith("\n") ? "\n" : "") + e + "\n" : n += n === "" || n.endsWith("\n") ? e : " " + e, r = e.endsWith("\\");
77
+ let i = v(n);
59
78
  return r = !1, n = n.split("\n").map((e) => {
60
- let t = e.startsWith("#") || r ? e : e.replace(h, "$1");
61
- return r = e.endsWith("\\"), t;
79
+ if (e.startsWith("#") || r) return r = e.endsWith("\\"), e;
80
+ let t = e.replace(g, "$1");
81
+ return i || (t = t.replace(y, "=")), r = e.endsWith("\\"), t;
62
82
  }).join("\n"), n.trim();
63
- }, y = i((i = {}) => {
64
- let a = i.tags || s, o = i.transform || v, u = e(i.include || c, i.exclude || l), d = "", f = "", p = 0;
83
+ }, T = i((i = {}) => {
84
+ let a = i.tags || s, o = i.transform || w, u = i.scan === "loose" ? C : x, d = e(i.include || c, i.exclude || l), f = "", p = "", m = 0;
65
85
  return {
66
86
  name: "compress-shader-literals",
67
87
  enforce: "pre",
68
88
  transform(e, t) {
69
- if (!u(t) || !a.some((t) => e.includes(t))) return null;
70
- let n = _(e, a);
89
+ if (!d(t) || !a.some((t) => e.includes(t))) return null;
90
+ let n = u(e, a);
71
91
  if (n.length === 0) return null;
72
92
  i.debug && console.log(`[compress-shader-literals] ${t}: ${n.length} literal(s) — ${n.map((e) => e.tag).join(", ")}`);
73
93
  let s = new r(e), c = !1;
74
94
  for (let e of n) {
75
95
  let t = o(e.value);
76
- e.value !== t && (s.overwrite(e.start, e.end, `\`${t}\``), c = !0), i.outputRatio && (d += e.value, f += t, p++);
96
+ e.value !== t && (s.overwrite(e.start, e.end, `\`${t}\``), c = !0), i.outputRatio && (f += e.value, p += t, m++);
77
97
  }
78
98
  return c ? {
79
99
  code: s.toString(),
@@ -84,12 +104,12 @@ var s = [
84
104
  } : null;
85
105
  },
86
106
  buildEnd() {
87
- if (i.outputRatio && d) {
88
- let e = `compress-shader-literals: ${p} shader literal${p === 1 ? "" : "s"}`;
89
- t(n.text(d), n.text(f)).print(e);
107
+ if (i.outputRatio && f) {
108
+ let e = `compress-shader-literals: ${m} shader literal${m === 1 ? "" : "s"}`;
109
+ t(n.text(f), n.text(p)).print(e);
90
110
  }
91
111
  }
92
112
  };
93
113
  });
94
114
  //#endregion
95
- export { y as compressShaderLiterals, _ as extractShaderLiterals, v as minifyShader };
115
+ export { T as compressShaderLiterals, x as extractShaderLiterals, w as minifyShader };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "compress-shader-literals",
3
- "version": "1.3.5",
3
+ "version": "1.3.7",
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",
@@ -45,8 +45,10 @@
45
45
  "devDependencies": {
46
46
  "@size-limit/file": "^11.0.0",
47
47
  "@trivago/prettier-plugin-sort-imports": "^5.0.0",
48
+ "plugin-include": "^1.0.2",
48
49
  "prettier": "^3.0.0",
49
50
  "size-limit": "^11.0.0",
51
+ "taglify": "^1.0.5",
50
52
  "typescript": "^5.6.0",
51
53
  "vite": "^8.1.0"
52
54
  },