gulp-dtsModuleSupport.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. var gutil = require('gulp-util');
  2. var through = require('through2');
  3. // inject - if set to true, it will add all declarations as imports.
  4. module.exports = function (moduleName, inject, declarations) {
  5. return through.obj(function (file, enc, cb) {
  6. let fileContent = file.contents.toString();
  7. let importsString = '';
  8. if (!inject) {
  9. declarations[moduleName] = declarations[moduleName] || [];
  10. let regexp = / (class|interface|type|const|enum|var) ([\w]*)/g;
  11. var match = regexp.exec(fileContent);
  12. while (match != null) {
  13. if (match[2]) {
  14. declarations[moduleName].push(match[2])
  15. }
  16. match = regexp.exec(fileContent);
  17. }
  18. } else {
  19. let declared = [];
  20. Object.keys(declarations).forEach(name => {
  21. if (name === moduleName) return;
  22. let imports = declarations[name].filter(obj => {
  23. let exists = declared.indexOf(obj) === -1;
  24. if (!exists) {
  25. declared.push(obj);
  26. }
  27. return exists;
  28. });
  29. importsString += `import {${imports.join(',')}} from 'babylonjs/${name}';
  30. `;
  31. });
  32. }
  33. if (file.isNull()) {
  34. cb(null, file);
  35. return;
  36. }
  37. if (file.isStream()) {
  38. //streams not supported, no need for now.
  39. return;
  40. }
  41. try {
  42. file.contents = new Buffer(String(file.contents) + '\n' + importsString);
  43. this.push(file);
  44. } catch (err) {
  45. this.emit('error', new gutil.PluginError('gulp-add-module-exports', err, { fileName: file.path }));
  46. }
  47. cb();
  48. });
  49. };