gulp-es6ModuleExports.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 < 2) {
  27. extendsAddition = '';
  28. }
  29. if (content.indexOf('__decorate') === -1) {
  30. decorateAddition = '';
  31. }
  32. let dependenciesText = `${extendsAddition}
  33. ${decorateAddition}
  34. `;
  35. if (dependencies) {
  36. /*if (dependencies.length > 1) {
  37. dependenciesText += 'function nse(ns1, ns2) { Object.keys(ns2).forEach(function(c) {if(!ns1[c]) {ns1[c] = ns2[c]}}) };\n';
  38. }*/
  39. dependencies.forEach(function (d, idx) {
  40. let name = d === 'core' ? 'BABYLON' : d;
  41. dependenciesText += `import * as ${name} from 'babylonjs/es6/${d}';
  42. `;
  43. if (idx > 0) {
  44. dependenciesText += `__extends(BABYLON, ${d});
  45. `;
  46. }
  47. });
  48. }
  49. let exportRegex = /BABYLON.([0-9A-Za-z-_]*) = .*;\n/g
  50. var match = exportRegex.exec(content);
  51. let exportsArray = [];
  52. while (match != null) {
  53. if (match[1]) {
  54. exportsArray.push(match[1])
  55. }
  56. match = exportRegex.exec(content);
  57. }
  58. let exportsText = '';
  59. if (moduleName === "core") {
  60. exportsText = `(function() {
  61. var globalObject = (typeof global !== 'undefined') ? global : ((typeof window !== 'undefined') ? window : this);
  62. globalObject["BABYLON"] = BABYLON;
  63. })();;
  64. `
  65. }
  66. let exportedItems = '';
  67. exportsArray.forEach((e, idx) => {
  68. if (e.indexOf('.') === -1) {
  69. exportedItems += `${idx ? ',' : ''}${e}`
  70. exportsText += `var ${e} = BABYLON.${e};
  71. `
  72. }
  73. });
  74. exportsText += `
  75. export { ${exportedItems} };`
  76. if (file.isNull()) {
  77. cb(null, file);
  78. return;
  79. }
  80. if (file.isStream()) {
  81. //streams not supported, no need for now.
  82. return;
  83. }
  84. try {
  85. file.contents = new Buffer(dependenciesText.concat(new Buffer(String(file.contents).concat(exportsText))));
  86. this.push(file);
  87. } catch (err) {
  88. this.emit('error', new gutil.PluginError('gulp-es6-module-exports', err, { fileName: file.path }));
  89. }
  90. cb();
  91. });
  92. }