@tsrx/vite-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/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@tsrx/vite-plugin-solid",
3
+ "description": "Vite plugin for @tsrx/solid (.tsrx modules) targeting Solid 2.0",
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/vite-plugin-solid"
15
+ },
16
+ "exports": {
17
+ ".": {
18
+ "types": "./types/index.d.ts",
19
+ "default": "./src/index.js"
20
+ }
21
+ },
22
+ "dependencies": {
23
+ "@tsrx/solid": "0.0.1"
24
+ },
25
+ "peerDependencies": {
26
+ "vite": "*"
27
+ },
28
+ "devDependencies": {
29
+ "solid-js": "2.0.0-beta.7",
30
+ "typescript": "^5.9.3",
31
+ "vite": "^8.0.0",
32
+ "vite-plugin-solid": "3.0.0-next.5"
33
+ },
34
+ "files": [
35
+ "src",
36
+ "types"
37
+ ]
38
+ }
package/src/index.js ADDED
@@ -0,0 +1,156 @@
1
+ /** @import { Plugin } from 'vite' */
2
+
3
+ import { readFile } from 'node:fs/promises';
4
+ import { existsSync } from 'node:fs';
5
+ import { resolve as path_resolve, isAbsolute } from 'node:path';
6
+ import { compile } from '@tsrx/solid';
7
+
8
+ const DEFAULT_TSRX_PATTERN = /\.tsrx$/;
9
+ const VIRTUAL_TSX_SUFFIX = '.tsx';
10
+ const CSS_QUERY = '?tsrx-solid-css&lang.css';
11
+
12
+ /**
13
+ * Vite plugin that compiles `.tsrx` files to Solid-flavoured TSX via
14
+ * `@tsrx/solid`. It does not run Solid's JSX-DOM-expressions transform
15
+ * itself — instead it rewrites module ids so the upstream `vite-plugin-solid`
16
+ * can handle that stage. Per-component `<style>` blocks become virtual CSS
17
+ * modules that the compiled JS imports.
18
+ *
19
+ * @param {import('../types/index.js').TsrxSolidOptions} [options]
20
+ * @returns {Plugin}
21
+ */
22
+ export function tsrxSolid(options = {}) {
23
+ /** @type {Map<string, string>} */
24
+ const css_cache = new Map();
25
+
26
+ /** @type {string} */
27
+ let root_dir = process.cwd();
28
+
29
+ const include_pattern = options.include ?? DEFAULT_TSRX_PATTERN;
30
+
31
+ /**
32
+ * Decide whether a real (on-disk) path should be treated as a tsrx
33
+ * source module. Falls back to matching `.tsrx` when no custom
34
+ * `include` regex was supplied. Resets `lastIndex` before testing so
35
+ * user-supplied regexes with the `g` or `y` flag don't produce
36
+ * alternating true/false results across calls.
37
+ *
38
+ * @param {string} path
39
+ * @returns {boolean}
40
+ */
41
+ const is_tsrx_source = (path) => {
42
+ include_pattern.lastIndex = 0;
43
+ return include_pattern.test(path);
44
+ };
45
+
46
+ /**
47
+ * Detect the virtual id form produced by {@link resolveId} (real path
48
+ * plus a `.tsx` suffix). A real path that matches `include_pattern`
49
+ * becomes virtual once we append `.tsx`, so the check is: strip `.tsx`
50
+ * and see if the remainder would have been accepted as a tsrx source.
51
+ *
52
+ * @param {string} id
53
+ * @returns {boolean}
54
+ */
55
+ const is_virtual = (id) => {
56
+ if (!id.endsWith(VIRTUAL_TSX_SUFFIX)) return false;
57
+ return is_tsrx_source(id.slice(0, -VIRTUAL_TSX_SUFFIX.length));
58
+ };
59
+
60
+ /**
61
+ * @param {string} id
62
+ * @returns {string}
63
+ */
64
+ const to_real_path = (id) => {
65
+ const stripped = id.slice(0, -VIRTUAL_TSX_SUFFIX.length);
66
+ if (isAbsolute(stripped) && existsSync(stripped)) return stripped;
67
+ // Vitest sometimes strips the workspace root from ids; re-anchor them.
68
+ const re_anchored = path_resolve(root_dir, stripped.replace(/^\/+/, ''));
69
+ if (existsSync(re_anchored)) return re_anchored;
70
+ return stripped;
71
+ };
72
+
73
+ return {
74
+ name: '@tsrx/vite-plugin-solid',
75
+ enforce: 'pre',
76
+
77
+ configResolved(config) {
78
+ root_dir = config.root;
79
+ },
80
+
81
+ async resolveId(source, importer, options) {
82
+ // Intercept virtual CSS imports.
83
+ if (source.includes(CSS_QUERY)) {
84
+ if (source.startsWith('\0')) return source;
85
+ return '\0' + source;
86
+ }
87
+ if (is_virtual(source)) return source;
88
+
89
+ // Rewrite tsrx source imports to their virtual `<path>.tsx` form
90
+ // so downstream extension-based plugins pick the module up as TSX.
91
+ if (is_tsrx_source(source)) {
92
+ const resolved = await this.resolve(source, importer, { ...options, skipSelf: true });
93
+ if (resolved && !is_virtual(resolved.id)) {
94
+ return { ...resolved, id: resolved.id + VIRTUAL_TSX_SUFFIX };
95
+ }
96
+ if (resolved) return resolved;
97
+ // Fallback: when `this.resolve` can't resolve (e.g. an absolute
98
+ // path coming in as a root entry such as a vitest test file),
99
+ // still rewrite to the virtual `.tsx` id directly so `load`
100
+ // can read the real file.
101
+ return source + VIRTUAL_TSX_SUFFIX;
102
+ }
103
+ return null;
104
+ },
105
+
106
+ async load(id) {
107
+ if (id.startsWith('\0') && id.includes(CSS_QUERY)) {
108
+ const key = id.slice(1).split('?')[0];
109
+ return css_cache.get(key) ?? '';
110
+ }
111
+ if (!is_virtual(id)) return null;
112
+
113
+ const real_path = to_real_path(id.split('?')[0]);
114
+ const source = await readFile(real_path, 'utf-8');
115
+ const { code, css, map } = compile(source, real_path);
116
+
117
+ let final_code = code;
118
+ let final_map = /** @type {any} */ (map);
119
+ if (css) {
120
+ css_cache.set(real_path, css.code);
121
+ final_code = `import ${JSON.stringify(real_path + CSS_QUERY)};\n${code}`;
122
+ // The prepended import adds one line to the generated output;
123
+ // shift every mapping down by one line so source positions stay
124
+ // aligned. In VLQ source maps, each `;` separates generated
125
+ // lines, so prefixing one `;` offsets all mappings by one line.
126
+ if (final_map && typeof final_map.mappings === 'string') {
127
+ final_map = { ...final_map, mappings: ';' + final_map.mappings };
128
+ }
129
+ } else {
130
+ css_cache.delete(real_path);
131
+ }
132
+
133
+ return { code: final_code, map: final_map };
134
+ },
135
+
136
+ handleHotUpdate(ctx) {
137
+ if (!is_tsrx_source(ctx.file)) return;
138
+ // Invalidate the virtual `<path>.tsx` module so Vite re-runs `load`.
139
+ // Also invalidate the virtual CSS module — Vite doesn't cascade
140
+ // invalidation from importer to importee, so without this the CSS
141
+ // module keeps serving the cached content and `<style>` edits in
142
+ // `.tsrx` files wouldn't hot-reload.
143
+ const virtual_id = ctx.file + VIRTUAL_TSX_SUFFIX;
144
+ const css_virtual_id = '\0' + ctx.file + CSS_QUERY;
145
+ const extra = [];
146
+ const mod = ctx.server.moduleGraph.getModuleById(virtual_id);
147
+ if (mod) extra.push(mod);
148
+ const css_mod = ctx.server.moduleGraph.getModuleById(css_virtual_id);
149
+ if (css_mod) extra.push(css_mod);
150
+ if (extra.length > 0) return [...extra, ...ctx.modules];
151
+ return ctx.modules;
152
+ },
153
+ };
154
+ }
155
+
156
+ export default tsrxSolid;
@@ -0,0 +1,15 @@
1
+ import type { Plugin } from 'vite';
2
+
3
+ export interface TsrxSolidOptions {
4
+ /**
5
+ * Regular expression matched against file paths to decide which modules
6
+ * the plugin should compile as tsrx sources. Defaults to `/\.tsrx$/`,
7
+ * i.e. any file whose path ends in `.tsrx`. Override when you want to
8
+ * compile additional extensions (e.g. `/\.(tsrx|foo)$/`) or narrow the
9
+ * set of `.tsrx` files that should go through this plugin.
10
+ */
11
+ include?: RegExp;
12
+ }
13
+
14
+ export function tsrxSolid(options?: TsrxSolidOptions): Plugin;
15
+ export default tsrxSolid;