index.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. __webpack_public_path__ = PageConfig.getOption('publicUrl');
  10. // This needs to come after __webpack_public_path__ is set.
  11. require('font-awesome/css/font-awesome.min.css');
  12. /**
  13. * The main entry point for the application.
  14. */
  15. function main() {
  16. var JupyterLab = require('@jupyterlab/application').JupyterLab;
  17. // Get the disabled extensions.
  18. var disabled = { patterns: [], matches: [] };
  19. var disabledExtensions = [];
  20. try {
  21. var tempDisabled = PageConfig.getOption('disabledExtensions');
  22. if (tempDisabled) {
  23. disabledExtensions = JSON.parse(tempDisabled).map(function(pattern) {
  24. disabled.patterns.push(pattern);
  25. return { raw: pattern, rule: new RegExp(pattern) };
  26. });
  27. }
  28. } catch (error) {
  29. console.warn('Unable to parse disabled extensions.', error);
  30. }
  31. // Get the deferred extensions.
  32. var deferred = { patterns: [], matches: [] };
  33. var deferredExtensions = [];
  34. var ignorePlugins = [];
  35. try {
  36. var tempDeferred = PageConfig.getOption('deferredExtensions');
  37. if (tempDeferred) {
  38. deferredExtensions = JSON.parse(tempDeferred).map(function(pattern) {
  39. deferred.patterns.push(pattern);
  40. return { raw: pattern, rule: new RegExp(pattern) };
  41. });
  42. }
  43. } catch (error) {
  44. console.warn('Unable to parse deferred extensions.', error);
  45. }
  46. function isDeferred(value) {
  47. return deferredExtensions.some(function(pattern) {
  48. return pattern.raw === value || pattern.rule.test(value);
  49. });
  50. }
  51. function isDisabled(value) {
  52. return disabledExtensions.some(function(pattern) {
  53. return pattern.raw === value || pattern.rule.test(value);
  54. });
  55. }
  56. var register = [];
  57. // Handle the registered mime extensions.
  58. var mimeExtensions = [];
  59. {{#each jupyterlab_mime_extensions}}
  60. try {
  61. if (isDeferred('{{key}}')) {
  62. deferred.matches.push('{{key}}');
  63. ignorePlugins.push('{{key}}');
  64. }
  65. if (isDisabled('{{@key}}')) {
  66. disabled.matches.push('{{@key}}');
  67. } else {
  68. var module = require('{{@key}}/{{this}}');
  69. var extension = module.default;
  70. // Handle CommonJS exports.
  71. if (!module.hasOwnProperty('__esModule')) {
  72. extension = module;
  73. }
  74. if (Array.isArray(extension)) {
  75. extension.forEach(function(plugin) {
  76. if (isDeferred(plugin.id)) {
  77. deferred.matches.push(plugin.id);
  78. ignorePlugins.push(plugin.id);
  79. }
  80. if (isDisabled(plugin.id)) {
  81. disabled.matches.push(plugin.id);
  82. return;
  83. }
  84. mimeExtensions.push(plugin);
  85. });
  86. } else {
  87. mimeExtensions.push(extension);
  88. }
  89. }
  90. } catch (e) {
  91. console.error(e);
  92. }
  93. {{/each}}
  94. // Handled the registered standard extensions.
  95. {{#each jupyterlab_extensions}}
  96. try {
  97. if (isDeferred('{{key}}')) {
  98. deferred.matches.push('{{key}}');
  99. ignorePlugins.push('{{key}}');
  100. }
  101. if (isDisabled('{{@key}}')) {
  102. disabled.matches.push('{{@key}}');
  103. } else {
  104. module = require('{{@key}}/{{this}}');
  105. extension = module.default;
  106. // Handle CommonJS exports.
  107. if (!module.hasOwnProperty('__esModule')) {
  108. extension = module;
  109. }
  110. if (Array.isArray(extension)) {
  111. extension.forEach(function(plugin) {
  112. if (isDeferred(plugin.id)) {
  113. deferred.matches.push(plugin.id);
  114. ignorePlugins.push(plugin.id);
  115. }
  116. if (isDisabled(plugin.id)) {
  117. disabled.matches.push(plugin.id);
  118. return;
  119. }
  120. register.push(plugin);
  121. });
  122. } else {
  123. register.push(extension);
  124. }
  125. }
  126. } catch (e) {
  127. console.error(e);
  128. }
  129. {{/each}}
  130. var lab = new JupyterLab({
  131. mimeExtensions: mimeExtensions,
  132. disabled: disabled,
  133. deferred: deferred
  134. });
  135. register.forEach(function(item) { lab.registerPluginModule(item); });
  136. lab.start({ ignorePlugins: ignorePlugins });
  137. // Expose global lab instance when in dev mode.
  138. if ((PageConfig.getOption('devMode') || '').toLowerCase() === 'true') {
  139. window.lab = lab;
  140. }
  141. // Handle a selenium test.
  142. var seleniumTest = PageConfig.getOption('seleniumTest');
  143. if (seleniumTest.toLowerCase() === 'true') {
  144. var caught_errors = [];
  145. window.onerror = function(msg, url, line, col, error) {
  146. caught_errors.push(String(error));
  147. };
  148. console.error = function(message) {
  149. caught_errors.push(String(message));
  150. };
  151. lab.restored.then(function() {
  152. var el = document.createElement('div');
  153. el.id = 'seleniumResult';
  154. el.textContent = JSON.stringify(caught_errors);
  155. document.body.appendChild(el);
  156. });
  157. }
  158. }
  159. window.addEventListener('load', main);