gulp-dtsModuleSupport.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 = / (abstract class|function|class|interface|type|const|enum|var) ([\w]*)/g;
  11. var match = regexp.exec(fileContent);
  12. while (match != null) {
  13. if (match[2]) {
  14. // check it is not SIMD:
  15. let simdMatch = / interface (\w*\dx\d{1,2}\w*)/.exec(match[0]);
  16. if (!simdMatch && match[2] !== 'earcut' && match[2] !== 'deviation' && match[2] !== 'flatten')
  17. declarations[moduleName].push(match[2]);
  18. }
  19. match = regexp.exec(fileContent);
  20. }
  21. } else {
  22. let declared = [];
  23. Object.keys(declarations).forEach(name => {
  24. if (name === moduleName) return;
  25. let imports = declarations[name].filter(obj => {
  26. let exists = declared.indexOf(obj) !== -1;
  27. if (!exists) {
  28. declared.push(obj);
  29. }
  30. return !exists;
  31. });
  32. if (imports.length)
  33. importsString += `import {${imports.join(',')}} from 'babylonjs/${name}';
  34. `;
  35. });
  36. }
  37. if (file.isNull()) {
  38. cb(null, file);
  39. return;
  40. }
  41. if (file.isStream()) {
  42. //streams not supported, no need for now.
  43. return;
  44. }
  45. try {
  46. file.contents = new Buffer(String(file.contents) + '\n' + importsString);
  47. this.push(file);
  48. } catch (err) {
  49. this.emit('error', new gutil.PluginError('gulp-add-module-exports', err, { fileName: file.path }));
  50. }
  51. cb();
  52. });
  53. };