compress-shader-literals 0.0.10 → 1.0.0

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
@@ -28,7 +28,7 @@ Measured on the real shaders shipped by popular libraries:
28
28
  | `curtainsjs` | 7 | 3,406 B | 2,563 B | **24.8%** |
29
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-26_
31
+ _Auto-generated by [`tests/e2e.js`](tests/e2e.js) · packages [verified](tests/validate.js) loadable · 2026-06-27_
32
32
 
33
33
  <!-- STATS:END -->
34
34
 
package/dist/index.cjs CHANGED
@@ -1,181 +1,2 @@
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
- });
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=[/\.[jt]sx?$/],g=[/node_modules/,/dist/],_=e=>RegExp(`^\\s*(${e.join(`|`)})\\s*$`),v=p.default.default||p.default,y=(e,t=m)=>{let n=new Set(t),r=_(t),i=[];try{v((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&&i.push({tag:a,value:r.quasis.map(e=>e.value.raw).join(``),start:r.start,end:r.end})},TemplateLiteral(e){let t=e.node,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.map(e=>e.value.raw).join(``),start:t.start,end:t.end});break}}}})}catch(e){if(e.name!==`SyntaxError`)throw e}return i},b=e=>e.replace(/\/\*[\s\S]*?\*\//g,``).replace(/\/\/.*$/gm,``).replace(/[ \t]+/g,` `).replace(/\n{2,}/g,`
2
+ `).trim(),x=(0,d.createUnplugin)((e={})=>{let t=e.tags||m,n=e.transform||b,r=(0,c.createFilter)(e.include||h,e.exclude||g),i=``,a=``;return{name:`compress-shader-literals`,enforce:`pre`,transform(o,s){if(!r(s)||!t.some(e=>o.includes(e)))return null;let c=y(o,t);if(c.length===0)return null;e.debug&&console.log(`[compress-shader-literals] ${s}: ${c.length} literal(s) — ${c.map(e=>e.tag).join(`, `)}`);let l=new u.default(o),d=!1;for(let t of c){let r=n(t.value);t.value!==r&&(l.overwrite(t.start,t.end,`\`${r}\``),d=!0),e.outputRatio&&(i+=t.value,a+=r)}return d?{code:l.toString(),map:l.generateMap({hires:!0,source:s})}:null},buildEnd(){e.outputRatio&&i&&(0,l.diff)(l.snap.text(i),l.snap.text(a)).print()}}});exports.compressShaderLiterals=x,exports.extractShaderLiterals=y,exports.minifyShader=b;
package/dist/index.js CHANGED
@@ -1,120 +1,83 @@
1
- import { createRequire } from "node:module";
2
- var __require = /* @__PURE__ */ createRequire(import.meta.url);
3
-
4
- // src/plugin.js
5
- import { createFilter } from "@rollup/pluginutils";
6
- import MagicString from "magic-string";
7
- import { createUnplugin } from "unplugin";
8
-
9
- // src/core.js
10
- import { parse } from "@babel/parser";
11
- import _traverse from "@babel/traverse";
12
- var traverse = _traverse.default || _traverse;
13
- function extractShaderLiterals(code, tags = ["glsl", "wgsl", "shader"]) {
14
- const tagSet = new Set(tags);
15
- const literals = [];
16
- try {
17
- const ast = parse(code, {
18
- sourceType: "module",
19
- plugins: ["typescript", "jsx", "decorators-legacy"],
20
- allowReturnOutsideFunction: true
21
- });
22
- traverse(ast, {
23
- TaggedTemplateExpression(path) {
24
- const { tag, quasi } = path.node;
25
- let tagName = null;
26
- if (tag.type === "Identifier" && tagSet.has(tag.name)) {
27
- tagName = tag.name;
28
- } else if (tag.type === "MemberExpression" && tag.property.type === "Identifier" && tagSet.has(tag.property.name)) {
29
- tagName = tag.property.name;
30
- }
31
- if (tagName) {
32
- literals.push({
33
- tag: tagName,
34
- value: quasi.quasis.map((q) => q.value.raw).join(""),
35
- start: quasi.start,
36
- end: quasi.end
37
- });
38
- }
39
- },
40
- TemplateLiteral(path) {
41
- const node = path.node;
42
- const leadingComments = node.leadingComments || path.parentPath?.node?.leadingComments || [];
43
- for (const comment of leadingComments) {
44
- if (!comment || comment.type !== "CommentBlock")
45
- continue;
46
- const m = comment.value.match(/^\s*(glsl|wgsl|shader)\s*$/);
47
- if (m) {
48
- literals.push({
49
- tag: m[1],
50
- value: node.quasis.map((q) => q.value.raw).join(""),
51
- start: node.start,
52
- end: node.end
53
- });
54
- break;
55
- }
56
- }
57
- }
58
- });
59
- } catch (e) {}
60
- return literals;
61
- }
62
- function minifyShader(src) {
63
- return src.replace(/\/\*[\s\S]*?\*\//g, "").replace(/\/\/.*$/gm, "").replace(/[ \t]+/g, " ").replace(/\n{2,}/g, `
64
- `).trim();
65
- }
66
-
67
- // src/plugin.js
68
- var compressShaderLiterals = createUnplugin((options = {}) => {
69
- const tags = options.tags || ["glsl", "wgsl", "shader"];
70
- const minify = options.transform || minifyShader;
71
- const filter = createFilter(options.include || [/\.[jt]sx?$/], options.exclude || [/node_modules/, /dist/]);
72
- let beforeText = "";
73
- let afterText = "";
74
- return {
75
- name: "compress-shader-literals",
76
- enforce: "pre",
77
- transform(code, id) {
78
- if (!filter(id))
79
- return null;
80
- if (!tags.some((tag) => code.includes(tag)))
81
- return null;
82
- const literals = extractShaderLiterals(code, tags);
83
- if (literals.length === 0)
84
- return null;
85
- if (options.debug) {
86
- console.log(`[compress-shader-literals] ${id}: ${literals.length} literal(s) — ${literals.map((l) => l.tag).join(", ")}`);
87
- }
88
- const ms = new MagicString(code);
89
- let hasChanges = false;
90
- for (const literal of literals) {
91
- const minified = minify(literal.value);
92
- if (literal.value !== minified) {
93
- ms.overwrite(literal.start, literal.end, `\`${minified}\``);
94
- hasChanges = true;
95
- }
96
- if (options.outputRatio) {
97
- beforeText += literal.value;
98
- afterText += minified;
99
- }
100
- }
101
- if (!hasChanges)
102
- return null;
103
- return {
104
- code: ms.toString(),
105
- map: ms.generateMap({ hires: true, source: id })
106
- };
107
- },
108
- async buildEnd() {
109
- if (options.outputRatio && beforeText) {
110
- const { diff, snap } = await import("byte-snap");
111
- diff(snap.text(beforeText), snap.text(afterText)).print();
112
- }
113
- }
114
- };
1
+ import { createFilter as e } from "@rollup/pluginutils";
2
+ import { diff as t, snap as n } from "byte-snap";
3
+ import r from "magic-string";
4
+ import { createUnplugin as i } from "unplugin";
5
+ import { parse as a } from "@babel/parser";
6
+ import o from "@babel/traverse";
7
+ //#region src/defaults.js
8
+ var s = [
9
+ "glsl",
10
+ "wgsl",
11
+ "shader"
12
+ ], c = [/\.[jt]sx?$/], l = [/node_modules/, /dist/], u = (e) => RegExp(`^\\s*(${e.join("|")})\\s*$`), d = o.default || o, f = (e, t = s) => {
13
+ let n = new Set(t), r = u(t), i = [];
14
+ try {
15
+ d(a(e, {
16
+ sourceType: "module",
17
+ plugins: [
18
+ "typescript",
19
+ "jsx",
20
+ "decorators-legacy"
21
+ ],
22
+ allowReturnOutsideFunction: !0
23
+ }), {
24
+ TaggedTemplateExpression(e) {
25
+ let { tag: t, quasi: r } = e.node, a = null;
26
+ 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 && i.push({
27
+ tag: a,
28
+ value: r.quasis.map((e) => e.value.raw).join(""),
29
+ start: r.start,
30
+ end: r.end
31
+ });
32
+ },
33
+ TemplateLiteral(e) {
34
+ let t = e.node, n = t.leadingComments || e.parentPath?.node?.leadingComments || [];
35
+ for (let e of n) {
36
+ if (!e || e.type !== "CommentBlock") continue;
37
+ let n = e.value.match(r);
38
+ if (n) {
39
+ i.push({
40
+ tag: n[1],
41
+ value: t.quasis.map((e) => e.value.raw).join(""),
42
+ start: t.start,
43
+ end: t.end
44
+ });
45
+ break;
46
+ }
47
+ }
48
+ }
49
+ });
50
+ } catch (e) {
51
+ if (e.name !== "SyntaxError") throw e;
52
+ }
53
+ return i;
54
+ }, p = (e) => e.replace(/\/\*[\s\S]*?\*\//g, "").replace(/\/\/.*$/gm, "").replace(/[ \t]+/g, " ").replace(/\n{2,}/g, "\n").trim(), m = i((i = {}) => {
55
+ let a = i.tags || s, o = i.transform || p, u = e(i.include || c, i.exclude || l), d = "", m = "";
56
+ return {
57
+ name: "compress-shader-literals",
58
+ enforce: "pre",
59
+ transform(e, t) {
60
+ if (!u(t) || !a.some((t) => e.includes(t))) return null;
61
+ let n = f(e, a);
62
+ if (n.length === 0) return null;
63
+ i.debug && console.log(`[compress-shader-literals] ${t}: ${n.length} literal(s) — ${n.map((e) => e.tag).join(", ")}`);
64
+ let s = new r(e), c = !1;
65
+ for (let e of n) {
66
+ let t = o(e.value);
67
+ e.value !== t && (s.overwrite(e.start, e.end, `\`${t}\``), c = !0), i.outputRatio && (d += e.value, m += t);
68
+ }
69
+ return c ? {
70
+ code: s.toString(),
71
+ map: s.generateMap({
72
+ hires: !0,
73
+ source: t
74
+ })
75
+ } : null;
76
+ },
77
+ buildEnd() {
78
+ i.outputRatio && d && t(n.text(d), n.text(m)).print();
79
+ }
80
+ };
115
81
  });
