ttsc
Version:
General-purpose TypeScript-Go compiler, runtime, plugin host, and LSP host.
85 lines • 3.75 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolvePluginConfigDir = resolvePluginConfigDir;
exports.isLinkedTransform = isLinkedTransform;
exports.assertSharedHostCompatibility = assertSharedHostCompatibility;
exports.selectSharedHostPlugin = selectSharedHostPlugin;
exports.linkedTransformPlugins = linkedTransformPlugins;
const node_path_1 = __importDefault(require("node:path"));
/**
* Resolve the caller-declared plugin config anchor to an absolute path, or
* `undefined` when the caller did not declare one.
*
* The anchor exists for embedders that compile through a generated tsconfig
* outside the project (the bundler adapters' alias overlay): they set
* `pluginConfigDir` to the real project directory and every native plugin spawn
* forwards it as `TTSC_PLUGIN_CONFIG_DIR`, so config-file discovery walks the
* project instead of the wrapper's temp-dir ancestry. Callers that point at a
* user-authored tsconfig (even a wrapper outside the project) leave it unset,
* keeping discovery anchored at the tsconfig's own directory.
*/
function resolvePluginConfigDir(options) {
if (options.pluginConfigDir === undefined || options.pluginConfigDir === "") {
return undefined;
}
return node_path_1.default.resolve(options.cwd ?? process.cwd(), options.pluginConfigDir);
}
/**
* Reports whether the given transform source is linked into another compiler
* host instead of owning the process itself.
*/
function isLinkedTransform(plugin) {
return plugin.stage === "transform" && plugin.kind === "linked";
}
/**
* Verifies that all transform plugins in `plugins` either resolve to the same
* native binary (the common case) after linked sources are removed from the
* compiler-owner set.
*
* Two callers exist with subtly different error wording: the build path
* (`runBuild.ts`) reports "multiple compiler native backends cannot share one
* emit pass" while the source-to-source path (`transformProjectInMemory.ts`)
* reports "cannot share one source-to-source pass". The `pass` argument selects
* the appropriate phrase so the error message remains diagnostic-grade instead
* of generic.
*/
function assertSharedHostCompatibility(plugins, pass) {
const binaries = [...new Set(plugins.map((plugin) => plugin.binary))];
if (binaries.length <= 1) {
return;
}
const ownerBinaries = [
...new Set(plugins
.filter((plugin) => !isLinkedTransform(plugin))
.map((plugin) => plugin.binary)),
];
if (ownerBinaries.length <= 1) {
return;
}
const phrase = pass === "emit"
? "multiple compiler native backends cannot share one emit pass"
: "multiple transform native backends cannot share one source-to-source pass";
throw new Error("ttsc: " +
phrase +
"; compose transform libraries through one aggregate native host");
}
/**
* Picks the native binary that must own the compiler pass. Linked transform
* sources ride inside a host that uses driver.LoadProgram, so an executable
* transform wins when one is present.
*/
function selectSharedHostPlugin(plugins) {
return plugins.find((plugin) => !isLinkedTransform(plugin)) ?? plugins[0];
}
/**
* Return every plugin whose transform source is linked into another host binary
* rather than owning the process. The host binary passes these via
* `TTSC_LINKED_PLUGINS_JSON` so their Go code runs inside the same process.
*/
function linkedTransformPlugins(plugins) {
return plugins.filter(isLinkedTransform);
}
//# sourceMappingURL=sharedHostHelpers.js.map