gulp-es6ModuleExports.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. var gutil = require('gulp-util');
  2. var through = require('through2');
  3. var path = require('path');
  4. module.exports = function (moduleName, dependencyTree, generateIndex, perFile, shaders, shaderIncludes) {
  5. return through.obj(function (file, enc, cb) {
  6. let basename = (path.basename(file.path, ".js"));
  7. //console.log("Compiling es6 module: " + moduleName + "/" + basename.replace("babylon.", ""));
  8. var extendsAddition =
  9. `var __extends=this&&this.__extends||function(){var t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,o){t.__proto__=o}||function(t,o){for(var n in o)o.hasOwnProperty(n)&&(t[n]=o[n])};return function(o,n){function r(){this.constructor=o}t(o,n),o.prototype=null===n?Object.create(n):(r.prototype=n.prototype,new r)}}();
  10. `;
  11. var decorateAddition =
  12. 'var __decorate=this&&this.__decorate||function(e,t,r,c){var o,f=arguments.length,n=f<3?t:null===c?c=Object.getOwnPropertyDescriptor(t,r):c;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)n=Reflect.decorate(e,t,r,c);else for(var l=e.length-1;l>=0;l--)(o=e[l])&&(n=(f<3?o(n):f>3?o(t,r,n):o(t,r))||n);return f>3&&n&&Object.defineProperty(t,r,n),n};\n';
  13. let content = file.contents.toString();
  14. if (content.indexOf('__extends') === -1 && !dependencyTree.length) {
  15. extendsAddition = '';
  16. }
  17. if (content.indexOf('__decorate') === -1) {
  18. decorateAddition = '';
  19. }
  20. let dependenciesText = `${extendsAddition}
  21. ${decorateAddition}
  22. `;
  23. //if (moduleName !== 'core') {
  24. dependenciesText += `var globalObject = (typeof global !== 'undefined') ? global : ((typeof window !== 'undefined') ? window : this);
  25. var BABYLON = globalObject["BABYLON"] || {};
  26. `;
  27. /*} else {
  28. dependenciesText += `var BABYLON;
  29. `;
  30. }*/
  31. let exportsText = '';
  32. if (!generateIndex) {
  33. // process parenting!
  34. let parentRegex = /__extends\(([A-Z]\w*), _super\)/g;
  35. var match = parentRegex.exec(content);
  36. while (match != null) {
  37. let extendingClass = match[1];
  38. // find the first row
  39. let extendedMatcher = new RegExp("}\\([BABYLON\\.]*([A-Z]\\w*)\\)\\);\\n\\s*BABYLON." + extendingClass + " = " + extendingClass + ";");
  40. let extendedArray = extendedMatcher.exec(content);
  41. if (extendedArray) {
  42. let firstRowReg = new RegExp("var " + extendingClass + " = .* \\(function");
  43. content = content.replace(firstRowReg, "var " + extendingClass + " = function");
  44. extended = extendedArray[1];
  45. content = content.replace(extendedMatcher, `};
  46. var CLS${extendingClass};
  47. BABYLON.__${extendingClass} = function() {
  48. CLS${extendingClass} = CLS${extendingClass} || ${extendingClass}.call(null, BABYLON.__${extended} && BABYLON.__${extended}() || BABYLON.${extended});
  49. }
  50. Object.defineProperty(BABYLON, "${extendingClass}", {
  51. get: function () {
  52. BABYLON.__${extendingClass}();
  53. return CLS${extendingClass};
  54. },
  55. enumerable: true,
  56. configurable: true
  57. });`);
  58. console.log(extendingClass, extended);
  59. } else {
  60. console.log(extendingClass + " is not exported");
  61. }
  62. match = parentRegex.exec(content);
  63. }
  64. let loadedFiles = [];
  65. dependencyTree[basename].forEach(function (d, idx) {
  66. //if (d.module.indexOf("core") !== -1) return;
  67. let name = d.file.split(".").pop();
  68. if (loadedFiles.indexOf(name) === -1/* && !d.newDec*/) {
  69. let regexp = new RegExp("BABYLON." + d.name);
  70. let match = regexp.exec(content);
  71. if (!match) return;
  72. if (d.main)
  73. dependenciesText += `import {${d.name}} from 'babylonjs/${d.module[0]}/es6/${name}';
  74. `;
  75. else
  76. exportsText += `import {${d.name}} from 'babylonjs/${d.module[0]}/es6/${name}';
  77. `;
  78. loadedFiles.push(name);
  79. }
  80. //dependenciesText += `if(BABYLON !== BABYLON${idx}) __extends(BABYLON, BABYLON${idx});
  81. });
  82. let exported = [];
  83. perFile[basename].declarations.forEach(dec => {
  84. if (exported.indexOf(dec) !== -1) return;
  85. exported.push(dec);
  86. exportsText += `var ${dec} = BABYLON.${dec}; export {${dec}};
  87. `;
  88. });
  89. if (shaders) {
  90. dependenciesText += `import * as Shaders from "babylonjs/${moduleName}/es6/shaders";
  91. if(BABYLON.Effect) Object.keys(Shaders).forEach(function(shaderName) {BABYLON.Effect.ShadersStore[shaderName] = Shaders[shaderName]})
  92. `;
  93. }
  94. if (shaderIncludes) {
  95. dependenciesText += `import * as ShaderIncludes from "babylonjs/${moduleName}/es6/shaderIncludes";
  96. if(BABYLON.Effect) Object.keys(ShaderIncludes).forEach(function(shaderName) {BABYLON.Effect.IncludesShadersStore[shaderName] = ShaderIncludes[shaderName]})
  97. `;
  98. }
  99. //if (moduleName === "core") {
  100. exportsText += `(function() {
  101. //var globalObject = (typeof global !== 'undefined') ? global : ((typeof window !== 'undefined') ? window : this);
  102. globalObject["BABYLON"] = globalObject["BABYLON"] || BABYLON;
  103. })();
  104. `;
  105. //}
  106. } else {
  107. content = '';
  108. let basenames = Object.keys(perFile).filter(basefilename => {
  109. return perFile[basefilename].module.indexOf(moduleName) !== -1;
  110. });
  111. basenames.forEach(bname => {
  112. let name = bname.split(".").pop();
  113. dependenciesText += `export * from "babylonjs/${moduleName}/es6/${name}";
  114. `;
  115. })
  116. }
  117. /*exportsText += `(function() {
  118. globalObject["BABYLON"] = globalObject["BABYLON"] || BABYLON;
  119. })();
  120. `;*/
  121. /*if (dependencies) {
  122. dependencies.forEach(function (d, idx) {
  123. if (d === 'core') return;
  124. dependenciesText += `import * as ${d} from 'babylonjs/${d}/es6';
  125. `;
  126. //if (idx > 0) {
  127. dependenciesText += `__extends(BABYLON, ${d});
  128. `;
  129. //}
  130. });
  131. }
  132. let exportRegex = /BABYLON.([0-9A-Za-z-_]*) = .*;\n/g
  133. var match = exportRegex.exec(content);
  134. let exportsArray = [];
  135. while (match != null) {
  136. if (match[1]) {
  137. exportsArray.push(match[1])
  138. }
  139. match = exportRegex.exec(content);
  140. }
  141. let exportsText = '';
  142. if (moduleName === "core") {
  143. exportsText = `(function() {
  144. var globalObject = (typeof global !== 'undefined') ? global : ((typeof window !== 'undefined') ? window : this);
  145. globalObject["BABYLON"] = BABYLON;
  146. })();
  147. `
  148. }
  149. let exportedItems = '';
  150. exportsArray.forEach((e, idx) => {
  151. if (e.indexOf('.') === -1) {
  152. exportedItems += `${idx ? ',' : ''}${e}`
  153. exportsText += `var ${e} = BABYLON.${e};
  154. `
  155. }
  156. });
  157. exportsText += `
  158. export { ${exportedItems} };`*/
  159. /*if (moduleName === "core") {
  160. exportsText = `(function() {
  161. var globalObject = (typeof global !== 'undefined') ? global : ((typeof window !== 'undefined') ? window : this);
  162. globalObject["BABYLON"] = BABYLON;
  163. })();
  164. `*/
  165. if (file.isNull()) {
  166. cb(null, file);
  167. return;
  168. }
  169. if (file.isStream()) {
  170. //streams not supported, no need for now.
  171. return;
  172. }
  173. try {
  174. file.contents = new Buffer(dependenciesText.concat(new Buffer(String(content).concat(exportsText))));
  175. this.push(file);
  176. } catch (err) {
  177. this.emit('error', new gutil.PluginError('gulp-es6-module-exports', err, { fileName: file.path }));
  178. }
  179. cb();
  180. });
  181. }