global.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. (function () {
  2. /*
  3. * SystemJS global script loading support
  4. * Extra for the s.js build only
  5. * (Included by default in system.js build)
  6. */
  7. (function (global) {
  8. var systemJSPrototype = global.System.constructor.prototype;
  9. // safari unpredictably lists some new globals first or second in object order
  10. var firstGlobalProp, secondGlobalProp, lastGlobalProp;
  11. function getGlobalProp (useFirstGlobalProp) {
  12. var cnt = 0;
  13. var foundLastProp, result;
  14. for (var p in global) {
  15. // do not check frames cause it could be removed during import
  16. if (shouldSkipProperty(p))
  17. continue;
  18. if (cnt === 0 && p !== firstGlobalProp || cnt === 1 && p !== secondGlobalProp)
  19. return p;
  20. if (foundLastProp) {
  21. lastGlobalProp = p;
  22. result = useFirstGlobalProp && result || p;
  23. }
  24. else {
  25. foundLastProp = p === lastGlobalProp;
  26. }
  27. cnt++;
  28. }
  29. return result;
  30. }
  31. function noteGlobalProps () {
  32. // alternatively Object.keys(global).pop()
  33. // but this may be faster (pending benchmarks)
  34. firstGlobalProp = secondGlobalProp = undefined;
  35. for (var p in global) {
  36. // do not check frames cause it could be removed during import
  37. if (shouldSkipProperty(p))
  38. continue;
  39. if (!firstGlobalProp)
  40. firstGlobalProp = p;
  41. else if (!secondGlobalProp)
  42. secondGlobalProp = p;
  43. lastGlobalProp = p;
  44. }
  45. return lastGlobalProp;
  46. }
  47. var impt = systemJSPrototype.import;
  48. systemJSPrototype.import = function (id, parentUrl, meta) {
  49. noteGlobalProps();
  50. return impt.call(this, id, parentUrl, meta);
  51. };
  52. var emptyInstantiation = [[], function () { return {} }];
  53. var getRegister = systemJSPrototype.getRegister;
  54. systemJSPrototype.getRegister = function () {
  55. var lastRegister = getRegister.call(this);
  56. if (lastRegister)
  57. return lastRegister;
  58. // no registration -> attempt a global detection as difference from snapshot
  59. // when multiple globals, we take the global value to be the last defined new global object property
  60. // for performance, this will not support multi-version / global collisions as previous SystemJS versions did
  61. // note in Edge, deleting and re-adding a global does not change its ordering
  62. var globalProp = getGlobalProp(this.firstGlobalProp);
  63. if (!globalProp)
  64. return emptyInstantiation;
  65. var globalExport;
  66. try {
  67. globalExport = global[globalProp];
  68. }
  69. catch (e) {
  70. return emptyInstantiation;
  71. }
  72. return [[], function (_export) {
  73. return {
  74. execute: function () {
  75. _export(globalExport);
  76. _export({ default: globalExport, __useDefault: true });
  77. }
  78. };
  79. }];
  80. };
  81. var isIE11 = typeof navigator !== 'undefined' && navigator.userAgent.indexOf('Trident') !== -1;
  82. function shouldSkipProperty(p) {
  83. return !global.hasOwnProperty(p)
  84. || !isNaN(p) && p < global.length
  85. || isIE11 && global[p] && typeof window !== 'undefined' && global[p].parent === window;
  86. }
  87. })(typeof self !== 'undefined' ? self : global);
  88. })();