compress-shader-literals 0.0.5 → 0.0.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
@@ -2,12 +2,14 @@
2
2
 
3
3
  > **Size matters.** Strip everything strippable from your GLSL & WGSL shaders at build time.
4
4
 
5
+ > ⭐ Star this repository to support development and help others discover it.
6
+
5
7
  [![npm version](https://img.shields.io/npm/v/compress-shader-literals?color=cb3837&logo=npm)](https://www.npmjs.com/package/compress-shader-literals)
6
8
  [![minzipped size](https://img.shields.io/bundlephobia/minzip/compress-shader-literals?color=success)](https://bundlephobia.com/package/compress-shader-literals)
7
9
  [![npm downloads](https://img.shields.io/npm/dm/compress-shader-literals?color=success)](https://www.npmjs.com/package/compress-shader-literals)
8
10
  [![license](https://img.shields.io/npm/l/compress-shader-literals?color=blue)](./LICENSE)
9
11
 
10
- Smaller WebGL & WebGPU bundles for free shrink your shaders at build time, in any bundler.
12
+ Your shaders ship to users as strings. This strips the comments and whitespace out of them at build time, before your bundler ever sees them — in any bundler.
11
13
 
12
14
  ![Alt Preview](./assets/preview.png)
13
15
 
@@ -26,10 +28,18 @@ Measured on the real shaders shipped by popular libraries:
26
28
  | `curtainsjs` | 7 | 3,406 B | 2,563 B | **24.8%** |
27
29
  | **Total** | 337 | 270,605 B | 227,492 B | **15.9%** |
28
30
 
29
- _Auto-generated by [`tests/e2e.js`](tests/e2e.js) · packages [verified](tests/validate.js) loadable · 2026-06-23_
31
+ _Auto-generated by [`tests/e2e.js`](tests/e2e.js) · packages [verified](tests/validate.js) loadable · 2026-06-24_
30
32
 
31
33
  <!-- STATS:END -->
32
34
 
35
+ <!-- <a href="https://star-history.com/#jayf0x/compress-shader-literals&Date">
36
+ <picture>
37
+ <source media="(prefers-color-scheme: dark)" srcset="https://api.star-history.com/chart?repos=jayf0x/compress-shader-literals&type=Date&theme=dark&legend=top-left" />
38
+ <source media="(prefers-color-scheme: light)" srcset="https://api.star-history.com/chart?repos=jayf0x/compress-shader-literals&type=Date&legend=top-left" />
39
+ <img alt="Star History Chart" src="https://api.star-history.com/chart?repos=jayf0x/compress-shader-literals&type=Date&legend=top-left" />
40
+ </picture>
41
+ </a> -->
42
+
33
43
  ## Install
34
44
 
35
45
  ```sh
@@ -53,45 +63,56 @@ export default { plugins: [compressShaderLiterals.vite({ outputRatio: true })] }
53
63
  // Rspack / Rolldown / Farm → .rspack() / .rolldown() / .farm()
54
64
  ```
55
65
 
56
- ## Options
57
-
58
- | Option | Default | Description |
59
- | ------------- | ---------------------------- | --------------------------------------- |
60
- | `tags` | `['glsl', 'wgsl', 'shader']` | Tag names / comment markers to match |
61
- | `include` | `[/\.[jt]sx?$/]` | Files to process |
62
- | `exclude` | `[/node_modules/, /dist/]` | Files to skip |
63
- | `outputRatio` | `false` | Print a bytes-saved summary after build |
64
-
65
- ---
66
-
67
- ## What it compresses
68
-
69
- Comments, indentation, and blank lines — stripped from your `glsl` / `wgsl` literals before your bundler even runs. Zero source changes, full sourcemaps. One [unplugin](https://github.com/unjs/unplugin) covers **Vite, Rollup, webpack, esbuild, Rspack, Rolldown & Farm**.
66
+ It finds tagged and comment-prefixed literals — and yes, the comment form keeps your editor's syntax highlighting:
70
67
 
71
68
  ```ts
72
- // Tagged template literal
73
69
  const vert = glsl`
74
70
  // vertex shader ← stripped
75
71
  precision highp float;
76
72
  void main() { gl_Position = vec4(0.0); }
77
73
  `;
78
74
 
79
- // Comment-prefixed template literal (keeps editor syntax highlighting)
80
75
  const frag = /* wgsl */ `
81
76
  /* fragment */
82
77
  fn main() {}
83
78
  `;
84
79
  ```
85
80
 
86
- Both collapse to a single tight line no comments, no padding.
81
+ Both collapse to a single tight line. No comments, no padding, no touched source files.
82
+
83
+ **Options**
84
+
85
+ | Option | Default | Description |
86
+ | ------------- | ---------------------------- | -------------------------------------------------- |
87
+ | `tags` | `['glsl', 'wgsl', 'shader']` | Tag names / comment markers to match |
88
+ | `include` | `[/\.[jt]sx?$/]` | Files to process |
89
+ | `exclude` | `[/node_modules/, /dist/]` | Files to skip |
90
+ | `outputRatio` | `false` | Print a bytes-saved summary after build |
91
+ | `transform` | built-in `minifyShader` | Custom minifier — `(shader: string) => string` |
92
+ | `debug` | `false` | Log each file's discovered literals to the console |
93
+
94
+ **Programmatic API** — the two core helpers are exported for tooling authors (validators, ESLint rules, CLIs), no plugin required:
95
+
96
+ ```js
97
+ import { extractShaderLiterals, minifyShader } from 'compress-shader-literals';
98
+
99
+ extractShaderLiterals('const v = glsl`void main() {}`');
100
+ // → [{ tag: 'glsl', value: 'void main() {}', start: 10, end: 26 }]
101
+
102
+ minifyShader('// comment\nvoid main() {}'); // → 'void main() {}'
103
+ ```
87
104
 
88
105
  ## How it works
89
106
 
107
+ Nothing clever, which is the point:
108
+
90
109
  1. Parses each matched file with Babel (TS/JSX aware).
91
110
  2. Finds tagged and comment-prefixed shader literals.
92
111
  3. Strips comments + collapses whitespace via [`magic-string`](https://github.com/Rich-Harris/magic-string), so **sourcemaps stay intact**.
93
112
  4. Runs as a `pre` transform, so your bundler's own minifier still sees the result.
94
113
 
114
+ One [unplugin](https://github.com/unjs/unplugin) covers **Vite, Rollup, webpack, esbuild, Rspack, Rolldown & Farm**.
115
+
95
116
  ## License
96
117
 
97
118
  [MIT](./LICENSE) © [jayF0x](https://github.com/jayf0x)
package/dist/index.d.ts CHANGED
@@ -12,6 +12,18 @@ export interface CompressShaderLiteralsOptions {
12
12
  exclude?: FilterPattern;
13
13
  /** Print a bytes-saved summary after build. Default: `false` */
14
14
  outputRatio?: boolean;
15
+ /** Custom minifier, replaces the built-in `minifyShader`. Receives the raw literal, returns the transformed source. */
16
+ transform?: (shader: string) => string;
17
+ /** Log each file's discovered literals to the console. Default: `false` */
18
+ debug?: boolean;
19
+ }
20
+
21
+ /** A shader literal discovered in source. */
22
+ export interface ShaderLiteral {
23
+ tag: string;
24
+ value: string;
25
+ start: number;
26
+ end: number;
15
27
  }
16
28
 
17
29
  /**
@@ -21,3 +33,9 @@ export interface CompressShaderLiteralsOptions {
21
33
  * `.webpack()`, `.esbuild()`, `.rspack()`, `.rolldown()`, `.farm()`.
22
34
  */
23
35
  export declare const compressShaderLiterals: UnpluginInstance<CompressShaderLiteralsOptions | undefined, boolean>;
36
+
37
+ /** Find tagged (`glsl\`...\``) and comment-prefixed (`/* wgsl *\/ \`...\``) shader literals in source. */
38
+ export declare function extractShaderLiterals(code: string, tags?: string[]): ShaderLiteral[];
39
+
40
+ /** Strip comments and collapse whitespace in a shader source string. */
41
+ export declare function minifyShader(src: string): string;
package/dist/index.js CHANGED
@@ -65,6 +65,7 @@ function minifyShader(src) {
65
65
  // src/plugin.js
66
66
  var compressShaderLiterals = createUnplugin((options = {}) => {
67
67
  const tags = options.tags || ["glsl", "wgsl", "shader"];
68
+ const minify = options.transform || minifyShader;
68
69
  const filter = createFilter(options.include || [/\.[jt]sx?$/], options.exclude || [/node_modules/, /dist/]);
69
70
  let beforeText = "";
70
71
  let afterText = "";
@@ -79,10 +80,13 @@ var compressShaderLiterals = createUnplugin((options = {}) => {
79
80
  const literals = extractShaderLiterals(code, tags);
80
81
  if (literals.length === 0)
81
82
  return null;
83
+ if (options.debug) {
84
+ console.log(`[compress-shader-literals] ${id}: ${literals.length} literal(s) — ${literals.map((l) => l.tag).join(", ")}`);
85
+ }
82
86
  const ms = new MagicString(code);
83
87
  let hasChanges = false;
84
88
  for (const literal of literals) {
85
- const minified = minifyShader(literal.value);
89
+ const minified = minify(literal.value);
86
90
  if (literal.value !== minified) {
87
91
  ms.overwrite(literal.start, literal.end, `\`${minified}\``);
88
92
  hasChanges = true;
package/dist/index.js.br CHANGED
Binary file
package/dist/index.js.gz CHANGED
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "compress-shader-literals",
3
- "version": "0.0.5",
3
+ "version": "0.0.7",
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": [
@@ -69,7 +69,7 @@
69
69
  "@babel/parser": "^8.0.0",
70
70
  "@babel/traverse": "^8.0.0",
71
71
  "@rollup/pluginutils": "^5.4.0",
72
- "byte-snap": "^1.0.0",
72
+ "byte-snap": "^1.0.3",
73
73
  "magic-string": "^0.30.21",
74
74
  "unplugin": "^3.0.0"
75
75
  },