Skip to content

Commit ba3586a

Browse files
committed
Prototyping TS Server plugins on web
This is a first exploration for microsoft#47376 microsoft/vscode#140455 It implements a very basic version of `require` for the web that lets us load plugins with a very specific structure. `require` being sync limits our options here, so I'm using `importScripts` plus a global to get the exports of a plugin. The plugin must also be compiled down to a single file at the moment
1 parent 852b1c2 commit ba3586a

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

src/webServer/webServer.ts

+30-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
/*@internal*/
2+
/// <reference lib="dom" />
3+
24
namespace ts.server {
35
export interface HostWithWriteMessage {
46
writeMessage(s: any): void;
@@ -136,7 +138,34 @@ namespace ts.server {
136138
clearImmediate: handle => clearTimeout(handle),
137139
/* eslint-enable no-restricted-globals */
138140

139-
require: () => ({ module: undefined, error: new Error("Not implemented") }),
141+
require: (initialPath: string, moduleName: string) => {
142+
// TODO: figure out how to implement this without having to implement
143+
// all of 'require'
144+
145+
// TODO: Path is currently hardcoded
146+
const fullPath = initialPath + "/" + moduleName + "/lib/index.js";
147+
if (fullPath.startsWith("http://") || fullPath.startsWith("https://")) {
148+
try {
149+
// Sync load the script into the current scope
150+
(globalThis as any).importScripts(fullPath);
151+
}
152+
catch (e) {
153+
return ({ module: undefined, error: new Error(`Could not evaluate module ${e}`) });
154+
}
155+
156+
// Try to get export from global. Use the module name for this.
157+
// Not sure a better way to do this synchronously
158+
const module = (globalThis as any)[moduleName];
159+
if (module) {
160+
return ({ module, error: undefined });
161+
}
162+
else {
163+
return ({ module: undefined, error: new Error(`Could not find module export for ${moduleName}`) });
164+
}
165+
}
166+
167+
return ({ module: undefined, error: new Error("Could not resolve module") });
168+
},
140169
exit: notImplemented,
141170

142171
// Debugging related

0 commit comments

Comments
 (0)