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(language-server): Dynamically download TypeScript from CDN on Web #71

Merged
merged 7 commits into from
Oct 30, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .npmrc

This file was deleted.

21 changes: 17 additions & 4 deletions packages/language-server/src/browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,25 @@ export function startLanguageServer(connection: vscode.Connection, ...plugins: L
return { dispose: () => clearTimeout(handle) };
},
},
loadTypescript() {
return require('typescript'); // force bundle because not support load by user config in web
async loadTypeScript(options) {
const tsdkUri = options.typescript?.tsdkUrl;
if (!tsdkUri) {
return;
}
const _module = globalThis.module;
globalThis.module = { exports: {} } as typeof _module;
await import(`${tsdkUri}/lib/typescript.js`);
const ts = globalThis.module.exports;
globalThis.module = _module;
return ts as typeof import('typescript/lib/tsserverlibrary');
},
async loadTypescriptLocalized(tsdk, locale) {
async loadTypeScriptLocalized(options, locale) {
const tsdkUri = options.typescript?.tsdkUrl;
if (!tsdkUri) {
return;
}
try {
const uri = fileNameToUri(`${tsdk}/${locale}/diagnosticMessages.generated.json`);
const uri = fileNameToUri(`${tsdkUri}/${locale}/diagnosticMessages.generated.json`);
const json = await httpSchemaRequestHandler(uri);
if (json) {
return JSON.parse(json);
Expand Down
16 changes: 9 additions & 7 deletions packages/language-server/src/common/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ export function startCommonLanguageServer(connection: vscode.Connection, _plugin
let plugins: ReturnType<LanguageServerPlugin>[];
let documents: ReturnType<typeof createDocuments>;
let context: ServerContext;
let ts: typeof import('typescript/lib/tsserverlibrary') | undefined;
let tsLocalized: {} | undefined;
let cancelTokenHost: ReturnType<typeof createCancellationTokenHost>;

const didChangeWatchedFilesCallbacks = new Set<vscode.NotificationHandler<vscode.DidChangeWatchedFilesParams>>();

Expand Down Expand Up @@ -59,9 +62,12 @@ export function startCommonLanguageServer(connection: vscode.Connection, _plugin
},
},
};
plugins = context.server.plugins.map(plugin => plugin(options, {
typescript: options.typescript ? context.server.runtimeEnv.loadTypescript(options.typescript.tsdk) : undefined
}));
ts = await context.server.runtimeEnv.loadTypeScript(options);
tsLocalized = initParams.locale
? await context.server.runtimeEnv.loadTypeScriptLocalized(options, initParams.locale)
: undefined;
cancelTokenHost = createCancellationTokenHost(options.cancellationPipeName);
plugins = context.server.plugins.map(plugin => plugin(options, { typescript: ts }));
documents = createDocuments(context.server.runtimeEnv, connection);

if (options.l10n) {
Expand Down Expand Up @@ -232,10 +238,6 @@ export function startCommonLanguageServer(connection: vscode.Connection, _plugin

async function createLanguageServiceHost() {

const ts = options.typescript ? context.server.runtimeEnv.loadTypescript(options.typescript.tsdk) : undefined;
const tsLocalized = options.typescript && initParams.locale ? await context.server.runtimeEnv.loadTypescriptLocalized(options.typescript.tsdk, initParams.locale) : undefined;
const cancelTokenHost = createCancellationTokenHost(options.cancellationPipeName);

workspaces = createWorkspaces({
...context,
workspaces: {
Expand Down
12 changes: 10 additions & 2 deletions packages/language-server/src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ export function startLanguageServer(connection: vscode.Connection, ...plugins: L
return { dispose: () => clearImmediate(handle) };
},
},
loadTypescript(tsdk) {
loadTypeScript(options) {
const tsdk = options.typescript?.tsdk;
if (!tsdk) {
return;
}
for (const name of ['./typescript.js', './tsserverlibrary.js']) {
try {
return require(require.resolve(name, { paths: [tsdk] }));
Expand All @@ -108,7 +112,11 @@ export function startLanguageServer(connection: vscode.Connection, ...plugins: L

throw new Error(`Can't find typescript.js or tsserverlibrary.js in ${tsdk}`);
},
async loadTypescriptLocalized(tsdk, locale) {
async loadTypeScriptLocalized(options, locale) {
const tsdk = options.typescript?.tsdk;
if (!tsdk) {
return;
}
try {
const path = require.resolve(`./${locale}/diagnosticMessages.generated.json`, { paths: [tsdk] });
return require(path);
Expand Down
13 changes: 10 additions & 3 deletions packages/language-server/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export interface Timer {
export interface RuntimeEnvironment {
uriToFileName(uri: string): string;
fileNameToUri(fileName: string): string;
loadTypescript(tsdk: string): typeof import('typescript/lib/tsserverlibrary');
loadTypescriptLocalized(tsdk: string, locale: string): Promise<{} | undefined>;
loadTypeScript(options: InitializationOptions): Promise<typeof import('typescript/lib/tsserverlibrary') | undefined>;
loadTypeScriptLocalized(options: InitializationOptions, locale: string): Promise<{} | undefined>;
fs: FileSystem;
// https://github.com/microsoft/vscode/blob/7927075f89db213bc6e2182fa684d514d69e2359/extensions/html-language-features/server/src/htmlServer.ts#L53-L56
timer: Timer;
Expand Down Expand Up @@ -53,9 +53,16 @@ export enum DiagnosticModel {
export interface InitializationOptions {
typescript?: {
/**
* Absolute path to node_modules/typescript/lib
* Absolute path to node_modules/typescript/lib, available for node
*/
tsdk: string;
/**
* URI to node_modules/typescript/lib, available for web
* @example "https://cdn.jsdelivr.net/npm/typescript"
* @example "https://cdn.jsdelivr.net/npm/typescript@latest"
* @example "https://cdn.jsdelivr.net/npm/typescript@5.0.0"
*/
tsdkUrl: string;
};
l10n?: {
location: string; // uri
Expand Down
5 changes: 4 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.