forked from jupyterlab/jupyterlab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
301 lines (265 loc) · 9.34 KB
/
Copy pathindex.js
File metadata and controls
301 lines (265 loc) · 9.34 KB
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
/*
* Copyright (c) Jupyter Development Team.
* Distributed under the terms of the Modified BSD License.
*/
import { PageConfig, JupyterPluginRegistry} from '@jupyterlab/coreutils';
import './style.js';
async function createModule(scope, module) {
try {
const factory = await window._JUPYTERLAB[scope].get(module);
const instance = factory();
instance.__scope__ = scope;
return instance;
} catch(e) {
console.warn(`Failed to create module: package: ${scope}; module: ${module}`);
throw e;
}
}
/**
* The main entry point for the application.
*/
export async function main() {
// Handle a browser test.
// Set up error handling prior to loading extensions.
var browserTest = PageConfig.getOption('browserTest');
if (browserTest.toLowerCase() === 'true') {
var el = document.createElement('div');
el.id = 'browserTest';
document.body.appendChild(el);
el.textContent = '[]';
el.style.display = 'none';
var errors = [];
var reported = false;
var timeout = 25000;
var report = function() {
if (reported) {
return;
}
reported = true;
el.className = 'completed';
}
window.onerror = function(msg, url, line, col, error) {
errors.push(String(error));
el.textContent = JSON.stringify(errors)
};
console.error = function(message) {
errors.push(String(message));
el.textContent = JSON.stringify(errors)
};
}
var pluginRegistry = new JupyterPluginRegistry();
var JupyterLab = require('@jupyterlab/application').JupyterLab;
var disabled = [];
var deferred = [];
var ignorePlugins = [];
var register = [];
const federatedExtensionPromises = [];
const federatedMimeExtensionPromises = [];
const federatedStylePromises = [];
// Start initializing the federated extensions
const extensions = JSON.parse(
PageConfig.getOption('federated_extensions')
);
// Keep a mapping of renamed plugin ids to ensure user configs don't break.
// The mapping is defined in the main index.js for JupyterLab, since it may not be relevant for
// other lab-based applications (they may not use the same set of plugins).
const renamedPluginIds = {
'@jupyterlab/application:mimedocument': '@jupyterlab/application-extension:mimedocument',
'@jupyterlab/help-extension:licenses': '@jupyterlab/apputils-extension:licenses-plugin',
'@jupyterlab/lsp:ILSPCodeExtractorsManager': '@jupyterlab/lsp-extension:code-extractor-manager',
'@jupyterlab/translation:translator': '@jupyterlab/translation-extension:translator',
'@jupyterlab/workspaces:commands': '@jupyterlab/workspaces-extension:commands'
};
// Transparently handle the case of renamed plugins, so current configs don't break.
// And emit a warning in the dev tools console to notify about the rename so
// users can update their config.
const disabledExtensions = PageConfig.Extension.disabled.map(id => {
if (renamedPluginIds[id]) {
console.warn(`Plugin ${id} has been renamed to ${renamedPluginIds[id]}. Consider updating your config to use the new name.`);
return renamedPluginIds[id];
}
return id;
});
const deferredExtensions = PageConfig.Extension.deferred.map(id => {
if (renamedPluginIds[id]) {
console.warn(`Plugin id ${id} has been renamed to ${renamedPluginIds[id]}. Consider updating your config to use the new name.`);
return renamedPluginIds[id];
}
return id;
});
// This is basically a copy of PageConfig.Extension.isDisabled to
// take into account the case of renamed plugins.
const isPluginDisabled = (id) => {
const separatorIndex = id.indexOf(':');
let extName = '';
if (separatorIndex !== -1) {
extName = id.slice(0, separatorIndex);
}
return disabledExtensions.some(val => val === id || (extName && val === extName));
}
// This is basically a copy of PageConfig.Extension.isDeferred to
// take into account the case of renamed plugins.
const isPluginDeferred = (id) => {
const separatorIndex = id.indexOf(':');
let extName = '';
if (separatorIndex !== -1) {
extName = id.slice(0, separatorIndex);
}
return deferredExtensions.some(val => val === id || (extName && val === extName));
}
const queuedFederated = [];
extensions.forEach(data => {
if (data.extension) {
queuedFederated.push(data.name);
federatedExtensionPromises.push(createModule(data.name, data.extension));
}
if (data.mimeExtension) {
queuedFederated.push(data.name);
federatedMimeExtensionPromises.push(createModule(data.name, data.mimeExtension));
}
if (data.style && !isPluginDisabled(data.name)) {
federatedStylePromises.push(createModule(data.name, data.style));
}
});
const allPlugins = [];
/**
* Get the plugins from an extension.
*/
function getPlugins(extension) {
// Handle commonjs or es2015 modules
let exports;
if (extension.hasOwnProperty('__esModule')) {
exports = extension.default;
} else {
// CommonJS exports.
exports = extension;
}
return Array.isArray(exports) ? exports : [exports];
}
/**
* Iterate over active plugins in an extension.
*
* #### Notes
* This also populates the disabled, deferred, and ignored arrays.
*/
function* activePlugins(extension) {
const plugins = getPlugins(extension);
for (let plugin of plugins) {
const isDisabled = isPluginDisabled(plugin.id);
allPlugins.push({
id: plugin.id,
description: plugin.description,
requires: plugin.requires ?? [],
optional: plugin.optional ?? [],
provides: plugin.provides ?? null,
autoStart: plugin.autoStart,
enabled: !isDisabled,
extension: extension.__scope__
});
if (isDisabled) {
disabled.push(plugin.id);
continue;
}
if (isPluginDeferred(plugin.id)) {
deferred.push(plugin.id);
ignorePlugins.push(plugin.id);
}
yield plugin;
}
}
// Handle the registered mime extensions.
const mimeExtensions = [];
{{#each jupyterlab_mime_extensions}}
if (!queuedFederated.includes('{{@key}}')) {
try {
let ext = require('{{@key}}{{#if this}}/{{this}}{{/if}}');
ext.__scope__ = '{{@key}}';
for (let plugin of activePlugins(ext)) {
mimeExtensions.push(plugin);
}
} catch (e) {
console.error(e);
}
}
{{/each}}
// 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);
}
});
// Handled the registered standard extensions.
{{#each jupyterlab_extensions}}
if (!queuedFederated.includes('{{@key}}')) {
try {
let ext = require('{{@key}}{{#if this}}/{{this}}{{/if}}');
ext.__scope__ = '{{@key}}';
for (let plugin of activePlugins(ext)) {
register.push(plugin);
}
} catch (e) {
console.error(e);
}
}
{{/each}}
// Add the federated extensions.
const federatedExtensions = await Promise.allSettled(federatedExtensionPromises);
federatedExtensions.forEach(p => {
if (p.status === "fulfilled") {
for (let plugin of activePlugins(p.value)) {
register.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);
});
// 2. Register the plugins
pluginRegistry.registerPlugins(register);
// 3. Get and resolve the service manager and connection status plugins
const IConnectionStatus = require('@jupyterlab/services').IConnectionStatus;
const IServiceManager = require('@jupyterlab/services').IServiceManager;
const connectionStatus = await pluginRegistry.resolveOptionalService(IConnectionStatus);
const serviceManager = await pluginRegistry.resolveRequiredService(IServiceManager);
const lab = new JupyterLab({
pluginRegistry,
serviceManager,
mimeExtensions,
connectionStatus,
disabled: {
matches: disabled,
patterns: disabledExtensions
.map(function (val) { return val.raw; })
},
deferred: {
matches: deferred,
patterns: deferredExtensions
.map(function (val) { return val.raw; })
},
availablePlugins: allPlugins
});
// 4. Start the application, which will activate the other plugins
lab.start({ ignorePlugins, bubblingKeydown: true });
// Expose global app instance when in dev mode or when toggled explicitly.
var exposeAppInBrowser = (PageConfig.getOption('exposeAppInBrowser') || '').toLowerCase() === 'true';
var devMode = (PageConfig.getOption('devMode') || '').toLowerCase() === 'true';
if (exposeAppInBrowser || devMode) {
window.jupyterapp = lab;
}
// Handle a browser test.
if (browserTest.toLowerCase() === 'true') {
lab.restored
.then(function() { report(errors); })
.catch(function(reason) { report([`RestoreError: ${reason.message}`]); });
// Handle failures to restore after the timeout has elapsed.
window.setTimeout(function() { report(errors); }, timeout);
}
}