bootstrap.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. // This is all the data needed to load and activate plugins. This should be
  4. // gathered by the server and put onto the initial page template.
  5. const PLUGIN_DATA = JSON.parse(
  6. document.getElementById('jupyterlab-plugin-data').textContent
  7. );
  8. import { PageConfig } from '@jupyterlab/coreutils';
  9. // eslint-disable-next-line
  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('./build/imports.css');
  14. function loadScript(url) {
  15. return new Promise((resolve, reject) => {
  16. const newScript = document.createElement('script');
  17. newScript.onerror = reject;
  18. newScript.onload = resolve;
  19. newScript.async = true;
  20. document.head.appendChild(newScript);
  21. newScript.src = url;
  22. });
  23. }
  24. async function loadComponent(url, scope, module) {
  25. await loadScript(url);
  26. // From MIT-licensed https://github.com/module-federation/module-federation-examples/blob/af043acd6be1718ee195b2511adf6011fba4233c/advanced-api/dynamic-remotes/app1/src/App.js#L6-L12
  27. await __webpack_init_sharing__('default');
  28. const container = window.MYNAMESPACE[scope];
  29. // Initialize the container, it may provide shared modules and may need ours
  30. await container.init(__webpack_share_scopes__.default);
  31. const factory = await window.MYNAMESPACE[scope].get(module);
  32. const Module = factory();
  33. return Module;
  34. }
  35. window.addEventListener('load', async function() {
  36. const JupyterLab = require('@jupyterlab/application').JupyterLab;
  37. const pluginPromises = PLUGIN_DATA.map(data =>
  38. loadComponent(
  39. `${PageConfig.getBaseUrl()}/${data.path}`,
  40. data.name,
  41. data.module
  42. )
  43. );
  44. const plugins = await Promise.all(pluginPromises);
  45. const mods = [
  46. require('@jupyterlab/application-extension'),
  47. require('@jupyterlab/apputils-extension'),
  48. require('@jupyterlab/codemirror-extension'),
  49. require('@jupyterlab/docmanager-extension'),
  50. require('@jupyterlab/fileeditor-extension'),
  51. require('@jupyterlab/filebrowser-extension'),
  52. require('@jupyterlab/help-extension'),
  53. require('@jupyterlab/imageviewer-extension'),
  54. require('@jupyterlab/mainmenu-extension'),
  55. require('@jupyterlab/rendermime-extension'),
  56. require('@jupyterlab/shortcuts-extension'),
  57. require('@jupyterlab/theme-dark-extension'),
  58. require('@jupyterlab/theme-light-extension'),
  59. require('@jupyterlab/ui-components-extension'),
  60. ...plugins
  61. ];
  62. const lab = new JupyterLab();
  63. lab.registerPluginModules(mods);
  64. /* eslint-disable no-console */
  65. console.log('Starting app');
  66. await lab.start();
  67. console.log('App started, waiting for restore');
  68. await lab.restored;
  69. console.log('Example started!');
  70. });