I'm trying to get intellisense in Monaco editor from the 2000+ .ts
files. Loading models is taking 4-5 seconds, but auto complete suggestions are coming after 10-15 seconds and 'Loading...' is seen.
Below is the simplified version of my code which can be run in Monaco Playground
monaco.languages.typescript.javascriptDefaults.setEagerModelSync(true);
monaco.languages.typescript.typescriptDefaults.setEagerModelSync(true);
monaco.languages.typescript.javascriptDefaults.setDiagnosticsOptions({
noSemanticValidation: true,
noSyntaxValidation: true,
noSuggestionDiagnostics: true
});
monaco.languages.typescript.typescriptDefaults.setDiagnosticsOptions({
noSemanticValidation: true,
noSyntaxValidation: true,
noSuggestionDiagnostics: true
});
editor = monaco.editor.create(
document.getElementById('container'),
{
minimap: { enabled: true },
scrollBeyondLastLine: true,
// model: null,
language: 'typescript',
theme: 'vs-dark',
value: 'function () {}\n\n'
}
);
// simulate 2000 .ts files
for (var fileIdx = 0; fileIdx < 2000; fileIdx++) {
for (var fnIdx = 0; fnIdx < 1; fnIdx++) {
monaco.editor.createModel(`
function test_${fileIdx}_${fnIdx}() {}
function fn_${fileIdx}_${fnIdx}() {}
function foo_${fileIdx}_${fnIdx}() {}
function bar_${fileIdx}_${fnIdx}() {}
function out_${fileIdx}_${fnIdx}() {}
`,
'typescript'
);
}
}
I followed https://github.com/microsoft/monaco-editor/issues/2030 and Monaco Editor intellisense from multiple files, but no luck.
Can someone shed some light on the performant way to achieve this?
Mod #1:
Loading .d.ts
files instead of .ts
files (taken directly from here):
monaco.languages.typescript.javascriptDefaults.setEagerModelSync(false);
monaco.languages.typescript.typescriptDefaults.setEagerModelSync(false);
monaco.languages.typescript.javascriptDefaults.setDiagnosticsOptions({
noSemanticValidation: true,
noSyntaxValidation: true,
noSuggestionDiagnostics: true
});
monaco.languages.typescript.typescriptDefaults.setDiagnosticsOptions({
noSemanticValidation: true,
noSyntaxValidation: true,
noSuggestionDiagnostics: true
});
monaco.languages.typescript.typescriptDefaults.setCompilerOptions({
target: monaco.languages.typescript.ScriptTarget.ES2016,
allowNonTsExtensions: true,
moduleResolution: monaco.languages.typescript.ModuleResolutionKind.NodeJs,
module: monaco.languages.typescript.ModuleKind.CommonJS,
noEmit: true,
typeRoots: ["node_modules/@types"]
});
// Case #1: Load 2000 .d.ts files with each having 1 function declaration
let extraLibs = []
for (let i = 0; i < 2000; i++) {
extraLibs.push(
{
content: `export declare function next${i}() : string`,
filePath: monaco.Uri.file(`/node_modules/@types/external${i}/index.d.ts`).toString(true)
}
);
}
// Case #2: Load 1 .d.ts file with 2000 function declarations
/*let def = ''
for (let i = 0; i < 2000; i++) {
def = def.concat(`export declare function next${i}() : string\n`);
}
let extraLibs = []
extraLibs.push(
{
content: def,
filePath: monaco.Uri.file(`/node_modules/@types/external1/index.d.ts`).toString(true)
}
);*/
monaco.languages.typescript.typescriptDefaults.setExtraLibs(extraLibs);
extraLibs.forEach((lib) => monaco.editor.createModel(lib.content, "typescript", monaco.Uri.parse(lib.filePath)))
var jsCode = `import * as x from "external1"
const tt : string = x.next1();`;
monaco.editor.create(document.getElementById("container"), {
model: monaco.editor.createModel(jsCode, "typescript", monaco.Uri.file("main.tsx")),
});
Observation: When loading 2000 .d.ts
files with each having 1 function declaration, the perf is not good. But when loading 1 .d.ts
file with 2000 function definition, the suggestions are instantaneous. Even if increased to 20000 function declarations, perf is good. But in my case, I have 3000+ ts files.
Edit #1: You can follow monaco Github issue if interested