-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
72 lines (62 loc) · 2.05 KB
/
index.js
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
63
64
65
66
67
68
69
70
71
72
/**
* @author pschroen / https://ufo.ai/
*/
import { dirname } from 'node:path';
import { createFilter } from '@rollup/pluginutils';
import { compile } from 'glslify';
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 glslify(userOptions = {}) {
const options = Object.assign(
{
include: [
'**/*.vs',
'**/*.fs',
'**/*.vert',
'**/*.frag',
'**/*.glsl'
]
},
userOptions
);
const filter = createFilter(options.include, options.exclude);
return {
name: 'glslify',
transform(code, id) {
if (!filter(id)) return;
const fileOptions = Object.assign(
{
basedir: dirname(id)
},
options
);
code = compile(code, fileOptions);
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: '' }
};
}
};
}