gulp-es6ModuleExports.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. var gutil = require('gulp-util');
  2. var through = require('through2');
  3. module.exports = function (moduleName, dependencies, es6) {
  4. return through.obj(function (file, enc, cb) {
  5. console.log("Compiling es6 module: " + moduleName);
  6. var extendsAddition =
  7. `var __extends = (this && this.__extends) || (function () {
  8. var extendStatics = Object.setPrototypeOf ||
  9. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  10. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  11. return function (d, b) {
  12. extendStatics(d, b);
  13. function __() { this.constructor = d; }
  14. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  15. };
  16. })();
  17. `;
  18. var decorateAddition =
  19. 'var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {\n' +
  20. 'var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;\n' +
  21. 'if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);\n' +
  22. 'else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;\n' +
  23. 'return c > 3 && r && Object.defineProperty(target, key, r), r;\n' +
  24. '};\n';
  25. let content = file.contents.toString();
  26. if (content.indexOf('__extends') === -1 && !dependencies.length) {
  27. extendsAddition = '';
  28. }
  29. if (content.indexOf('__decorate') === -1) {
  30. decorateAddition = '';
  31. }
  32. let dependenciesText = `${extendsAddition}
  33. ${decorateAddition}
  34. `;
  35. if (moduleName !== 'core') {
  36. dependenciesText += `var globalObject = (typeof global !== 'undefined') ? global : ((typeof window !== 'undefined') ? window : this);
  37. var BABYLON = globalObject["BABYLON"] || {};
  38. `;
  39. } else {
  40. dependenciesText += `var BABYLON;
  41. `;
  42. }
  43. if (dependencies) {
  44. /*if (dependencies.length > 1) {
  45. dependenciesText += 'function nse(ns1, ns2) { Object.keys(ns2).forEach(function(c) {if(!ns1[c]) {ns1[c] = ns2[c]}}) };\n';
  46. }*/
  47. dependencies.forEach(function (d, idx) {
  48. if (d === 'core') return;
  49. dependenciesText += `import * as ${d} from 'babylonjs/${d}/es6';
  50. `;
  51. //if (idx > 0) {
  52. dependenciesText += `__extends(BABYLON, ${d});
  53. `;
  54. //}
  55. });
  56. }
  57. let exportRegex = /BABYLON.([0-9A-Za-z-_]*) = .*;\n/g
  58. var match = exportRegex.exec(content);
  59. let exportsArray = [];
  60. while (match != null) {
  61. if (match[1]) {
  62. exportsArray.push(match[1])
  63. }
  64. match = exportRegex.exec(content);
  65. }
  66. let exportsText = '';
  67. if (moduleName === "core") {
  68. exportsText = `(function() {
  69. var globalObject = (typeof global !== 'undefined') ? global : ((typeof window !== 'undefined') ? window : this);
  70. globalObject["BABYLON"] = BABYLON;
  71. })();;
  72. `
  73. }
  74. let exportedItems = '';
  75. exportsArray.forEach((e, idx) => {
  76. if (e.indexOf('.') === -1) {
  77. exportedItems += `${idx ? ',' : ''}${e}`
  78. exportsText += `var ${e} = BABYLON.${e};
  79. `
  80. }
  81. });
  82. exportsText += `
  83. export { ${exportedItems} };`
  84. if (file.isNull()) {
  85. cb(null, file);
  86. return;
  87. }
  88. if (file.isStream()) {
  89. //streams not supported, no need for now.
  90. return;
  91. }
  92. try {
  93. file.contents = new Buffer(dependenciesText.concat(new Buffer(String(file.contents).concat(exportsText))));
  94. this.push(file);
  95. } catch (err) {
  96. this.emit('error', new gutil.PluginError('gulp-es6-module-exports', err, { fileName: file.path }));
  97. }
  98. cb();
  99. });
  100. }