Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
feat(codemirror/autocomplete): embed d.ts
Elian Doran66b518a457e8
diff --git a/packages/codemirror/scripts/generate_ts_libs.mjs b/packages/codemirror/scripts/generate_ts_libs.mjsnew file mode 100644index 0000000000..e2d1a14fad--- /dev/null+++ b/packages/codemirror/scripts/generate_ts_libs.mjs@@ -0,0 +1,54 @@+// Regenerates src/type_completion/ts_lib_files.ts — the LIST of TypeScript+// lib.*.d.ts files (the transitive closure for target ES2020, lib ["es2020",+// "dom"]) that the in-browser language service loads, imported as raw text so it+// works offline (no CDN).+//+// The file content is resolved at build time from the installed `typescript`, so+// a normal TS upgrade needs no regeneration. Only re-run this when a TS upgrade+// changes which lib files exist in the closure, or when changing the target lib:+// node scripts/generate_ts_libs.mjs+import { existsSync, readFileSync, writeFileSync } from "node:fs";+import { createRequire } from "node:module";+import { dirname, join } from "node:path";++const require = createRequire(import.meta.url);+const libDir = dirname(require.resolve("typescript/lib/lib.es5.d.ts"));+const OUT = new URL("../src/type_completion/ts_lib_files.ts", import.meta.url);++// Entry libs for `lib: ["es2020", "dom"]`; follow /// <reference lib="…"/> transitively.+const ENTRIES = ["lib.es2020.d.ts", "lib.dom.d.ts", "lib.dom.iterable.d.ts"];+const REFERENCE = /\/\/\/\s*<reference\s+lib=["']([^"']+)["']\s*\/>/g;++const seen = new Set();+function visit(name) {+ if (seen.has(name)) return;+ const file = join(libDir, name);+ if (!existsSync(file)) return; // skip legacy phantom names (lib.core.*, lib.es6, …)+ seen.add(name);+ const text = readFileSync(file, "utf8");+ for (const match of text.matchAll(REFERENCE)) {+ visit(`lib.${match[1]}.d.ts`);+ }+}+ENTRIES.forEach(visit);++const files = [...seen].sort();+const ident = (name) => `_${name.replace(/[.]/g, "_")}`;++let out = "// AUTO-GENERATED — do not edit by hand.\n";+out += "// The transitive closure of TypeScript lib.*.d.ts for target ES2020 + lib [\"es2020\", \"dom\"],\n";+out += "// imported as raw text so the language service works offline (no CDN). These ship in the\n";+out += "// lazy script-editor chunk.\n";+out += "//\n";+out += "// Only the file LIST is pinned here. The .d.ts CONTENT is read from the installed `typescript`\n";+out += "// at build time (via ?raw), so it tracks the installed version automatically. Re-run\n";+out += "// scripts/generate_ts_libs.mjs only if a TypeScript upgrade changes which lib files exist in\n";+out += "// this set — a removed/renamed file fails the build loudly; an added one is silently missing.\n\n";+files.forEach((name) => { out += `import ${ident(name)} from "typescript/lib/${name}?raw";\n`; });+out += "\n/** Map of vfs path (\"/lib.*.d.ts\") to file contents. */\n";+out += "export const tsLibFiles: Record<string, string> = {\n";+files.forEach((name) => { out += ` "/${name}": ${ident(name)},\n`; });+out += "};\n";++writeFileSync(OUT, out);+console.log(`Wrote ${files.length} lib files to ${OUT.pathname}`);diff --git a/packages/codemirror/src/type_completion/index.ts b/packages/codemirror/src/type_completion/index.tsindex 44b6ee9d9a..72171d8563 100644--- a/packages/codemirror/src/type_completion/index.ts+++ b/packages/codemirror/src/type_completion/index.ts@@ -37,35 +37,20 @@ const COMPILER_OPTIONS = {ignoreDeprecations: "6.0"};-// The default TypeScript lib.*.d.ts map is large and identical for every-// editor, so fetch it once per session and share it (cloned) across envs.-let libMapPromise: Promise<Map<string, string>> | null = null;--async function getLibMap(ts: typeof import("typescript")) {- const { createDefaultMapFromCDN } = await import("@typescript/vfs");- const compilerOptions = {- ...COMPILER_OPTIONS,- target: ts.ScriptTarget.ES2020,- lib: ["es2020", "dom"]- };- // cache=false: we keep the map in module memory instead of localStorage- // (Trilium avoids localStorage; in-memory sharing is enough per session).- return createDefaultMapFromCDN(compilerOptions, ts.version, false, ts);-}-async function createEnv(mime: string) {const tsModule = await import("typescript");const ts = tsModule.default ?? tsModule;const { createSystem, createVirtualTypeScriptEnvironment } = await import("@typescript/vfs");-- if (!libMapPromise) {- libMapPromise = getLibMap(ts);- }- const libMap = await libMapPromise;-- // Each editor gets its own copy so concurrent script notes (e.g. split- // view) don't clobber each other's source file.- const fsMap = new Map(libMap);+ // Dynamically imported so the bundled lib.*.d.ts text lands in the lazy+ // script-editor chunk (loaded only when a script note opens), not the editor+ // core used by every code note.+ const { tsLibFiles } = await import("./ts_lib_files.js");++ // The TypeScript lib.*.d.ts files are bundled (see ./ts_lib_files) so the+ // language service works offline — no CDN fetch. Each editor gets its own+ // map so concurrent script notes (e.g. split view) don't clobber each+ // other's source file.+ const fsMap = new Map<string, string>(Object.entries(tsLibFiles));fsMap.set(API_DTS_PATH, mime === SCRIPT_MIME_BACKEND ? backendApiDts : frontendApiDts);// Seed with a space, never an empty string: `@typescript/vfs` treats an// empty root file as "not found" (TS6053) at program creation. `tsSync`diff --git a/packages/codemirror/src/type_completion/ts_lib_files.ts b/packages/codemirror/src/type_completion/ts_lib_files.tsnew file mode 100644index 0000000000..5f0c424c7a--- /dev/null+++ b/packages/codemirror/src/type_completion/ts_lib_files.ts@@ -0,0 +1,108 @@+// AUTO-GENERATED — do not edit by hand.+// The transitive closure of TypeScript lib.*.d.ts for target ES2020 + lib ["es2020", "dom"],+// imported as raw text so the language service works offline (no CDN). These ship in the+// lazy script-editor chunk.+//+// Only the file LIST is pinned here. The .d.ts CONTENT is read from the installed `typescript`+// at build time (via ?raw), so it tracks the installed version automatically. Re-run+// scripts/generate_ts_libs.mjs only if a TypeScript upgrade changes which lib files exist in+// this set — a removed/renamed file fails the build loudly; an added one is silently missing.++import _lib_decorators_d_ts from "typescript/lib/lib.decorators.d.ts?raw";+import _lib_decorators_legacy_d_ts from "typescript/lib/lib.decorators.legacy.d.ts?raw";+import _lib_dom_d_ts from "typescript/lib/lib.dom.d.ts?raw";+import _lib_dom_iterable_d_ts from "typescript/lib/lib.dom.iterable.d.ts?raw";+import _lib_es2015_collection_d_ts from "typescript/lib/lib.es2015.collection.d.ts?raw";+import _lib_es2015_core_d_ts from "typescript/lib/lib.es2015.core.d.ts?raw";+import _lib_es2015_d_ts from "typescript/lib/lib.es2015.d.ts?raw";+import _lib_es2015_generator_d_ts from "typescript/lib/lib.es2015.generator.d.ts?raw";+import _lib_es2015_iterable_d_ts from "typescript/lib/lib.es2015.iterable.d.ts?raw";+import _lib_es2015_promise_d_ts from "typescript/lib/lib.es2015.promise.d.ts?raw";+import _lib_es2015_proxy_d_ts from "typescript/lib/lib.es2015.proxy.d.ts?raw";+import _lib_es2015_reflect_d_ts from "typescript/lib/lib.es2015.reflect.d.ts?raw";+import _lib_es2015_symbol_d_ts from "typescript/lib/lib.es2015.symbol.d.ts?raw";+import _lib_es2015_symbol_wellknown_d_ts from "typescript/lib/lib.es2015.symbol.wellknown.d.ts?raw";+import _lib_es2016_array_include_d_ts from "typescript/lib/lib.es2016.array.include.d.ts?raw";+import _lib_es2016_d_ts from "typescript/lib/lib.es2016.d.ts?raw";+import _lib_es2016_intl_d_ts from "typescript/lib/lib.es2016.intl.d.ts?raw";+import _lib_es2017_arraybuffer_d_ts from "typescript/lib/lib.es2017.arraybuffer.d.ts?raw";+import _lib_es2017_d_ts from "typescript/lib/lib.es2017.d.ts?raw";+import _lib_es2017_date_d_ts from "typescript/lib/lib.es2017.date.d.ts?raw";+import _lib_es2017_intl_d_ts from "typescript/lib/lib.es2017.intl.d.ts?raw";+import _lib_es2017_object_d_ts from "typescript/lib/lib.es2017.object.d.ts?raw";+import _lib_es2017_sharedmemory_d_ts from "typescript/lib/lib.es2017.sharedmemory.d.ts?raw";+import _lib_es2017_string_d_ts from "typescript/lib/lib.es2017.string.d.ts?raw";+import _lib_es2017_typedarrays_d_ts from "typescript/lib/lib.es2017.typedarrays.d.ts?raw";+import _lib_es2018_asyncgenerator_d_ts from "typescript/lib/lib.es2018.asyncgenerator.d.ts?raw";+import _lib_es2018_asynciterable_d_ts from "typescript/lib/lib.es2018.asynciterable.d.ts?raw";+import _lib_es2018_d_ts from "typescript/lib/lib.es2018.d.ts?raw";+import _lib_es2018_intl_d_ts from "typescript/lib/lib.es2018.intl.d.ts?raw";+import _lib_es2018_promise_d_ts from "typescript/lib/lib.es2018.promise.d.ts?raw";+import _lib_es2018_regexp_d_ts from "typescript/lib/lib.es2018.regexp.d.ts?raw";+import _lib_es2019_array_d_ts from "typescript/lib/lib.es2019.array.d.ts?raw";+import _lib_es2019_d_ts from "typescript/lib/lib.es2019.d.ts?raw";+import _lib_es2019_intl_d_ts from "typescript/lib/lib.es2019.intl.d.ts?raw";+import _lib_es2019_object_d_ts from "typescript/lib/lib.es2019.object.d.ts?raw";+import _lib_es2019_string_d_ts from "typescript/lib/lib.es2019.string.d.ts?raw";+import _lib_es2019_symbol_d_ts from "typescript/lib/lib.es2019.symbol.d.ts?raw";+import _lib_es2020_bigint_d_ts from "typescript/lib/lib.es2020.bigint.d.ts?raw";+import _lib_es2020_d_ts from "typescript/lib/lib.es2020.d.ts?raw";+import _lib_es2020_date_d_ts from "typescript/lib/lib.es2020.date.d.ts?raw";+import _lib_es2020_intl_d_ts from "typescript/lib/lib.es2020.intl.d.ts?raw";+import _lib_es2020_number_d_ts from "typescript/lib/lib.es2020.number.d.ts?raw";+import _lib_es2020_promise_d_ts from "typescript/lib/lib.es2020.promise.d.ts?raw";+import _lib_es2020_sharedmemory_d_ts from "typescript/lib/lib.es2020.sharedmemory.d.ts?raw";+import _lib_es2020_string_d_ts from "typescript/lib/lib.es2020.string.d.ts?raw";+import _lib_es2020_symbol_wellknown_d_ts from "typescript/lib/lib.es2020.symbol.wellknown.d.ts?raw";+import _lib_es5_d_ts from "typescript/lib/lib.es5.d.ts?raw";++/** Map of vfs path ("/lib.*.d.ts") to file contents. */+export const tsLibFiles: Record<string, string> = {+ "/lib.decorators.d.ts": _lib_decorators_d_ts,+ "/lib.decorators.legacy.d.ts": _lib_decorators_legacy_d_ts,+ "/lib.dom.d.ts": _lib_dom_d_ts,+ "/lib.dom.iterable.d.ts": _lib_dom_iterable_d_ts,+ "/lib.es2015.collection.d.ts": _lib_es2015_collection_d_ts,+ "/lib.es2015.core.d.ts": _lib_es2015_core_d_ts,+ "/lib.es2015.d.ts": _lib_es2015_d_ts,+ "/lib.es2015.generator.d.ts": _lib_es2015_generator_d_ts,+ "/lib.es2015.iterable.d.ts": _lib_es2015_iterable_d_ts,+ "/lib.es2015.promise.d.ts": _lib_es2015_promise_d_ts,+ "/lib.es2015.proxy.d.ts": _lib_es2015_proxy_d_ts,+ "/lib.es2015.reflect.d.ts": _lib_es2015_reflect_d_ts,+ "/lib.es2015.symbol.d.ts": _lib_es2015_symbol_d_ts,+ "/lib.es2015.symbol.wellknown.d.ts": _lib_es2015_symbol_wellknown_d_ts,+ "/lib.es2016.array.include.d.ts": _lib_es2016_array_include_d_ts,+ "/lib.es2016.d.ts": _lib_es2016_d_ts,+ "/lib.es2016.intl.d.ts": _lib_es2016_intl_d_ts,+ "/lib.es2017.arraybuffer.d.ts": _lib_es2017_arraybuffer_d_ts,+ "/lib.es2017.d.ts": _lib_es2017_d_ts,+ "/lib.es2017.date.d.ts": _lib_es2017_date_d_ts,+ "/lib.es2017.intl.d.ts": _lib_es2017_intl_d_ts,+ "/lib.es2017.object.d.ts": _lib_es2017_object_d_ts,+ "/lib.es2017.sharedmemory.d.ts": _lib_es2017_sharedmemory_d_ts,+ "/lib.es2017.string.d.ts": _lib_es2017_string_d_ts,+ "/lib.es2017.typedarrays.d.ts": _lib_es2017_typedarrays_d_ts,+ "/lib.es2018.asyncgenerator.d.ts": _lib_es2018_asyncgenerator_d_ts,+ "/lib.es2018.asynciterable.d.ts": _lib_es2018_asynciterable_d_ts,+ "/lib.es2018.d.ts": _lib_es2018_d_ts,+ "/lib.es2018.intl.d.ts": _lib_es2018_intl_d_ts,+ "/lib.es2018.promise.d.ts": _lib_es2018_promise_d_ts,+ "/lib.es2018.regexp.d.ts": _lib_es2018_regexp_d_ts,+ "/lib.es2019.array.d.ts": _lib_es2019_array_d_ts,+ "/lib.es2019.d.ts": _lib_es2019_d_ts,+ "/lib.es2019.intl.d.ts": _lib_es2019_intl_d_ts,+ "/lib.es2019.object.d.ts": _lib_es2019_object_d_ts,+ "/lib.es2019.string.d.ts": _lib_es2019_string_d_ts,+ "/lib.es2019.symbol.d.ts": _lib_es2019_symbol_d_ts,+ "/lib.es2020.bigint.d.ts": _lib_es2020_bigint_d_ts,+ "/lib.es2020.d.ts": _lib_es2020_d_ts,+ "/lib.es2020.date.d.ts": _lib_es2020_date_d_ts,+ "/lib.es2020.intl.d.ts": _lib_es2020_intl_d_ts,+ "/lib.es2020.number.d.ts": _lib_es2020_number_d_ts,+ "/lib.es2020.promise.d.ts": _lib_es2020_promise_d_ts,+ "/lib.es2020.sharedmemory.d.ts": _lib_es2020_sharedmemory_d_ts,+ "/lib.es2020.string.d.ts": _lib_es2020_string_d_ts,+ "/lib.es2020.symbol.wellknown.d.ts": _lib_es2020_symbol_wellknown_d_ts,+ "/lib.es5.d.ts": _lib_es5_d_ts,+};