index.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // By default assume browserify was used to bundle app. These arguments are passed to
  2. // the module by browserify.
  3. var bundleFn = arguments[3];
  4. var sources = arguments[4];
  5. var cache = arguments[5];
  6. var stringify = JSON.stringify;
  7. var webpack = false;
  8. // webpackBootstrap
  9. var webpackBootstrapFn = function(modules) {
  10. // The module cache
  11. var installedModules = {};
  12. // The require function
  13. function __webpack_require__(moduleId) {
  14. // Check if module is in cache
  15. if(installedModules[moduleId]) {
  16. return installedModules[moduleId].exports;
  17. }
  18. // Create a new module (and put it into the cache)
  19. var module = installedModules[moduleId] = {
  20. i: moduleId,
  21. l: false,
  22. exports: {}
  23. };
  24. // Execute the module function
  25. modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
  26. // Flag the module as loaded
  27. module.l = true;
  28. // Return the exports of the module
  29. return module.exports;
  30. }
  31. // expose the modules object (__webpack_modules__)
  32. __webpack_require__.m = modules;
  33. // expose the module cache
  34. __webpack_require__.c = installedModules;
  35. // define getter function for harmony exports
  36. __webpack_require__.d = function(exports, name, getter) {
  37. if(!__webpack_require__.o(exports, name)) {
  38. Object.defineProperty(exports, name, {
  39. configurable: false,
  40. enumerable: true,
  41. get: getter
  42. });
  43. }
  44. };
  45. // getDefaultExport function for compatibility with non-harmony modules
  46. __webpack_require__.n = function(module) {
  47. var getter = module && module.__esModule ?
  48. function getDefault() { return module['default']; } :
  49. function getModuleExports() { return module; };
  50. __webpack_require__.d(getter, 'a', getter);
  51. return getter;
  52. };
  53. // Object.prototype.hasOwnProperty.call
  54. __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
  55. // __webpack_public_path__
  56. __webpack_require__.p = "";
  57. // Load entry module and return exports
  58. return __webpack_require__(__webpack_require__.s = entryModule);
  59. }
  60. if (typeof bundleFn === 'undefined') {
  61. // Assume this was bundled with webpack and not browserify
  62. webpack = true;
  63. bundleFn = webpackBootstrapFn;
  64. sources = __webpack_modules__;
  65. }
  66. var bundleWithBrowserify = function(fn) {
  67. // with browserify we must find the module key ourselves
  68. var cacheKeys = Object.keys(cache);
  69. var fnModuleKey;
  70. for (var i = 0; i < cacheKeys.length; i++) {
  71. var cacheKey = cacheKeys[i];
  72. var cacheExports = cache[cacheKey].exports;
  73. // Using babel as a transpiler to use esmodule, the export will always
  74. // be an object with the default export as a property of it. To ensure
  75. // the existing api and babel esmodule exports are both supported we
  76. // check for both
  77. if (cacheExports === fn || cacheExports && cacheExports.default === fn) {
  78. fnModuleKey = cacheKey;
  79. break;
  80. }
  81. }
  82. // if we couldn't find one, lets make one
  83. if (!fnModuleKey) {
  84. fnModuleKey = Math.floor(Math.pow(16, 8) * Math.random()).toString(16);
  85. var fnModuleCache = {};
  86. for (var i = 0; i < cacheKeys.length; i++) {
  87. var cacheKey = cacheKeys[i];
  88. fnModuleCache[cacheKey] = cacheKey;
  89. }
  90. sources[fnModuleKey] = [
  91. 'function(require,module,exports){' + fn + '(self); }',
  92. fnModuleCache
  93. ];
  94. }
  95. var entryKey = Math.floor(Math.pow(16, 8) * Math.random()).toString(16);
  96. var entryCache = {};
  97. entryCache[fnModuleKey] = fnModuleKey;
  98. sources[entryKey] = [
  99. 'function(require,module,exports){' +
  100. // try to call default if defined to also support babel esmodule exports
  101. 'var f = require(' + stringify(fnModuleKey) + ');' +
  102. '(f.default ? f.default : f)(self);' +
  103. '}',
  104. entryCache
  105. ];
  106. return '(' + bundleFn + ')({'
  107. + Object.keys(sources).map(function(key) {
  108. return stringify(key) + ':['
  109. + sources[key][0] + ','
  110. + stringify(sources[key][1]) + ']';
  111. }).join(',')
  112. + '},{},[' + stringify(entryKey) + '])';
  113. };
  114. var bundleWithWebpack = function(fn, fnModuleId) {
  115. var devMode = typeof fnModuleId === 'string';
  116. var sourceStrings;
  117. if (devMode) {
  118. sourceStrings = {};
  119. } else {
  120. sourceStrings = [];
  121. }
  122. Object.keys(sources).forEach(function(sKey) {
  123. if (!sources[sKey]) {
  124. return;
  125. }
  126. sourceStrings[sKey] = sources[sKey].toString();
  127. });
  128. var fnModuleExports = __webpack_require__(fnModuleId);
  129. // Using babel as a transpiler to use esmodule, the export will always
  130. // be an object with the default export as a property of it. To ensure
  131. // the existing api and babel esmodule exports are both supported we
  132. // check for both
  133. if (!(fnModuleExports && (fnModuleExports === fn || fnModuleExports.default === fn))) {
  134. var fnSourceString = sourceStrings[fnModuleId];
  135. sourceStrings[fnModuleId] = fnSourceString.substring(0, fnSourceString.length - 1) +
  136. '\n' + fn.name + '();\n}';
  137. }
  138. var modulesString;
  139. if (devMode) {
  140. // must escape quotes to support webpack loader options
  141. fnModuleId = stringify(fnModuleId);
  142. // dev mode in webpack4, modules are passed as an object
  143. var mappedSourceStrings = Object.keys(sourceStrings).map(function(sKey) {
  144. return stringify(sKey) + ':' + sourceStrings[sKey];
  145. });
  146. modulesString = '{' + mappedSourceStrings.join(',') + '}';
  147. } else {
  148. modulesString = '[' + sourceStrings.join(',') + ']';
  149. }
  150. return 'var fn = (' + bundleFn.toString().replace('entryModule', fnModuleId) + ')('
  151. + modulesString
  152. + ');\n'
  153. // not a function when calling a function from the current scope
  154. + '(typeof fn === "function") && fn(self);';
  155. };
  156. module.exports = function webwackify(fn, fnModuleId) {
  157. var src;
  158. if (webpack) {
  159. src = bundleWithWebpack(fn, fnModuleId);
  160. } else {
  161. src = bundleWithBrowserify(fn);
  162. }
  163. var blob = new Blob([src], { type: 'text/javascript' });
  164. var URL = window.URL || window.webkitURL || window.mozURL || window.msURL;
  165. var workerUrl = URL.createObjectURL(blob);
  166. var worker = new Worker(workerUrl);
  167. worker.objectURL = workerUrl;
  168. return worker;
  169. };