inline-transform.js 1003 B

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