-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
70 lines (58 loc) · 1.66 KB
/
index.ts
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
import { OnLoadArgs, Plugin, PluginBuild } from 'esbuild';
import glslify from 'glslify';
import * as fs from 'node:fs';
import { dirname } from 'node:path';
import * as util from 'node:util';
export interface GLSLOptions {
minify?: boolean;
fileTypes?: string[];
useGlslify?: boolean;
}
function minifyShader(text: string) {
let forceNewline = false;
const lines = text.replace(
/\\(?:\r\n|\n\r|\n|\r)|\/\*.*?\*\/|\/\/(?:\\(?:\r\n|\n\r|\n|\r)|[^\n\r])*/g,
''
).split(/\n+/);
return lines.reduce((result: string[], line) => {
line = line.trim().replace(/\s{2,}|\t/, ' ');
if (line.charAt(0) === '#') {
if (forceNewline) {
result.push('\n');
}
result.push(line, '\n');
forceNewline = false;
} else {
result.push(
line.replace(
/\s*({|}|=|\*|,|\+|\/|>|<|&|\||\[|\]|\(|\)|-|!|;)\s*/g, '$1'
)
);
forceNewline = true;
}
return result;
}, []).join('').replace(/\n+/g, '\n');
}
export default function glsl({
minify = true,
useGlslify = true,
fileTypes = ['frag', 'vert', 'wgsl', 'vs', 'fs', 'glsl']
}: GLSLOptions = {}): Plugin {
const readFile = util.promisify(fs.readFile);
const filter = new RegExp(`\.(?:${fileTypes.join('|')})$`);
return {
name: 'glsl',
setup: (build: PluginBuild) => {
build.onLoad({ filter }, async (args: OnLoadArgs) => {
let source = await readFile(args.path, 'utf8');
if (useGlslify) {
source = glslify.compile(source, { basedir: dirname(args.path) });
}
return {
loader: 'text',
contents: minify ? minifyShader(source) : source
};
});
}
};
}