diff --git a/src/webServer/webServer.ts b/src/webServer/webServer.ts index 31f0a47915bf8..03030b4d9574b 100644 --- a/src/webServer/webServer.ts +++ b/src/webServer/webServer.ts @@ -1,4 +1,6 @@ /*@internal*/ +/// + namespace ts.server { export interface HostWithWriteMessage { writeMessage(s: any): void; @@ -136,7 +138,34 @@ namespace ts.server { clearImmediate: handle => clearTimeout(handle), /* eslint-enable no-restricted-globals */ - require: () => ({ module: undefined, error: new Error("Not implemented") }), + require: (initialPath: string, moduleName: string) => { + // TODO: figure out how to implement this without having to implement + // all of 'require' + + // TODO: Path is currently hardcoded + const fullPath = initialPath + "/" + moduleName + "/lib/index.js"; + if (fullPath.startsWith("http://") || fullPath.startsWith("https://")) { + try { + // Sync load the script into the current scope + (globalThis as any).importScripts(fullPath); + } + catch (e) { + return ({ module: undefined, error: new Error(`Could not evaluate module ${e}`) }); + } + + // Try to get export from global. Use the module name for this. + // Not sure a better way to do this synchronously + const module = (globalThis as any)[moduleName]; + if (module) { + return ({ module, error: undefined }); + } + else { + return ({ module: undefined, error: new Error(`Could not find module export for ${moduleName}`) }); + } + } + + return ({ module: undefined, error: new Error("Could not resolve module") }); + }, exit: notImplemented, // Debugging related