compress-shader-literals 0.0.3
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/LICENSE +21 -0
- package/README.md +43 -0
- package/dist/index.js +132 -0
- package/dist/index.js.br +0 -0
- package/dist/index.js.gz +0 -0
- package/package.json +61 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Jonatan
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# compress-shader-literals
|
|
2
|
+
|
|
3
|
+
Local Vite plugin that minifies GLSL and WGSL shader strings at build time.
|
|
4
|
+
|
|
5
|
+
Strips block comments, line comments, and redundant whitespace from shader template literals. Runs as a `pre` transform so the result feeds into esbuild minification.
|
|
6
|
+
|
|
7
|
+
## Usage
|
|
8
|
+
|
|
9
|
+
```js
|
|
10
|
+
// vite.config.js
|
|
11
|
+
import { compressShaderLiterals } from './compress-shader-literals/plugin.js';
|
|
12
|
+
|
|
13
|
+
export default {
|
|
14
|
+
plugins: [compressShaderLiterals.vite({ outputRatio: true })],
|
|
15
|
+
};
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Options
|
|
19
|
+
|
|
20
|
+
| Option | Default | Description |
|
|
21
|
+
| ------------- | ---------------------------- | ------------------------------ |
|
|
22
|
+
| `tags` | `['glsl', 'wgsl', 'shader']` | Tag names to match |
|
|
23
|
+
| `include` | `/\.[jt]sx?$/` | Files to process |
|
|
24
|
+
| `exclude` | `/node_modules/`, `/dist/` | Files to skip |
|
|
25
|
+
| `outputRatio` | `false` | Print size summary after build |
|
|
26
|
+
|
|
27
|
+
## Supported syntax
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
// Tagged template literal
|
|
31
|
+
const vert = wgsl`void main() { ... }`;
|
|
32
|
+
|
|
33
|
+
// Comment-prefixed template literal
|
|
34
|
+
const frag = /* glsl */ `void main() { ... }`;
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Test
|
|
38
|
+
|
|
39
|
+
```sh
|
|
40
|
+
node compress-shader-literals/run-test.js
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Runs the transform against `src/core/shaders.ts` and prints bytes saved.
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
// src/plugin.js
|
|
2
|
+
import { createFilter } from "@rollup/pluginutils";
|
|
3
|
+
import MagicString from "magic-string";
|
|
4
|
+
import { createUnplugin } from "unplugin";
|
|
5
|
+
|
|
6
|
+
// src/core.js
|
|
7
|
+
import { parse } from "@babel/parser";
|
|
8
|
+
import _traverse from "@babel/traverse";
|
|
9
|
+
var traverse = _traverse.default || _traverse;
|
|
10
|
+
function extractShaderLiterals(code, tags = ["glsl", "wgsl", "shader"]) {
|
|
11
|
+
const tagSet = new Set(tags);
|
|
12
|
+
const literals = [];
|
|
13
|
+
try {
|
|
14
|
+
const ast = parse(code, {
|
|
15
|
+
sourceType: "module",
|
|
16
|
+
plugins: ["typescript", "jsx", "decorators-legacy"],
|
|
17
|
+
allowReturnOutsideFunction: true
|
|
18
|
+
});
|
|
19
|
+
traverse(ast, {
|
|
20
|
+
TaggedTemplateExpression(path) {
|
|
21
|
+
const { tag, quasi } = path.node;
|
|
22
|
+
let tagName = null;
|
|
23
|
+
if (tag.type === "Identifier" && tagSet.has(tag.name)) {
|
|
24
|
+
tagName = tag.name;
|
|
25
|
+
} else if (tag.type === "MemberExpression" && tag.property.type === "Identifier" && tagSet.has(tag.property.name)) {
|
|
26
|
+
tagName = tag.property.name;
|
|
27
|
+
}
|
|
28
|
+
if (tagName) {
|
|
29
|
+
literals.push({
|
|
30
|
+
tag: tagName,
|
|
31
|
+
value: quasi.quasis.map((q) => q.value.raw).join(""),
|
|
32
|
+
start: quasi.start,
|
|
33
|
+
end: quasi.end
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
TemplateLiteral(path) {
|
|
38
|
+
const node = path.node;
|
|
39
|
+
const leadingComments = node.leadingComments || path.parentPath?.node?.leadingComments || [];
|
|
40
|
+
for (const comment of leadingComments) {
|
|
41
|
+
if (!comment || comment.type !== "CommentBlock")
|
|
42
|
+
continue;
|
|
43
|
+
const m = comment.value.match(/^\s*(glsl|wgsl|shader)\s*$/);
|
|
44
|
+
if (m) {
|
|
45
|
+
literals.push({
|
|
46
|
+
tag: m[1],
|
|
47
|
+
value: node.quasis.map((q) => q.value.raw).join(""),
|
|
48
|
+
start: node.start,
|
|
49
|
+
end: node.end
|
|
50
|
+
});
|
|
51
|
+
break;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
} catch (e) {}
|
|
57
|
+
return literals;
|
|
58
|
+
}
|
|
59
|
+
function minifyShader(src) {
|
|
60
|
+
return src.replace(/\/\*[\s\S]*?\*\//g, "").replace(/\/\/.*$/gm, "").replace(/[ \t]+/g, " ").replace(/\n{2,}/g, `
|
|
61
|
+
`).trim();
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// src/plugin.js
|
|
65
|
+
function fmtBytes(bytes) {
|
|
66
|
+
const units = ["B", "KB", "MB", "GB"];
|
|
67
|
+
let v = bytes;
|
|
68
|
+
let u = 0;
|
|
69
|
+
while (v >= 1024 && u < units.length - 1) {
|
|
70
|
+
v /= 1024;
|
|
71
|
+
u++;
|
|
72
|
+
}
|
|
73
|
+
return `${v.toFixed(2)} ${units[u]}`;
|
|
74
|
+
}
|
|
75
|
+
function printRatio(beforeBytes, afterBytes, files) {
|
|
76
|
+
const saved = beforeBytes - afterBytes;
|
|
77
|
+
const pct = beforeBytes === 0 ? 0 : saved / beforeBytes * 100;
|
|
78
|
+
console.log(`
|
|
79
|
+
compress-shader-literals`);
|
|
80
|
+
console.log("────────────────────────");
|
|
81
|
+
console.log(`${fmtBytes(beforeBytes)} → ${fmtBytes(afterBytes)}`);
|
|
82
|
+
console.log(`saved: ${fmtBytes(saved)} (${pct.toFixed(2)}% smaller)`);
|
|
83
|
+
console.log(`shaders: ${files}
|
|
84
|
+
`);
|
|
85
|
+
}
|
|
86
|
+
var compressShaderLiterals = createUnplugin((options = {}) => {
|
|
87
|
+
const tags = options.tags || ["glsl", "wgsl", "shader"];
|
|
88
|
+
const filter = createFilter(options.include || [/\.[jt]sx?$/], options.exclude || [/node_modules/, /dist/]);
|
|
89
|
+
const stats = { before: 0, after: 0, files: 0 };
|
|
90
|
+
return {
|
|
91
|
+
name: "compress-shader-literals",
|
|
92
|
+
enforce: "pre",
|
|
93
|
+
transform(code, id) {
|
|
94
|
+
if (!filter(id))
|
|
95
|
+
return null;
|
|
96
|
+
if (!tags.some((tag) => code.includes(tag)))
|
|
97
|
+
return null;
|
|
98
|
+
const literals = extractShaderLiterals(code, tags);
|
|
99
|
+
if (literals.length === 0)
|
|
100
|
+
return null;
|
|
101
|
+
const ms = new MagicString(code);
|
|
102
|
+
let hasChanges = false;
|
|
103
|
+
for (const literal of literals) {
|
|
104
|
+
const minified = minifyShader(literal.value);
|
|
105
|
+
if (literal.value !== minified) {
|
|
106
|
+
ms.overwrite(literal.start, literal.end, `\`${minified}\``);
|
|
107
|
+
hasChanges = true;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
if (!hasChanges)
|
|
111
|
+
return null;
|
|
112
|
+
const resultCode = ms.toString();
|
|
113
|
+
if (options.outputRatio) {
|
|
114
|
+
stats.before += Buffer.byteLength(code);
|
|
115
|
+
stats.after += Buffer.byteLength(resultCode);
|
|
116
|
+
stats.files += 1;
|
|
117
|
+
}
|
|
118
|
+
return {
|
|
119
|
+
code: resultCode,
|
|
120
|
+
map: ms.generateMap({ hires: true, source: id })
|
|
121
|
+
};
|
|
122
|
+
},
|
|
123
|
+
buildEnd() {
|
|
124
|
+
if (options.outputRatio && stats.files > 0) {
|
|
125
|
+
printRatio(stats.before, stats.after, stats.files);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
});
|
|
130
|
+
export {
|
|
131
|
+
compressShaderLiterals
|
|
132
|
+
};
|
package/dist/index.js.br
ADDED
|
Binary file
|
package/dist/index.js.gz
ADDED
|
Binary file
|
package/package.json
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "compress-shader-literals",
|
|
3
|
+
"version": "0.0.3",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Size matters. Minify GLSL/WGSL shader template literals at build time — universal plugin for Vite, Rollup, webpack, esbuild & more.",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"shader",
|
|
8
|
+
"glsl",
|
|
9
|
+
"wgsl",
|
|
10
|
+
"minify",
|
|
11
|
+
"compress",
|
|
12
|
+
"webgl",
|
|
13
|
+
"webgpu",
|
|
14
|
+
"vite-plugin",
|
|
15
|
+
"rollup-plugin",
|
|
16
|
+
"webpack-plugin",
|
|
17
|
+
"esbuild-plugin",
|
|
18
|
+
"unplugin"
|
|
19
|
+
],
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"author": "jayF0x",
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "https://github.com/jayf0x/compress-shader-literals"
|
|
25
|
+
},
|
|
26
|
+
"homepage": "https://github.com/jayF0x/compress-shader-literals#readme",
|
|
27
|
+
"bugs": {
|
|
28
|
+
"url": "https://github.com/jayF0x/compress-shader-literals/issues"
|
|
29
|
+
},
|
|
30
|
+
"main": "./dist/index.js",
|
|
31
|
+
"module": "./dist/index.js",
|
|
32
|
+
"exports": {
|
|
33
|
+
".": {
|
|
34
|
+
"import": "./dist/index.js"
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"files": [
|
|
38
|
+
"dist"
|
|
39
|
+
],
|
|
40
|
+
"sideEffects": false,
|
|
41
|
+
"scripts": {
|
|
42
|
+
"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 magic-string --external unplugin && node scripts/compress-dist.js",
|
|
43
|
+
"test": "bun test",
|
|
44
|
+
"test:run": "bun test",
|
|
45
|
+
"format": "prettier --write .",
|
|
46
|
+
"format:check": "prettier --check .",
|
|
47
|
+
"npm:deploy": "bash ./scripts/publish-npm.sh",
|
|
48
|
+
"rm:stalepr": "git branch -vv | grep '\\[gone\\]' | awk '{print $1}' | xargs git branch -D"
|
|
49
|
+
},
|
|
50
|
+
"dependencies": {
|
|
51
|
+
"@babel/parser": "^8.0.0",
|
|
52
|
+
"@babel/traverse": "^8.0.0",
|
|
53
|
+
"@rollup/pluginutils": "^5.4.0",
|
|
54
|
+
"magic-string": "^0.30.21",
|
|
55
|
+
"unplugin": "^3.0.0"
|
|
56
|
+
},
|
|
57
|
+
"devDependencies": {
|
|
58
|
+
"@trivago/prettier-plugin-sort-imports": "^5.0.0",
|
|
59
|
+
"prettier": "^3.0.0"
|
|
60
|
+
}
|
|
61
|
+
}
|