index.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*-----------------------------------------------------------------------------
  2. | Copyright (c) Jupyter Development Team.
  3. | Distributed under the terms of the Modified BSD License.
  4. |----------------------------------------------------------------------------*/
  5. require('es6-promise/auto'); // polyfill Promise on IE
  6. import {
  7. PageConfig
  8. } from '@jupyterlab/coreutils';
  9. // eslint-disable-next-line no-undef
  10. __webpack_public_path__ = PageConfig.getOption('fullStaticUrl') + '/';
  11. // This must be after the public path is set.
  12. // This cannot be extracted because the public path is dynamic.
  13. require('./imports.css');
  14. /**
  15. * The main entry point for the application.
  16. */
  17. function main() {
  18. var JupyterLab = require('@jupyterlab/application').JupyterLab;
  19. var disabled = [];
  20. var deferred = [];
  21. var ignorePlugins = [];
  22. var register = [];
  23. // Handle the registered mime extensions.
  24. var mimeExtensions = [];
  25. var extension;
  26. var extMod;
  27. {{#each jupyterlab_mime_extensions}}
  28. try {
  29. if (PageConfig.Extension.isDeferred('{{key}}')) {
  30. deferred.push('{{key}}');
  31. ignorePlugins.push('{{key}}');
  32. }
  33. if (PageConfig.Extension.isDisabled('{{@key}}')) {
  34. disabled.push('{{@key}}');
  35. } else {
  36. extMod = require('{{@key}}/{{this}}');
  37. extension = extMod.default;
  38. // Handle CommonJS exports.
  39. if (!extMod.hasOwnProperty('__esModule')) {
  40. extension = extMod;
  41. }
  42. var list = Array.isArray(extension) ? extension : [extension];
  43. list.forEach(function(plugin) {
  44. if (PageConfig.Extension.isDeferred(plugin.id)) {
  45. deferred.push(plugin.id);
  46. ignorePlugins.push(plugin.id);
  47. }
  48. if (PageConfig.Extension.isDisabled(plugin.id)) {
  49. disabled.push(plugin.id);
  50. return;
  51. }
  52. mimeExtensions.push(plugin);
  53. });
  54. }
  55. } catch (e) {
  56. console.error(e);
  57. }
  58. {{/each}}
  59. // Handled the registered standard extensions.
  60. {{#each jupyterlab_extensions}}
  61. try {
  62. if (PageConfig.Extension.isDeferred('{{key}}')) {
  63. deferred.push('{{key}}');
  64. ignorePlugins.push('{{key}}');
  65. }
  66. if (PageConfig.Extension.isDisabled('{{@key}}')) {
  67. disabled.push('{{@key}}');
  68. } else {
  69. extMod = require('{{@key}}/{{this}}');
  70. extension = extMod.default;
  71. // Handle CommonJS exports.
  72. if (!extMod.hasOwnProperty('__esModule')) {
  73. extension = extMod;
  74. }
  75. var list = Array.isArray(extension) ? extension : [extension];
  76. list.forEach(function(plugin) {
  77. if (PageConfig.Extension.isDeferred(plugin.id)) {
  78. deferred.push(plugin.id);
  79. ignorePlugins.push(plugin.id);
  80. }
  81. if (PageConfig.Extension.isDisabled(plugin.id)) {
  82. disabled.push(plugin.id);
  83. return;
  84. }
  85. register.push(plugin);
  86. });
  87. }
  88. } catch (e) {
  89. console.error(e);
  90. }
  91. {{/each}}
  92. var lab = new JupyterLab({
  93. mimeExtensions: mimeExtensions,
  94. disabled: {
  95. matches: disabled,
  96. patterns: PageConfig.Extension.disabled
  97. .map(function (val) { return val.raw; })
  98. },
  99. deferred: {
  100. matches: deferred,
  101. patterns: PageConfig.Extension.deferred
  102. .map(function (val) { return val.raw; })
  103. },
  104. });
  105. register.forEach(function(item) { lab.registerPluginModule(item); });
  106. lab.start({ ignorePlugins: ignorePlugins });
  107. // Expose global lab instance when in dev mode.
  108. if ((PageConfig.getOption('devMode') || '').toLowerCase() === 'true') {
  109. window.lab = lab;
  110. }
  111. // Handle a browser test.
  112. var browserTest = PageConfig.getOption('browserTest');
  113. if (browserTest.toLowerCase() === 'true') {
  114. var el = document.createElement('div');
  115. el.id = 'browserTest';
  116. document.body.appendChild(el);
  117. el.textContent = '[]';
  118. el.style.display = 'none';
  119. var errors = [];
  120. var reported = false;
  121. var timeout = 25000;
  122. var report = function() {
  123. if (reported) {
  124. return;
  125. }
  126. reported = true;
  127. el.className = 'completed';
  128. }
  129. window.onerror = function(msg, url, line, col, error) {
  130. errors.push(String(error));
  131. el.textContent = JSON.stringify(errors)
  132. };
  133. console.error = function(message) {
  134. errors.push(String(message));
  135. el.textContent = JSON.stringify(errors)
  136. };
  137. lab.restored
  138. .then(function() { report(errors); })
  139. .catch(function(reason) { report([`RestoreError: ${reason.message}`]); });
  140. // Handle failures to restore after the timeout has elapsed.
  141. window.setTimeout(function() { report(errors); }, timeout);
  142. }
  143. }
  144. window.addEventListener('load', main);