-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprocessGlslTag.js
More file actions
44 lines (40 loc) · 1.3 KB
/
processGlslTag.js
File metadata and controls
44 lines (40 loc) · 1.3 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
const { readFileSync } = require('fs');
const { dirname, resolve } = require('path');
const { types: t } = require('@babel/core');
const evaluateExpression = require('./evaluateExpression');
const compile = require('./compile');
function processGlslTag(path, state) {
const cwd = dirname(state.filename);
const env = { cwd };
if (
t.isCallExpression(path.node) &&
t.isStringLiteral(path.node.arguments[0])
) {
const filename = resolve(
dirname(state.filename),
path.node.arguments[0].value
);
const contents = readFileSync(filename, "utf8");
const result = compile(contents, { basedir: dirname(filename) });
path.replaceWith(t.stringLiteral(result));
return;
} else {
const literal = evaluateExpression(path, path, env);
if (t.isTemplateLiteral(literal)) {
literal.quasis = literal.quasis.map(quasi => {
quasi.value.raw = `\n${compile(quasi.value.raw, { basedir: cwd })}`;
return quasi;
});
path.replaceWith(literal);
return;
} else if (typeof literal === 'string') {
const result = compile(literal, { basedir: cwd });
path.replaceWith(t.stringLiteral(result));
return;
}
}
throw new Error(
'babel-plugin-glsl: string template could not be evaluated'
);
}
module.exports = processGlslTag;