gulp-addModuleExports.js 4.3 KB

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