gulp-dtsModuleSupport.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. var gutil = require('gulp-util');
  2. var through = require('through2');
  3. var path = require('path');
  4. // inject - if set to true, it will add all declarations as imports.
  5. module.exports = function (moduleName, inject, declarations, perFile, dependencyTree) {
  6. return through.obj(function (file, enc, cb) {
  7. let basename = (path.basename(file.path, ".d.ts"));
  8. let fileContent = file.contents.toString();
  9. let importsString = '';
  10. if (!inject) {
  11. perFile[basename] = perFile[basename] || {
  12. module: [moduleName],
  13. dependencies: [],
  14. declarations: []
  15. };
  16. if (perFile[basename].module.indexOf(moduleName) === -1) {
  17. perFile[basename].module.push(moduleName);
  18. }
  19. declarations[moduleName] = declarations[moduleName] || [];
  20. let regexp = / (abstract class|function|class|interface|type|const|enum|var) ([\w]*)/g;
  21. var match = regexp.exec(fileContent);
  22. while (match != null) {
  23. if (match[2]) {
  24. // check it is not SIMD:
  25. let simdMatch = / interface (\w*\dx\d{1,2}\w*)/.exec(match[0]);
  26. if (!simdMatch && match[2] !== 'earcut' && match[2] !== 'deviation' && match[2] !== 'flatten') {
  27. declarations[moduleName].push(match[2]);
  28. perFile[basename].declarations.push(match[2]);
  29. }
  30. }
  31. match = regexp.exec(fileContent);
  32. }
  33. } else {
  34. /*let declared = [];
  35. Object.keys(declarations).forEach(name => {
  36. if (name === moduleName) return;
  37. let imports = declarations[name].filter(obj => {
  38. let exists = declared.indexOf(obj) !== -1;
  39. if (!exists) {
  40. declared.push(obj);
  41. }
  42. return !exists;
  43. });
  44. if (imports.length)
  45. importsString += `import {${imports.join(',')}} from 'babylonjs/${name}';
  46. `;
  47. });*/
  48. // find all of the related files for the dependency tree integration
  49. let basenames = Object.keys(perFile).filter(basefilename => {
  50. return perFile[basefilename].module.indexOf(moduleName) !== -1;
  51. });
  52. let classesForImports = {} // key : module name, content - array of objects
  53. basenames.forEach(bname => {
  54. dependencyTree[bname].forEach(dep => {
  55. if (dep.module.indexOf(moduleName) !== -1) return;
  56. let depModule = dep.module.indexOf("core") === -1 ? dep.module[0] : "core";
  57. classesForImports[depModule] = classesForImports[depModule] || [];
  58. if (classesForImports[depModule].indexOf(dep.name) === -1) {
  59. //babylon.imageProcessingPostProcess
  60. classesForImports[depModule].push(dep.name);
  61. }
  62. });
  63. });
  64. Object.keys(classesForImports).forEach(modName => {
  65. importsString += `import {${classesForImports[modName].join(',')}} from 'babylonjs/${modName}';
  66. `;
  67. });
  68. }
  69. if (file.isNull()) {
  70. cb(null, file);
  71. return;
  72. }
  73. if (file.isStream()) {
  74. //streams not supported, no need for now.
  75. return;
  76. }
  77. try {
  78. file.contents = new Buffer(String(file.contents) + '\n' + importsString);
  79. this.push(file);
  80. } catch (err) {
  81. this.emit('error', new gutil.PluginError('gulp-add-module-exports', err, { fileName: file.path }));
  82. }
  83. cb();
  84. });
  85. };