@tsrx/bun-plugin-vue 0.0.2

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,40 @@
1
+ # @tsrx/bun-plugin-vue
2
+
3
+ Bun plugin for compiling `@tsrx/vue` `.tsrx` files.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pnpm add -D @tsrx/bun-plugin-vue
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```ts
14
+ import tsrxVue from '@tsrx/bun-plugin-vue';
15
+
16
+ await Bun.build({
17
+ entrypoints: ['./src/App.tsrx'],
18
+ outdir: './dist',
19
+ target: 'browser',
20
+ plugins: [tsrxVue()],
21
+ });
22
+ ```
23
+
24
+ The plugin compiles `.tsrx` modules with `@tsrx/vue`, runs the downstream
25
+ `vue-jsx-vapor` transform, strips the remaining TypeScript syntax with Bun, and
26
+ emits component-local `<style>` blocks as virtual CSS modules.
27
+
28
+ For `bun:test`, register it from a preload:
29
+
30
+ ```ts
31
+ import tsrxVue from '@tsrx/bun-plugin-vue';
32
+
33
+ Bun.plugin(tsrxVue());
34
+ ```
35
+
36
+ ## Options
37
+
38
+ - `vapor`: options forwarded to `vue-jsx-vapor`.
39
+ - `emitCss`: whether to emit virtual CSS imports (default: `true`).
40
+ - `include`, `exclude`: regex filters for source files.
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@tsrx/bun-plugin-vue",
3
+ "description": "Bun plugin for @tsrx/vue (.tsrx modules)",
4
+ "license": "MIT",
5
+ "author": "Dominic Gannaway",
6
+ "version": "0.0.2",
7
+ "type": "module",
8
+ "publishConfig": {
9
+ "access": "public"
10
+ },
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/Ripple-TS/ripple.git",
14
+ "directory": "packages/bun-plugin-vue"
15
+ },
16
+ "exports": {
17
+ ".": {
18
+ "types": "./types/index.d.ts",
19
+ "default": "./src/index.js"
20
+ }
21
+ },
22
+ "dependencies": {
23
+ "@tsrx/vue": "0.0.22"
24
+ },
25
+ "peerDependencies": {
26
+ "bun": "^1.0.0",
27
+ "vue": ">=3.5",
28
+ "vue-jsx-vapor": ">=3.2.12"
29
+ },
30
+ "devDependencies": {
31
+ "@types/bun": "^1.3.9",
32
+ "typescript": "^5.9.3",
33
+ "vue": "3.6.0-beta.10",
34
+ "vue-jsx-vapor": "^3.2.12"
35
+ },
36
+ "files": [
37
+ "src",
38
+ "types"
39
+ ],
40
+ "scripts": {
41
+ "test": "pnpm -w test --project bun-plugin-vue"
42
+ }
43
+ }
package/src/index.js ADDED
@@ -0,0 +1,179 @@
1
+ /** @import { BunPlugin, Target, Transpiler } from 'bun' */
2
+
3
+ import { readFile } from 'node:fs/promises';
4
+ import { createRequire } from 'node:module';
5
+ import { compile } from '@tsrx/vue';
6
+
7
+ const require = createRequire(import.meta.url);
8
+ const { transformVueJsxVapor } = require('vue-jsx-vapor/api');
9
+
10
+ const DEFAULT_INCLUDE = /\.tsrx$/;
11
+ const CSS_QUERY = '?tsrx-css&lang.css';
12
+ const CSS_QUERY_PATTERN = /\?tsrx-css&lang\.css$/;
13
+ const DEFAULT_VAPOR_OPTIONS = {
14
+ macros: true,
15
+ compiler: {
16
+ runtimeModuleName: 'vue-jsx-vapor',
17
+ },
18
+ };
19
+
20
+ /**
21
+ * @typedef {{
22
+ * include?: RegExp,
23
+ * exclude?: RegExp | RegExp[],
24
+ * emitCss?: boolean,
25
+ * vapor?: {
26
+ * interop?: boolean,
27
+ * macros?: boolean | object,
28
+ * compiler?: { runtimeModuleName?: string },
29
+ * },
30
+ * }} TsrxVueBunPluginOptions
31
+ */
32
+
33
+ /**
34
+ * @param {RegExp} pattern
35
+ * @param {string} value
36
+ * @returns {boolean}
37
+ */
38
+ function test_pattern(pattern, value) {
39
+ pattern.lastIndex = 0;
40
+ return pattern.test(value);
41
+ }
42
+
43
+ /**
44
+ * @param {RegExp | RegExp[] | undefined} pattern
45
+ * @param {string} value
46
+ * @returns {boolean}
47
+ */
48
+ function matches_pattern(pattern, value) {
49
+ if (!pattern) return false;
50
+ if (Array.isArray(pattern)) {
51
+ return pattern.some((entry) => test_pattern(entry, value));
52
+ }
53
+ return test_pattern(pattern, value);
54
+ }
55
+
56
+ /**
57
+ * @param {TsrxVueBunPluginOptions} options
58
+ * @param {string} value
59
+ * @returns {boolean}
60
+ */
61
+ function should_compile(options, value) {
62
+ const include = options.include ?? DEFAULT_INCLUDE;
63
+ return test_pattern(include, value) && !matches_pattern(options.exclude, value);
64
+ }
65
+
66
+ /**
67
+ * @param {string} file_path
68
+ * @returns {string}
69
+ */
70
+ function to_css_id(file_path) {
71
+ return file_path + CSS_QUERY;
72
+ }
73
+
74
+ /**
75
+ * @param {Target | undefined} target
76
+ * @returns {Transpiler | null}
77
+ */
78
+ function create_transpiler(target) {
79
+ const Transpiler = globalThis.Bun?.Transpiler;
80
+ if (typeof Transpiler !== 'function') return null;
81
+
82
+ return new Transpiler({
83
+ loader: 'ts',
84
+ target,
85
+ });
86
+ }
87
+
88
+ /**
89
+ * @param {TsrxVueBunPluginOptions['vapor']} options
90
+ */
91
+ function resolve_vapor_options(options) {
92
+ return {
93
+ ...DEFAULT_VAPOR_OPTIONS,
94
+ ...options,
95
+ compiler: {
96
+ ...DEFAULT_VAPOR_OPTIONS.compiler,
97
+ ...options?.compiler,
98
+ },
99
+ };
100
+ }
101
+
102
+ /**
103
+ * Bun plugin for `.tsrx` files that compiles them through `@tsrx/vue`, runs
104
+ * the downstream `vue-jsx-vapor` transform, then strips the remaining
105
+ * TypeScript syntax with Bun. Component-local styles are exposed as virtual CSS
106
+ * modules.
107
+ *
108
+ * @param {TsrxVueBunPluginOptions} [options]
109
+ * @returns {BunPlugin}
110
+ */
111
+ export function tsrxVue(options = {}) {
112
+ const emit_css = options.emitCss ?? true;
113
+ const vapor_options = resolve_vapor_options(options.vapor);
114
+
115
+ /** @type {Map<string, string>} */
116
+ const css_cache = new Map();
117
+
118
+ return {
119
+ name: '@tsrx/bun-plugin-vue',
120
+
121
+ setup(build) {
122
+ // build.config is only present for Bun.build(); runtime registration
123
+ // via Bun.plugin(), including bun:test preloads, does not provide it.
124
+ const build_config = build.config ?? {};
125
+ const transpiler = create_transpiler(build_config.target);
126
+
127
+ build.onResolve({ filter: CSS_QUERY_PATTERN }, (args) => ({
128
+ path: args.path,
129
+ }));
130
+
131
+ build.onLoad({ filter: CSS_QUERY_PATTERN }, (args) => ({
132
+ contents: css_cache.get(args.path) ?? '',
133
+ loader: 'css',
134
+ }));
135
+
136
+ build.onLoad(
137
+ { filter: options.include ?? DEFAULT_INCLUDE, namespace: 'file' },
138
+ async (args) => {
139
+ if (!should_compile(options, args.path)) return undefined;
140
+
141
+ const source = await readFile(args.path, 'utf-8');
142
+ const { code, css } = compile(source, args.path);
143
+ const css_id = to_css_id(args.path);
144
+
145
+ let output = code;
146
+ if (emit_css && css) {
147
+ css_cache.set(css_id, css);
148
+ output = `import ${JSON.stringify(css_id)};\n${code}`;
149
+ } else {
150
+ css_cache.delete(css_id);
151
+ }
152
+
153
+ const transformed = transformVueJsxVapor(
154
+ output,
155
+ args.path.replace(/\.tsrx$/, '.tsx'),
156
+ vapor_options,
157
+ false,
158
+ false,
159
+ false,
160
+ );
161
+
162
+ if (transpiler) {
163
+ return {
164
+ contents: transpiler.transformSync(transformed.code),
165
+ loader: 'js',
166
+ };
167
+ }
168
+
169
+ return {
170
+ contents: transformed.code,
171
+ loader: 'ts',
172
+ };
173
+ },
174
+ );
175
+ },
176
+ };
177
+ }
178
+
179
+ export default tsrxVue;
@@ -0,0 +1,19 @@
1
+ import type { BunPlugin } from 'bun';
2
+
3
+ export interface TsrxVueBunPluginVaporOptions {
4
+ interop?: boolean;
5
+ macros?: boolean | object;
6
+ compiler?: {
7
+ runtimeModuleName?: string;
8
+ };
9
+ }
10
+
11
+ export interface TsrxVueBunPluginOptions {
12
+ include?: RegExp;
13
+ exclude?: RegExp | RegExp[];
14
+ emitCss?: boolean;
15
+ vapor?: TsrxVueBunPluginVaporOptions;
16
+ }
17
+
18
+ export function tsrxVue(options?: TsrxVueBunPluginOptions): BunPlugin;
19
+ export default tsrxVue;