gulp-addModuleExports.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. var gutil = require('gulp-util');
  2. var through = require('through2');
  3. module.exports = function (varName) {
  4. return through.obj(function (file, enc, cb) {
  5. var moduleExportsAddition = `(function universalModuleDefinition(root, factory) {
  6. if(typeof exports === 'object' && typeof module === 'object')
  7. module.exports = factory();
  8. else if(typeof define === 'function' && define.amd)
  9. define([], factory);
  10. else if(typeof exports === 'object')
  11. exports["BABYLON"] = factory();
  12. else
  13. root["BABYLON"] = factory();
  14. })(this, function() {
  15. return BABYLON;
  16. });
  17. `;
  18. var extendsAddition =
  19. `var __extends = (this && this.__extends) || (function () {
  20. var extendStatics = Object.setPrototypeOf ||
  21. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  22. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  23. return function (d, b) {
  24. extendStatics(d, b);
  25. function __() { this.constructor = d; }
  26. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  27. };
  28. })();
  29. `;
  30. var decorateAddition =
  31. 'var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {\n' +
  32. 'var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;\n' +
  33. 'if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);\n' +
  34. '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' +
  35. 'return c > 3 && r && Object.defineProperty(target, key, r), r;\n' +
  36. '};\n';
  37. if (file.isNull()) {
  38. cb(null, file);
  39. return;
  40. }
  41. if (file.isStream()) {
  42. //streams not supported, no need for now.
  43. return;
  44. }
  45. try {
  46. file.contents = new Buffer(decorateAddition.concat(new Buffer(extendsAddition.concat(String(file.contents)).concat(moduleExportsAddition))));
  47. this.push(file);
  48. } catch (err) {
  49. this.emit('error', new gutil.PluginError('gulp-add-module-exports', err, { fileName: file.path }));
  50. }
  51. cb();
  52. });
  53. };