gulp-babylonModule.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. var gutil = require('gulp-util');
  2. var through = require('through2');
  3. module.exports = function (moduleName, dependencies) {
  4. return through.obj(function (file, enc, cb) {
  5. console.log("Compiling 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. if(typeof require !== 'undefined'){
  35. `;
  36. if (dependencies) {
  37. /*if (dependencies.length > 1) {
  38. dependenciesText += 'function nse(ns1, ns2) { Object.keys(ns2).forEach(function(c) {if(!ns1[c]) {ns1[c] = ns2[c]}}) };\n';
  39. }*/
  40. dependencies.forEach(function (d, idx) {
  41. dependenciesText += `var BABYLON${idx === 0 ? '' : '' + idx} = require('babylonjs/${d}');
  42. `;
  43. if (idx > 0) {
  44. dependenciesText += `__extends(BABYLON, BABYLON${idx});
  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. module.exports = BABYLON;
  64. })();
  65. }`
  66. }
  67. else {
  68. exportsText = `(function() {
  69. var EXPORTS = {};`
  70. exportsArray.forEach(e => {
  71. if (e.indexOf('.') === -1)
  72. exportsText += `EXPORTS['${e}'] = BABYLON['${e}'];`
  73. });
  74. exportsText += `
  75. module.exports = EXPORTS;
  76. })();
  77. }`
  78. }
  79. if (file.isNull()) {
  80. cb(null, file);
  81. return;
  82. }
  83. if (file.isStream()) {
  84. //streams not supported, no need for now.
  85. return;
  86. }
  87. try {
  88. file.contents = new Buffer(dependenciesText.concat(new Buffer(String(file.contents).concat(exportsText))));
  89. this.push(file);
  90. } catch (err) {
  91. this.emit('error', new gutil.PluginError('gulp-add-babylon-module', err, { fileName: file.path }));
  92. }
  93. cb();
  94. });
  95. }