Skip to content

Commit 54b93ea

Browse files
committed
🐛 Load TypeScript components from selected source
1 parent c195b20 commit 54b93ea

35 files changed

Lines changed: 1269 additions & 88 deletions

‎architecture.md‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,27 @@ hidden inside library objects that accumulate. One exception: metadata an
201201
author declares at module evaluation, about a value the author owns, may live
202202
on that value.
203203

204+
Ordinary TypeScript components use `ComponentModuleLoader`, the same-name
205+
interface and scoped Context in `@executablemd/core/api`, with stable name
206+
`@executablemd/core/ComponentModuleLoader` and default `undefined`. Canonical
207+
setup captures its bound `load(source, origin)` before profile resolution.
208+
Profile and durable imports pass exact selected direct bytes and the logical
209+
file URL; core still validates the complete namespace and owns durable identity
210+
and admission. Runtime-named CLI entrypoints install their matching core
211+
`component-modules/deno`, `/node` or `/bun` provider; compiled installs Deno's.
212+
Embeddings explicitly use the matching exported installer. Eval compilation is
213+
a separate service. Absent installation refuses TypeScript loading before native
214+
work; Markdown and registered functions are unaffected.
215+
216+
These adapters transform direct TypeScript in memory, preserving module exports,
217+
logical origin, source maps and ordinary live dependencies without staging files
218+
or permission grants. Deno and Node retain owned native hooks; Bun retains an
219+
owned JavaScript object URL. Loading children belong to canonical execution,
220+
survive the short-lived materialization task and namespace return, and end after
221+
later module use. Cancellation joins native import work before deregistration
222+
or revocation. Retained imports execute retained direct text after file removal;
223+
they do not snapshot dependencies or repeat completed effects.
224+
204225
A callback registered with something outside the process — a listener on a
205226
socket, a child process, a stream or a DOM target — is state of exactly this
206227
kind, and the source it is attached to knows nothing about the operation that

