@tsrx/vite-plugin-preact 0.0.74 → 0.0.76

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 +4 -4
  2. package/src/index.js +52 -1
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.74",
6
+ "version": "0.0.76",
7
7
  "type": "module",
8
8
  "publishConfig": {
9
9
  "access": "public"
@@ -20,7 +20,8 @@
20
20
  }
21
21
  },
22
22
  "dependencies": {
23
- "@tsrx/preact": "0.1.49"
23
+ "@tsrx/core": "0.1.51",
24
+ "@tsrx/preact": "0.1.51"
24
25
  },
25
26
  "peerDependencies": {
26
27
  "vite": "*"
@@ -28,8 +29,7 @@
28
29
  "devDependencies": {
29
30
  "preact": "^10.27.0",
30
31
  "typescript": "^5.9.3",
31
- "vite": "^8.1.5",
32
- "@tsrx/core": "0.1.49"
32
+ "vite": "^8.1.5"
33
33
  },
34
34
  "files": [
35
35
  "src",
package/src/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  /** @import { Plugin } from 'vite' */
2
+ /** @import { DepScanTransformPlugin } from '@tsrx/core/types/vite/dep-scan' */
2
3
 
3
4
  /**
4
5
  * @typedef {{ code: string, map: unknown }} TsrxPreactTransformResult
@@ -14,7 +15,17 @@
14
15
  * (id: `\0${string}?tsrx-css&lang.css`): string,
15
16
  * (id: string): string | null,
16
17
  * }} TsrxPreactLoad
17
- * @typedef {Omit<Plugin, 'transform' | 'resolveId' | 'load'> & {
18
+ * @typedef {() => {
19
+ * optimizeDeps: {
20
+ * extensions: string[],
21
+ * rolldownOptions: {
22
+ * transform: { jsx: { importSource: string } },
23
+ * plugins: [DepScanTransformPlugin],
24
+ * },
25
+ * },
26
+ * }} TsrxPreactConfigHook
27
+ * @typedef {Omit<Plugin, 'config' | 'transform' | 'resolveId' | 'load'> & {
28
+ * config: TsrxPreactConfigHook,
18
29
  * transform: TsrxPreactTransform,
19
30
  * resolveId: TsrxPreactResolveId,
20
31
  * load: TsrxPreactLoad,
@@ -23,6 +34,7 @@
23
34
 
24
35
  import { transformWithOxc } from 'vite';
25
36
  import { compile } from '@tsrx/preact';
37
+ import { createDepScanTransformPlugin } from '@tsrx/core/vite/dep-scan';
26
38
 
27
39
  const TSRX_EXTENSION_PATTERN = /\.tsrx$/;
28
40
  const CSS_QUERY = '?tsrx-css&lang.css';
@@ -66,6 +78,26 @@ export function tsrxPreact(options = {}) {
66
78
  name: '@tsrx/vite-plugin-preact',
67
79
  enforce: 'pre',
68
80
 
81
+ config() {
82
+ return {
83
+ optimizeDeps: {
84
+ // The scanner externalizes anything that is not a known JS
85
+ // type unless its extension is listed here, so without this
86
+ // entry the dep-scan plugin below never runs.
87
+ extensions: ['.tsrx'],
88
+ rolldownOptions: {
89
+ // The scan runs its own jsx transform over the tsx the
90
+ // dep-scan plugin hands back, and that transform defaults to
91
+ // react's runtime. Point it at the configured source, or the
92
+ // scan fails outright on an unresolvable
93
+ // `react/jsx-dev-runtime` in a project that has no react.
94
+ transform: { jsx: { importSource: jsxImportSource } },
95
+ plugins: [create_dep_scan_plugin(jsxImportSource, compile_options)],
96
+ },
97
+ },
98
+ };
99
+ },
100
+
69
101
  resolveId(/** @type {string} */ source) {
70
102
  if (!source.includes(CSS_QUERY)) return null;
71
103
  if (source.startsWith('\0')) return source;
@@ -127,4 +159,23 @@ export function tsrxPreact(options = {}) {
127
159
  });
128
160
  }
129
161
 
162
+ /**
163
+ * @param {string} jsxImportSource
164
+ * @param {{ suspenseSource?: string }} compile_options
165
+ * @returns {DepScanTransformPlugin}
166
+ */
167
+ function create_dep_scan_plugin(jsxImportSource, compile_options) {
168
+ return createDepScanTransformPlugin({
169
+ name: '@tsrx/vite-plugin-preact:dep-scan',
170
+ filter: TSRX_EXTENSION_PATTERN,
171
+ compile: (code, id) => compile(code, id, compile_options),
172
+ // The main transform always emits automatic-runtime JSX, so the jsx
173
+ // runtime module is a dependency of every compiled `.tsrx` file. Import
174
+ // it explicitly so the scanner records it no matter how the scan's own
175
+ // jsx transform is configured — in dev it resolves `jsx-dev-runtime`,
176
+ // while the transform above emits plain `jsx-runtime`.
177
+ imports: [jsxImportSource + '/jsx-runtime'],
178
+ });
179
+ }
180
+
130
181
  export default tsrxPreact;