index.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { PageConfig } from '@jupyterlab/coreutils';
  4. // This must be after the public path is set.
  5. // This cannot be extracted because the public path is dynamic.
  6. require('./imports.css');
  7. function loadScript(url) {
  8. return new Promise((resolve, reject) => {
  9. const newScript = document.createElement('script');
  10. newScript.onerror = reject;
  11. newScript.onload = resolve;
  12. newScript.async = true;
  13. document.head.appendChild(newScript);
  14. newScript.src = url;
  15. });
  16. }
  17. async function loadComponent(url, scope, module) {
  18. await loadScript(url);
  19. // From MIT-licensed https://github.com/module-federation/module-federation-examples/blob/af043acd6be1718ee195b2511adf6011fba4233c/advanced-api/dynamic-remotes/app1/src/App.js#L6-L12
  20. await __webpack_init_sharing__('default');
  21. const container = window._JUPYTERLAB[scope];
  22. // Initialize the container, it may provide shared modules and may need ours
  23. await container.init(__webpack_share_scopes__.default);
  24. const factory = await window._JUPYTERLAB[scope].get(module);
  25. const Module = factory();
  26. return Module;
  27. }
  28. /**
  29. * The main entry point for the application.
  30. */
  31. async function main() {
  32. var JupyterLab = require('@jupyterlab/application').JupyterLab;
  33. var disabled = [];
  34. var deferred = [];
  35. var ignorePlugins = [];
  36. var register = [];
  37. // This is all the data needed to load and activate plugins. This should be
  38. // gathered by the server and put onto the initial page template.
  39. const extension_data = JSON.parse(
  40. PageConfig.getOption('dynamic_extensions')
  41. );
  42. const mime_extension_data = JSON.parse(
  43. PageConfig.getOption('dynamic_mime_extensions')
  44. );
  45. // Get dynamic plugins
  46. // TODO: deconflict these with builtins?
  47. const dynamicPromises = extension_data.map(data =>
  48. loadComponent(
  49. data.path,
  50. data.name,
  51. data.module
  52. )
  53. );
  54. const dynamicPlugins = await Promise.all(dynamicPromises);
  55. const dynamicMimePromises = mime_extension_data.map(data =>
  56. loadComponent(
  57. data.path,
  58. data.name,
  59. data.module
  60. )
  61. );
  62. const dynamicMimePlugins = await Promise.all(dynamicMimePromises);
  63. // Handle the registered mime extensions.
  64. var mimeExtensions = [];
  65. var extension;
  66. var extMod;
  67. var plugins = [];
  68. {{#each jupyterlab_mime_extensions}}
  69. try {
  70. extMod = require('{{@key}}/{{this}}');
  71. extension = extMod.default;
  72. // Handle CommonJS exports.
  73. if (!extMod.hasOwnProperty('__esModule')) {
  74. extension = extMod;
  75. }
  76. plugins = Array.isArray(extension) ? extension : [extension];
  77. plugins.forEach(function(plugin) {
  78. if (PageConfig.Extension.isDeferred(plugin.id)) {
  79. deferred.push(plugin.id);
  80. ignorePlugins.push(plugin.id);
  81. }
  82. if (PageConfig.Extension.isDisabled(plugin.id)) {
  83. disabled.push(plugin.id);
  84. return;
  85. }
  86. mimeExtensions.push(plugin);
  87. });
  88. } catch (e) {
  89. console.error(e);
  90. }
  91. {{/each}}
  92. // Add the dyanmic mime extensions.
  93. dynamicMimePlugins.forEach(plugin => { mimeExtensions.push(plugin); });
  94. // Handled the registered standard extensions.
  95. {{#each jupyterlab_extensions}}
  96. try {
  97. extMod = require('{{@key}}/{{this}}');
  98. extension = extMod.default;
  99. // Handle CommonJS exports.
  100. if (!extMod.hasOwnProperty('__esModule')) {
  101. extension = extMod;
  102. }
  103. plugins = Array.isArray(extension) ? extension : [extension];
  104. plugins.forEach(function(plugin) {
  105. if (PageConfig.Extension.isDeferred(plugin.id)) {
  106. deferred.push(plugin.id);
  107. ignorePlugins.push(plugin.id);
  108. }
  109. if (PageConfig.Extension.isDisabled(plugin.id)) {
  110. disabled.push(plugin.id);
  111. return;
  112. }
  113. register.push(plugin);
  114. });
  115. } catch (e) {
  116. console.error(e);
  117. }
  118. {{/each}}
  119. // Add the dynamic extensions.
  120. dynamicPlugins.forEach(plugin => { register.push(plugin) });
  121. var lab = new JupyterLab({
  122. mimeExtensions: mimeExtensions,
  123. disabled: {
  124. matches: disabled,
  125. patterns: PageConfig.Extension.disabled
  126. .map(function (val) { return val.raw; })
  127. },
  128. deferred: {
  129. matches: deferred,
  130. patterns: PageConfig.Extension.deferred
  131. .map(function (val) { return val.raw; })
  132. },
  133. });
  134. register.forEach(function(item) { lab.registerPluginModule(item); });
  135. lab.start({ ignorePlugins: ignorePlugins });
  136. lab.restored.then(() => {
  137. console.debug('Example started!');
  138. });
  139. }
  140. window.addEventListener('load', main);