gulp-addModuleExports.js 4.6 KB

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