-
Notifications
You must be signed in to change notification settings - Fork 32
/
inline-transform.js
23 lines (22 loc) · 1003 Bytes
/
inline-transform.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
var through = require('through2');
module.exports = function (file) {
return through(function (buf, enc, next) {
let originalContent = buf.toString('utf8');
let dimensions = 2; // change this if you need different number of dimensions
if (file.match(/codeGenerators\/generate/)) {
let content = require(file);
let matches = originalContent.match(/^\/\/ InlineTransform: (.+)$/gm);
let additionalTransform = matches ? matches.map(name => {
let f = name.substr('// InlineTransform: '.length);
return content[f](dimensions);
}).join('\n') : '';
let exportCodeMatch = originalContent.match(/^\/\/ InlineTransformExport: (.+)$/m);
let codeExport = exportCodeMatch ? exportCodeMatch[1] :
`module.exports = function() { return ${content(dimensions).toString()} }`;
this.push(`${additionalTransform}\n${codeExport}`);
} else {
this.push(originalContent);
}
next();
});
};