gulp-addES6Exports.js 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 (baseModule, subModule, extendsRoot, externalUsingBabylon) {
  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. let fileContent = file.contents.toString();
  14. function moduleExportAddition(varName) {
  15. let base = subModule ? 'BABYLON' : baseModule;
  16. let sadGlobalPolution = (!subModule) ? `var globalObject = (typeof global !== 'undefined') ? global : ((typeof window !== 'undefined') ? window : this);
  17. globalObject["${base}"] = ${base}${(subModule && !extendsRoot) ? '.' + varName : ''};` : '';
  18. /*if (extendsRoot) {
  19. basicInit = `__extends(root["BABYLON"], factory()); `
  20. }*/
  21. let listOfExports = [];
  22. // find the exported members. es6 exports can NOT be generated dynamically.
  23. let matcher = new RegExp(base + "\\.(\\w*) = (\\w*);", "g");
  24. let match = matcher.exec(fileContent);
  25. while (match != null) {
  26. if (match[1] && match[2] && match[1] === match[2]) {
  27. listOfExports.push(match[1]);
  28. }
  29. match = matcher.exec(fileContent);
  30. }
  31. let enumMatcher = new RegExp(`\\(${base}\\.([A-Za-z0-9].*)= {}\\)`, "g");
  32. let enumMatch = enumMatcher.exec(fileContent);
  33. while (enumMatch != null) {
  34. if (enumMatch[1]) {
  35. listOfExports.push(enumMatch[1]);
  36. }
  37. enumMatch = enumMatcher.exec(fileContent);
  38. }
  39. let exportsText = '';
  40. listOfExports.forEach(cls => {
  41. exportsText += `var ${cls} = ${base}.${cls};`;
  42. });
  43. exportsText += `
  44. export { ${listOfExports.join(",")} };`
  45. return `${sadGlobalPolution}
  46. ${exportsText}
  47. `;
  48. }
  49. var extendsAddition = `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)}}();
  50. `;
  51. 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};\n';
  52. if (file.isNull()) {
  53. cb(null, file);
  54. return;
  55. }
  56. if (file.isStream()) {
  57. //streams not supported, no need for now.
  58. return;
  59. }
  60. try {
  61. if (externalUsingBabylon) {
  62. //file.contents = new Buffer(optionalRequire.concat(String(file.contents)));
  63. file.contents = new Buffer(optionalRequire.concat(new Buffer(String(file.contents).concat(moduleExportAddition(baseModule)))));
  64. } else {
  65. let pretext = subModule ? optionalRequire : '';
  66. file.contents = new Buffer(pretext.concat(decorateAddition).concat(new Buffer(extendsAddition.concat(String(file.contents)).concat(moduleExportAddition(baseModule)))));
  67. }
  68. this.push(file);
  69. } catch (err) {
  70. this.emit('error', new gutil.PluginError('gulp-add-module-exports', err, { fileName: file.path }));
  71. }
  72. cb();
  73. });
  74. };