Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: webpack compatibility #144

Merged
merged 2 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
32 changes: 16 additions & 16 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 tsdkPath = path.join(folder.uri.fsPath, tsdk);
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