Skip to content

Commit

Permalink
Make tsserver and typingsInstaller thin wrappers around public API (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jakebailey authored Mar 15, 2024
1 parent b009837 commit aeddd65
Show file tree
Hide file tree
Showing 12 changed files with 146 additions and 213 deletions.
24 changes: 21 additions & 3 deletions Herebyfile.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ async function runDtsBundler(entrypoint, output) {
* @typedef BundlerTaskOptions
* @property {boolean} [exportIsTsObject]
* @property {boolean} [treeShaking]
* @property {boolean} [usePublicAPI]
* @property {() => void} [onWatchRebuild]
*/
function createBundler(entrypoint, outfile, taskOptions = {}) {
Expand All @@ -208,6 +209,19 @@ function createBundler(entrypoint, outfile, taskOptions = {}) {
// legalComments: "none", // If we add copyright headers to the source files, uncomment.
};

if (taskOptions.usePublicAPI) {
options.external = ["./typescript.js"];
options.plugins = options.plugins || [];
options.plugins.push({
name: "remap-typescript-to-require",
setup(build) {
build.onLoad({ filter: /src[\\/]typescript[\\/]typescript\.ts$/ }, () => {
return { contents: `export * from "./typescript.js"` };
});
},
});
}

if (taskOptions.exportIsTsObject) {
// Monaco bundles us as ESM by wrapping our code with something that defines module.exports
// but then does not use it, instead using the `ts` variable. Ensure that if we think we're CJS
Expand Down Expand Up @@ -235,7 +249,8 @@ function createBundler(entrypoint, outfile, taskOptions = {}) {
const toCommonJsRegExp = /var __toCommonJS .*/;
const toCommonJsRegExpReplacement = "var __toCommonJS = (mod) => (__copyProps, mod); // Modified helper to skip setting __esModule.";

options.plugins = [
options.plugins = options.plugins || [];
options.plugins.push(
{
name: "post-process",
setup: build => {
Expand All @@ -252,7 +267,7 @@ function createBundler(entrypoint, outfile, taskOptions = {}) {
});
},
},
];
);
}

return options;
Expand Down Expand Up @@ -422,7 +437,8 @@ const { main: tsserver, watch: watchTsserver } = entrypointBuildTask({
srcEntrypoint: "./src/tsserver/server.ts",
builtEntrypoint: "./built/local/tsserver/server.js",
output: "./built/local/tsserver.js",
mainDeps: [generateLibs],
mainDeps: [generateLibs, services],
bundlerOptions: { usePublicAPI: true },
});
export { tsserver, watchTsserver };

Expand Down Expand Up @@ -572,6 +588,8 @@ const { main: typingsInstaller, watch: watchTypingsInstaller } = entrypointBuild
srcEntrypoint: "./src/typingsInstaller/nodeTypingsInstaller.ts",
builtEntrypoint: "./built/local/typingsInstaller/nodeTypingsInstaller.js",
output: "./built/local/typingsInstaller.js",
mainDeps: [services],
bundlerOptions: { usePublicAPI: true },
});

const { main: watchGuard, watch: watchWatchGuard } = entrypointBuildTask({
Expand Down
6 changes: 0 additions & 6 deletions src/tsserver/_namespaces/ts.server.ts

This file was deleted.

9 changes: 1 addition & 8 deletions src/tsserver/_namespaces/ts.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1 @@
/* Generated file to emulate the ts namespace. */

export * from "../../compiler/_namespaces/ts";
export * from "../../services/_namespaces/ts";
export * from "../../jsTyping/_namespaces/ts";
export * from "../../server/_namespaces/ts";
import * as server from "./ts.server";
export { server };
export * from "../../typescript/typescript";
40 changes: 16 additions & 24 deletions src/tsserver/common.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
import {
LanguageServiceMode,
} from "./_namespaces/ts";
import {
Logger,
LogLevel,
ServerCancellationToken,
SessionOptions,
} from "./_namespaces/ts.server";
import * as ts from "./_namespaces/ts";

/** @internal */
export function getLogLevel(level: string | undefined) {
if (level) {
const l = level.toLowerCase();
for (const name in LogLevel) {
for (const name in ts.server.LogLevel) {
if (isNaN(+name) && l === name.toLowerCase()) {
return LogLevel[name] as any as LogLevel;
return ts.server.LogLevel[name] as any as ts.server.LogLevel;
}
}
}
Expand All @@ -23,23 +15,23 @@ export function getLogLevel(level: string | undefined) {

/** @internal */
export interface StartSessionOptions {
globalPlugins: SessionOptions["globalPlugins"];
pluginProbeLocations: SessionOptions["pluginProbeLocations"];
allowLocalPluginLoads: SessionOptions["allowLocalPluginLoads"];
useSingleInferredProject: SessionOptions["useSingleInferredProject"];
useInferredProjectPerProjectRoot: SessionOptions["useInferredProjectPerProjectRoot"];
suppressDiagnosticEvents: SessionOptions["suppressDiagnosticEvents"];
noGetErrOnBackgroundUpdate: SessionOptions["noGetErrOnBackgroundUpdate"];
canUseWatchEvents: SessionOptions["canUseWatchEvents"];
serverMode: SessionOptions["serverMode"];
globalPlugins: ts.server.SessionOptions["globalPlugins"];
pluginProbeLocations: ts.server.SessionOptions["pluginProbeLocations"];
allowLocalPluginLoads: ts.server.SessionOptions["allowLocalPluginLoads"];
useSingleInferredProject: ts.server.SessionOptions["useSingleInferredProject"];
useInferredProjectPerProjectRoot: ts.server.SessionOptions["useInferredProjectPerProjectRoot"];
suppressDiagnosticEvents: ts.server.SessionOptions["suppressDiagnosticEvents"];
noGetErrOnBackgroundUpdate: ts.server.SessionOptions["noGetErrOnBackgroundUpdate"];
canUseWatchEvents: ts.server.SessionOptions["canUseWatchEvents"];
serverMode: ts.server.SessionOptions["serverMode"];
}

/** @internal */
export interface StartInput {
args: readonly string[];
logger: Logger;
cancellationToken: ServerCancellationToken;
serverMode: LanguageServiceMode | undefined;
logger: ts.server.Logger;
cancellationToken: ts.server.ServerCancellationToken;
serverMode: ts.LanguageServiceMode | undefined;
unknownServerMode?: string;
startSession: (option: StartSessionOptions, logger: Logger, cancellationToken: ServerCancellationToken) => void;
startSession: (option: StartSessionOptions, logger: ts.server.Logger, cancellationToken: ts.server.ServerCancellationToken) => void;
}
Loading

0 comments on commit aeddd65

Please sign in to comment.