index.js 5.1 KB

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