Skip to content

Commit bd8d19a

Browse files
committed
feat: allow external typescript compiler
1 parent 65ff63d commit bd8d19a

File tree

11 files changed

+175
-62
lines changed

11 files changed

+175
-62
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ compact_str = "0.8.0"
1616
cssparser = "0.34.0"
1717
cssparser-color = "0.2.0"
1818
futures = "0.3.31"
19-
glass-easel-template-compiler = "0.15.1"
19+
glass-easel-template-compiler = "0.15.2"
2020
itertools = "0.13.0"
2121
log = "0.4.22"
2222
lsp-server = "0.7.7"

pnpm-lock.yaml

Lines changed: 38 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pnpm-workspace.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
packages:
22
- 'vscode-extension'
3+
- 'ts-server-bundled'

ts-server-bundled/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"private": true,
3+
"devDependencies": {
4+
"typescript": "latest"
5+
}
6+
}

vscode-extension/package.json

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,23 @@
108108
"type": "boolean",
109109
"default": false,
110110
"description": "Also analyze any CSS/LESS/SCSS file inside a project if corresponding WXML file can be found."
111+
},
112+
"glass-easel-analyzer.preferredTypescriptVersion": {
113+
"scope": "window",
114+
"type": "string",
115+
"enum": [
116+
"local",
117+
"built-in",
118+
"disabled"
119+
],
120+
"default": "local",
121+
"description": "The TypeScript compiler module to use. Set to `disabled` will disable TypeScript-related features."
122+
},
123+
"glass-easel-analyzer.localTypescriptNodeModulePath": {
124+
"scope": "window",
125+
"type": "string",
126+
"default": "",
127+
"description": "The path to the `typescript` node module, used to find the TypeScript compiler (default to auto-detect)."
111128
}
112129
}
113130
},
@@ -146,11 +163,11 @@
146163
"eslint-plugin-import": "^2.32.0",
147164
"eslint-plugin-prettier": "^5.5.4",
148165
"eslint-plugin-promise": "^7.2.1",
149-
"glass-easel": "^0.15.1",
150-
"glass-easel-miniprogram-adapter": "^0.15.1",
151-
"glass-easel-miniprogram-template": "^0.15.1",
152-
"glass-easel-miniprogram-typescript": "^0.15.1",
153-
"glass-easel-template-compiler": "^0.15.1",
166+
"glass-easel": "^0.15.2",
167+
"glass-easel-miniprogram-adapter": "^0.15.2",
168+
"glass-easel-miniprogram-template": "^0.15.2",
169+
"glass-easel-miniprogram-typescript": "^0.15.2",
170+
"glass-easel-template-compiler": "^0.15.2",
154171
"prettier": "^3.6.2",
155172
"ts-loader": "^9.5.1",
156173
"ts-node": "^10.9.2",

vscode-extension/src/client.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
import fs from 'node:fs'
2-
import path from 'node:path'
32
import * as vscode from 'vscode'
43
import {
54
type Executable,
65
LanguageClient,
76
type LanguageClientOptions,
87
} from 'vscode-languageclient/node'
98
import { middleware, updateInlineWxsScripts } from './middleware'
10-
import { TsService } from './typescript'
9+
import { TsServiceHost } from './typescript'
10+
import { resolveRelativePath } from './utils'
1111

1212
export type ClientOptions = {
1313
serverPath: string
1414
backendConfigPath: string
1515
ignorePaths: string[]
1616
analyzeOtherStylesheets: boolean
17+
preferredTypescriptVersion: string
18+
localTypescriptNodeModulePath: string
1719
}
1820

