Skip to content
Prev Previous commit
Next Next commit
add tsserverWeb to patch in dynamic import
  • Loading branch information
rbuckton committed May 4, 2022
commit 85e0fcad540d97609b862c25db7fc7bec340a8e7
16 changes: 13 additions & 3 deletions Gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,20 +214,29 @@ task("watch-services").flags = {
" --built": "Compile using the built version of the compiler."
};

const buildServer = () => buildProject("src/tsserver", cmdLineOptions);
const buildServerWeb = () => buildProject("src/tsserverWeb", cmdLineOptions);
task("tsserverWeb", buildServerWeb);

const buildServerMain = () => buildProject("src/tsserver", cmdLineOptions);
const buildServer = series(buildServerWeb, buildServerMain);
buildServer.displayName = "buildServer";
task("tsserver", series(preBuild, buildServer));
task("tsserver").description = "Builds the language server";
task("tsserver").flags = {
" --built": "Compile using the built version of the compiler."
};

const cleanServer = () => cleanProject("src/tsserver");
const cleanServerWeb = () => cleanProject("src/tsserverWeb");
const cleanServerMain = () => cleanProject("src/tsserver");
const cleanServer = series(cleanServerWeb, cleanServerMain);
cleanServer.displayName = "cleanServer";
cleanTasks.push(cleanServer);
task("clean-tsserver", cleanServer);
task("clean-tsserver").description = "Cleans outputs for the language server";

const watchServerWeb = () => watchProject("src/tsserverWeb", cmdLineOptions);
const watchServer = () => watchProject("src/tsserver", cmdLineOptions);
task("watch-tsserver", series(preBuild, parallel(watchLib, watchDiagnostics, watchServer)));
task("watch-tsserver", series(preBuild, parallel(watchLib, watchDiagnostics, watchServerWeb, watchServer)));
task("watch-tsserver").description = "Watch for changes and rebuild the language server only";
task("watch-tsserver").flags = {
" --built": "Compile using the built version of the compiler."
Expand Down Expand Up @@ -549,6 +558,7 @@ const produceLKG = async () => {
"built/local/typescriptServices.js",
"built/local/typescriptServices.d.ts",
"built/local/tsserver.js",
"built/local/tsserverWeb.js",
"built/local/typescript.js",
"built/local/typescript.d.ts",
"built/local/tsserverlibrary.js",
Expand Down
1 change: 1 addition & 0 deletions scripts/produceLKG.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ async function copyScriptOutputs() {
await copyWithCopyright("cancellationToken.js");
await copyWithCopyright("tsc.release.js", "tsc.js");
await copyWithCopyright("tsserver.js");
await copyWithCopyright("tsserverWeb.js");
await copyFromBuiltLocal("tsserverlibrary.js"); // copyright added by build
await copyFromBuiltLocal("typescript.js"); // copyright added by build
await copyFromBuiltLocal("typescriptServices.js"); // copyright added by build
Expand Down
1 change: 1 addition & 0 deletions src/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
{ "path": "./watchGuard" },
{ "path": "./debug" },
{ "path": "./cancellationToken" },
{ "path": "./tsserverWeb" },
{ "path": "./testRunner" }
]
}
16 changes: 16 additions & 0 deletions src/tsserverWeb/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"extends": "../tsconfig-library-base",
"compilerOptions": {
"outDir": "../../built/local",
"rootDir": ".",
"target": "esnext",
Comment thread
sandersn marked this conversation as resolved.
"module": "esnext",
"lib": ["esnext"],
"declaration": false,
"sourceMap": true,
"tsBuildInfoFile": "../../built/local/tsserverWeb.tsbuildinfo"
},
"files": [
"tsserverWeb.ts",
Comment thread
sheetalkamat marked this conversation as resolved.
Outdated
Comment thread
rbuckton marked this conversation as resolved.
Outdated
]
}
3 changes: 3 additions & 0 deletions src/tsserverWeb/tsserverWeb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace ts.server {
export const dynamicImport = (id: string) => import(id);
}
30 changes: 26 additions & 4 deletions src/webServer/webServer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*@internal*/
/// <reference lib="dom" />
/// <reference lib="webworker.importscripts" />

namespace ts.server {
export interface HostWithWriteMessage {
Expand Down Expand Up @@ -111,11 +112,35 @@ namespace ts.server {
}
}

export declare const dynamicImport: ((id: string) => Promise<any>) | undefined;

// Attempt to load `dynamicImport`
if (typeof importScripts === "function") {
try {
// NOTE: importScripts is synchronous
importScripts("tsserverWeb.js");
}
catch {
// ignored
}
}

export function createWebSystem(host: WebHost, args: string[], getExecutingFilePath: () => string): ServerHost {
const returnEmptyString = () => "";
const getExecutingDirectoryPath = memoize(() => memoize(() => ensureTrailingDirectorySeparator(getDirectoryPath(getExecutingFilePath()))));
// Later we could map ^memfs:/ to do something special if we want to enable more functionality like module resolution or something like that
const getWebPath = (path: string) => startsWith(path, directorySeparator) ? path.replace(directorySeparator, getExecutingDirectoryPath()) : undefined;

const dynamicImport = async (id: string): Promise<any> => {
// Use syntactic dynamic import first, if available
if (ts.server.dynamicImport) {
return ts.server.dynamicImport(id);
}

// Fall back to `eval` if dynamic import wasn't avaialble.
return eval(`import(${JSON.stringify(id)})`); // eslint-disable-line no-eval
};

return {
args,
newLine: "\r\n", // This can be configured by clients
Expand Down Expand Up @@ -157,11 +182,8 @@ namespace ts.server {
}

const scriptPath = combinePaths(packageRoot, browser);

// TODO: TS rewrites `import(...)` to `require`. Use eval to bypass this
// eslint-disable-next-line no-eval
try {
const module = (await eval(`import(${JSON.stringify(scriptPath)})`)).default;
const { default: module } = await dynamicImport(scriptPath);
return { module, error: undefined };
}
catch (e) {
Expand Down