gulp-addES6Exports.js 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 exportsText = '';
  32. listOfExports.forEach(cls => {
  33. exportsText += `var ${cls} = ${base}.${cls};`;
  34. });
  35. exportsText += `
  36. export { ${listOfExports.join(",")} };`
  37. return `${sadGlobalPolution}
  38. ${exportsText}
  39. `;
  40. }
  41. 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)}}();
  42. `;
  43. 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';
  44. if (file.isNull()) {
  45. cb(null, file);
  46. return;
  47. }
  48. if (file.isStream()) {
  49. //streams not supported, no need for now.
  50. return;
  51. }
  52. try {
  53. if (externalUsingBabylon) {
  54. //file.contents = new Buffer(optionalRequire.concat(String(file.contents)));
  55. file.contents = new Buffer(optionalRequire.concat(new Buffer(String(file.contents).concat(moduleExportAddition(baseModule)))));
  56. } else {
  57. let pretext = subModule ? optionalRequire : '';
  58. file.contents = new Buffer(pretext.concat(decorateAddition).concat(new Buffer(extendsAddition.concat(String(file.contents)).concat(moduleExportAddition(baseModule)))));
  59. }
  60. this.push(file);
  61. } catch (err) {
  62. this.emit('error', new gutil.PluginError('gulp-add-module-exports', err, { fileName: file.path }));
  63. }
  64. cb();
  65. });
  66. };