compress-shader-literals 0.0.7 → 0.0.10

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
@@ -22,13 +22,13 @@ Measured on the real shaders shipped by popular libraries:
22
22
  | Package | Shaders | Before | After | Saved |
23
23
  | --------------------- | ------: | --------: | --------: | --------: |
24
24
  | `three` | 281 | 240,772 B | 203,428 B | **15.5%** |
25
- | `@jayf0x/fluidity-js` | 9 | 9,524 B | 7,133 B | **25.1%** |
25
+ | `@jayf0x/fluidity-js` | 9 | 11,095 B | 7,788 B | **29.8%** |
26
26
  | `ogl` | 22 | 6,109 B | 5,335 B | **12.7%** |
27
27
  | `shader-park-core` | 18 | 10,794 B | 9,033 B | **16.3%** |
28
28
  | `curtainsjs` | 7 | 3,406 B | 2,563 B | **24.8%** |
29
- | **Total** | 337 | 270,605 B | 227,492 B | **15.9%** |
29
+ | **Total** | 337 | 272,176 B | 228,147 B | **16.2%** |
30
30
 
31
- _Auto-generated by [`tests/e2e.js`](tests/e2e.js) · packages [verified](tests/validate.js) loadable · 2026-06-24_
31
+ _Auto-generated by [`tests/e2e.js`](tests/e2e.js) · packages [verified](tests/validate.js) loadable · 2026-06-26_
32
32
 
33
33
  <!-- STATS:END -->
34
34
 
