Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve lazy loading of Monaco editor and fix webworker warning
- Load web worker through blob URL - Avoid scripts from being loaded automatically if they are dynamic Co-authored-by: Olaf Lessenich <olessenich@eclipsesource.com>
- Loading branch information
1 parent
ee5ce00
commit 890626e
Showing
4 changed files
with
119 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import fs from 'fs'; | ||
import * as vscode from 'vscode'; | ||
|
||
// from https://github.com/vitejs/vite/blob/main/packages/vite/src/node/plugins/manifest.ts | ||
export type ViteManifest = Record<string, ViteManifestChunk>; | ||
|
||
export interface ViteManifestChunk { | ||
src?: string; | ||
file: string; | ||
css?: string[]; | ||
assets?: string[]; | ||
isEntry?: boolean; | ||
name?: string; | ||
isDynamicEntry?: boolean; | ||
imports?: string[]; | ||
dynamicImports?: string[]; | ||
} | ||
|
||
export interface ViteManifestEntry { | ||
source: string; | ||
chunk: ViteManifestChunk; | ||
} | ||
|
||
export const ROOT_ENTRY = 'index.html'; | ||
export const EDITOR_WORKER_ENTRY = 'editor.worker.js?worker'; | ||
|
||
export function parseBuildManifest(path: string): ViteManifest { | ||
return JSON.parse(fs.readFileSync(path, 'utf8')); | ||
} | ||
|
||
export function findRootEntry(manifest: ViteManifest): ViteManifestEntry { | ||
const rootEntry = Object.entries(manifest).find(entry => entry[1].isEntry); | ||
return { source: rootEntry![0], chunk: rootEntry![1] }; | ||
} | ||
|
||
export function findEditorWorkerWrapperChunk(manifest: ViteManifest): ViteManifestChunk | undefined { | ||
return Object.entries(manifest).find(entry => entry[0].endsWith(EDITOR_WORKER_ENTRY))?.[1]; | ||
} | ||
|
||
export function findEditorWorker(appPath: vscode.Uri, manifest: ViteManifest): vscode.Uri | undefined { | ||
// Finding the location of the editor worker is a bit tricky as Vite automatically generates a wrapper for it | ||
// But for web views we need the actual code so we can turn it into a blob URL later on | ||
const workerWrapper = findEditorWorkerWrapperChunk(manifest); | ||
if (!workerWrapper) { | ||
return; | ||
} | ||
// find the editor worker file that is not the wrapper | ||
const assetsDirectory = vscode.Uri.joinPath(appPath, 'assets'); | ||
const assetFiles = fs.readdirSync(assetsDirectory.fsPath); | ||
const editorWorker = assetFiles.find(fileName => fileName.includes('editor.worker') && !workerWrapper.file.includes(fileName)); | ||
return editorWorker ? vscode.Uri.joinPath(assetsDirectory, editorWorker) : undefined; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters