-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
48 lines (40 loc) · 1.32 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
import { createFilter } from 'rollup-pluginutils';
import MagicString from 'magic-string'
function compressShader(source) {
let needNewline = false;
return source.replace(/\\(?:\r\n|\n\r|\n|\r)|\/\*.*?\*\/|\/\/(?:\\(?:\r\n|\n\r|\n|\r)|[^\n\r])*/g, "").split(/\n+/).reduce((result, line) => {
line = line.trim().replace(/\s{2,}|\t/, " ");
if (line[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");
}
function generateCode(source) {
return `export default ${JSON.stringify(source)};`;
}
export default function glsl(options = {}) {
const filter = createFilter(options.include, options.exclude);
return {
name: 'glsl',
transform(source, id) {
if (!filter(id)) return;
const code = generateCode(options.compress !== false ? compressShader(source) : source),
magicString = new MagicString(code);
let result = { code: magicString.toString() };
if (options.sourceMap !== false) {
result.map = magicString.generateMap({ hires: true })
}
return result
}
};
}
glsl.compressShader = compressShader