1921
export class Client {
20-
options: ClientOptions
21-
client: LanguageClient | null = null
22+
private options: ClientOptions
23+
private client: LanguageClient | null = null
24+
private tsServerHost: TsServiceHost | null = null
2225

2326
constructor(options: ClientOptions) {
2427
this.options = options
@@ -45,17 +48,12 @@ export class Client {
4548
: (vscode.workspace.workspaceFolders?.[0]?.uri ?? vscode.Uri.file(process.cwd()))
4649
}
4750

48-
private resolveRelativePath(homeUri: vscode.Uri, p: string): vscode.Uri {
49-
const uri = path.isAbsolute(p) ? vscode.Uri.file(p) : vscode.Uri.joinPath(homeUri, p)
50-
return uri
51-
}
52-
5351
async start() {
5452
let backendConfig = ''
5553
const homeUri = this.getHomeUri()
5654
const backendConfigPath = this.getBackendConfigPath()
5755
const backendConfigUrl = backendConfigPath
58-
? this.resolveRelativePath(homeUri, backendConfigPath)
56+
? resolveRelativePath(homeUri, backendConfigPath)
5957
: vscode.Uri.file(`${__dirname}/web.toml`)
6058
try {
6159
backendConfig = new TextDecoder().decode(await vscode.workspace.fs.readFile(backendConfigUrl))
@@ -67,7 +65,7 @@ export class Client {
6765
}
6866
const workspaceFolders = vscode.workspace.workspaceFolders?.map((x) => x.uri.toString()) ?? []
6967
const ignorePaths = this.options.ignorePaths.map((x) =>
70-
this.resolveRelativePath(homeUri, x).toString(),
68+
resolveRelativePath(homeUri, x).toString(),
7169
)
7270
const command = this.getServerPath()
7371
const args: string[] = []
@@ -126,13 +124,16 @@ export class Client {
126124
this.client.onNotification('glassEaselAnalyzer/inlineWxsScripts', (msg) => {
127125
updateInlineWxsScripts(msg)
128126
})
127+
this.tsServerHost = new TsServiceHost(homeUri, this.options)
129128
this.client.onNotification('glassEaselAnalyzer/discoveredProject', (msg: { path: string }) => {
130-
TsService.initTsService(msg.path)
129+
this.tsServerHost?.initTsService(msg.path)
131130
})
132131
await this.client.start()
133132
}
134133

135134
async stop() {
135+
this.tsServerHost?.destroy()
136+
this.tsServerHost = null
136137
await this.client?.stop()
137138
}
138139
}

vscode-extension/src/extension.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,20 @@ const startLanguageServer = async () => {
1616
const analyzeOtherStylesheets = vscode.workspace
1717
.getConfiguration('glass-easel-analyzer')
1818
.get('analyzeOtherStylesheets') as boolean
19-
const options = { serverPath, backendConfigPath, ignorePaths, analyzeOtherStylesheets }
19+
const preferredTypescriptVersion = vscode.workspace
20+
.getConfiguration('glass-easel-analyzer')
21+
.get('preferredTypescriptVersion') as string
22+
const localTypescriptNodeModulePath = vscode.workspace
23+
.getConfiguration('glass-easel-analyzer')
24+
.get('localTypescriptNodeModulePath') as string
25+
const options = {
26+
serverPath,
27+
backendConfigPath,
28+
ignorePaths,
29+
analyzeOtherStylesheets,
30+
preferredTypescriptVersion,
31+
localTypescriptNodeModulePath,
32+
}
2033
languageServer = new Client(options)
2134
await languageServer.start()
2235
}
@@ -41,7 +54,9 @@ export async function activate(context: vscode.ExtensionContext) {
4154
ev.affectsConfiguration('glass-easel-analyzer.serverPath') ||
4255
ev.affectsConfiguration('glass-easel-analyzer.backendConfigurationPath') ||
4356
ev.affectsConfiguration('glass-easel-analyzer.ignorePaths') ||
44-
ev.affectsConfiguration('glass-easel-analyzer.analyzeOtherStylesheets')
57+
ev.affectsConfiguration('glass-easel-analyzer.analyzeOtherStylesheets') ||
58+
ev.affectsConfiguration('glass-easel-analyzer.preferredTypescriptVersion') ||
59+
ev.affectsConfiguration('glass-easel-analyzer.localTypescriptNodeModulePath')
4560
if (changed) {
4661
// eslint-disable-next-line promise/catch-or-return
4762
vscode.window

0 commit comments

Comments
 (0)