@tsrx/vite-plugin-preact 0.0.21 → 0.0.23

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.
Files changed (2) hide show
  1. package/package.json +2 -2
  2. package/src/index.js +28 -7
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "Vite plugin for @tsrx/preact (.tsrx modules)",
4
4
  "license": "MIT",
5
5
  "author": "Dominic Gannaway",
6
- "version": "0.0.21",
6
+ "version": "0.0.23",
7
7
  "type": "module",
8
8
  "publishConfig": {
9
9
  "access": "public"
@@ -20,7 +20,7 @@
20
20
  }
21
21
  },
22
22
  "dependencies": {
23
- "@tsrx/preact": "0.0.21"
23
+ "@tsrx/preact": "0.0.23"
24
24
  },
25
25
  "peerDependencies": {
26
26
  "vite": "*"
package/src/index.js CHANGED
@@ -1,5 +1,26 @@
1
1
  /** @import { Plugin } from 'vite' */
2
2
 
3
+ /**
4
+ * @typedef {{ code: string, map: unknown }} TsrxPreactTransformResult
5
+ * @typedef {{
6
+ * (code: string, id: `${string}.tsrx`): Promise<TsrxPreactTransformResult>,
7
+ * (code: string, id: string): Promise<TsrxPreactTransformResult | null>,
8
+ * }} TsrxPreactTransform
9
+ * @typedef {{
10
+ * (source: `${string}?tsrx-css&lang.css`): `\0${string}?tsrx-css&lang.css`,
11
+ * (source: string): string | null,
12
+ * }} TsrxPreactResolveId
13
+ * @typedef {{
14
+ * (id: `\0${string}?tsrx-css&lang.css`): string,
15
+ * (id: string): string | null,
16
+ * }} TsrxPreactLoad
17
+ * @typedef {Omit<Plugin, 'transform' | 'resolveId' | 'load'> & {
18
+ * transform: TsrxPreactTransform,
19
+ * resolveId: TsrxPreactResolveId,
20
+ * load: TsrxPreactLoad,
21
+ * }} TsrxPreactPlugin
22
+ */
23
+
3
24
  import { transformWithOxc } from 'vite';
4
25
  import { compile } from '@tsrx/preact';
5
26
 
@@ -16,7 +37,7 @@ const CSS_QUERY = '?tsrx-css&lang.css';
16
37
  * jsxImportSource?: string,
17
38
  * suspenseSource?: string,
18
39
  * }} [options]
19
- * @returns {Plugin}
40
+ * @returns {TsrxPreactPlugin}
20
41
  */
21
42
  export function tsrxPreact(options = {}) {
22
43
  const jsxImportSource = options.jsxImportSource ?? 'preact';
@@ -27,31 +48,31 @@ export function tsrxPreact(options = {}) {
27
48
  /** @type {Map<string, string>} */
28
49
  const css_cache = new Map();
29
50
 
30
- return {
51
+ return /** @type {TsrxPreactPlugin} */ ({
31
52
  name: '@tsrx/vite-plugin-preact',
32
53
  enforce: 'pre',
33
54
 
34
- resolveId(source) {
55
+ resolveId(/** @type {string} */ source) {
35
56
  if (!source.includes(CSS_QUERY)) return null;
36
57
  if (source.startsWith('\0')) return source;
37
58
  return '\0' + source;
38
59
  },
39
60
 
40
- load(id) {
61
+ load(/** @type {string} */ id) {
41
62
  if (!id.startsWith('\0') || !id.includes(CSS_QUERY)) return null;
42
63
  const key = id.slice(1).split('?')[0];
43
64
  const css = css_cache.get(key);
44
65
  return css ?? '';
45
66
  },
46
67
 
47
- async transform(code, id) {
68
+ async transform(/** @type {string} */ code, /** @type {string} */ id) {
48
69
  if (!TSRX_EXTENSION_PATTERN.test(id)) return null;
49
70
 
50
71
  const { code: tsx_code, css } = compile(code, id, compile_options);
51
72
 
52
73
  let source = tsx_code;
53
74
  if (css) {
54
- css_cache.set(id, css.code);
75
+ css_cache.set(id, css);
55
76
  source = `import ${JSON.stringify(id + CSS_QUERY)};\n${tsx_code}`;
56
77
  } else {
57
78
  css_cache.delete(id);
@@ -69,7 +90,7 @@ export function tsrxPreact(options = {}) {
69
90
 
70
91
  return { code: result.code, map: result.map };
71
92
  },
72
- };
93
+ });
73
94
  }
74
95
 
75
96
  export default tsrxPreact;