‎knip.json‎

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
{
22
"$schema": "https://unpkg.com/knip@6/schema.json",
3-
"ignore": [
4-
"**/vendor/**",
5-
"**/npm/**",
6-
"scripts/tests/fixtures/**",
7-
"**/_fresh/**",
8-
"**/*.d.ts"
9-
],
3+
"ignore": ["**/vendor/**", "**/npm/**", "scripts/tests/fixtures/**", "**/_fresh/**", "**/*.d.ts"],
104
"workspaces": {
115
".": {
126
"entry": [
@@ -36,6 +30,9 @@
3630
],
3731
"@executablemd/core": ["packages/core/mod.ts"],
3832
"@executablemd/core/api": ["packages/core/api.ts"],
33+
"@executablemd/core/component-modules/deno": ["packages/core/component-modules/deno.ts"],
34+
"@executablemd/core/component-modules/node": ["packages/core/component-modules/node.ts"],
35+
"@executablemd/core/component-modules/bun": ["packages/core/component-modules/bun.ts"],
3936
"@executablemd/core/host": ["packages/core/host.ts"],
4037
"@executablemd/durable-streams": ["packages/durable-streams/mod.ts"],
4138
"@executablemd/git": ["packages/git/mod.ts"],
@@ -66,12 +63,7 @@
6663
"demo/**/*.{ts,tsx}",
6764
"examples/**/*.{ts,tsx}"
6865
],
69-
"project": [
70-
"**/*.{ts,tsx,js,jsx}",
71-
"!vendor/**",
72-
"!src/repl/vendor/**",
73-
"!npm/**"
74-
],
66+
"project": ["**/*.{ts,tsx,js,jsx}", "!vendor/**", "!src/repl/vendor/**", "!npm/**"],
7567
"paths": {
7668
"@executablemd/acp": ["../../packages/acp/mod.ts"],
7769
"@executablemd/acp/embedded-adapters": ["../../packages/acp/embedded-adapters.ts"],
@@ -82,6 +74,15 @@
8274
],
8375
"@executablemd/core": ["../../packages/core/mod.ts"],
8476
"@executablemd/core/api": ["../../packages/core/api.ts"],
77+
"@executablemd/core/component-modules/deno": [
78+
"../../packages/core/component-modules/deno.ts"
79+
],
80+
"@executablemd/core/component-modules/node": [
81+
"../../packages/core/component-modules/node.ts"
82+
],
83+
"@executablemd/core/component-modules/bun": [
84+
"../../packages/core/component-modules/bun.ts"
85+
],
8586
"@executablemd/core/host": ["../../packages/core/host.ts"],
8687
"@executablemd/durable-streams": ["../../packages/durable-streams/mod.ts"],
8788
"@executablemd/git": ["../../packages/git/mod.ts"],

‎packages/cli/src/bun.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import process from "node:process";
1111
import { API, useHostFiles } from "@executablemd/runtime";
1212
import { compileTempFile } from "@executablemd/core";
1313
import { runXmd, XMD_VERSION } from "./cli.ts";
14+
import { useBunComponentModuleLoader } from "@executablemd/core/component-modules/bun";
1415
import { useBunRepl } from "./bun-repl.ts";
1516
import { readInputStream } from "./standard-input.ts";
1617
import { importPluginModule } from "./host-plugin-modules.ts";
@@ -36,6 +37,7 @@ const UPGRADE: UpgradeAssembly = {
3637
};
3738

3839
await main(function* (args) {
40+
yield* useBunComponentModuleLoader();
3941
// The base providers for this host. `at: "min"` puts them beneath ordinary
4042
// middleware, so middleware installed later can wrap either one.
4143
yield* API.Env.around(

‎packages/cli/src/cli.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ import { selectPlugins } from "./plugin-selection.ts";
154154
import type { PluginSelection } from "./plugin-selection.ts";
155155
import { deliverWhole } from "./stdout-delivery.ts";
156156
import { testingExecutionHost } from "./testing-host.ts";
157+
import { ComponentModuleLoader } from "@executablemd/core/api";
157158
import type { ChildPlanDeclaration } from "./testing-host.ts";
158159
import { EVAL_ALIAS, EVAL_OPTION, evalGrammarError, readEvalFlags } from "./eval-source.ts";
159160
import type { EvalFlags } from "./eval-source.ts";
@@ -1436,6 +1437,7 @@ function* runDocument(
14361437
// that inherits no `API.Env` handler. What crosses is the argv, not the Api
14371438
// that produced it.
14381439
const testingHost = testingExecutionHost({
1440+
componentModuleLoader: yield* ComponentModuleLoader.get(),
14391441
includes: include,
14401442
// The Plugin *values*, so an isolated child installs them again for itself
14411443
// with command `run`. Live middleware and installation-scoped resources

‎packages/cli/src/compiled.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import process from "node:process";
1010
import { API, useHostFiles } from "@executablemd/runtime";
1111
import { compileDataUri } from "@executablemd/core";
1212
import { runXmd, XMD_VERSION } from "./cli.ts";
13+
import { useDenoComponentModuleLoader } from "@executablemd/core/component-modules/deno";
1314
import { useCompiledRepl } from "./compiled-repl.ts";
1415
import { readInputStream } from "./standard-input.ts";
1516
import { importPluginModule } from "./host-plugin-modules.ts";
@@ -54,6 +55,7 @@ if (isCredentialHelperMode(process.argv.slice(2))) {
5455
await main(() => runCredentialHelper(process.argv.slice(2)));
5556
} else {
5657
await main(function* (args) {
58+
yield* useDenoComponentModuleLoader();
5759
// The base providers for this host. `at: "min"` puts them beneath ordinary
5860
// middleware, so middleware installed later can wrap either one.
5961
yield* API.Env.around(

‎packages/cli/src/deno.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import process from "node:process";
1313
import { API, useHostFiles } from "@executablemd/runtime";
1414
import { compileDataUri } from "@executablemd/core";
1515
import { runXmd, XMD_VERSION } from "./cli.ts";
16+
import { useDenoComponentModuleLoader } from "@executablemd/core/component-modules/deno";
1617
import { useDenoRepl } from "./deno-repl.ts";
1718
import { readInputStream } from "./standard-input.ts";
1819
import { importPluginModule } from "./host-plugin-modules.ts";
@@ -70,6 +71,7 @@ if (isCredentialHelperMode(process.argv.slice(2))) {
7071
await main(() => runCredentialHelper(process.argv.slice(2)));
7172
} else {
7273
await main(function* (args) {
74+
yield* useDenoComponentModuleLoader();
7375
// The base providers for this host. `at: "min"` puts them beneath ordinary
7476
// middleware, so middleware installed later can wrap either one.
7577
yield* API.Env.around(

‎packages/cli/src/node.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import process from "node:process";
1717
import { API, useHostFiles } from "@executablemd/runtime";
1818
import { compileTempFile } from "@executablemd/core";
1919
import { runXmd, XMD_VERSION } from "./cli.ts";
20+
import { useNodeComponentModuleLoader } from "@executablemd/core/component-modules/node";
2021
import { useNodeRepl } from "./node-repl.ts";
2122
import { readInputStream } from "./standard-input.ts";
2223
import { importPluginModule } from "./host-plugin-modules.ts";
@@ -43,6 +44,7 @@ const UPGRADE: UpgradeAssembly = {
4344
};
4445

4546
await main(function* (args) {
47+
yield* useNodeComponentModuleLoader();
4648
// The base providers for this host. `at: "min"` puts them beneath ordinary
4749
// middleware, so middleware installed later can wrap either one.
4850
yield* API.Env.around(

‎packages/cli/src/testing-host.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import { installDocumentComponents } from "./cli.ts";
6161
import { installPlugins } from "./plugin-host.ts";
6262
import { nestedRunProfile } from "./run-profile.ts";
6363
import type { Plugin } from "@executablemd/core/api";
64+
import { ComponentModuleLoader } from "@executablemd/core/api";
6465
import { useOrdinaryGeneratedSelection } from "./evaluation-profile.ts";
6566
import type { HostServiceInstaller } from "./cli.ts";
6667

@@ -87,6 +88,7 @@ export interface ChildPlanDeclaration {
8788

8889
/** What the entrypoint already decided, and a child must not decide again. */
8990
export interface TestingHostSettings {
91+
readonly componentModuleLoader?: ComponentModuleLoader;
9092
/** The component search path this run resolves names through. */
9193
readonly includes: string[];
9294
/**
@@ -270,6 +272,7 @@ function* runProfileChild(
270272
settings: TestingHostSettings,
271273
): Operation<ChildSettlement> {
272274
const request = invocation.request;
275+
yield* ComponentModuleLoader.set(settings.componentModuleLoader);
273276
if (request.host !== "run") {
274277
throw new Error(`the ${request.host} host profile is not available on this entrypoint`);
275278
}

‎packages/cli/tests/testing-execution-host-native.test.ts‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import { Execution } from "@executablemd/core/api";
2323
import type { Plugin, PluginInstallation } from "@executablemd/core/api";
2424
import { cwd } from "@executablemd/runtime";
2525
import { cliBase } from "@executablemd/test-support/launch";
26+
import { runCli, runShell, shellQuote } from "@executablemd/test-support/launch";
27+
import { writeTextFile } from "@effectionx/fs";
2628
import { useTempDirectory } from "@executablemd/test-support/temp";
2729
import type { HelperAssembly } from "@executablemd/git/credential-helper";
2830
import { planComponentDeclaration } from "../src/plan-component.ts";
@@ -48,6 +50,50 @@ import { denoRunRepositories } from "../src/deno-repositories.ts";
4850
* configured.
4951
*/
5052
describe("C1 — the native host a run child initializes", () => {
53+
it("L4: source entrypoints install the loader and carry it into detached run children", function* () {
54+
const project = yield* useTempDirectory("xmd-module-host-");
55+
const repository = fileURLToPath(new URL("../../../", import.meta.url));
56+
yield* writeTextFile(
57+
join(project, "helper.ts"),
58+
'export const value: string = "host-typed-component";',
59+
);
60+
yield* writeTextFile(
61+
join(project, "Typed.ts"),
62+
'import { value } from "./helper.ts";\nexport default function*() { const typed: string = value; return typed; }',
63+
);
64+
yield* writeTextFile(
65+
join(project, "README.test.md"),
66+
[
67+
'<Test name="host module selection">',
68+
'<Typed as="direct" />',
69+
'<AssertEquals actual={direct} expected="host-typed-component" />',
70+
'<Execution host="run" source={"<Typed />"} as="child">',
71+
'<CollectOutput as="output" />',
72+
'<AssertStringIncludes actual={output} expected="host-typed-component" />',
73+
"</Execution>",
74+
"</Test>",
75+
].join("\n"),
76+
);
77+
const args = ["test", "README.test.md", "--include", project];
78+
console.log(JSON.stringify({ argv: [...cliBase(), ...args], cwd: project }));
79+
const deno = yield* runCli(args, { cwd: project }).join();
80+
expect({ code: deno.code, stderr: deno.stderr }).toEqual({ code: 0, stderr: "" });
81+
for (const argv of [
82+
[
83+
"tsx",
84+
"--tsconfig",
85+
join(repository, "tsconfig.node.json"),
86+
join(repository, "packages/cli/src/node.ts"),
87+
...args,
88+
],
89+
["bun", join(repository, "packages/cli/src/bun.ts"), ...args],
90+
]) {
91+
console.log(JSON.stringify({ argv, cwd: project }));
92+
const result = yield* runShell(argv.map(shellQuote).join(" "), { cwd: project }).join();
93+
expect({ argv, code: result.code, stderr: result.stderr }).toMatchObject({ argv, code: 0 });
94+
}
95+
});
96+
5197
/** A bare remote a child may clone without reaching a network. */
5298
function* useBareRemote(home: string): Operation<string> {
5399
const parent = yield* until(realpath(yield* useTempDirectory("xmd-c1-remote-")));

‎packages/core/api.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
*/
3434

3535
export { Plugin } from "./src/plugin.ts";
36+
export { ComponentModuleLoader } from "./src/component-module-loader.ts";
3637
export type { PluginInstallation, PluginInstallRequest } from "./src/plugin.ts";
3738

3839
export {

0 commit comments

Comments
 (0)