-
Notifications
You must be signed in to change notification settings - Fork 2
/
babel.transform.cjs
72 lines (67 loc) · 1.97 KB
/
babel.transform.cjs
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
const { types } = require('@babel/core');
const syntaxTypeScript = require('@babel/plugin-syntax-typescript').default;
/**
* Plugin to help minifying the JS:
* - Transpiles all enums into const object for inlining
* - Replace all const with let
* - Remove unused class methods
*/
module.exports = function minify() {
return {
name: 'minify',
inherits: syntaxTypeScript,
visitor: {
TSEnumDeclaration(path) {
const [constObjectPath] = path.replaceWith(
tsEnumDeclarationToConstObject(path)
);
path.scope.registerDeclaration(constObjectPath);
},
VariableDeclaration(path) {
path.replaceWith(variableDeclarationToLet(path));
},
ClassDeclaration(path) {
path.replaceWith(removeMethodsFromClassDeclaration(path));
},
},
};
};
function tsEnumDeclarationToConstObject(path) {
return types.variableDeclaration('const', [
types.variableDeclarator(
path.node.id,
types.objectExpression(
tsEnumMembersToObjectProperties(path.get('members')),
),
),
]);
}
function tsEnumMembersToObjectProperties(memberPaths) {
return memberPaths.map(({ node }) => {
const keyNode = node.id;
const valueNode = node.initializer;
return types.objectProperty(keyNode, valueNode);
});
}
function variableDeclarationToLet({ node }) {
if (node.kind === 'const') {
node.kind = 'let';
}
return node;
}
function removeMethodsFromClassDeclaration(path) {
const { node } = path;
const name = node.id.name;
if (['NanoGLRenderingDevice', 'NanoGLRenderPassContext', 'GLBuffer', 'GLRenderPass', 'GLShader', 'GLPipeline'].includes(name)) {
const properties = [];
for (const property of node.body.body) {
const propName = property.key.name;
if (['texture', 'scissor', 'blendColor', 'stencilRef', 'viewport', 'destroy'].includes(propName)) {
continue;
}
properties.push(property);
}
node.body.body = properties;
}
return node;
}