Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kernel preloads are always loaded as ES modules #170909

Merged
merged 3 commits into from
Jan 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -182,16 +182,6 @@ async function webviewPreloads(ctx: PreloadContext) {

document.body.addEventListener('click', handleInnerClick);

async function loadScriptSource(url: string, originalUri: string): Promise<string> {
const res = await fetch(url);
const text = await res.text();
if (!res.ok) {
throw new Error(`Unexpected ${res.status} requesting ${originalUri}: ${text || res.statusText}`);
}

return text;
}

interface RendererContext extends rendererApi.RendererContext<unknown> {
readonly settings: RenderOptions;
}
Expand Down Expand Up @@ -224,24 +214,9 @@ async function webviewPreloads(ctx: PreloadContext) {
});
}

const invokeSourceWithGlobals = (functionSrc: string, globals: { [name: string]: unknown }) => {
const args = Object.entries(globals);
return new Function(...args.map(([k]) => k), functionSrc)(...args.map(([, v]) => v));
};

async function runKernelPreload(url: string, originalUri: string, forceLoadAsModule: boolean): Promise<void> {
if (forceLoadAsModule) {
return activateModuleKernelPreload(url);
}

const text = await loadScriptSource(url, originalUri);
const isModule = /\bexport\b.*\bactivate\b/.test(text);
async function runKernelPreload(url: string): Promise<void> {
try {
if (isModule) {
return activateModuleKernelPreload(url);
} else {
return invokeSourceWithGlobals(text, { ...kernelPreloadGlobals, scriptUrl: url });
}
return await activateModuleKernelPreload(url);
} catch (e) {
console.error(e);
throw e;
Expand Down Expand Up @@ -818,12 +793,6 @@ async function webviewPreloads(ctx: PreloadContext) {

const onDidReceiveKernelMessage = createEmitter<unknown>();

const kernelPreloadGlobals = {
acquireVsCodeApi,
onDidReceiveKernelMessage: onDidReceiveKernelMessage.event,
postKernelMessage: (data: unknown) => postNotebookMessage('customKernelMessage', { message: data }),
};

const ttPolicy = window.trustedTypes?.createPolicy('notebookRenderer', {
createHTML: value => value,
createScript: value => value,
Expand Down Expand Up @@ -1209,8 +1178,8 @@ async function webviewPreloads(ctx: PreloadContext) {
}
case 'preload': {
const resources = event.data.resources;
for (const { uri, originalUri } of resources) {
kernelPreloads.load(uri, originalUri, false);
for (const { uri } of resources) {
kernelPreloads.load(uri);
}
break;
}
Expand Down Expand Up @@ -1462,9 +1431,9 @@ async function webviewPreloads(ctx: PreloadContext) {
* @param uri URI to load from
* @param originalUri URI to show in an error message if the preload is invalid.
*/
public load(uri: string, originalUri: string, forceLoadAsModule: boolean) {
public load(uri: string) {
const promise = Promise.all([
runKernelPreload(uri, originalUri, forceLoadAsModule),
runKernelPreload(uri),
this.waitForAllCurrent(),
]);

Expand Down Expand Up @@ -2229,7 +2198,7 @@ async function webviewPreloads(ctx: PreloadContext) {
});

for (const preload of ctx.staticPreloadsData) {
kernelPreloads.load(preload.entrypoint, preload.entrypoint, true);
kernelPreloads.load(preload.entrypoint);
}

function postNotebookMessage<T extends webviewMessages.FromWebviewMessage>(
Expand Down