@tsrx/vite-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 +21 -0
- package/package.json +42 -0
- package/src/index.js +220 -0
- package/types/index.d.ts +21 -0
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/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tsrx/vite-plugin-hono",
|
|
3
|
+
"description": "Vite 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/vite-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
|
+
"vite": "^8.0.0"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"hono": "^4.13.7",
|
|
31
|
+
"typescript": "^5.9.3",
|
|
32
|
+
"vite": "^8.1.5",
|
|
33
|
+
"@tsrx/typescript-plugin": "0.4.3"
|
|
34
|
+
},
|
|
35
|
+
"files": [
|
|
36
|
+
"src",
|
|
37
|
+
"types"
|
|
38
|
+
],
|
|
39
|
+
"scripts": {
|
|
40
|
+
"typecheck:tests": "tsrx-tsc --noEmit -p tests/tsconfig.json"
|
|
41
|
+
}
|
|
42
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
/** @import { Plugin } from 'vite' */
|
|
2
|
+
/** @import { DepScanTransformPlugin } from '@tsrx/core/types/vite/dep-scan' */
|
|
3
|
+
/** @import { Platform, RuntimeImportMode } from '@tsrx/hono' */
|
|
4
|
+
|
|
5
|
+
import { perEnvironmentState, transformWithOxc } from 'vite';
|
|
6
|
+
import { mergePlatformDefinitions, validatePlatform } from '@tsrx/core';
|
|
7
|
+
import { compile as compileServer } from '@tsrx/hono';
|
|
8
|
+
import { compile as compileDom } from '@tsrx/hono/dom';
|
|
9
|
+
import { resolveBuildPlatform } from '@tsrx/core/config';
|
|
10
|
+
import { createDepScanTransformPlugin } from '@tsrx/core/vite/dep-scan';
|
|
11
|
+
|
|
12
|
+
const TSRX_EXTENSION_PATTERN = /\.tsrx$/;
|
|
13
|
+
const CSS_QUERY = '?tsrx-css&lang.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
|
+
* runtimeImports?: RuntimeImportMode,
|
|
22
|
+
* platform?: Platform,
|
|
23
|
+
* tsconfig?: string,
|
|
24
|
+
* }} TsrxHonoPluginOptions
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Compile `.tsrx` to Hono-flavoured TSX and finish the automatic JSX runtime
|
|
29
|
+
* transform in Vite. The explicit mode keeps compiler-injected Hono helpers
|
|
30
|
+
* aligned with the chosen `hono/jsx` or `hono/jsx/dom` runtime.
|
|
31
|
+
*
|
|
32
|
+
* @param {TsrxHonoPluginOptions} [options]
|
|
33
|
+
* @returns {Plugin}
|
|
34
|
+
*/
|
|
35
|
+
export function tsrxHono(options = {}) {
|
|
36
|
+
const mode = options.mode ?? 'server';
|
|
37
|
+
if (mode !== 'server' && mode !== 'dom') {
|
|
38
|
+
throw new TypeError(`@tsrx/vite-plugin-hono: invalid mode ${JSON.stringify(mode)}`);
|
|
39
|
+
}
|
|
40
|
+
const jsx_import_source = mode === 'dom' ? 'hono/jsx/dom' : 'hono/jsx';
|
|
41
|
+
const compile = mode === 'dom' ? compileDom : compileServer;
|
|
42
|
+
const explicit_platform = validatePlatform(options.platform);
|
|
43
|
+
let platform = explicit_platform;
|
|
44
|
+
const compile_options = { runtimeImports: options.runtimeImports, platform };
|
|
45
|
+
|
|
46
|
+
/** @param {import('vite').UserConfig} config */
|
|
47
|
+
function resolve_platform(config) {
|
|
48
|
+
platform = resolveBuildPlatform({
|
|
49
|
+
root: config.root ?? process.cwd(),
|
|
50
|
+
tsconfig:
|
|
51
|
+
options.tsconfig ??
|
|
52
|
+
/** @type {{ tsconfig?: string }} */ (/** @type {unknown} */ (config)).tsconfig,
|
|
53
|
+
platform: explicit_platform,
|
|
54
|
+
integration: '@tsrx/vite-plugin-hono',
|
|
55
|
+
});
|
|
56
|
+
compile_options.platform = platform;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Vite may share this plugin instance across build environments. Keep CSS
|
|
60
|
+
// owned by the environment that transformed the source so one environment's
|
|
61
|
+
// buildStart cannot invalidate another environment's virtual CSS modules.
|
|
62
|
+
const get_environment_css_cache = perEnvironmentState(() => new Map());
|
|
63
|
+
|
|
64
|
+
/** @param {{ environment: import('vite').Environment }} context */
|
|
65
|
+
function css_cache_for(context) {
|
|
66
|
+
return get_environment_css_cache(
|
|
67
|
+
/** @type {Parameters<typeof get_environment_css_cache>[0]} */ (context),
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** @param {Map<string, string>} css_cache @param {string} id @param {string | undefined} css */
|
|
72
|
+
function cache_css(css_cache, id, css) {
|
|
73
|
+
// Retain ownership after CSS removal so HMR can serve an empty module.
|
|
74
|
+
if (css || css_cache.has(id)) css_cache.set(id, css ?? '');
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** @param {Map<string, string>} css_cache @param {string} id */
|
|
78
|
+
function css_owner(css_cache, id) {
|
|
79
|
+
if (!id.endsWith(CSS_QUERY)) return null;
|
|
80
|
+
const owner = id.slice(id.startsWith('\0') ? 1 : 0, -CSS_QUERY.length);
|
|
81
|
+
return css_cache.has(owner) ? owner : null;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function update_css_cache(
|
|
85
|
+
/** @type {Map<string, string>} */ css_cache,
|
|
86
|
+
/** @type {string} */ source,
|
|
87
|
+
/** @type {string} */ id,
|
|
88
|
+
) {
|
|
89
|
+
const { css } = compile(source, id, compile_options);
|
|
90
|
+
cache_css(css_cache, id, css);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** @param {Map<string, string>} css_cache @param {string} code @param {string} id @param {string | undefined} css */
|
|
94
|
+
function append_css_import(css_cache, code, id, css) {
|
|
95
|
+
cache_css(css_cache, id, css);
|
|
96
|
+
return css ? `${code}\nimport ${JSON.stringify(id + CSS_QUERY)};\n` : code;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return /** @type {Plugin} */ ({
|
|
100
|
+
name: '@tsrx/vite-plugin-hono',
|
|
101
|
+
enforce: 'pre',
|
|
102
|
+
perEnvironmentStartEndDuringDev: true,
|
|
103
|
+
perEnvironmentWatchChangeDuringDev: true,
|
|
104
|
+
|
|
105
|
+
config(config = /** @type {import('vite').UserConfig} */ ({})) {
|
|
106
|
+
resolve_platform(config);
|
|
107
|
+
if (platform === undefined) return;
|
|
108
|
+
return {
|
|
109
|
+
define: mergePlatformDefinitions(config.define, platform, {
|
|
110
|
+
integration: 'Vite',
|
|
111
|
+
}),
|
|
112
|
+
};
|
|
113
|
+
},
|
|
114
|
+
|
|
115
|
+
configEnvironment(name, config = /** @type {import('vite').EnvironmentOptions} */ ({})) {
|
|
116
|
+
const discovers_dependencies =
|
|
117
|
+
name === 'client' || config.optimizeDeps?.noDiscovery === false;
|
|
118
|
+
if (!discovers_dependencies && platform === undefined) return;
|
|
119
|
+
|
|
120
|
+
return {
|
|
121
|
+
...(platform === undefined
|
|
122
|
+
? {}
|
|
123
|
+
: {
|
|
124
|
+
define: mergePlatformDefinitions(config.define, platform, {
|
|
125
|
+
integration: `Vite environment ${JSON.stringify(name)}`,
|
|
126
|
+
}),
|
|
127
|
+
}),
|
|
128
|
+
...(discovers_dependencies
|
|
129
|
+
? {
|
|
130
|
+
optimizeDeps: {
|
|
131
|
+
extensions: ['.tsrx'],
|
|
132
|
+
rolldownOptions: {
|
|
133
|
+
transform: { jsx: { importSource: jsx_import_source } },
|
|
134
|
+
plugins: [create_dep_scan_plugin(jsx_import_source, compile_options, compile)],
|
|
135
|
+
},
|
|
136
|
+
},
|
|
137
|
+
}
|
|
138
|
+
: {}),
|
|
139
|
+
};
|
|
140
|
+
},
|
|
141
|
+
|
|
142
|
+
resolveId(source) {
|
|
143
|
+
const css_cache = css_cache_for(this);
|
|
144
|
+
if (css_owner(css_cache, source) === null) return null;
|
|
145
|
+
if (source.startsWith('\0')) return source;
|
|
146
|
+
return '\0' + source;
|
|
147
|
+
},
|
|
148
|
+
|
|
149
|
+
load(id) {
|
|
150
|
+
if (!id.startsWith('\0')) return null;
|
|
151
|
+
const css_cache = css_cache_for(this);
|
|
152
|
+
const owner = css_owner(css_cache, id);
|
|
153
|
+
return owner === null ? null : css_cache.get(owner);
|
|
154
|
+
},
|
|
155
|
+
|
|
156
|
+
buildStart() {
|
|
157
|
+
css_cache_for(this).clear();
|
|
158
|
+
},
|
|
159
|
+
|
|
160
|
+
watchChange(id, { event }) {
|
|
161
|
+
if (event === 'delete') css_cache_for(this).delete(id);
|
|
162
|
+
},
|
|
163
|
+
|
|
164
|
+
async transform(code, id) {
|
|
165
|
+
if (!TSRX_EXTENSION_PATTERN.test(id)) return null;
|
|
166
|
+
|
|
167
|
+
const result = compile(code, id, compile_options);
|
|
168
|
+
const source = append_css_import(css_cache_for(this), result.code, id, result.css);
|
|
169
|
+
|
|
170
|
+
const transformed = await transformWithOxc(
|
|
171
|
+
source,
|
|
172
|
+
id,
|
|
173
|
+
{
|
|
174
|
+
lang: 'tsx',
|
|
175
|
+
sourcemap: true,
|
|
176
|
+
jsx: {
|
|
177
|
+
runtime: 'automatic',
|
|
178
|
+
importSource: jsx_import_source,
|
|
179
|
+
},
|
|
180
|
+
target: 'esnext',
|
|
181
|
+
},
|
|
182
|
+
result.map,
|
|
183
|
+
);
|
|
184
|
+
|
|
185
|
+
return { code: transformed.code, map: transformed.map };
|
|
186
|
+
},
|
|
187
|
+
|
|
188
|
+
async hotUpdate(options) {
|
|
189
|
+
if (!TSRX_EXTENSION_PATTERN.test(options.file)) return;
|
|
190
|
+
// Deleted files cannot be read. watchChange already removed their CSS.
|
|
191
|
+
if (options.type === 'delete') return options.modules;
|
|
192
|
+
const css_module = this.environment.moduleGraph.getModuleById(
|
|
193
|
+
'\0' + options.file + CSS_QUERY,
|
|
194
|
+
);
|
|
195
|
+
if (!css_module) return options.modules;
|
|
196
|
+
|
|
197
|
+
update_css_cache(css_cache_for(this), await options.read(), options.file);
|
|
198
|
+
|
|
199
|
+
this.environment.moduleGraph.invalidateModule(css_module);
|
|
200
|
+
return [...options.modules, css_module];
|
|
201
|
+
},
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* @param {string} jsx_import_source
|
|
207
|
+
* @param {{ runtimeImports?: RuntimeImportMode, platform?: Platform }} compile_options
|
|
208
|
+
* @param {(code: string, id: string, options?: object) => { code: string }} compile
|
|
209
|
+
* @returns {DepScanTransformPlugin}
|
|
210
|
+
*/
|
|
211
|
+
function create_dep_scan_plugin(jsx_import_source, compile_options, compile) {
|
|
212
|
+
return createDepScanTransformPlugin({
|
|
213
|
+
name: '@tsrx/vite-plugin-hono:dep-scan',
|
|
214
|
+
filter: TSRX_EXTENSION_PATTERN,
|
|
215
|
+
compile: (code, id) => compile(code, id, compile_options),
|
|
216
|
+
imports: [jsx_import_source + '/jsx-runtime'],
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
export default tsrxHono;
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Plugin } from 'vite';
|
|
2
|
+
import type { Platform, RuntimeImportMode } from '@tsrx/hono';
|
|
3
|
+
|
|
4
|
+
export type TsrxHonoMode = 'server' | 'dom';
|
|
5
|
+
|
|
6
|
+
export interface TsrxHonoPluginOptions {
|
|
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 tsconfig by default and must agree when both exist. */
|
|
15
|
+
platform?: Platform;
|
|
16
|
+
/** Optional tsconfig path override, resolved from Vite's project root. */
|
|
17
|
+
tsconfig?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function tsrxHono(options?: TsrxHonoPluginOptions): Plugin;
|
|
21
|
+
export default tsrxHono;
|