116
- export {
117
- minifyShader,
118
- extractShaderLiterals,
119
- compressShaderLiterals
120
- };
82
+ //#endregion
83
+ export { m as compressShaderLiterals, f as extractShaderLiterals, p as minifyShader };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "compress-shader-literals",
3
- "version": "0.0.10",
3
+ "version": "1.0.0",
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": [
@@ -55,7 +55,8 @@
55
55
  ],
56
56
  "sideEffects": false,
57
57
  "scripts": {
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
+ "build": "vite build && cp src/index.d.ts dist/index.d.ts",
59
+ "build_old": "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",
59
60
  "typecheck": "tsc --noEmit --strict --skipLibCheck src/index.d.ts",
60
61
  "test": "bun test",
61
62
  "test:run": "bun test",
@@ -77,11 +78,12 @@
77
78
  "unplugin": "^3.0.0"
78
79
  },
79
80
  "devDependencies": {
81
+ "@size-limit/file": "^11.0.0",
80
82
  "@trivago/prettier-plugin-sort-imports": "^5.0.0",
81
83
  "prettier": "^3.0.0",
82
- "typescript": "^5.6.0",
83
84
  "size-limit": "^11.0.0",
84
- "@size-limit/file": "^11.0.0"
85
+ "typescript": "^5.6.0",
86
+ "vite": "^8.1.0"
85
87
  },
86
88
  "engines": {
87
89
  "node": ">=18"