package/dist/index.cjs ADDED
@@ -0,0 +1,181 @@
1
+ var __create = Object.create;
2
+ var __getProtoOf = Object.getPrototypeOf;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ function __accessProp(key) {
8
+ return this[key];
9
+ }
10
+ var __toESMCache_node;
11
+ var __toESMCache_esm;
12
+ var __toESM = (mod, isNodeMode, target) => {
13
+ var canCache = mod != null && typeof mod === "object";
14
+ if (canCache) {
15
+ var cache = isNodeMode ? __toESMCache_node ??= new WeakMap : __toESMCache_esm ??= new WeakMap;
16
+ var cached = cache.get(mod);
17
+ if (cached)
18
+ return cached;
19
+ }
20
+ target = mod != null ? __create(__getProtoOf(mod)) : {};
21
+ const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
22
+ for (let key of __getOwnPropNames(mod))
23
+ if (!__hasOwnProp.call(to, key))
24
+ __defProp(to, key, {
25
+ get: __accessProp.bind(mod, key),
26
+ enumerable: true
27
+ });
28
+ if (canCache)
29
+ cache.set(mod, to);
30
+ return to;
31
+ };
32
+ var __toCommonJS = (from) => {
33
+ var entry = (__moduleCache ??= new WeakMap).get(from), desc;
34
+ if (entry)
35
+ return entry;
36
+ entry = __defProp({}, "__esModule", { value: true });
37
+ if (from && typeof from === "object" || typeof from === "function") {
38
+ for (var key of __getOwnPropNames(from))
39
+ if (!__hasOwnProp.call(entry, key))
40
+ __defProp(entry, key, {
41
+ get: __accessProp.bind(from, key),
42
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
43
+ });
44
+ }
45
+ __moduleCache.set(from, entry);
46
+ return entry;
47
+ };
48
+ var __moduleCache;
49
+ var __returnValue = (v) => v;
50
+ function __exportSetter(name, newValue) {
51
+ this[name] = __returnValue.bind(null, newValue);
52
+ }
53
+ var __export = (target, all) => {
54
+ for (var name in all)
55
+ __defProp(target, name, {
56
+ get: all[name],
57
+ enumerable: true,
58
+ configurable: true,
59
+ set: __exportSetter.bind(all, name)
60
+ });
61
+ };
62
+
63
+ // src/plugin.js
64
+ var exports_plugin = {};
65
+ __export(exports_plugin, {
66
+ minifyShader: () => minifyShader,
67
+ extractShaderLiterals: () => extractShaderLiterals,
68
+ compressShaderLiterals: () => compressShaderLiterals
69
+ });
70
+ module.exports = __toCommonJS(exports_plugin);
71
+ var import_pluginutils = require("@rollup/pluginutils");
72
+ var import_magic_string = __toESM(require("magic-string"));
73
+ var import_unplugin = require("unplugin");
74
+
75
+ // src/core.js
76
+ var import_parser = require("@babel/parser");
77
+ var import_traverse = __toESM(require("@babel/traverse"));
78
+ var traverse = import_traverse.default.default || import_traverse.default;
79
+ function extractShaderLiterals(code, tags = ["glsl", "wgsl", "shader"]) {
80
+ const tagSet = new Set(tags);
81
+ const literals = [];
82
+ try {
83
+ const ast = import_parser.parse(code, {
84
+ sourceType: "module",
85
+ plugins: ["typescript", "jsx", "decorators-legacy"],
86
+ allowReturnOutsideFunction: true
87
+ });
88
+ traverse(ast, {
89
+ TaggedTemplateExpression(path) {
90
+ const { tag, quasi } = path.node;
91
+ let tagName = null;
92
+ if (tag.type === "Identifier" && tagSet.has(tag.name)) {
93
+ tagName = tag.name;
94
+ } else if (tag.type === "MemberExpression" && tag.property.type === "Identifier" && tagSet.has(tag.property.name)) {
95
+ tagName = tag.property.name;
96
+ }
97
+ if (tagName) {
98
+ literals.push({
99
+ tag: tagName,
100
+ value: quasi.quasis.map((q) => q.value.raw).join(""),
101
+ start: quasi.start,
102
+ end: quasi.end
103
+ });
104
+ }
105
+ },
106
+ TemplateLiteral(path) {
107
+ const node = path.node;
108
+ const leadingComments = node.leadingComments || path.parentPath?.node?.leadingComments || [];
109
+ for (const comment of leadingComments) {
110
+ if (!comment || comment.type !== "CommentBlock")
111
+ continue;
112
+ const m = comment.value.match(/^\s*(glsl|wgsl|shader)\s*$/);
113
+ if (m) {
114
+ literals.push({
115
+ tag: m[1],
116
+ value: node.quasis.map((q) => q.value.raw).join(""),
117
+ start: node.start,
118
+ end: node.end
119
+ });
120
+ break;
121
+ }
122
+ }
123
+ }
124
+ });
125
+ } catch (e) {}
126
+ return literals;
127
+ }
128
+ function minifyShader(src) {
129
+ return src.replace(/\/\*[\s\S]*?\*\//g, "").replace(/\/\/.*$/gm, "").replace(/[ \t]+/g, " ").replace(/\n{2,}/g, `
130
+ `).trim();
131
+ }
132
+
133
+ // src/plugin.js
134
+ var compressShaderLiterals = import_unplugin.createUnplugin((options = {}) => {
135
+ const tags = options.tags || ["glsl", "wgsl", "shader"];
136
+ const minify = options.transform || minifyShader;
137
+ const filter = import_pluginutils.createFilter(options.include || [/\.[jt]sx?$/], options.exclude || [/node_modules/, /dist/]);
138
+ let beforeText = "";
139
+ let afterText = "";
140
+ return {
141
+ name: "compress-shader-literals",
142
+ enforce: "pre",
143
+ transform(code, id) {
144
+ if (!filter(id))
145
+ return null;
146
+ if (!tags.some((tag) => code.includes(tag)))
147
+ return null;
148
+ const literals = extractShaderLiterals(code, tags);
149
+ if (literals.length === 0)
150
+ return null;
151
+ if (options.debug) {
152
+ console.log(`[compress-shader-literals] ${id}: ${literals.length} literal(s) — ${literals.map((l) => l.tag).join(", ")}`);
153
+ }
154
+ const ms = new import_magic_string.default(code);
155
+ let hasChanges = false;
156
+ for (const literal of literals) {
157
+ const minified = minify(literal.value);
158
+ if (literal.value !== minified) {
159
+ ms.overwrite(literal.start, literal.end, `\`${minified}\``);
160
+ hasChanges = true;
161
+ }
162
+ if (options.outputRatio) {
163
+ beforeText += literal.value;
164
+ afterText += minified;
165
+ }
166
+ }
167
+ if (!hasChanges)
168
+ return null;
169
+ return {
170
+ code: ms.toString(),
171
+ map: ms.generateMap({ hires: true, source: id })
172
+ };
173
+ },
174
+ async buildEnd() {
175
+ if (options.outputRatio && beforeText) {
176
+ const { diff, snap } = await import("byte-snap");
177
+ diff(snap.text(beforeText), snap.text(afterText)).print();
178
+ }
179
+ }
180
+ };
181
+ });
package/dist/index.js CHANGED
@@ -1,6 +1,8 @@
1
+ import { createRequire } from "node:module";
2
+ var __require = /* @__PURE__ */ createRequire(import.meta.url);
3
+
1
4
  // src/plugin.js
2
5
  import { createFilter } from "@rollup/pluginutils";
3
- import { diff, snap } from "byte-snap";
4
6
  import MagicString from "magic-string";
5
7
  import { createUnplugin } from "unplugin";
