From ed967b65f75bbb2c20d8d28f4fe1cf074c0978b1 Mon Sep 17 00:00:00 2001 From: Johnson Chu Date: Tue, 27 Feb 2024 22:25:55 +0800 Subject: [PATCH] feat: webpack compatibility (#144) --- packages/language-server/node.ts | 11 ++++++--- packages/vscode/lib/features/tsVersion.ts | 30 +++++++++++------------ 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/packages/language-server/node.ts b/packages/language-server/node.ts index 7fe07af8..ecf6d3e9 100644 --- a/packages/language-server/node.ts +++ b/packages/language-server/node.ts @@ -87,6 +87,9 @@ export function createServer(connection: vscode.Connection) { export function loadTsdkByPath(tsdk: string, locale: string | undefined) { + // webpack compatibility + const _require: NodeRequire = eval('require'); + return { typescript: loadLib(), diagnosticMessages: loadLocalizedDiagnosticMessages(), @@ -95,13 +98,13 @@ export function loadTsdkByPath(tsdk: string, locale: string | undefined) { function loadLib(): typeof import('typescript') { for (const name of ['./typescript.js', './tsserverlibrary.js']) { try { - return require(require.resolve(name, { paths: [tsdk] })); + return _require(_require.resolve(name, { paths: [tsdk] })); } catch { } } // for bun for (const name of ['typescript.js', 'tsserverlibrary.js']) { try { - return require(tsdk + '/' + name); + return _require(tsdk + '/' + name); } catch { } } throw new Error(`Can't find typescript.js or tsserverlibrary.js in ${JSON.stringify(tsdk)}`); @@ -109,8 +112,8 @@ export function loadTsdkByPath(tsdk: string, locale: string | undefined) { function loadLocalizedDiagnosticMessages(): import('typescript').MapLike | undefined { try { - const path = require.resolve(`./${locale}/diagnosticMessages.generated.json`, { paths: [tsdk] }); - return require(path); + const path = _require.resolve(`./${locale}/diagnosticMessages.generated.json`, { paths: [tsdk] }); + return _require(path); } catch { } } } diff --git a/packages/vscode/lib/features/tsVersion.ts b/packages/vscode/lib/features/tsVersion.ts index 9891c38d..875004c7 100644 --- a/packages/vscode/lib/features/tsVersion.ts +++ b/packages/vscode/lib/features/tsVersion.ts @@ -42,12 +42,12 @@ export function activate( }, useConfigWorkspaceTsdk: configTsdkPath && !vscodeTsdk.isWeb ? { label: (tsdk.isWorkspacePath ? '• ' : '') + 'Use Workspace Version', - description: await getTsVersion(resolveWorkspaceTsdk(configTsdkPath) ?? '/') ?? 'Could not load the TypeScript version at this path', + description: await getTsVersion(await resolveWorkspaceTsdk(configTsdkPath) ?? '/') ?? 'Could not load the TypeScript version at this path', detail: configTsdkPath, } : undefined, useDefaultWorkspaceTsdk: configTsdkPath !== defaultTsdkPath && !vscodeTsdk.isWeb ? { label: (tsdk.isWorkspacePath ? '• ' : '') + 'Use Workspace Version', - description: await getTsVersion(resolveWorkspaceTsdk(defaultTsdkPath) ?? '/') ?? 'Could not load the TypeScript version at this path', + description: await getTsVersion(await resolveWorkspaceTsdk(defaultTsdkPath) ?? '/') ?? 'Could not load the TypeScript version at this path', detail: defaultTsdkPath, } : undefined, }, @@ -101,7 +101,7 @@ export function activate( export async function getTsdk(context: vscode.ExtensionContext) { if (isUseWorkspaceTsdk(context)) { - const resolvedTsdk = resolveWorkspaceTsdk(getConfigTsdkPath() || defaultTsdkPath); + const resolvedTsdk = await resolveWorkspaceTsdk(getConfigTsdkPath() || defaultTsdkPath); if (resolvedTsdk) { return { tsdk: resolvedTsdk, @@ -118,22 +118,22 @@ export async function getTsdk(context: vscode.ExtensionContext) { }; } -function resolveWorkspaceTsdk(tsdk: string) { +async function resolveWorkspaceTsdk(tsdk: string) { if (path.isAbsolute(tsdk)) { - try { - if (require.resolve('./typescript.js', { paths: [tsdk] })) { - return tsdk; - } - } catch { } + const libUri = vscode.Uri.joinPath(vscode.Uri.file(tsdk), 'typescript.js'); + const stat = await vscode.workspace.fs.stat(libUri); + if (stat.type === vscode.FileType.File) { + return tsdk; + } } - if (vscode.workspace.workspaceFolders) { + else if (vscode.workspace.workspaceFolders) { for (const folder of vscode.workspace.workspaceFolders) { const tsdkPath = path.join(folder.uri.fsPath.replace(/\\/g, '/'), tsdk); - try { - if (require.resolve('./typescript.js', { paths: [tsdkPath] })) { - return tsdkPath; - } - } catch { } + const libUri = vscode.Uri.joinPath(vscode.Uri.file(tsdkPath), 'typescript.js'); + const stat = await vscode.workspace.fs.stat(libUri); + if (stat.type === vscode.FileType.File) { + return tsdkPath; + } } } }