bootstrap.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // This file is auto-generated from the corresponding file in /dev_mode
  2. /*-----------------------------------------------------------------------------
  3. | Copyright (c) Jupyter Development Team.
  4. | Distributed under the terms of the Modified BSD License.
  5. |----------------------------------------------------------------------------*/
  6. // We copy some of the pageconfig parsing logic in @jupyterlab/coreutils
  7. // below, since this must run before any other files are loaded (including
  8. // @jupyterlab/coreutils).
  9. /**
  10. * Get global configuration data for the Jupyter application.
  11. *
  12. * @param name - The name of the configuration option.
  13. *
  14. * @returns The config value or an empty string if not found.
  15. *
  16. * #### Notes
  17. * All values are treated as strings. For browser based applications, it is
  18. * assumed that the page HTML includes a script tag with the id
  19. * `jupyter-config-data` containing the configuration as valid JSON.
  20. */
  21. let _CONFIG_DATA = null;
  22. function getOption(name) {
  23. if (_CONFIG_DATA === null) {
  24. let configData = {};
  25. // Use script tag if available.
  26. if (typeof document !== 'undefined' && document) {
  27. const el = document.getElementById('jupyter-config-data');
  28. if (el) {
  29. configData = JSON.parse(el.textContent || '{}');
  30. }
  31. }
  32. _CONFIG_DATA = configData;
  33. }
  34. return _CONFIG_DATA[name] || '';
  35. }
  36. // eslint-disable-next-line no-undef
  37. __webpack_public_path__ = getOption('fullStaticUrl') + '/';
  38. // Promise.allSettled polyfill, until our supported browsers implement it
  39. // See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled
  40. if (Promise.allSettled === undefined) {
  41. Promise.allSettled = promises =>
  42. Promise.all(
  43. promises.map(promise =>
  44. promise.then(
  45. value => ({
  46. status: 'fulfilled',
  47. value
  48. }),
  49. reason => ({
  50. status: 'rejected',
  51. reason
  52. })
  53. )
  54. )
  55. );
  56. }
  57. function loadScript(url) {
  58. return new Promise((resolve, reject) => {
  59. const newScript = document.createElement('script');
  60. newScript.onerror = reject;
  61. newScript.onload = resolve;
  62. newScript.async = true;
  63. document.head.appendChild(newScript);
  64. newScript.src = url;
  65. });
  66. }
  67. async function loadComponent(url, scope) {
  68. await loadScript(url);
  69. // From https://webpack.js.org/concepts/module-federation/#dynamic-remote-containers
  70. await __webpack_init_sharing__('default');
  71. const container = window._JUPYTERLAB[scope];
  72. // Initialize the container, it may provide shared modules and may need ours
  73. await container.init(__webpack_share_scopes__.default);
  74. }
  75. void (async function bootstrap() {
  76. // This is all the data needed to load and activate plugins. This should be
  77. // gathered by the server and put onto the initial page template.
  78. const extension_data = getOption('federated_extensions');
  79. // We first load all federated components so that the shared module
  80. // deduplication can run and figure out which shared modules from all
  81. // components should be actually used. We have to do this before importing
  82. // and using the module that actually uses these components so that all
  83. // dependencies are initialized.
  84. let labExtensionUrl = getOption('fullLabextensionsUrl');
  85. const extensions = await Promise.allSettled(
  86. extension_data.map(async data => {
  87. await loadComponent(
  88. `${labExtensionUrl}/${data.name}/${data.load}`,
  89. data.name
  90. );
  91. })
  92. );
  93. extensions.forEach(p => {
  94. if (p.status === 'rejected') {
  95. // There was an error loading the component
  96. console.error(p.reason);
  97. }
  98. });
  99. // Now that all federated containers are initialized with the main
  100. // container, we can import the main function.
  101. let main = (await import('./index.out.js')).main;
  102. window.addEventListener('load', main);
  103. })();