|
1 | 1 | /*@internal*/
|
| 2 | +/// <reference lib="dom" /> |
| 3 | + |
2 | 4 | namespace ts.server {
|
3 | 5 | export interface HostWithWriteMessage {
|
4 | 6 | writeMessage(s: any): void;
|
@@ -136,7 +138,34 @@ namespace ts.server {
|
136 | 138 | clearImmediate: handle => clearTimeout(handle),
|
137 | 139 | /* eslint-enable no-restricted-globals */
|
138 | 140 |
|
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 | + }, |
140 | 169 | exit: notImplemented,
|
141 | 170 |
|
142 | 171 | // Debugging related
|
|
0 commit comments