gulp-babylonModule.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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() { module.exports = BABYLON; })();
  61. }`
  62. }
  63. else {
  64. exportsText = `(function() {
  65. var EXPORTS = {};`
  66. exportsArray.forEach(e => {
  67. if (e.indexOf('.') === -1)
  68. exportsText += `EXPORTS['${e}'] = BABYLON['${e}'];`
  69. });
  70. exportsText += `
  71. module.exports = EXPORTS;
  72. })();
  73. }`
  74. }
  75. if (file.isNull()) {
  76. cb(null, file);
  77. return;
  78. }
  79. if (file.isStream()) {
  80. //streams not supported, no need for now.
  81. return;
  82. }
  83. try {
  84. file.contents = new Buffer(dependenciesText.concat(new Buffer(String(file.contents).concat(exportsText))));
  85. this.push(file);
  86. } catch (err) {
  87. this.emit('error', new gutil.PluginError('gulp-add-babylon-module', err, { fileName: file.path }));
  88. }
  89. cb();
  90. });
  91. }