@tsrx/vite-plugin-preact 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/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "@tsrx/vite-plugin-preact",
3
+ "description": "Vite plugin for @tsrx/preact (.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/vite-plugin-preact"
15
+ },
16
+ "exports": {
17
+ ".": {
18
+ "types": "./types/index.d.ts",
19
+ "default": "./src/index.js"
20
+ }
21
+ },
22
+ "dependencies": {
23
+ "@tsrx/preact": "0.0.2"
24
+ },
25
+ "peerDependencies": {
26
+ "vite": "*"
27
+ },
28
+ "devDependencies": {
29
+ "preact": "^10.27.0",
30
+ "typescript": "^5.9.3",
31
+ "vite": "^8.0.0"
32
+ },
33
+ "files": [
34
+ "src",
35
+ "types"
36
+ ]
37
+ }
package/src/index.js ADDED
@@ -0,0 +1,75 @@
1
+ /** @import { Plugin } from 'vite' */
2
+
3
+ import { transformWithOxc } from 'vite';
4
+ import { compile } from '@tsrx/preact';
5
+
6
+ const TSRX_EXTENSION_PATTERN = /\.tsrx$/;
7
+ const CSS_QUERY = '?tsrx-css&lang.css';
8
+
9
+ /**
10
+ * Vite plugin for `.tsrx` files that compiles them via `@tsrx/preact` and then
11
+ * runs esbuild's JSX transform so the final output calls Preact's automatic
12
+ * `jsx-runtime`. Per-component `<style>` blocks are emitted as virtual CSS
13
+ * modules that are imported by the compiled JS output.
14
+ *
15
+ * @param {{
16
+ * jsxImportSource?: string,
17
+ * suspenseSource?: string,
18
+ * }} [options]
19
+ * @returns {Plugin}
20
+ */
21
+ export function tsrxPreact(options = {}) {
22
+ const jsxImportSource = options.jsxImportSource ?? 'preact';
23
+ const compile_options = {
24
+ suspenseSource: options.suspenseSource,
25
+ };
26
+
27
+ /** @type {Map<string, string>} */
28
+ const css_cache = new Map();
29
+
30
+ return {
31
+ name: '@tsrx/vite-plugin-preact',
32
+ enforce: 'pre',
33
+
34
+ resolveId(source) {
35
+ if (!source.includes(CSS_QUERY)) return null;
36
+ if (source.startsWith('\0')) return source;
37
+ return '\0' + source;
38
+ },
39
+
40
+ load(id) {
41
+ if (!id.startsWith('\0') || !id.includes(CSS_QUERY)) return null;
42
+ const key = id.slice(1).split('?')[0];
43
+ const css = css_cache.get(key);
44
+ return css ?? '';
45
+ },
46
+
47
+ async transform(code, id) {
48
+ if (!TSRX_EXTENSION_PATTERN.test(id)) return null;
49
+
50
+ const { code: tsx_code, css } = compile(code, id, compile_options);
51
+
52
+ let source = tsx_code;
53
+ if (css) {
54
+ css_cache.set(id, css.code);
55
+ source = `import ${JSON.stringify(id + CSS_QUERY)};\n${tsx_code}`;
56
+ } else {
57
+ css_cache.delete(id);
58
+ }
59
+
60
+ const result = await transformWithOxc(source, id, {
61
+ lang: 'tsx',
62
+ sourcemap: true,
63
+ jsx: {
64
+ runtime: 'automatic',
65
+ importSource: jsxImportSource,
66
+ },
67
+ target: 'esnext',
68
+ });
69
+
70
+ return { code: result.code, map: result.map };
71
+ },
72
+ };
73
+ }
74
+
75
+ export default tsrxPreact;
@@ -0,0 +1,9 @@
1
+ import type { Plugin } from 'vite';
2
+
3
+ export interface TsrxPreactPluginOptions {
4
+ jsxImportSource?: string;
5
+ suspenseSource?: string;
6
+ }
7
+
8
+ export function tsrxPreact(options?: TsrxPreactPluginOptions): Plugin;
9
+ export default tsrxPreact;