This repository has been archived by the owner on Feb 16, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
index.js
343 lines (314 loc) · 10.6 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
// Inspired by: https://github.com/jupyterlab/jupyterlab/blob/master/dev_mode/index.js
import { PageConfig, URLExt } from '@jupyterlab/coreutils';
// Promise.allSettled polyfill, until our supported browsers implement it
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled
if (Promise.allSettled === undefined) {
Promise.allSettled = promises =>
Promise.all(
promises.map(promise =>
promise.then(
value => ({
status: 'fulfilled',
value
}),
reason => ({
status: 'rejected',
reason
})
)
)
);
}
require('./style.js');
require('./extraStyle.js');
function loadScript(url) {
return new Promise((resolve, reject) => {
const newScript = document.createElement('script');
newScript.onerror = reject;
newScript.onload = resolve;
newScript.async = true;
document.head.appendChild(newScript);
newScript.src = url;
});
}
async function loadComponent(url, scope) {
await loadScript(url);
// From MIT-licensed https://github.com/module-federation/module-federation-examples/blob/af043acd6be1718ee195b2511adf6011fba4233c/advanced-api/dynamic-remotes/app1/src/App.js#L6-L12
// eslint-disable-next-line no-undef
await __webpack_init_sharing__('default');
const container = window._JUPYTERLAB[scope];
// Initialize the container, it may provide shared modules and may need ours
// eslint-disable-next-line no-undef
await container.init(__webpack_share_scopes__.default);
}
async function createModule(scope, module) {
try {
const factory = await window._JUPYTERLAB[scope].get(module);
return factory();
} catch (e) {
console.warn(
`Failed to create module: package: ${scope}; module: ${module}`
);
throw e;
}
}
/**
* The main function
*/
async function main() {
// load extra packages
require('@jupyterlab/celltags');
const mimeExtensionsMods = [
require('@jupyterlab/javascript-extension'),
require('@jupyterlab/json-extension'),
require('@jupyterlab/pdf-extension'),
require('@jupyterlab/vega5-extension')
];
const mimeExtensions = await Promise.all(mimeExtensionsMods);
const disabled = [];
// TODO: formalize the way the set of initial extensions and plugins are specified
let baseMods = [
// @retrolab plugins
require('@retrolab/application-extension'),
require('@retrolab/console-extension'),
require('@retrolab/docmanager-extension'),
require('@retrolab/help-extension'),
require('@retrolab/notebook-extension'),
// to handle opening new tabs after creating a new terminal
require('@retrolab/terminal-extension'),
// @jupyterlab plugins
require('@jupyterlab/application-extension').default.filter(({ id }) =>
[
'@jupyterlab/application-extension:commands',
'@jupyterlab/application-extension:context-menu',
'@jupyterlab/application-extension:faviconbusy'
].includes(id)
),
require('@jupyterlab/apputils-extension').default.filter(({ id }) =>
[
'@jupyterlab/apputils-extension:palette',
'@jupyterlab/apputils-extension:settings',
'@jupyterlab/apputils-extension:state',
'@jupyterlab/apputils-extension:themes',
'@jupyterlab/apputils-extension:themes-palette-menu'
].includes(id)
),
require('@jupyterlab/codemirror-extension').default.filter(({ id }) =>
[
'@jupyterlab/codemirror-extension:services',
'@jupyterlab/codemirror-extension:codemirror'
].includes(id)
),
require('@jupyterlab/completer-extension').default.filter(({ id }) =>
['@jupyterlab/completer-extension:manager'].includes(id)
),
require('@jupyterlab/console-extension'),
require('@jupyterlab/docmanager-extension').default.filter(({ id }) =>
[
'@jupyterlab/docmanager-extension:plugin',
'@jupyterlab/docmanager-extension:download'
].includes(id)
),
require('@jupyterlab/docprovider-extension'),
require('@jupyterlab/filebrowser-extension').default.filter(({ id }) =>
['@jupyterlab/filebrowser-extension:factory'].includes(id)
),
require('@jupyterlab/fileeditor-extension').default.filter(({ id }) =>
['@jupyterlab/fileeditor-extension:plugin'].includes(id)
),
require('@jupyterlab/mainmenu-extension'),
require('@jupyterlab/mathjax2-extension'),
require('@jupyterlab/notebook-extension').default.filter(({ id }) =>
[
'@jupyterlab/notebook-extension:code-console',
'@jupyterlab/notebook-extension:factory',
'@jupyterlab/notebook-extension:tracker',
'@jupyterlab/notebook-extension:widget-factory'
].includes(id)
),
require('@jupyterlab/rendermime-extension'),
require('@jupyterlab/shortcuts-extension'),
// so new terminals can be create from the menu
require('@jupyterlab/terminal-extension'),
require('@jupyterlab/theme-light-extension'),
require('@jupyterlab/theme-dark-extension'),
require('@jupyterlab/translation-extension'),
// Add the "Hub Control Panel" menu option when running in JupyterHub
require('@jupyterlab/hub-extension')
];
// The motivation here is to only load a specific set of plugins dependending on
// the current page
const page = PageConfig.getOption('retroPage');
switch (page) {
case 'tree': {
baseMods = baseMods.concat([
require('@jupyterlab/filebrowser-extension').default.filter(({ id }) =>
[
'@jupyterlab/filebrowser-extension:browser',
'@jupyterlab/filebrowser-extension:download',
'@jupyterlab/filebrowser-extension:file-upload-status',
'@jupyterlab/filebrowser-extension:open-with',
'@jupyterlab/filebrowser-extension:share-file'
].includes(id)
),
require('@retrolab/tree-extension'),
require('@jupyterlab/running-extension')
]);
break;
}
case 'notebooks': {
baseMods = baseMods.concat([
require('@jupyterlab/completer-extension').default.filter(({ id }) =>
['@jupyterlab/completer-extension:notebooks'].includes(id)
),
require('@jupyterlab/tooltip-extension').default.filter(({ id }) =>
[
'@jupyterlab/tooltip-extension:manager',
'@jupyterlab/tooltip-extension:notebooks'
].includes(id)
)
]);
break;
}
case 'consoles': {
baseMods = baseMods.concat([
require('@jupyterlab/completer-extension').default.filter(({ id }) =>
['@jupyterlab/completer-extension:consoles'].includes(id)
),
require('@jupyterlab/tooltip-extension').default.filter(({ id }) =>
[
'@jupyterlab/tooltip-extension:manager',
'@jupyterlab/tooltip-extension:consoles'
].includes(id)
)
]);
break;
}
case 'edit': {
baseMods = baseMods.concat([
require('@jupyterlab/completer-extension').default.filter(({ id }) =>
['@jupyterlab/completer-extension:files'].includes(id)
),
require('@jupyterlab/filebrowser-extension').default.filter(({ id }) =>
['@jupyterlab/filebrowser-extension:browser'].includes(id)
)
]);
break;
}
}
/**
* Iterate over active plugins in an extension.
*
* #### Notes
* This also populates the disabled
*/
function* activePlugins(extension) {
// Handle commonjs or es2015 modules
let exports;
if (Object.prototype.hasOwnProperty.call(extension, '__esModule')) {
exports = extension.default;
} else {
// CommonJS exports.
exports = extension;
}
let plugins = Array.isArray(exports) ? exports : [exports];
for (let plugin of plugins) {
if (PageConfig.Extension.isDisabled(plugin.id)) {
disabled.push(plugin.id);
continue;
}
yield plugin;
}
}
const extension_data = JSON.parse(
PageConfig.getOption('federated_extensions')
);
const mods = [];
const federatedExtensionPromises = [];
const federatedMimeExtensionPromises = [];
const federatedStylePromises = [];
const extensions = await Promise.allSettled(
extension_data.map(async data => {
await loadComponent(
`${URLExt.join(
PageConfig.getOption('fullLabextensionsUrl'),
data.name,
data.load
)}`,
data.name
);
return data;
})
);
extensions.forEach(p => {
if (p.status === 'rejected') {
// There was an error loading the component
console.error(p.reason);
return;
}
const data = p.value;
if (data.extension) {
federatedExtensionPromises.push(createModule(data.name, data.extension));
}
if (data.mimeExtension) {
federatedMimeExtensionPromises.push(
createModule(data.name, data.mimeExtension)
);
}
if (data.style) {
federatedStylePromises.push(createModule(data.name, data.style));
}
});
// Add the base frontend extensions
const baseFrontendMods = await Promise.all(baseMods);
baseFrontendMods.forEach(p => {
for (let plugin of activePlugins(p)) {
mods.push(plugin);
}
});
// Add the federated extensions.
const federatedExtensions = await Promise.allSettled(
federatedExtensionPromises
);
federatedExtensions.forEach(p => {
if (p.status === 'fulfilled') {
for (let plugin of activePlugins(p.value)) {
mods.push(plugin);
}
} else {
console.error(p.reason);
}
});
// Add the federated mime extensions.
const federatedMimeExtensions = await Promise.allSettled(
federatedMimeExtensionPromises
);
federatedMimeExtensions.forEach(p => {
if (p.status === 'fulfilled') {
for (let plugin of activePlugins(p.value)) {
mimeExtensions.push(plugin);
}
} else {
console.error(p.reason);
}
});
// Load all federated component styles and log errors for any that do not
(await Promise.allSettled(federatedStylePromises))
.filter(({ status }) => status === 'rejected')
.forEach(({ reason }) => {
console.error(reason);
});
const RetroApp = require('@retrolab/application').RetroApp;
const app = new RetroApp({ mimeExtensions });
app.registerPluginModules(mods);
// Expose global app instance when in dev mode or when toggled explicitly.
const exposeAppInBrowser =
(PageConfig.getOption('exposeAppInBrowser') || '').toLowerCase() === 'true';
if (exposeAppInBrowser) {
window.jupyterapp = app;
}
await app.start();
}
window.addEventListener('load', main);