Skip to content

Commit

Permalink
feat: webpack compatibility (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Feb 27, 2024
1 parent 09e1792 commit ed967b6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
11 changes: 7 additions & 4 deletions packages/language-server/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -95,22 +98,22 @@ 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)}`);
}

function loadLocalizedDiagnosticMessages(): import('typescript').MapLike<string> | 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 { }
}
}
Expand Down
30 changes: 15 additions & 15 deletions packages/vscode/lib/features/tsVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down Expand Up @@ -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,
Expand All @@ -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;
}
}
}
}
Expand Down

0 comments on commit ed967b6

Please sign in to comment.