index.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. var plugins = [];
  28. {{#each jupyterlab_mime_extensions}}
  29. try {
  30. extMod = require('{{@key}}/{{this}}');
  31. extension = extMod.default;
  32. // Handle CommonJS exports.
  33. if (!extMod.hasOwnProperty('__esModule')) {
  34. extension = extMod;
  35. }
  36. plugins = Array.isArray(extension) ? extension : [extension];
  37. plugins.forEach(function(plugin) {
  38. if (PageConfig.Extension.isDeferred(plugin.id)) {
  39. deferred.push(plugin.id);
  40. ignorePlugins.push(plugin.id);
  41. }
  42. if (PageConfig.Extension.isDisabled(plugin.id)) {
  43. disabled.push(plugin.id);
  44. return;
  45. }
  46. mimeExtensions.push(plugin);
  47. });
  48. } catch (e) {
  49. console.error(e);
  50. }
  51. {{/each}}
  52. // Handled the registered standard extensions.
  53. {{#each jupyterlab_extensions}}
  54. try {
  55. extMod = require('{{@key}}/{{this}}');
  56. extension = extMod.default;
  57. // Handle CommonJS exports.
  58. if (!extMod.hasOwnProperty('__esModule')) {
  59. extension = extMod;
  60. }
  61. plugins = Array.isArray(extension) ? extension : [extension];
  62. plugins.forEach(function(plugin) {
  63. if (PageConfig.Extension.isDeferred(plugin.id)) {
  64. deferred.push(plugin.id);
  65. ignorePlugins.push(plugin.id);
  66. }
  67. if (PageConfig.Extension.isDisabled(plugin.id)) {
  68. disabled.push(plugin.id);
  69. return;
  70. }
  71. register.push(plugin);
  72. });
  73. } catch (e) {
  74. console.error(e);
  75. }
  76. {{/each}}
  77. var lab = new JupyterLab({
  78. mimeExtensions: mimeExtensions,
  79. disabled: {
  80. matches: disabled,
  81. patterns: PageConfig.Extension.disabled
  82. .map(function (val) { return val.raw; })
  83. },
  84. deferred: {
  85. matches: deferred,
  86. patterns: PageConfig.Extension.deferred
  87. .map(function (val) { return val.raw; })
  88. },
  89. });
  90. register.forEach(function(item) { lab.registerPluginModule(item); });
  91. lab.start({ ignorePlugins: ignorePlugins });
  92. // Expose global app instance when in dev mode or when toggled explicitly.
  93. var exposeAppInBrowser = (PageConfig.getOption('exposeAppInBrowser') || '').toLowerCase() === 'true';
  94. var devMode = (PageConfig.getOption('devMode') || '').toLowerCase() === 'true';
  95. if (exposeAppInBrowser || devMode) {
  96. window.jupyterlab = lab;
  97. }
  98. // Handle a browser test.
  99. var browserTest = PageConfig.getOption('browserTest');
  100. if (browserTest.toLowerCase() === 'true') {
  101. var el = document.createElement('div');
  102. el.id = 'browserTest';
  103. document.body.appendChild(el);
  104. el.textContent = '[]';
  105. el.style.display = 'none';
  106. var errors = [];
  107. var reported = false;
  108. var timeout = 25000;
  109. var report = function() {
  110. if (reported) {
  111. return;
  112. }
  113. reported = true;
  114. el.className = 'completed';
  115. }
  116. window.onerror = function(msg, url, line, col, error) {
  117. errors.push(String(error));
  118. el.textContent = JSON.stringify(errors)
  119. };
  120. console.error = function(message) {
  121. errors.push(String(message));
  122. el.textContent = JSON.stringify(errors)
  123. };
  124. lab.restored
  125. .then(function() { report(errors); })
  126. .catch(function(reason) { report([`RestoreError: ${reason.message}`]); });
  127. // Handle failures to restore after the timeout has elapsed.
  128. window.setTimeout(function() { report(errors); }, timeout);
  129. }
  130. }
  131. window.addEventListener('load', main);