@opentui/core 0.3.4 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/NativeSpanFeed.d.ts +4 -0
- package/{index-54s7pk0d.js → index-59t85rvq.js} +3129 -2499
- package/index-59t85rvq.js.map +84 -0
- package/index.js +11256 -77
- package/index.js.map +29 -3
- package/lib/selection.d.ts +2 -1
- package/lib/singleton.d.ts +1 -0
- package/lib/tree-sitter/client.d.ts +17 -1
- package/lib/tree-sitter/index.d.ts +1 -0
- package/lib/tree-sitter/types.d.ts +104 -1
- package/lib/tree-sitter/update-assets.js +8 -6
- package/lib/tree-sitter/update-assets.js.map +3 -3
- package/package.json +27 -34
- package/parser.worker.js +452 -78
- package/parser.worker.js.map +8 -7
- package/platform/ffi.d.ts +6 -3
- package/platform/test.d.ts +12 -0
- package/platform/worker.d.ts +26 -4
- package/platform/worker.node-test.d.ts +1 -0
- package/runtime-plugin-support-configure.js +36 -18
- package/runtime-plugin-support-configure.js.map +1 -9
- package/runtime-plugin-support-configure.node.js +15 -0
- package/runtime-plugin-support.js +4 -19
- package/runtime-plugin-support.js.map +1 -10
- package/runtime-plugin-support.node.js +15 -0
- package/runtime-plugin.js +473 -16
- package/runtime-plugin.js.map +1 -9
- package/runtime-plugin.node.js +15 -0
- package/testing/bun-test-node.d.ts +87 -0
- package/testing/mock-tree-sitter-client.d.ts +4 -0
- package/testing.js +15 -9
- package/testing.js.map +3 -3
- package/zig.d.ts +1 -1
- package/index-0nvgrgam.js +0 -11684
- package/index-0nvgrgam.js.map +0 -35
- package/index-54s7pk0d.js.map +0 -84
- package/index-8t841rbx.js +0 -44
- package/index-8t841rbx.js.map +0 -10
- package/index-r49y8kdq.js +0 -421
- package/index-r49y8kdq.js.map +0 -10
package/platform/worker.d.ts
CHANGED
|
@@ -1,4 +1,26 @@
|
|
|
1
|
-
export declare const
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
}
|
|
1
|
+
export declare const WORKER_UNAVAILABLE = "OpenTUI tree-sitter workers are not available for this runtime yet.";
|
|
2
|
+
export interface WorkerMessageEvent<T = unknown> {
|
|
3
|
+
readonly data: T;
|
|
4
|
+
}
|
|
5
|
+
export interface WorkerErrorEvent {
|
|
6
|
+
readonly error?: unknown;
|
|
7
|
+
readonly message: string;
|
|
8
|
+
}
|
|
9
|
+
export type WorkerMessageHandler<T = unknown> = (event: WorkerMessageEvent<T>) => void | Promise<void>;
|
|
10
|
+
export type WorkerErrorHandler = (event: WorkerErrorEvent) => void;
|
|
11
|
+
export interface PlatformWorkerOptions {
|
|
12
|
+
name?: string;
|
|
13
|
+
}
|
|
14
|
+
export interface PlatformWorkerHandle {
|
|
15
|
+
onmessage: WorkerMessageHandler | null;
|
|
16
|
+
onerror: WorkerErrorHandler | null;
|
|
17
|
+
postMessage(value: unknown): void;
|
|
18
|
+
terminate(): void | Promise<number>;
|
|
19
|
+
addEventListener(type: "message" | "error", listener: WorkerMessageHandler | WorkerErrorHandler): void;
|
|
20
|
+
removeEventListener(type: "message" | "error", listener: WorkerMessageHandler | WorkerErrorHandler): void;
|
|
21
|
+
}
|
|
22
|
+
export type PlatformWorkerConstructor = new (specifier: string | URL, options?: PlatformWorkerOptions) => PlatformWorkerHandle;
|
|
23
|
+
export declare const Worker: PlatformWorkerConstructor;
|
|
24
|
+
export declare const isWorkerRuntime: boolean;
|
|
25
|
+
export declare function postWorkerMessage(value: unknown): void;
|
|
26
|
+
export declare function setWorkerMessageHandler<T>(handler: WorkerMessageHandler<T>): () => void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,18 +1,36 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
1
|
+
import { plugin as registerBunPlugin } from "bun";
|
|
2
|
+
import { createRuntimePlugin } from "./runtime-plugin.js";
|
|
3
|
+
const runtimePluginSupportInstalledKey = "__opentuiCoreRuntimePluginSupportInstalled__";
|
|
4
|
+
function normalizeRewriteKey(rewrite) {
|
|
5
|
+
return `${rewrite?.nodeModulesRuntimeSpecifiers ?? true}:${rewrite?.nodeModulesBareSpecifiers ?? false}`;
|
|
6
|
+
}
|
|
7
|
+
function assertCompatibleInstall(install, options) {
|
|
8
|
+
for (const specifier of Object.keys(options.additional ?? {})) {
|
|
9
|
+
if (!install.additionalSpecifiers.has(specifier)) {
|
|
10
|
+
throw new Error(`OpenTUI Core runtime plugin support is already installed without ${specifier}. Call ensureRuntimePluginSupport({ additional }) from @opentui/core/runtime-plugin-support/configure before importing @opentui/core/runtime-plugin-support.`);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
if (options.core && options.core !== install.core) {
|
|
14
|
+
throw new Error("OpenTUI Core runtime plugin support is already installed with a different core runtime module.");
|
|
15
|
+
}
|
|
16
|
+
if (options.rewrite && normalizeRewriteKey(options.rewrite) !== install.rewriteKey) {
|
|
17
|
+
throw new Error("OpenTUI Core runtime plugin support is already installed with different rewrite options.");
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
export function ensureRuntimePluginSupport(options = {}) {
|
|
21
|
+
const state = globalThis;
|
|
22
|
+
const install = state[runtimePluginSupportInstalledKey];
|
|
23
|
+
if (install) {
|
|
24
|
+
assertCompatibleInstall(install, options);
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
registerBunPlugin(createRuntimePlugin(options));
|
|
28
|
+
state[runtimePluginSupportInstalledKey] = {
|
|
29
|
+
additionalSpecifiers: new Set(Object.keys(options.additional ?? {})),
|
|
30
|
+
core: options.core,
|
|
31
|
+
rewriteKey: normalizeRewriteKey(options.rewrite),
|
|
32
|
+
};
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
export { createRuntimePlugin, runtimeModuleIdForSpecifier } from "./runtime-plugin.js";
|
|
36
|
+
//# sourceMappingURL=runtime-plugin-support-configure.js.map
|
|
@@ -1,9 +1 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": [],
|
|
4
|
-
"sourcesContent": [
|
|
5
|
-
],
|
|
6
|
-
"mappings": "",
|
|
7
|
-
"debugId": "20E180CC53AAFA2A64756E2164756E21",
|
|
8
|
-
"names": []
|
|
9
|
-
}
|
|
1
|
+
{"version":3,"file":"runtime-plugin-support-configure.js","sourceRoot":"","sources":["runtime-plugin-support-configure.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,iBAAiB,EAAE,MAAM,KAAK,CAAA;AACjD,OAAO,EAAE,mBAAmB,EAA4D,MAAM,qBAAqB,CAAA;AAEnH,MAAM,gCAAgC,GAAG,8CAA8C,CAAA;AAYvF,SAAS,mBAAmB,CAAC,OAA0D;IACrF,OAAO,GAAG,OAAO,EAAE,4BAA4B,IAAI,IAAI,IAAI,OAAO,EAAE,yBAAyB,IAAI,KAAK,EAAE,CAAA;AAC1G,CAAC;AAED,SAAS,uBAAuB,CAAC,OAAoC,EAAE,OAAmC;IACxG,KAAK,MAAM,SAAS,IAAI,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,UAAU,IAAI,EAAE,CAAC,EAAE,CAAC;QAC9D,IAAI,CAAC,OAAO,CAAC,oBAAoB,CAAC,GAAG,CAAC,SAAS,CAAC,EAAE,CAAC;YACjD,MAAM,IAAI,KAAK,CACb,oEAAoE,SAAS,8JAA8J,CAC5O,CAAA;QACH,CAAC;IACH,CAAC;IAED,IAAI,OAAO,CAAC,IAAI,IAAI,OAAO,CAAC,IAAI,KAAK,OAAO,CAAC,IAAI,EAAE,CAAC;QAClD,MAAM,IAAI,KAAK,CAAC,gGAAgG,CAAC,CAAA;IACnH,CAAC;IAED,IAAI,OAAO,CAAC,OAAO,IAAI,mBAAmB,CAAC,OAAO,CAAC,OAAO,CAAC,KAAK,OAAO,CAAC,UAAU,EAAE,CAAC;QACnF,MAAM,IAAI,KAAK,CAAC,0FAA0F,CAAC,CAAA;IAC7G,CAAC;AACH,CAAC;AAED,MAAM,UAAU,0BAA0B,CAAC,UAAsC,EAAE;IACjF,MAAM,KAAK,GAAG,UAAuC,CAAA;IACrD,MAAM,OAAO,GAAG,KAAK,CAAC,gCAAgC,CAAC,CAAA;IAEvD,IAAI,OAAO,EAAE,CAAC;QACZ,uBAAuB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAA;QACzC,OAAO,KAAK,CAAA;IACd,CAAC;IAED,iBAAiB,CAAC,mBAAmB,CAAC,OAAO,CAAC,CAAC,CAAA;IAE/C,KAAK,CAAC,gCAAgC,CAAC,GAAG;QACxC,oBAAoB,EAAE,IAAI,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,UAAU,IAAI,EAAE,CAAC,CAAC;QACpE,IAAI,EAAE,OAAO,CAAC,IAAI;QAClB,UAAU,EAAE,mBAAmB,CAAC,OAAO,CAAC,OAAO,CAAC;KACjD,CAAA;IACD,OAAO,IAAI,CAAA;AACb,CAAC;AAED,OAAO,EAAE,mBAAmB,EAAE,2BAA2B,EAAE,MAAM,qBAAqB,CAAA"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
const errorMessage = "@opentui/core/runtime-plugin-support/configure is Bun-only and is not available in Node.js. Use Bun to import this entrypoint."
|
|
2
|
+
|
|
3
|
+
export function ensureRuntimePluginSupport() {
|
|
4
|
+
throw new Error("@opentui/core/runtime-plugin-support/configure is Bun-only and is not available in Node.js. Use Bun to import this entrypoint.")
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function createRuntimePlugin() {
|
|
8
|
+
throw new Error("@opentui/core/runtime-plugin-support/configure is Bun-only and is not available in Node.js. Use Bun to import this entrypoint.")
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function runtimeModuleIdForSpecifier() {
|
|
12
|
+
throw new Error("@opentui/core/runtime-plugin-support/configure is Bun-only and is not available in Node.js. Use Bun to import this entrypoint.")
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
throw new Error(errorMessage)
|
|
@@ -1,20 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
} from "./index-8t841rbx.js";
|
|
5
|
-
import {
|
|
6
|
-
createRuntimePlugin,
|
|
7
|
-
runtimeModuleIdForSpecifier
|
|
8
|
-
} from "./index-r49y8kdq.js";
|
|
9
|
-
import"./index-0nvgrgam.js";
|
|
10
|
-
import"./index-54s7pk0d.js";
|
|
11
|
-
// src/runtime-plugin-support.ts
|
|
1
|
+
import { ensureRuntimePluginSupport } from "./runtime-plugin-support-configure.js";
|
|
2
|
+
export { ensureRuntimePluginSupport };
|
|
3
|
+
export { createRuntimePlugin, runtimeModuleIdForSpecifier, } from "./runtime-plugin-support-configure.js";
|
|
12
4
|
ensureRuntimePluginSupport();
|
|
13
|
-
|
|
14
|
-
runtimeModuleIdForSpecifier,
|
|
15
|
-
ensureRuntimePluginSupport,
|
|
16
|
-
createRuntimePlugin
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
//# debugId=77F70D7DCCE5C8F764756E2164756E21
|
|
20
|
-
//# sourceMappingURL=runtime-plugin-support.js.map
|
|
5
|
+
//# sourceMappingURL=runtime-plugin-support.js.map
|
|
@@ -1,10 +1 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../src/runtime-plugin-support.ts"],
|
|
4
|
-
"sourcesContent": [
|
|
5
|
-
"import { ensureRuntimePluginSupport } from \"./runtime-plugin-support-configure.js\"\n\nexport { ensureRuntimePluginSupport }\nexport {\n createRuntimePlugin,\n runtimeModuleIdForSpecifier,\n type CreateRuntimePluginOptions,\n type RuntimeModuleEntry,\n type RuntimeModuleExports,\n type RuntimeModuleLoader,\n} from \"./runtime-plugin-support-configure.js\"\n\nensureRuntimePluginSupport()\n"
|
|
6
|
-
],
|
|
7
|
-
"mappings": ";;;;;;;;;;;AAYA,2BAA2B;",
|
|
8
|
-
"debugId": "77F70D7DCCE5C8F764756E2164756E21",
|
|
9
|
-
"names": []
|
|
10
|
-
}
|
|
1
|
+
{"version":3,"file":"runtime-plugin-support.js","sourceRoot":"","sources":["runtime-plugin-support.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,0BAA0B,EAAE,MAAM,uCAAuC,CAAA;AAElF,OAAO,EAAE,0BAA0B,EAAE,CAAA;AACrC,OAAO,EACL,mBAAmB,EACnB,2BAA2B,GAK5B,MAAM,uCAAuC,CAAA;AAE9C,0BAA0B,EAAE,CAAA"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
const errorMessage = "@opentui/core/runtime-plugin-support is Bun-only and is not available in Node.js. Use Bun to import this entrypoint."
|
|
2
|
+
|
|
3
|
+
export function ensureRuntimePluginSupport() {
|
|
4
|
+
throw new Error("@opentui/core/runtime-plugin-support is Bun-only and is not available in Node.js. Use Bun to import this entrypoint.")
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function createRuntimePlugin() {
|
|
8
|
+
throw new Error("@opentui/core/runtime-plugin-support is Bun-only and is not available in Node.js. Use Bun to import this entrypoint.")
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function runtimeModuleIdForSpecifier() {
|
|
12
|
+
throw new Error("@opentui/core/runtime-plugin-support is Bun-only and is not available in Node.js. Use Bun to import this entrypoint.")
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
throw new Error(errorMessage)
|
package/runtime-plugin.js
CHANGED
|
@@ -1,16 +1,473 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
1
|
+
/*
|
|
2
|
+
* Exposes runtime-only modules (for example `@opentui/core`, `@opentui/solid`,
|
|
3
|
+
* `solid-js`) to externally loaded plugins by rewriting matching imports to
|
|
4
|
+
* virtual `opentui:runtime-module:*` ids.
|
|
5
|
+
*
|
|
6
|
+
* Why this is exact-path + prescan instead of one broad `onLoad`:
|
|
7
|
+
* - Bun can break CJS/UMD interop if a file is routed through plugin `onLoad`
|
|
8
|
+
* (real repro: `jsonc-parser` resolving to `lib/umd/main.js`;
|
|
9
|
+
* https://github.com/oven-sh/bun/issues/19279,
|
|
10
|
+
* https://github.com/oven-sh/bun/issues/21369), so arbitrary `node_modules`
|
|
11
|
+
* JS cannot be blanket-rewritten.
|
|
12
|
+
* - runtime `onResolve` is sync-only, so package/type/source discovery here is
|
|
13
|
+
* synchronous and cached.
|
|
14
|
+
* - a matched `onLoad` cannot safely fall through, so loaders must be narrow.
|
|
15
|
+
* - Bun may canonicalize paths before `onLoad`, so loaders are registered for
|
|
16
|
+
* both the resolved path spelling and its realpath, then canonical-checked.
|
|
17
|
+
* - Bun may native-load `node_modules` ESM without firing `onResolve` for
|
|
18
|
+
* nested package imports, so `node_modules` ESM is recursively prescanned and
|
|
19
|
+
* only files that actually need runtime rewriting get exact-path loaders.
|
|
20
|
+
*
|
|
21
|
+
* Behavior:
|
|
22
|
+
* - non-`node_modules` source files get a dedicated rewrite loader immediately.
|
|
23
|
+
* - `node_modules` files are rewritten only if they are ESM (`.mjs`, `.mts`,
|
|
24
|
+
* `.ts`, `.tsx`, `.jsx`, or `.js` under `package.json#type="module"`) and
|
|
25
|
+
* directly or transitively need runtime-module rewriting; unrelated CJS stays
|
|
26
|
+
* untouched.
|
|
27
|
+
* - optional bare-specifier rewriting is preserved for sibling files in
|
|
28
|
+
* packages already marked for runtime rewriting.
|
|
29
|
+
*
|
|
30
|
+
* Notes:
|
|
31
|
+
* - import scanning is regex-based, not a full parser.
|
|
32
|
+
* - CJS helper libraries that themselves import runtime modules are still not
|
|
33
|
+
* supported.
|
|
34
|
+
* - `package.json#type` caching is per plugin setup, not module-global, so a
|
|
35
|
+
* later plugin instance in the same process can observe filesystem changes.
|
|
36
|
+
*/
|
|
37
|
+
import { existsSync, readFileSync, realpathSync } from "node:fs";
|
|
38
|
+
import { basename, dirname, isAbsolute, join } from "node:path";
|
|
39
|
+
import { fileURLToPath } from "node:url";
|
|
40
|
+
import * as coreRuntime from "./index.js";
|
|
41
|
+
const CORE_RUNTIME_SPECIFIER = "@opentui/core";
|
|
42
|
+
const CORE_TESTING_RUNTIME_SPECIFIER = "@opentui/core/testing";
|
|
43
|
+
const RUNTIME_MODULE_PREFIX = "opentui:runtime-module:";
|
|
44
|
+
const MAX_RUNTIME_RESOLVE_PARENTS = 64;
|
|
45
|
+
const DEFAULT_RUNTIME_PLUGIN_REWRITE_OPTIONS = {
|
|
46
|
+
nodeModulesRuntimeSpecifiers: true,
|
|
47
|
+
nodeModulesBareSpecifiers: false,
|
|
48
|
+
};
|
|
49
|
+
const DEFAULT_CORE_RUNTIME_MODULE_SPECIFIERS = [CORE_RUNTIME_SPECIFIER, CORE_TESTING_RUNTIME_SPECIFIER];
|
|
50
|
+
const DEFAULT_CORE_RUNTIME_MODULE_SPECIFIER_SET = new Set(DEFAULT_CORE_RUNTIME_MODULE_SPECIFIERS);
|
|
51
|
+
export const isCoreRuntimeModuleSpecifier = (specifier) => {
|
|
52
|
+
return DEFAULT_CORE_RUNTIME_MODULE_SPECIFIER_SET.has(specifier);
|
|
53
|
+
};
|
|
54
|
+
const loadCoreTestingRuntimeModule = async () => {
|
|
55
|
+
return (await import("./testing.js"));
|
|
56
|
+
};
|
|
57
|
+
const escapeRegExp = (value) => {
|
|
58
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
59
|
+
};
|
|
60
|
+
const exactSpecifierFilter = (specifier) => {
|
|
61
|
+
return new RegExp(`^${escapeRegExp(specifier)}$`);
|
|
62
|
+
};
|
|
63
|
+
const exactPathFilter = (paths) => {
|
|
64
|
+
const candidates = [...new Set(paths.map(sourcePath))];
|
|
65
|
+
return new RegExp(`^(?:${candidates.map(escapeRegExp).join("|")})(?:[?#].*)?$`);
|
|
66
|
+
};
|
|
67
|
+
export const runtimeModuleIdForSpecifier = (specifier) => {
|
|
68
|
+
return `${RUNTIME_MODULE_PREFIX}${encodeURIComponent(specifier)}`;
|
|
69
|
+
};
|
|
70
|
+
const resolveRuntimeModuleExports = async (moduleEntry) => {
|
|
71
|
+
if (typeof moduleEntry === "function") {
|
|
72
|
+
return await moduleEntry();
|
|
73
|
+
}
|
|
74
|
+
return moduleEntry;
|
|
75
|
+
};
|
|
76
|
+
const sourcePath = (path) => {
|
|
77
|
+
const searchIndex = path.indexOf("?");
|
|
78
|
+
const hashIndex = path.indexOf("#");
|
|
79
|
+
const end = [searchIndex, hashIndex].filter((index) => index >= 0).sort((a, b) => a - b)[0];
|
|
80
|
+
return end === undefined ? path : path.slice(0, end);
|
|
81
|
+
};
|
|
82
|
+
const normalizedSourcePathByPath = new Map();
|
|
83
|
+
const normalizeSourcePath = (path) => {
|
|
84
|
+
const cleanPath = sourcePath(path);
|
|
85
|
+
const cachedPath = normalizedSourcePathByPath.get(cleanPath);
|
|
86
|
+
if (cachedPath !== undefined) {
|
|
87
|
+
return cachedPath;
|
|
88
|
+
}
|
|
89
|
+
let normalizedPath = cleanPath;
|
|
90
|
+
try {
|
|
91
|
+
normalizedPath = realpathSync(cleanPath);
|
|
92
|
+
}
|
|
93
|
+
catch {
|
|
94
|
+
normalizedPath = cleanPath;
|
|
95
|
+
}
|
|
96
|
+
normalizedSourcePathByPath.set(cleanPath, normalizedPath);
|
|
97
|
+
return normalizedPath;
|
|
98
|
+
};
|
|
99
|
+
const isNodeModulesPath = (path) => {
|
|
100
|
+
return /(?:^|[/\\])node_modules(?:[/\\])/.test(path);
|
|
101
|
+
};
|
|
102
|
+
const packageTypeForPath = (path, packageTypeByPackageJsonPath) => {
|
|
103
|
+
let currentDir = dirname(path);
|
|
104
|
+
while (true) {
|
|
105
|
+
const packageJsonPath = join(currentDir, "package.json");
|
|
106
|
+
if (existsSync(packageJsonPath)) {
|
|
107
|
+
const cachedPackageType = packageTypeByPackageJsonPath.get(packageJsonPath);
|
|
108
|
+
if (cachedPackageType) {
|
|
109
|
+
return cachedPackageType;
|
|
110
|
+
}
|
|
111
|
+
let packageType = "commonjs";
|
|
112
|
+
try {
|
|
113
|
+
const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf8"));
|
|
114
|
+
if (packageJson.type === "module") {
|
|
115
|
+
packageType = "module";
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
catch {
|
|
119
|
+
packageType = "commonjs";
|
|
120
|
+
}
|
|
121
|
+
packageTypeByPackageJsonPath.set(packageJsonPath, packageType);
|
|
122
|
+
return packageType;
|
|
123
|
+
}
|
|
124
|
+
const parentDir = dirname(currentDir);
|
|
125
|
+
if (parentDir === currentDir) {
|
|
126
|
+
return "commonjs";
|
|
127
|
+
}
|
|
128
|
+
currentDir = parentDir;
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
const isNodeModulesEsmPath = (path, packageTypeByPackageJsonPath) => {
|
|
132
|
+
const normalizedPath = normalizeSourcePath(path);
|
|
133
|
+
if (!isNodeModulesPath(normalizedPath)) {
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
136
|
+
if (normalizedPath.endsWith(".mjs") ||
|
|
137
|
+
normalizedPath.endsWith(".mts") ||
|
|
138
|
+
normalizedPath.endsWith(".ts") ||
|
|
139
|
+
normalizedPath.endsWith(".tsx") ||
|
|
140
|
+
normalizedPath.endsWith(".jsx")) {
|
|
141
|
+
return true;
|
|
142
|
+
}
|
|
143
|
+
if (normalizedPath.endsWith(".cjs") || normalizedPath.endsWith(".cts") || !normalizedPath.endsWith(".js")) {
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
return packageTypeForPath(normalizedPath, packageTypeByPackageJsonPath) === "module";
|
|
147
|
+
};
|
|
148
|
+
const nodeModulesPackageRootForPath = (path) => {
|
|
149
|
+
let currentDir = dirname(path);
|
|
150
|
+
while (true) {
|
|
151
|
+
const parentDir = dirname(currentDir);
|
|
152
|
+
if (parentDir === currentDir) {
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
if (basename(parentDir) === "node_modules") {
|
|
156
|
+
return currentDir;
|
|
157
|
+
}
|
|
158
|
+
if (basename(dirname(parentDir)) === "node_modules" && basename(parentDir).startsWith("@")) {
|
|
159
|
+
return currentDir;
|
|
160
|
+
}
|
|
161
|
+
currentDir = parentDir;
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
const resolveRuntimePluginRewriteOptions = (options) => {
|
|
165
|
+
return {
|
|
166
|
+
nodeModulesRuntimeSpecifiers: options?.nodeModulesRuntimeSpecifiers ?? DEFAULT_RUNTIME_PLUGIN_REWRITE_OPTIONS.nodeModulesRuntimeSpecifiers,
|
|
167
|
+
nodeModulesBareSpecifiers: options?.nodeModulesBareSpecifiers ?? DEFAULT_RUNTIME_PLUGIN_REWRITE_OPTIONS.nodeModulesBareSpecifiers,
|
|
168
|
+
};
|
|
169
|
+
};
|
|
170
|
+
const runtimeLoaderForPath = (path) => {
|
|
171
|
+
const cleanPath = sourcePath(path);
|
|
172
|
+
if (cleanPath.endsWith(".tsx")) {
|
|
173
|
+
return "tsx";
|
|
174
|
+
}
|
|
175
|
+
if (cleanPath.endsWith(".jsx")) {
|
|
176
|
+
return "jsx";
|
|
177
|
+
}
|
|
178
|
+
if (cleanPath.endsWith(".ts") || cleanPath.endsWith(".mts") || cleanPath.endsWith(".cts")) {
|
|
179
|
+
return "ts";
|
|
180
|
+
}
|
|
181
|
+
if (cleanPath.endsWith(".js") || cleanPath.endsWith(".mjs") || cleanPath.endsWith(".cjs")) {
|
|
182
|
+
return "js";
|
|
183
|
+
}
|
|
184
|
+
return null;
|
|
185
|
+
};
|
|
186
|
+
const resolveImportSpecifierPatterns = [
|
|
187
|
+
/(from\s+["'])([^"']+)(["'])/g,
|
|
188
|
+
/(import\s+["'])([^"']+)(["'])/g,
|
|
189
|
+
/(import\s*\(\s*["'])([^"']+)(["']\s*\))/g,
|
|
190
|
+
/(require\s*\(\s*["'])([^"']+)(["']\s*\))/g,
|
|
191
|
+
];
|
|
192
|
+
const isBareSpecifier = (specifier) => {
|
|
193
|
+
if (specifier.startsWith(".") || specifier.startsWith("/") || specifier.startsWith("\\")) {
|
|
194
|
+
return false;
|
|
195
|
+
}
|
|
196
|
+
if (specifier.startsWith("node:") ||
|
|
197
|
+
specifier.startsWith("bun:") ||
|
|
198
|
+
specifier.startsWith("http:") ||
|
|
199
|
+
specifier.startsWith("https:") ||
|
|
200
|
+
specifier.startsWith("file:") ||
|
|
201
|
+
specifier.startsWith("data:")) {
|
|
202
|
+
return false;
|
|
203
|
+
}
|
|
204
|
+
if (specifier.startsWith(RUNTIME_MODULE_PREFIX)) {
|
|
205
|
+
return false;
|
|
206
|
+
}
|
|
207
|
+
return true;
|
|
208
|
+
};
|
|
209
|
+
const registerResolveParent = (resolveParentsByRecency, resolveParent) => {
|
|
210
|
+
const existingIndex = resolveParentsByRecency.indexOf(resolveParent);
|
|
211
|
+
if (existingIndex >= 0) {
|
|
212
|
+
resolveParentsByRecency.splice(existingIndex, 1);
|
|
213
|
+
}
|
|
214
|
+
resolveParentsByRecency.push(resolveParent);
|
|
215
|
+
if (resolveParentsByRecency.length > MAX_RUNTIME_RESOLVE_PARENTS) {
|
|
216
|
+
resolveParentsByRecency.shift();
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
const rewriteImportSpecifiers = (code, resolveReplacement) => {
|
|
220
|
+
let transformedCode = code;
|
|
221
|
+
for (const pattern of resolveImportSpecifierPatterns) {
|
|
222
|
+
transformedCode = transformedCode.replace(pattern, (fullMatch, prefix, specifier, suffix) => {
|
|
223
|
+
const replacement = resolveReplacement(specifier);
|
|
224
|
+
if (!replacement || replacement === specifier) {
|
|
225
|
+
return fullMatch;
|
|
226
|
+
}
|
|
227
|
+
return `${prefix}${replacement}${suffix}`;
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
return transformedCode;
|
|
231
|
+
};
|
|
232
|
+
const collectImportSpecifiers = (code) => {
|
|
233
|
+
const specifiers = new Set();
|
|
234
|
+
for (const pattern of resolveImportSpecifierPatterns) {
|
|
235
|
+
code.replace(pattern, (_fullMatch, _prefix, specifier) => {
|
|
236
|
+
specifiers.add(specifier);
|
|
237
|
+
return _fullMatch;
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
return [...specifiers];
|
|
241
|
+
};
|
|
242
|
+
const resolveFromParent = (specifier, parent) => {
|
|
243
|
+
try {
|
|
244
|
+
const resolvedSpecifier = import.meta.resolve(specifier, parent);
|
|
245
|
+
if (resolvedSpecifier === specifier ||
|
|
246
|
+
resolvedSpecifier.startsWith("node:") ||
|
|
247
|
+
resolvedSpecifier.startsWith("bun:")) {
|
|
248
|
+
return null;
|
|
249
|
+
}
|
|
250
|
+
return resolvedSpecifier;
|
|
251
|
+
}
|
|
252
|
+
catch {
|
|
253
|
+
return null;
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
|
+
const resolveSourcePathFromSpecifier = (specifier, importer) => {
|
|
257
|
+
if (specifier.startsWith("node:") ||
|
|
258
|
+
specifier.startsWith("bun:") ||
|
|
259
|
+
specifier.startsWith("http:") ||
|
|
260
|
+
specifier.startsWith("https:") ||
|
|
261
|
+
specifier.startsWith("data:") ||
|
|
262
|
+
specifier.startsWith(RUNTIME_MODULE_PREFIX)) {
|
|
263
|
+
return null;
|
|
264
|
+
}
|
|
265
|
+
if (specifier.startsWith("file:")) {
|
|
266
|
+
return sourcePath(fileURLToPath(specifier));
|
|
267
|
+
}
|
|
268
|
+
if (isAbsolute(specifier)) {
|
|
269
|
+
return sourcePath(specifier);
|
|
270
|
+
}
|
|
271
|
+
const resolvedSpecifier = resolveFromParent(specifier, importer);
|
|
272
|
+
if (!resolvedSpecifier) {
|
|
273
|
+
return null;
|
|
274
|
+
}
|
|
275
|
+
if (resolvedSpecifier.startsWith("file:")) {
|
|
276
|
+
return sourcePath(fileURLToPath(resolvedSpecifier));
|
|
277
|
+
}
|
|
278
|
+
if (isAbsolute(resolvedSpecifier)) {
|
|
279
|
+
return sourcePath(resolvedSpecifier);
|
|
280
|
+
}
|
|
281
|
+
return null;
|
|
282
|
+
};
|
|
283
|
+
const rewriteImportsFromResolveParents = (code, resolveParentsByRecency) => {
|
|
284
|
+
if (resolveParentsByRecency.length === 0) {
|
|
285
|
+
return code;
|
|
286
|
+
}
|
|
287
|
+
const resolveFromParents = (specifier) => {
|
|
288
|
+
if (!isBareSpecifier(specifier)) {
|
|
289
|
+
return null;
|
|
290
|
+
}
|
|
291
|
+
for (let index = resolveParentsByRecency.length - 1; index >= 0; index -= 1) {
|
|
292
|
+
const resolveParent = resolveParentsByRecency[index];
|
|
293
|
+
const resolvedSpecifier = resolveFromParent(specifier, resolveParent);
|
|
294
|
+
if (resolvedSpecifier) {
|
|
295
|
+
return resolvedSpecifier;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
return null;
|
|
299
|
+
};
|
|
300
|
+
return rewriteImportSpecifiers(code, resolveFromParents);
|
|
301
|
+
};
|
|
302
|
+
const rewriteRuntimeSpecifiers = (code, runtimeModuleIdsBySpecifier) => {
|
|
303
|
+
return rewriteImportSpecifiers(code, (specifier) => {
|
|
304
|
+
const runtimeModuleId = runtimeModuleIdsBySpecifier.get(specifier);
|
|
305
|
+
return runtimeModuleId ?? null;
|
|
306
|
+
});
|
|
307
|
+
};
|
|
308
|
+
export function createRuntimePlugin(input = {}) {
|
|
309
|
+
const runtimeModules = new Map();
|
|
310
|
+
runtimeModules.set(CORE_RUNTIME_SPECIFIER, input.core ?? coreRuntime);
|
|
311
|
+
runtimeModules.set(CORE_TESTING_RUNTIME_SPECIFIER, loadCoreTestingRuntimeModule);
|
|
312
|
+
const rewriteOptions = resolveRuntimePluginRewriteOptions(input.rewrite);
|
|
313
|
+
for (const [specifier, moduleEntry] of Object.entries(input.additional ?? {})) {
|
|
314
|
+
runtimeModules.set(specifier, moduleEntry);
|
|
315
|
+
}
|
|
316
|
+
const runtimeModuleIdsBySpecifier = new Map();
|
|
317
|
+
for (const specifier of runtimeModules.keys()) {
|
|
318
|
+
runtimeModuleIdsBySpecifier.set(specifier, runtimeModuleIdForSpecifier(specifier));
|
|
319
|
+
}
|
|
320
|
+
return {
|
|
321
|
+
name: "bun-plugin-opentui-runtime-modules",
|
|
322
|
+
setup: (build) => {
|
|
323
|
+
const resolveParentsByRecency = [];
|
|
324
|
+
const installedRewriteLoaders = new Set();
|
|
325
|
+
const nodeModulesBareRewritePackageRoots = new Set();
|
|
326
|
+
const packageTypeByPackageJsonPath = new Map();
|
|
327
|
+
const sourceAnalysisByPath = new Map();
|
|
328
|
+
const nodeModulesRuntimeRewritePathsByPath = new Map();
|
|
329
|
+
const installRewriteLoader = (path) => {
|
|
330
|
+
const resolvedTargetPath = sourcePath(path);
|
|
331
|
+
const canonicalTargetPath = normalizeSourcePath(resolvedTargetPath);
|
|
332
|
+
if (installedRewriteLoaders.has(canonicalTargetPath)) {
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
installedRewriteLoaders.add(canonicalTargetPath);
|
|
336
|
+
// Register both the resolved path spelling and its canonical realpath so Bun
|
|
337
|
+
// can reach the loader even if it reports the same file through a different alias.
|
|
338
|
+
build.onLoad({ filter: exactPathFilter([resolvedTargetPath, canonicalTargetPath]) }, async (args) => {
|
|
339
|
+
const loadedPath = normalizeSourcePath(args.path);
|
|
340
|
+
if (loadedPath !== canonicalTargetPath) {
|
|
341
|
+
return undefined;
|
|
342
|
+
}
|
|
343
|
+
const nodeModulesPath = isNodeModulesPath(loadedPath);
|
|
344
|
+
const shouldRewriteRuntimeSpecifiers = !nodeModulesPath || rewriteOptions.nodeModulesRuntimeSpecifiers;
|
|
345
|
+
const shouldRewriteBareSpecifiers = !nodeModulesPath || rewriteOptions.nodeModulesBareSpecifiers;
|
|
346
|
+
const loader = runtimeLoaderForPath(args.path);
|
|
347
|
+
if (!loader) {
|
|
348
|
+
throw new Error(`Unable to determine runtime loader for path: ${args.path}`);
|
|
349
|
+
}
|
|
350
|
+
const contents = await Bun.file(loadedPath).text();
|
|
351
|
+
const runtimeRewrittenContents = shouldRewriteRuntimeSpecifiers
|
|
352
|
+
? rewriteRuntimeSpecifiers(contents, runtimeModuleIdsBySpecifier)
|
|
353
|
+
: contents;
|
|
354
|
+
if (runtimeRewrittenContents !== contents && shouldRewriteBareSpecifiers) {
|
|
355
|
+
registerResolveParent(resolveParentsByRecency, loadedPath);
|
|
356
|
+
}
|
|
357
|
+
const transformedContents = shouldRewriteBareSpecifiers
|
|
358
|
+
? rewriteImportsFromResolveParents(runtimeRewrittenContents, resolveParentsByRecency)
|
|
359
|
+
: runtimeRewrittenContents;
|
|
360
|
+
return {
|
|
361
|
+
contents: transformedContents,
|
|
362
|
+
loader,
|
|
363
|
+
};
|
|
364
|
+
});
|
|
365
|
+
};
|
|
366
|
+
const analyzeSourcePath = (path) => {
|
|
367
|
+
const normalizedPath = normalizeSourcePath(path);
|
|
368
|
+
const cachedAnalysis = sourceAnalysisByPath.get(normalizedPath);
|
|
369
|
+
if (cachedAnalysis) {
|
|
370
|
+
return cachedAnalysis;
|
|
371
|
+
}
|
|
372
|
+
let contents;
|
|
373
|
+
try {
|
|
374
|
+
contents = readFileSync(normalizedPath, "utf8");
|
|
375
|
+
}
|
|
376
|
+
catch (error) {
|
|
377
|
+
if (error && typeof error === "object" && "code" in error && error.code === "ENOENT") {
|
|
378
|
+
const analysis = { importSpecifiers: [], needsRuntimeSpecifierRewrite: false };
|
|
379
|
+
sourceAnalysisByPath.set(normalizedPath, analysis);
|
|
380
|
+
return analysis;
|
|
381
|
+
}
|
|
382
|
+
throw error;
|
|
383
|
+
}
|
|
384
|
+
const importSpecifiers = collectImportSpecifiers(contents);
|
|
385
|
+
const analysis = {
|
|
386
|
+
importSpecifiers,
|
|
387
|
+
needsRuntimeSpecifierRewrite: importSpecifiers.some((specifier) => runtimeModuleIdsBySpecifier.has(specifier)),
|
|
388
|
+
};
|
|
389
|
+
sourceAnalysisByPath.set(normalizedPath, analysis);
|
|
390
|
+
return analysis;
|
|
391
|
+
};
|
|
392
|
+
const collectNodeModulesRuntimeRewritePaths = (path, visiting = new Set()) => {
|
|
393
|
+
const normalizedPath = normalizeSourcePath(path);
|
|
394
|
+
if (!isNodeModulesEsmPath(normalizedPath, packageTypeByPackageJsonPath)) {
|
|
395
|
+
return [];
|
|
396
|
+
}
|
|
397
|
+
const cachedPaths = nodeModulesRuntimeRewritePathsByPath.get(normalizedPath);
|
|
398
|
+
if (cachedPaths) {
|
|
399
|
+
return cachedPaths;
|
|
400
|
+
}
|
|
401
|
+
if (visiting.has(normalizedPath)) {
|
|
402
|
+
return [];
|
|
403
|
+
}
|
|
404
|
+
visiting.add(normalizedPath);
|
|
405
|
+
const rewritePaths = new Set();
|
|
406
|
+
const analysis = analyzeSourcePath(normalizedPath);
|
|
407
|
+
if (analysis.needsRuntimeSpecifierRewrite) {
|
|
408
|
+
rewritePaths.add(normalizedPath);
|
|
409
|
+
}
|
|
410
|
+
for (const specifier of analysis.importSpecifiers) {
|
|
411
|
+
const resolvedPath = resolveSourcePathFromSpecifier(specifier, normalizedPath);
|
|
412
|
+
if (!resolvedPath || !isNodeModulesEsmPath(resolvedPath, packageTypeByPackageJsonPath)) {
|
|
413
|
+
continue;
|
|
414
|
+
}
|
|
415
|
+
for (const nestedPath of collectNodeModulesRuntimeRewritePaths(resolvedPath, visiting)) {
|
|
416
|
+
rewritePaths.add(nestedPath);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
visiting.delete(normalizedPath);
|
|
420
|
+
const resolvedRewritePaths = [...rewritePaths];
|
|
421
|
+
nodeModulesRuntimeRewritePathsByPath.set(normalizedPath, resolvedRewritePaths);
|
|
422
|
+
return resolvedRewritePaths;
|
|
423
|
+
};
|
|
424
|
+
for (const [specifier, moduleEntry] of runtimeModules.entries()) {
|
|
425
|
+
const moduleId = runtimeModuleIdsBySpecifier.get(specifier);
|
|
426
|
+
if (!moduleId) {
|
|
427
|
+
continue;
|
|
428
|
+
}
|
|
429
|
+
build.module(moduleId, async () => ({
|
|
430
|
+
exports: await resolveRuntimeModuleExports(moduleEntry),
|
|
431
|
+
loader: "object",
|
|
432
|
+
}));
|
|
433
|
+
build.onResolve({ filter: exactSpecifierFilter(specifier) }, () => ({ path: moduleId }));
|
|
434
|
+
}
|
|
435
|
+
build.onResolve({ filter: /.*/ }, (args) => {
|
|
436
|
+
if (runtimeModuleIdsBySpecifier.has(args.path) || args.path.startsWith(RUNTIME_MODULE_PREFIX)) {
|
|
437
|
+
return undefined;
|
|
438
|
+
}
|
|
439
|
+
const path = resolveSourcePathFromSpecifier(args.path, args.importer);
|
|
440
|
+
if (!path || !runtimeLoaderForPath(path)) {
|
|
441
|
+
return undefined;
|
|
442
|
+
}
|
|
443
|
+
const nodeModulesPath = isNodeModulesPath(path);
|
|
444
|
+
if (!nodeModulesPath) {
|
|
445
|
+
installRewriteLoader(path);
|
|
446
|
+
return undefined;
|
|
447
|
+
}
|
|
448
|
+
if (!rewriteOptions.nodeModulesRuntimeSpecifiers && !rewriteOptions.nodeModulesBareSpecifiers) {
|
|
449
|
+
return undefined;
|
|
450
|
+
}
|
|
451
|
+
for (const rewritePath of collectNodeModulesRuntimeRewritePaths(path)) {
|
|
452
|
+
installRewriteLoader(rewritePath);
|
|
453
|
+
}
|
|
454
|
+
const packageRoot = nodeModulesPackageRootForPath(path);
|
|
455
|
+
if (rewriteOptions.nodeModulesBareSpecifiers &&
|
|
456
|
+
packageRoot &&
|
|
457
|
+
nodeModulesBareRewritePackageRoots.has(packageRoot)) {
|
|
458
|
+
installRewriteLoader(path);
|
|
459
|
+
return undefined;
|
|
460
|
+
}
|
|
461
|
+
if (!rewriteOptions.nodeModulesRuntimeSpecifiers || !analyzeSourcePath(path).needsRuntimeSpecifierRewrite) {
|
|
462
|
+
return undefined;
|
|
463
|
+
}
|
|
464
|
+
if (rewriteOptions.nodeModulesBareSpecifiers && packageRoot) {
|
|
465
|
+
nodeModulesBareRewritePackageRoots.add(packageRoot);
|
|
466
|
+
}
|
|
467
|
+
installRewriteLoader(path);
|
|
468
|
+
return undefined;
|
|
469
|
+
});
|
|
470
|
+
},
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
//# sourceMappingURL=runtime-plugin.js.map
|