@tsrx/bun-plugin-solid 0.0.1

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-solid
2
+
3
+ Bun plugin for compiling `@tsrx/solid` `.tsrx` files.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pnpm add @tsrx/solid solid-js @solidjs/web
9
+ pnpm add -D @tsrx/bun-plugin-solid
10
+ ```
11
+
12
+ ## Usage
13
+
14
+ ```ts
15
+ import tsrxSolid from '@tsrx/bun-plugin-solid';
16
+
17
+ await Bun.build({
18
+ entrypoints: ['./src/App.tsrx'],
19
+ outdir: './dist',
20
+ target: 'browser',
21
+ plugins: [tsrxSolid()],
22
+ });
23
+ ```
24
+
25
+ The plugin compiles `.tsrx` modules with `@tsrx/solid`, runs Solid's JSX transform
26
+ through Babel, and emits component-local `<style>` blocks as virtual CSS modules.
27
+
28
+ For `bun:test`, register it from a preload:
29
+
30
+ ```ts
31
+ import tsrxSolid from '@tsrx/bun-plugin-solid';
32
+
33
+ Bun.plugin(tsrxSolid());
34
+ ```
35
+
36
+ ## Options
37
+
38
+ - `solid`: options forwarded to `babel-preset-solid`.
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,47 @@
1
+ {
2
+ "name": "@tsrx/bun-plugin-solid",
3
+ "description": "Bun plugin for @tsrx/solid (.tsrx modules)",
4
+ "license": "MIT",
5
+ "author": "Dominic Gannaway",
6
+ "version": "0.0.1",
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-solid"
15
+ },
16
+ "exports": {
17
+ ".": {
18
+ "types": "./types/index.d.ts",
19
+ "default": "./src/index.js"
20
+ }
21
+ },
22
+ "dependencies": {
23
+ "@babel/core": "^7.29.0",
24
+ "@babel/preset-typescript": "^7.28.5",
25
+ "babel-preset-solid": "2.0.0-beta.11",
26
+ "@tsrx/solid": "0.1.7"
27
+ },
28
+ "peerDependencies": {
29
+ "@solidjs/web": ">=2.0.0-beta.11",
30
+ "bun": "^1.0.0",
31
+ "solid-js": ">=2.0.0-beta.11"
32
+ },
33
+ "devDependencies": {
34
+ "@solidjs/web": ">=2.0.0-beta.11",
35
+ "@types/babel__core": "^7.20.5",
36
+ "@types/bun": "^1.3.9",
37
+ "solid-js": ">=2.0.0-beta.11",
38
+ "typescript": "^5.9.3"
39
+ },
40
+ "files": [
41
+ "src",
42
+ "types"
43
+ ],
44
+ "scripts": {
45
+ "test": "pnpm -w test --project bun-plugin-solid"
46
+ }
47
+ }
package/src/index.js ADDED
@@ -0,0 +1,146 @@
1
+ /** @import { BunPlugin } from 'bun' */
2
+
3
+ import { readFile } from 'node:fs/promises';
4
+ import { createRequire } from 'node:module';
5
+ import { compile } from '@tsrx/solid';
6
+
7
+ const require = createRequire(import.meta.url);
8
+ const { transformAsync } = require('@babel/core');
9
+ const BABEL_PRESET_SOLID = require.resolve('babel-preset-solid');
10
+ const BABEL_PRESET_TYPESCRIPT = require.resolve('@babel/preset-typescript');
11
+
12
+ const DEFAULT_INCLUDE = /\.tsrx$/;
13
+ const CSS_QUERY = '?tsrx-css&lang.css';
14
+ const CSS_QUERY_PATTERN = /\?tsrx-css&lang\.css$/;
15
+
16
+ /**
17
+ * @typedef {{
18
+ * include?: RegExp,
19
+ * exclude?: RegExp | RegExp[],
20
+ * emitCss?: boolean,
21
+ * solid?: object,
22
+ * }} TsrxSolidBunPluginOptions
23
+ */
24
+
25
+ /**
26
+ * @param {RegExp} pattern
27
+ * @param {string} value
28
+ * @returns {boolean}
29
+ */
30
+ function test_pattern(pattern, value) {
31
+ pattern.lastIndex = 0;
32
+ return pattern.test(value);
33
+ }
34
+
35
+ /**
36
+ * @param {RegExp | RegExp[] | undefined} pattern
37
+ * @param {string} value
38
+ * @returns {boolean}
39
+ */
40
+ function matches_pattern(pattern, value) {
41
+ if (!pattern) return false;
42
+ if (Array.isArray(pattern)) {
43
+ return pattern.some((entry) => test_pattern(entry, value));
44
+ }
45
+ return test_pattern(pattern, value);
46
+ }
47
+
48
+ /**
49
+ * @param {TsrxSolidBunPluginOptions} options
50
+ * @param {string} value
51
+ * @returns {boolean}
52
+ */
53
+ function should_compile(options, value) {
54
+ const include = options.include ?? DEFAULT_INCLUDE;
55
+ return test_pattern(include, value) && !matches_pattern(options.exclude, value);
56
+ }
57
+
58
+ /**
59
+ * @param {string} file_path
60
+ * @returns {string}
61
+ */
62
+ function to_css_id(file_path) {
63
+ return file_path + CSS_QUERY;
64
+ }
65
+
66
+ /**
67
+ * @param {string} source
68
+ * @param {string} file_path
69
+ * @param {object | undefined} solid_options
70
+ */
71
+ async function transform_solid(source, file_path, solid_options) {
72
+ const result = await transformAsync(source, {
73
+ filename: file_path.replace(/\.tsrx$/, '.tsx'),
74
+ babelrc: false,
75
+ configFile: false,
76
+ sourceMaps: true,
77
+ presets: [
78
+ [BABEL_PRESET_SOLID, solid_options ?? {}],
79
+ [
80
+ BABEL_PRESET_TYPESCRIPT,
81
+ {
82
+ allExtensions: true,
83
+ isTSX: true,
84
+ },
85
+ ],
86
+ ],
87
+ });
88
+
89
+ return result?.code ?? source;
90
+ }
91
+
92
+ /**
93
+ * Bun plugin for `.tsrx` files that compiles them through `@tsrx/solid`, runs
94
+ * Solid's JSX transform with Babel, and exposes component-local styles as
95
+ * virtual CSS modules.
96
+ *
97
+ * @param {TsrxSolidBunPluginOptions} [options]
98
+ * @returns {BunPlugin}
99
+ */
100
+ export function tsrxSolid(options = {}) {
101
+ const emit_css = options.emitCss ?? true;
102
+
103
+ /** @type {Map<string, string>} */
104
+ const css_cache = new Map();
105
+
106
+ return {
107
+ name: '@tsrx/bun-plugin-solid',
108
+
109
+ setup(build) {
110
+ build.onResolve({ filter: CSS_QUERY_PATTERN }, (args) => ({
111
+ path: args.path,
112
+ }));
113
+
114
+ build.onLoad({ filter: CSS_QUERY_PATTERN }, (args) => ({
115
+ contents: css_cache.get(args.path) ?? '',
116
+ loader: 'css',
117
+ }));
118
+
119
+ build.onLoad(
120
+ { filter: options.include ?? DEFAULT_INCLUDE, namespace: 'file' },
121
+ async (args) => {
122
+ if (!should_compile(options, args.path)) return undefined;
123
+
124
+ const source = await readFile(args.path, 'utf-8');
125
+ const { code, css } = compile(source, args.path);
126
+ const css_id = to_css_id(args.path);
127
+
128
+ let output = code;
129
+ if (emit_css && css) {
130
+ css_cache.set(css_id, css);
131
+ output = `import ${JSON.stringify(css_id)};\n${code}`;
132
+ } else {
133
+ css_cache.delete(css_id);
134
+ }
135
+
136
+ return {
137
+ contents: await transform_solid(output, args.path, options.solid),
138
+ loader: 'js',
139
+ };
140
+ },
141
+ );
142
+ },
143
+ };
144
+ }
145
+
146
+ export default tsrxSolid;
@@ -0,0 +1,11 @@
1
+ import type { BunPlugin } from 'bun';
2
+
3
+ export interface TsrxSolidBunPluginOptions {
4
+ include?: RegExp;
5
+ exclude?: RegExp | RegExp[];
6
+ emitCss?: boolean;
7
+ solid?: object;
8
+ }
9
+
10
+ export function tsrxSolid(options?: TsrxSolidBunPluginOptions): BunPlugin;
11
+ export default tsrxSolid;