gulp-processLooseDeclarationsEs6.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Dependencies.
  2. var through = require('through2');
  3. var PluginError = require('plugin-error');
  4. let fs = require('fs');
  5. /**
  6. * Encapsulates types in declare global { }
  7. */
  8. function processLooseDeclarations(sourceCode) {
  9. // To replace if that causes issue (defining start point of the concat
  10. // as interface like the first code line of the first mixin)
  11. sourceCode = sourceCode.replace(/declare /g, "");
  12. sourceCode = sourceCode.replace(/interface /, `declare global {
  13. interface `);
  14. sourceCode += `
  15. }`;
  16. return sourceCode;
  17. }
  18. /**
  19. * Prepare loose declarations to be added to the package.
  20. */
  21. function main(replacements) {
  22. return through.obj(function (file, enc, cb) {
  23. if (file.isNull()) {
  24. cb(null, file);
  25. return;
  26. }
  27. if (file.isStream()) {
  28. cb(new PluginError("Process Shader", "Streaming not supported."));
  29. }
  30. let data = file.contents.toString();
  31. data = processLooseDeclarations(data, replacements);
  32. file.contents = Buffer.from(data);
  33. this.push(file);
  34. return cb();
  35. });
  36. }
  37. module.exports = main;