6
8
 
@@ -103,13 +105,16 @@ var compressShaderLiterals = createUnplugin((options = {}) => {
103
105
  map: ms.generateMap({ hires: true, source: id })
104
106
  };
105
107
  },
106
- buildEnd() {
108
+ async buildEnd() {
107
109
  if (options.outputRatio && beforeText) {
110
+ const { diff, snap } = await import("byte-snap");
108
111
  diff(snap.text(beforeText), snap.text(afterText)).print();
109
112
  }
110
113
  }
111
114
  };
112
115
  });
113
116
  export {
117
+ minifyShader,
118
+ extractShaderLiterals,
114
119
  compressShaderLiterals
115
120
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "compress-shader-literals",
3
- "version": "0.0.7",
3
+ "version": "0.0.10",
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": [
@@ -34,19 +34,20 @@
34
34
  "author": "jayF0x",
35
35
  "repository": {
36
36
  "type": "git",
37
- "url": "https://github.com/jayf0x/compress-shader-literals"
37
+ "url": "git+https://github.com/jayf0x/compress-shader-literals.git"
38
38
  },
39
- "homepage": "https://github.com/jayF0x/compress-shader-literals#readme",
39
+ "homepage": "https://github.com/jayf0x/compress-shader-literals#readme",
40
40
  "bugs": {
41
- "url": "https://github.com/jayF0x/compress-shader-literals/issues"
41
+ "url": "https://github.com/jayf0x/compress-shader-literals/issues"
42
42
  },
43
- "main": "./dist/index.js",
43
+ "main": "./dist/index.cjs",
44
44
  "module": "./dist/index.js",
45
45
  "types": "./dist/index.d.ts",
46
46
  "exports": {
47
47
  ".": {
48
48
  "types": "./dist/index.d.ts",
49
- "import": "./dist/index.js"
49
+ "import": "./dist/index.js",
50
+ "require": "./dist/index.cjs"
50
51
  }
51
52
  },
52
53
  "files": [
@@ -54,28 +55,41 @@
54
55
  ],
55
56
  "sideEffects": false,
56
57
  "scripts": {
57
- "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 byte-snap --external magic-string --external unplugin && cp src/index.d.ts dist/index.d.ts && node scripts/compress-dist.js",
58
+ "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 byte-snap --external magic-string --external unplugin && cp src/index.d.ts dist/index.d.ts && bun build src/plugin.js --outdir dist --entry-naming index.cjs --target node --format cjs --external @babel/parser --external @babel/traverse --external @rollup/pluginutils --external byte-snap --external magic-string --external unplugin",
58
59
  "typecheck": "tsc --noEmit --strict --skipLibCheck src/index.d.ts",
59
60
  "test": "bun test",
60
61
  "test:run": "bun test",
62
+ "test:build": "bun run build && node tests/build-smoke.js",
61
63
  "test:validate": "cd tests && bun install && node validate.js",
62
64
  "test:e2e": "cd tests && bun install && node e2e.js",
63
65
  "format": "prettier --write .",
64
66
  "format:check": "prettier --check .",
65
67
  "npm:deploy": "bash ./scripts/publish-npm.sh",
66
- "rm:stalepr": "git branch -vv | grep '\\[gone\\]' | awk '{print $1}' | xargs git branch -D"
68
+ "rm:stalepr": "git branch -vv | grep '\\[gone\\]' | awk '{print $1}' | xargs git branch -D",
69
+ "size": "size-limit"
67
70
  },
68
71
  "dependencies": {
69
72
  "@babel/parser": "^8.0.0",
70
73
  "@babel/traverse": "^8.0.0",
71
74
  "@rollup/pluginutils": "^5.4.0",
72
- "byte-snap": "^1.0.3",
75
+ "byte-snap": "^1.0.5",
73
76
  "magic-string": "^0.30.21",
74
77
  "unplugin": "^3.0.0"
75
78
  },
76
79
  "devDependencies": {
77
80
  "@trivago/prettier-plugin-sort-imports": "^5.0.0",
78
81
  "prettier": "^3.0.0",
79
- "typescript": "^5.6.0"
80
- }
82
+ "typescript": "^5.6.0",
83
+ "size-limit": "^11.0.0",
84
+ "@size-limit/file": "^11.0.0"
85
+ },
86
+ "engines": {
87
+ "node": ">=18"
88
+ },
89
+ "size-limit": [
90
+ {
91
+ "path": "dist/index.js",
92
+ "limit": "1.7 kB"
93
+ }
94
+ ]
81
95
  }
package/dist/index.js.br DELETED
Binary file
package/dist/index.js.gz DELETED
Binary file