gulp-addModuleExports.js 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. var gutil = require('gulp-util');
  2. var through = require('through2');
  3. /**
  4. * The parameters for this function has grown during development.
  5. * Eventually, this function will need to be reorganized.
  6. */
  7. module.exports = function (varName, subModule, extendsRoot, requireOnly) {
  8. return through.obj(function (file, enc, cb) {
  9. var optionalRequire = `var globalObject = (typeof global !== 'undefined') ? global : ((typeof window !== 'undefined') ? window : this);
  10. var babylonDependency = (globalObject && globalObject.BABYLON) || BABYLON || (typeof require !== 'undefined' && require("babylonjs"));
  11. var BABYLON = babylonDependency;
  12. `;
  13. function moduleExportAddition(varName) {
  14. let base = subModule ? 'BABYLON' : varName;
  15. let basicInit = `root["${base}"]${(subModule && !extendsRoot) ? '["' + varName + '"]' : ''} = f;`;
  16. let sadGlobalPolution = (!subModule) ? `var globalObject = (typeof global !== 'undefined') ? global : ((typeof window !== 'undefined') ? window : this);
  17. globalObject["${base}"] = f;` : '';
  18. /*if (extendsRoot) {
  19. basicInit = `__extends(root["BABYLON"], factory()); `
  20. }*/
  21. return `\n\n(function universalModuleDefinition(root, factory) {
  22. if (root && root["${base}"]) {
  23. return;
  24. }
  25. var f = factory();
  26. ${sadGlobalPolution}
  27. if(typeof exports === 'object' && typeof module === 'object')
  28. module.exports = f;
  29. else if(typeof define === 'function' && define.amd)
  30. define([], factory);
  31. else if(typeof exports === 'object')
  32. exports["${varName}"] = f;
  33. else {
  34. ${basicInit}
  35. }
  36. })(this, function() {
  37. return ${base}${(subModule && !extendsRoot) ? '.' + varName : ''};
  38. });
  39. `;
  40. }
  41. var extendsAddition =
  42. `var __extends = (this && this.__extends) || (function () {
  43. var extendStatics = Object.setPrototypeOf ||
  44. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  45. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  46. return function (d, b) {
  47. extendStatics(d, b);
  48. function __() { this.constructor = d; }
  49. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  50. };
  51. })();
  52. `;
  53. var decorateAddition =
  54. 'var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {\n' +
  55. 'var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;\n' +
  56. 'if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);\n' +
  57. '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' +
  58. 'return c > 3 && r && Object.defineProperty(target, key, r), r;\n' +
  59. '};\n';
  60. if (file.isNull()) {
  61. cb(null, file);
  62. return;
  63. }
  64. if (file.isStream()) {
  65. //streams not supported, no need for now.
  66. return;
  67. }
  68. try {
  69. if (requireOnly) {
  70. file.contents = new Buffer(optionalRequire.concat(String(file.contents)));
  71. } else {
  72. let pretext = subModule ? optionalRequire : '';
  73. file.contents = new Buffer(pretext.concat(decorateAddition).concat(new Buffer(extendsAddition.concat(String(file.contents)).concat(moduleExportAddition(varName)))));
  74. }
  75. this.push(file);
  76. } catch (err) {
  77. this.emit('error', new gutil.PluginError('gulp-add-module-exports', err, { fileName: file.path }));
  78. }
  79. cb();
  80. });
  81. };