@tsrx/bun-plugin-hono 0.1.13 → 0.1.15
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/README.md +7 -0
- package/package.json +3 -3
- package/src/index.js +147 -46
package/README.md
CHANGED
|
@@ -28,6 +28,13 @@ The default `server` mode targets `hono/jsx`. Use `tsrxHono({ mode: 'dom' })` fo
|
|
|
28
28
|
browser builds targeting `hono/jsx/dom`. The plugin runs Bun's automatic JSX
|
|
29
29
|
transform and emits TSRX `<style>` blocks as virtual CSS modules.
|
|
30
30
|
|
|
31
|
+
Bun selects the development or production JSX runtime for each build, as it does
|
|
32
|
+
for `.tsx` files. The production runtime needs an automatic JSX setting such as
|
|
33
|
+
`"jsx": "react-jsx"` in the tsconfig Bun applies: the nearest one for `Bun.build`,
|
|
34
|
+
the working directory's for `Bun.plugin()`. Under the classic `"jsx": "react"`,
|
|
35
|
+
the plugin keeps the automatic runtime, but Bun then always uses the Hono
|
|
36
|
+
`jsx-dev-runtime`.
|
|
37
|
+
|
|
31
38
|
To build server and browser entrypoints from one script, run two Bun builds and
|
|
32
39
|
share only the options that are independent of the Hono runtime:
|
|
33
40
|
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"description": "Bun plugin for the TSRX Hono JSX targets",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Dominic Gannaway",
|
|
6
|
-
"version": "0.1.
|
|
6
|
+
"version": "0.1.15",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"publishConfig": {
|
|
9
9
|
"access": "public"
|
|
@@ -20,8 +20,8 @@
|
|
|
20
20
|
}
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@tsrx/core": "0.
|
|
24
|
-
"@tsrx/hono": "0.
|
|
23
|
+
"@tsrx/core": "0.6.0",
|
|
24
|
+
"@tsrx/hono": "0.3.0"
|
|
25
25
|
},
|
|
26
26
|
"peerDependencies": {
|
|
27
27
|
"bun": ">=1",
|
package/src/index.js
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
|
-
/** @import { BunPlugin,
|
|
1
|
+
/** @import { BunPlugin, OnLoadResult } from 'bun' */
|
|
2
2
|
/** @import { Platform, RuntimeImportMode } from '@tsrx/hono' */
|
|
3
3
|
|
|
4
|
+
import { existsSync } from 'node:fs';
|
|
4
5
|
import { readFile } from 'node:fs/promises';
|
|
5
|
-
import
|
|
6
|
+
import path from 'node:path';
|
|
7
|
+
import { mergePlatformDefinitions, replacePlatformFlags, validatePlatform } from '@tsrx/core';
|
|
6
8
|
import { compile as compileServer } from '@tsrx/hono';
|
|
7
9
|
import { compile as compileDom } from '@tsrx/hono/dom';
|
|
8
|
-
import { resolveBuildPlatform } from '@tsrx/core/config';
|
|
10
|
+
import { resolveBuildPlatform, resolveTsconfigJsx } from '@tsrx/core/config';
|
|
9
11
|
|
|
10
12
|
const TSRX_EXTENSION_PATTERN = /\.tsrx$/;
|
|
11
13
|
const CSS_QUERY = '?tsrx-css&lang.css';
|
|
12
14
|
const CSS_QUERY_PATTERN = /\?tsrx-css&lang\.css$/;
|
|
13
|
-
const CSS_NAMESPACE = '@tsrx/bun-plugin-hono-css';
|
|
14
15
|
|
|
15
16
|
/**
|
|
16
17
|
* @typedef {'server' | 'dom'} TsrxHonoMode
|
|
@@ -24,26 +25,84 @@ const CSS_NAMESPACE = '@tsrx/bun-plugin-hono-css';
|
|
|
24
25
|
* }} TsrxHonoBunPluginOptions
|
|
25
26
|
*/
|
|
26
27
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
28
|
+
const RUNTIME_STATE = Symbol.for('@tsrx/bun-plugin.runtime.v1');
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @typedef {{
|
|
32
|
+
* loaders: Array<{ compiles: (file_path: string) => boolean, load: (file_path: string) => Promise<OnLoadResult> }>,
|
|
33
|
+
* css: Map<string, string>,
|
|
34
|
+
* }} RuntimeState
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* State that every TSRX Bun plugin instance registered with Bun.plugin()
|
|
39
|
+
* shares. There Bun calls only the first onLoad hook whose filter matches and
|
|
40
|
+
* needs an object back, so the instance it calls serves the stylesheets any
|
|
41
|
+
* instance compiled and hands on the `.tsrx` files it leaves out. The key has
|
|
42
|
+
* a version, so a later change to this shape can take a new key without
|
|
43
|
+
* breaking older plugins installed alongside. `Bun.plugin.clearAll()` doesn't
|
|
44
|
+
* tell plugins, so instances registered before it stay in the list.
|
|
45
|
+
*
|
|
46
|
+
* @returns {RuntimeState}
|
|
47
|
+
*/
|
|
48
|
+
function get_runtime_state() {
|
|
49
|
+
const scope = /** @type {any} */ (globalThis);
|
|
50
|
+
return (scope[RUNTIME_STATE] ??= { loaders: [], css: new Map() });
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Directory the tsconfig lookup starts from. Without `root`, Bun builds from
|
|
55
|
+
* the common ancestor directory of the entrypoints, or from the working
|
|
56
|
+
* directory when every entrypoint is an in-memory `files` entry. The nearest
|
|
57
|
+
* tsconfig is searched from there. A relative `tsconfig` option resolves from
|
|
58
|
+
* the working directory.
|
|
59
|
+
*
|
|
60
|
+
* @param {{ root?: string, tsconfig?: unknown, entrypoints?: unknown[], files?: object }} build_config
|
|
61
|
+
* @returns {string}
|
|
62
|
+
*/
|
|
63
|
+
function get_tsconfig_base(build_config) {
|
|
64
|
+
if (build_config.root) return build_config.root;
|
|
65
|
+
const entrypoints = (build_config.entrypoints ?? []).filter(
|
|
66
|
+
(entrypoint) => typeof entrypoint === 'string',
|
|
67
|
+
);
|
|
68
|
+
const virtual_files = new Set(
|
|
69
|
+
Object.keys(build_config.files ?? {}).map((file) => path.resolve(file)),
|
|
70
|
+
);
|
|
71
|
+
if (
|
|
72
|
+
typeof build_config.tsconfig === 'string' ||
|
|
73
|
+
entrypoints.every((entrypoint) => virtual_files.has(path.resolve(entrypoint)))
|
|
74
|
+
) {
|
|
75
|
+
return process.cwd();
|
|
34
76
|
}
|
|
35
77
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
78
|
+
let base = path.dirname(path.resolve(entrypoints[0]));
|
|
79
|
+
for (const entrypoint of entrypoints) {
|
|
80
|
+
const file_path = path.resolve(entrypoint);
|
|
81
|
+
while (/^\.\.(?:[/\\]|$)/.test(path.relative(base, file_path))) {
|
|
82
|
+
base = path.dirname(base);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return base;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Prefix compiled TSX with the pragma that selects the JSX import source, so
|
|
90
|
+
* Bun's own TSX transform picks the development or production runtime for the
|
|
91
|
+
* build. Bun ignores a lone `@jsxImportSource` under a classic `"jsx": "react"`
|
|
92
|
+
* tsconfig. There `@jsxRuntime automatic` keeps the automatic runtime, which
|
|
93
|
+
* Bun then takes from the development entry point in every build.
|
|
94
|
+
*
|
|
95
|
+
* @param {string} code
|
|
96
|
+
* @param {string} jsx_import_source
|
|
97
|
+
* @param {boolean} classic
|
|
98
|
+
* @returns {string}
|
|
99
|
+
*/
|
|
100
|
+
function add_jsx_pragma(code, jsx_import_source, classic) {
|
|
101
|
+
const runtime = classic ? '@jsxRuntime automatic ' : '';
|
|
102
|
+
const pragma = `/** ${runtime}@jsxImportSource ${jsx_import_source} */\n`;
|
|
103
|
+
// A shebang has to stay on the first line.
|
|
104
|
+
const shebang_end = code.startsWith('#!') ? code.indexOf('\n') + 1 || code.length : 0;
|
|
105
|
+
return code.slice(0, shebang_end) + pragma + code.slice(shebang_end);
|
|
47
106
|
}
|
|
48
107
|
|
|
49
108
|
/**
|
|
@@ -68,23 +127,13 @@ export function tsrxHono(options = {}) {
|
|
|
68
127
|
/** @type {Map<string, string>} */
|
|
69
128
|
const css_cache = new Map();
|
|
70
129
|
|
|
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
130
|
return {
|
|
82
131
|
name: '@tsrx/bun-plugin-hono',
|
|
83
132
|
|
|
84
133
|
setup(build) {
|
|
85
134
|
const build_config = build.config ?? {};
|
|
86
135
|
const platform = resolveBuildPlatform({
|
|
87
|
-
root: build_config
|
|
136
|
+
root: get_tsconfig_base(build_config),
|
|
88
137
|
tsconfig: typeof build_config.tsconfig === 'string' ? build_config.tsconfig : undefined,
|
|
89
138
|
platform: explicit_platform,
|
|
90
139
|
integration: '@tsrx/bun-plugin-hono',
|
|
@@ -98,26 +147,78 @@ export function tsrxHono(options = {}) {
|
|
|
98
147
|
})
|
|
99
148
|
);
|
|
100
149
|
}
|
|
101
|
-
|
|
150
|
+
// Bun.plugin() registration has no `define`, so the flags left in compiled
|
|
151
|
+
// output get their values written in directly.
|
|
152
|
+
const inline_platform = build.config ? undefined : platform;
|
|
153
|
+
/** @type {Map<string, unknown>} */
|
|
154
|
+
const tsconfig_cache = new Map();
|
|
155
|
+
// Bun.build() applies the tsconfig nearest each file. Bun.plugin()
|
|
156
|
+
// registration applies the working directory's own tsconfig, if any, to
|
|
157
|
+
// every file, without searching parent folders.
|
|
158
|
+
const runtime_classic_jsx =
|
|
159
|
+
!build.config &&
|
|
160
|
+
existsSync(path.join(process.cwd(), 'tsconfig.json')) &&
|
|
161
|
+
resolveTsconfigJsx(process.cwd(), tsconfig_cache) === 'react';
|
|
162
|
+
/** @param {string} file_path */
|
|
163
|
+
const uses_classic_jsx = (file_path) =>
|
|
164
|
+
build.config
|
|
165
|
+
? resolveTsconfigJsx(file_path, tsconfig_cache) === 'react'
|
|
166
|
+
: runtime_classic_jsx;
|
|
167
|
+
|
|
168
|
+
const runtime = build.config ? undefined : get_runtime_state();
|
|
169
|
+
const stylesheets = runtime ? runtime.css : css_cache;
|
|
102
170
|
|
|
171
|
+
// Every instance sees every virtual CSS request. Leave the ones another
|
|
172
|
+
// instance compiled to that instance, except under Bun.plugin(), where
|
|
173
|
+
// the first instance has to answer and every instance shares the sheets.
|
|
174
|
+
// The stylesheets stay in the file namespace, the only one where
|
|
175
|
+
// Bun.plugin() registration accepts the css loader.
|
|
103
176
|
build.onResolve({ filter: CSS_QUERY_PATTERN }, (args) => {
|
|
104
|
-
if (!
|
|
105
|
-
return { path: args.path
|
|
177
|
+
if (!stylesheets.has(args.path)) return undefined;
|
|
178
|
+
return { path: args.path };
|
|
106
179
|
});
|
|
107
180
|
|
|
108
|
-
build.onLoad({ filter: CSS_QUERY_PATTERN
|
|
109
|
-
|
|
110
|
-
loader: 'css'
|
|
111
|
-
|
|
181
|
+
build.onLoad({ filter: CSS_QUERY_PATTERN }, (args) => {
|
|
182
|
+
const css = stylesheets.get(args.path);
|
|
183
|
+
if (css !== undefined) return { contents: css, loader: 'css' };
|
|
184
|
+
return runtime ? { contents: '', loader: 'css' } : undefined;
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* @param {string} file_path
|
|
189
|
+
* @returns {Promise<OnLoadResult>}
|
|
190
|
+
*/
|
|
191
|
+
const load_tsrx = async (file_path) => {
|
|
192
|
+
const source = await readFile(file_path, 'utf-8');
|
|
193
|
+
const { code: compiled, css } = compile(source, file_path, compile_options);
|
|
194
|
+
const code =
|
|
195
|
+
inline_platform === undefined
|
|
196
|
+
? compiled
|
|
197
|
+
: replacePlatformFlags(compiled, file_path, inline_platform).code;
|
|
198
|
+
const css_id = file_path + CSS_QUERY;
|
|
112
199
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
200
|
+
let output = code;
|
|
201
|
+
if (emit_css && css) {
|
|
202
|
+
stylesheets.set(css_id, css);
|
|
203
|
+
output = `${code}\nimport ${JSON.stringify(css_id)};\n`;
|
|
204
|
+
} else {
|
|
205
|
+
stylesheets.delete(css_id);
|
|
206
|
+
}
|
|
118
207
|
|
|
119
|
-
return {
|
|
208
|
+
return {
|
|
209
|
+
contents: add_jsx_pragma(output, jsx_import_source, uses_classic_jsx(file_path)),
|
|
210
|
+
loader: 'tsx',
|
|
211
|
+
};
|
|
212
|
+
};
|
|
213
|
+
// Other TSRX plugins hand on the `.tsrx` files they leave out.
|
|
214
|
+
runtime?.loaders.push({
|
|
215
|
+
compiles: (file_path) => TSRX_EXTENSION_PATTERN.test(file_path),
|
|
216
|
+
load: load_tsrx,
|
|
120
217
|
});
|
|
218
|
+
|
|
219
|
+
build.onLoad({ filter: TSRX_EXTENSION_PATTERN, namespace: 'file' }, (args) =>
|
|
220
|
+
load_tsrx(args.path),
|
|
221
|
+
);
|
|
121
222
|
},
|
|
122
223
|
};
|
|
123
224
|
}
|