-
-
Notifications
You must be signed in to change notification settings - Fork 401
Expand file tree
/
Copy pathrollup-plugin-glsl.js
More file actions
62 lines (54 loc) · 1.72 KB
/
rollup-plugin-glsl.js
File metadata and controls
62 lines (54 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* Based on rollup-plugin-glslify: https://github.com/glslify/rollup-plugin-glslify/blob/master/index.js
* Modifications:
* Remove use `glslify` to compile GLSL files, cause it is not necessary for shaderlab.
*/
import { createFilter } from "@rollup/pluginutils";
function compressShader(code) {
// Based on https://github.com/vwochnik/rollup-plugin-glsl
// Modified to remove multiline comments. See #16
let needNewline = false;
return code
.replace(/\\(?:\r\n|\n\r|\n|\r)|\/\*.*?\*\/|\/\/(?:\\(?:\r\n|\n\r|\n|\r)|[^\n\r])*/gs, "")
.split(/\n+/)
.reduce((result, line) => {
line = line.trim().replace(/\s{2,}|\t/, " "); // lgtm[js/incomplete-sanitization]
if (line.charAt(0) === "#") {
if (needNewline) {
result.push("\n");
}
result.push(line, "\n");
needNewline = false;
} else {
result.push(line.replace(/\s*({|}|=|\*|,|\+|\/|>|<|&|\||\[|\]|\(|\)|-|!|;)\s*/g, "$1"));
needNewline = true;
}
return result;
}, [])
.join("")
.replace(/\n+/g, "\n");
}
export default function glsl(userOptions = {}) {
const options = Object.assign(
{
include: ["**/*.vs", "**/*.fs", "**/*.vert", "**/*.frag", "**/*.glsl"]
},
userOptions
);
const filter = createFilter(options.include, options.exclude);
return {
name: "glsl",
transform(code, id) {
if (!filter(id)) return;
if (typeof options.compress === "function") {
code = options.compress(code);
} else if (options.compress !== false) {
code = compressShader(code);
}
return {
code: `export default ${JSON.stringify(code)}; // eslint-disable-line`,
map: { mappings: "" }
};
}
};
}