gulp-dtsModuleSupport.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. if (imports.length)
  30. importsString += `import {${imports.join(',')}} from 'babylonjs/${name}';
  31. `;
  32. });
  33. }
  34. if (file.isNull()) {
  35. cb(null, file);
  36. return;
  37. }
  38. if (file.isStream()) {
  39. //streams not supported, no need for now.
  40. return;
  41. }
  42. try {
  43. file.contents = new Buffer(String(file.contents) + '\n' + importsString);
  44. this.push(file);
  45. } catch (err) {
  46. this.emit('error', new gutil.PluginError('gulp-add-module-exports', err, { fileName: file.path }));
  47. }
  48. cb();
  49. });
  50. };