@xaendar/language-core 0.9.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,89 @@
1
+ import { CompilerOptions, LanguageService } from 'typescript';
2
+ import { TypeCheckResult } from '@xaendar/compiler';
3
+
4
+ /**
5
+ * Returns the shared LanguageService, creating it (or recreating it) as
6
+ * needed. If the given compilerOptions differ from the ones the current
7
+ * LanguageService was created with, the old instance is disposed and a
8
+ * fresh one is created — this covers the case where vite.config / tsconfig
9
+ * changes while the dev server is running.
10
+ */
11
+ declare function getLanguageService(compilerOptions: CompilerOptions): LanguageService;
12
+ /**
13
+ * Disposes the current LanguageService and clears all associated state
14
+ * (virtual files, real file version cache, known root files). Call this
15
+ * from `configureServer` -> `httpServer.on('close', ...)`, or from
16
+ * `closeBundle`, to avoid leaking a stale instance across server restarts.
17
+ */
18
+ declare function disposeLanguageService(): void;
19
+ /**
20
+ * Registers a real project file as a root file for the Program, so the
21
+ * LanguageService's module resolver can find it when a virtual shim
22
+ * imports the corresponding component class.
23
+ */
24
+ declare function registerRealFile(fileName: string): void;
25
+ /**
26
+ * Removes a real project file, e.g. when its source DSL file is deleted from
27
+ * the project or no longer matches the plugin.
28
+ */
29
+ declare function removeRealFile(filename: string): void;
30
+ /**
31
+ * Updates (or creates) the virtual shim for the given path, bumping its
32
+ * version only if the content actually changed — this avoids invalidating
33
+ * the LanguageService's internal cache for no-op writes.
34
+ */
35
+ declare function upsertVirtualFile(virtualPath: string, content: string): void;
36
+ /**
37
+ * Removes a virtual shim, e.g. when its source DSL file is deleted from
38
+ * the project or no longer matches the plugin.
39
+ */
40
+ declare function removeVirtualFile(virtualPath: string): void;
41
+ /**
42
+ * Extracts the class name from the Babel-transpiled JS source.
43
+
44
+ *
45
+ * Babel always emits `class ClassName extends ...` so this is safe to match.
46
+ * Used to generate a unique name for the per-class CSSStyleSheet variable
47
+ * that must be declared outside the class body to guarantee it exists before
48
+ * `connectedCallback` fires.
49
+ *
50
+ * @param jsSource - The Babel-transpiled JS source of the component.
51
+ * @returns The class name, or `__Component` as a safe fallback.
52
+ */
53
+ declare function extractClassName(jsSource: string): string;
54
+
55
+ /**
56
+ * Loads the `tsconfig.json` compilerOptions applicable to the given
57
+ * directory, using TypeScript's standard config file resolution
58
+ * (`findConfigFile` walks up parent directories). Falls back to an empty
59
+ * options object if no config file is found, rather than throwing —
60
+ * type checking simply runs with default settings in that case.
61
+ *
62
+ * @param fromDir - Directory to start searching for `tsconfig.json` from,
63
+ * typically the directory of the component file being transformed.
64
+ */
65
+ declare function loadCompilerOptions(fromDir: string): CompilerOptions;
66
+
67
+ declare function createShim(componentData: Map<string, string[]>, typecheckBody: TypeCheckResult): {
68
+ code: string;
69
+ bodyLineOffset: number;
70
+ path: string;
71
+ classNames: string[];
72
+ };
73
+ /**
74
+ * Extracts the `templateUrl` and `styleUrl` values from the `@WebComponent`
75
+ * decorator in the component source and resolves them to absolute paths.
76
+ *
77
+ * @param jsSource - The raw TypeScript source of the component file.
78
+ * @param componentDir - The directory containing the component file, used as
79
+ * base for resolving relative decorator paths.
80
+ * @returns An object with the resolved `templatePath` and `stylePath`.
81
+ * Either field may be `undefined` when the corresponding decorator
82
+ * property is absent.
83
+ */
84
+ declare function extractDecoratorPaths(jsSource: string, componentDir: string): {
85
+ templatePath?: string;
86
+ stylePath?: string;
87
+ };
88
+
89
+ export { createShim, disposeLanguageService, extractClassName, extractDecoratorPaths, getLanguageService, loadCompilerOptions, registerRealFile, removeRealFile, removeVirtualFile, upsertVirtualFile };
@@ -0,0 +1,2 @@
1
+ import{statSync as v}from"fs";import{createDocumentRegistry as S,createLanguageService as F,getDefaultLibFilePath as C,ScriptSnapshot as h,sys as s}from"typescript";var i=new Map,p=new Map,l=new Set,a,c;function A(e){let t=c&&JSON.stringify(c)!==JSON.stringify(e);return a&&t&&O(),c=e,a??=F(L(()=>[...l]),S()),a}function L(e){return{getScriptFileNames:()=>[...e(),...i.keys()],getScriptVersion(t){let r=i.get(t);return r?String(r.version):b(t)},getScriptSnapshot(t){let r=i.get(t);if(r)return h.fromString(r.content);if(!s.fileExists(t))return;let n=s.readFile(t);if(n!==void 0)return h.fromString(n)},getCurrentDirectory:()=>process.cwd(),getCompilationSettings:()=>c??{},getDefaultLibFileName:t=>C(t),fileExists(t){return i.has(t)||s.fileExists(t)},readFile(t){let r=i.get(t);return r?r.content:s.readFile(t)},readDirectory:s.readDirectory,directoryExists:s.directoryExists,getDirectories:s.getDirectories,realpath:s.realpath}}function O(){a.dispose(),a=void 0,c=void 0,i.clear(),p.clear(),l.clear()}function H(e){l.add(e)}function J(e){l.delete(e)}function b(e){try{let t=v(e),r=String(t.mtimeMs);return p.set(e,r),r}catch{return p.get(e)??"0"}}function y(e,t){let r=i.get(e);r?.content!==t&&i.set(e,{content:t,version:(r?.version??0)+1})}function T(e){i.delete(e)}function N(e){return e.match(/class\s+(\w+)\s+extends/)?.[1]??"__Component"}import{dirname as E}from"path";import{findConfigFile as V,sys as f,readConfigFile as w,parseJsonConfigFileContent as P}from"typescript";function I(e){let t=V(e,f.fileExists,"tsconfig.json");if(!t)return{};let r=w(t,f.readFile);return P(r.config,f,E(t)).options}import{basename as D,extname as R,resolve as x}from"path";function X(e,t){let r=Array.from(e.entries()),n=`${r[0][0]}.__typecheck__.ts`,o=_(r,n,t.text);return y(n,o.code),o}function _(e,t,r){let n=e.length+3,o=new Array(n).fill(""),u=new Array;for(let g=0;g<e.length;g++){let[d,m]=e[g];u.push(...m),o[g]=`import { ${m.join(", ")} } from './${D(d,R(d))}';`}return o[n-2]=`declare const root: ${u.join(" & ")};`,{code:[...o,r].join(`
2
+ `),bodyLineOffset:n,path:t,classNames:u}}function Y(e,t){let r=e.match(/templateUrl\s*:\s*["'](.+?)["']/)?.[1],n=e.match(/styleUrl\s*:\s*["'](.+?)["']/)?.[1];return{templatePath:r?x(t,r):void 0,stylePath:n?x(t,n):void 0}}export{X as createShim,O as disposeLanguageService,N as extractClassName,Y as extractDecoratorPaths,A as getLanguageService,I as loadCompilerOptions,H as registerRealFile,J as removeRealFile,T as removeVirtualFile,y as upsertVirtualFile};
package/package.json ADDED
@@ -0,0 +1,21 @@
1
+ {
2
+ "name": "@xaendar/language-core",
3
+ "version": "0.9.18",
4
+ "description": "A library containing common functionalities for build tools and language service",
5
+ "author": "Kaitenjo",
6
+ "license": "MIT",
7
+ "type": "module",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/language-core.d.ts",
11
+ "import": "./dist/language-core.js",
12
+ "require": "./dist/language-core.js"
13
+ }
14
+ },
15
+ "main": "./dist/language-core.js",
16
+ "types": "./dist/language-core.d.ts",
17
+ "dependencies": {
18
+ "@xaendar/compiler": "0.9.18",
19
+ "typescript": "^6.0.3"
20
+ }
21
+ }