@tsrx/bun-plugin-hono 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Dominic Gannaway
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,86 @@
1
+ # @tsrx/bun-plugin-hono
2
+
3
+ Bun plugin for compiling `@tsrx/hono` `.tsrx` files.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pnpm add hono @tsrx/hono
9
+ pnpm add -D @tsrx/bun-plugin-hono
10
+ ```
11
+
12
+ Install `@tsrx/hono` directly so the TSRX TypeScript plugin can discover the
13
+ compiler from the application workspace under strict package managers.
14
+
15
+ ## Usage
16
+
17
+ ```ts
18
+ import tsrxHono from '@tsrx/bun-plugin-hono';
19
+
20
+ await Bun.build({
21
+ entrypoints: ['./src/App.tsrx'],
22
+ outdir: './dist',
23
+ plugins: [tsrxHono()],
24
+ });
25
+ ```
26
+
27
+ The default `server` mode targets `hono/jsx`. Use `tsrxHono({ mode: 'dom' })` for
28
+ browser builds targeting `hono/jsx/dom`. The plugin runs Bun's automatic JSX
29
+ transform and emits TSRX `<style>` blocks as virtual CSS modules.
30
+
31
+ To build server and browser entrypoints from one script, run two Bun builds and
32
+ share only the options that are independent of the Hono runtime:
33
+
34
+ ```ts
35
+ const common = {
36
+ splitting: true,
37
+ sourcemap: 'external',
38
+ };
39
+
40
+ for (const target of [
41
+ { mode: 'dom', entrypoint: './src/client-entry.tsrx', outdir: './dist/client' },
42
+ {
43
+ mode: 'server',
44
+ entrypoint: './src/server-entry.tsrx',
45
+ outdir: './dist/server',
46
+ },
47
+ ] as const) {
48
+ const result = await Bun.build({
49
+ ...common,
50
+ entrypoints: [target.entrypoint],
51
+ outdir: target.outdir,
52
+ plugins: [tsrxHono({ mode: target.mode })],
53
+ });
54
+
55
+ if (!result.success) throw new Error(`Bun build failed: ${target.mode}`);
56
+ }
57
+ ```
58
+
59
+ One plugin instance and one Bun build use one Hono mode. A `.tsrx` module used by
60
+ both entrypoint graphs is compiled separately in each build and must be valid for
61
+ both runtimes.
62
+
63
+ For `bun:test`, register the plugin from a preload:
64
+
65
+ ```ts
66
+ import tsrxHono from '@tsrx/bun-plugin-hono';
67
+
68
+ Bun.plugin(tsrxHono());
69
+ ```
70
+
71
+ Options are `mode`, `runtimeImports`, `platform`, and `emitCss`.
72
+
73
+ The plugin reads `tsrx.platform` (`web`, `ios`, or `android`) from the active
74
+ build tsconfig and specializes `import.meta.env.platform.*` at compile time.
75
+ `platform` is an optional explicit override and is independent of the Hono
76
+ `server`/`dom` mode.
77
+
78
+ `runtimeImports: 'direct'` is a limited helper-import mode for Hono: shared
79
+ helpers use `@tsrx/core/runtime/*`, while Hono-specific adapters still use
80
+ `@tsrx/hono/*` because Hono has no separate standalone runtime package. If the
81
+ compiled modules are published or built with direct imports, declare both packages
82
+ directly:
83
+
84
+ ```bash
85
+ pnpm add @tsrx/core @tsrx/hono hono
86
+ ```
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@tsrx/bun-plugin-hono",
3
+ "description": "Bun plugin for the TSRX Hono JSX targets",
4
+ "license": "MIT",
5
+ "author": "Dominic Gannaway",
6
+ "version": "0.1.0",
7
+ "type": "module",
8
+ "publishConfig": {
9
+ "access": "public"
10
+ },
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/tsrx-org/tsrx.git",
14
+ "directory": "packages/bun-plugin-hono"
15
+ },
16
+ "exports": {
17
+ ".": {
18
+ "types": "./types/index.d.ts",
19
+ "default": "./src/index.js"
20
+ }
21
+ },
22
+ "dependencies": {
23
+ "@tsrx/core": "0.2.2",
24
+ "@tsrx/hono": "0.1.0"
25
+ },
26
+ "peerDependencies": {
27
+ "bun": ">=1",
28
+ "hono": "^4.13.7"
29
+ },
30
+ "devDependencies": {
31
+ "@types/bun": "^1.3.9",
32
+ "typescript": "^5.9.3"
33
+ },
34
+ "files": [
35
+ "src",
36
+ "types"
37
+ ],
38
+ "scripts": {
39
+ "test": "pnpm -w test --project bun-plugin-hono",
40
+ "test:native": "bun test tests/native.test.ts"
41
+ }
42
+ }
package/src/index.js ADDED
@@ -0,0 +1,125 @@
1
+ /** @import { BunPlugin, Target, Transpiler } from 'bun' */
2
+ /** @import { Platform, RuntimeImportMode } from '@tsrx/hono' */
3
+
4
+ import { readFile } from 'node:fs/promises';
5
+ import { mergePlatformDefinitions, validatePlatform } from '@tsrx/core';
6
+ import { compile as compileServer } from '@tsrx/hono';
7
+ import { compile as compileDom } from '@tsrx/hono/dom';
8
+ import { resolveBuildPlatform } from '@tsrx/core/config';
9
+
10
+ const TSRX_EXTENSION_PATTERN = /\.tsrx$/;
11
+ const CSS_QUERY = '?tsrx-css&lang.css';
12
+ const CSS_QUERY_PATTERN = /\?tsrx-css&lang\.css$/;
13
+ const CSS_NAMESPACE = '@tsrx/bun-plugin-hono-css';
14
+
15
+ /**
16
+ * @typedef {'server' | 'dom'} TsrxHonoMode
17
+ * Hono-specific adapters remain under `@tsrx/hono/*` in direct mode.
18
+ *
19
+ * @typedef {{
20
+ * mode?: TsrxHonoMode,
21
+ * emitCss?: boolean,
22
+ * runtimeImports?: RuntimeImportMode,
23
+ * platform?: Platform,
24
+ * }} TsrxHonoBunPluginOptions
25
+ */
26
+
27
+ /** @param {string} jsx_import_source @param {Target | undefined} target */
28
+ function create_transpiler(jsx_import_source, target) {
29
+ const Transpiler = globalThis.Bun?.Transpiler;
30
+ if (typeof Transpiler !== 'function') {
31
+ throw new Error(
32
+ '@tsrx/bun-plugin-hono requires Bun.Transpiler to select the configured Hono JSX runtime.',
33
+ );
34
+ }
35
+
36
+ return new Transpiler({
37
+ loader: 'tsx',
38
+ target,
39
+ autoImportJSX: true,
40
+ tsconfig: {
41
+ compilerOptions: {
42
+ jsx: 'react-jsx',
43
+ jsxImportSource: jsx_import_source,
44
+ },
45
+ },
46
+ });
47
+ }
48
+
49
+ /**
50
+ * Compile `.tsrx` files and pass the resulting TSX through Bun's automatic
51
+ * JSX transform. Hono's server and DOM runtimes are explicit modes because
52
+ * changing only the JSX import source would leave compiler-injected helpers
53
+ * pointed at the wrong runtime.
54
+ *
55
+ * @param {TsrxHonoBunPluginOptions} [options]
56
+ * @returns {BunPlugin}
57
+ */
58
+ export function tsrxHono(options = {}) {
59
+ const mode = options.mode ?? 'server';
60
+ if (mode !== 'server' && mode !== 'dom') {
61
+ throw new TypeError(`@tsrx/bun-plugin-hono: invalid mode ${JSON.stringify(mode)}`);
62
+ }
63
+ const jsx_import_source = mode === 'dom' ? 'hono/jsx/dom' : 'hono/jsx';
64
+ const compile = mode === 'dom' ? compileDom : compileServer;
65
+ const emit_css = options.emitCss ?? true;
66
+ const explicit_platform = validatePlatform(options.platform);
67
+
68
+ /** @type {Map<string, string>} */
69
+ const css_cache = new Map();
70
+
71
+ /** @param {string} code @param {string} id @param {string | undefined} css @param {boolean} emit_css */
72
+ function append_css_import(code, id, css, emit_css) {
73
+ if (!emit_css || !css) {
74
+ css_cache.delete(id);
75
+ return code;
76
+ }
77
+ css_cache.set(id, css);
78
+ return `${code}\nimport ${JSON.stringify(id)};\n`;
79
+ }
80
+
81
+ return {
82
+ name: '@tsrx/bun-plugin-hono',
83
+
84
+ setup(build) {
85
+ const build_config = build.config ?? {};
86
+ const platform = resolveBuildPlatform({
87
+ root: build_config.root ?? process.cwd(),
88
+ tsconfig: typeof build_config.tsconfig === 'string' ? build_config.tsconfig : undefined,
89
+ platform: explicit_platform,
90
+ integration: '@tsrx/bun-plugin-hono',
91
+ });
92
+ const compile_options = { runtimeImports: options.runtimeImports, platform };
93
+ if (platform !== undefined && build.config) {
94
+ build.config.define = /** @type {Record<string, string>} */ (
95
+ mergePlatformDefinitions(build.config.define, platform, {
96
+ integration: 'Bun',
97
+ serialize: true,
98
+ })
99
+ );
100
+ }
101
+ const transpiler = create_transpiler(jsx_import_source, build_config.target);
102
+
103
+ build.onResolve({ filter: CSS_QUERY_PATTERN }, (args) => {
104
+ if (!css_cache.has(args.path)) return undefined;
105
+ return { path: args.path, namespace: CSS_NAMESPACE };
106
+ });
107
+
108
+ build.onLoad({ filter: CSS_QUERY_PATTERN, namespace: CSS_NAMESPACE }, (args) => ({
109
+ contents: css_cache.get(args.path) ?? '',
110
+ loader: 'css',
111
+ }));
112
+
113
+ build.onLoad({ filter: TSRX_EXTENSION_PATTERN, namespace: 'file' }, async (args) => {
114
+ const source = await readFile(args.path, 'utf-8');
115
+ const { code, css } = compile(source, args.path, compile_options);
116
+ const css_id = args.path + CSS_QUERY;
117
+ const output = append_css_import(code, css_id, css, emit_css);
118
+
119
+ return { contents: transpiler.transformSync(output), loader: 'js' };
120
+ });
121
+ },
122
+ };
123
+ }
124
+
125
+ export default tsrxHono;
@@ -0,0 +1,20 @@
1
+ import type { BunPlugin } from 'bun';
2
+ import type { Platform, RuntimeImportMode } from '@tsrx/hono';
3
+
4
+ export type TsrxHonoMode = 'server' | 'dom';
5
+
6
+ export interface TsrxHonoBunPluginOptions {
7
+ /** Selects `hono/jsx` or `hono/jsx/dom`; defaults to `server`. */
8
+ mode?: TsrxHonoMode;
9
+ /**
10
+ * Direct mode uses `@tsrx/core/runtime/*` for shared helpers. Hono-specific
11
+ * adapters remain under `@tsrx/hono/*` because Hono has no standalone runtime.
12
+ */
13
+ runtimeImports?: RuntimeImportMode;
14
+ /** Optional override; inferred from the active Bun build tsconfig by default. */
15
+ platform?: Platform;
16
+ emitCss?: boolean;
17
+ }
18
+
19
+ export function tsrxHono(options?: TsrxHonoBunPluginOptions): BunPlugin;
20
+ export default tsrxHono;