@tsrx/vite-plugin-solid 0.0.116 → 0.0.118

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/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "Vite plugin for @tsrx/solid (.tsrx modules) targeting Solid 2.0",
4
4
  "license": "MIT",
5
5
  "author": "Dominic Gannaway",
6
- "version": "0.0.116",
6
+ "version": "0.0.118",
7
7
  "type": "module",
8
8
  "publishConfig": {
9
9
  "access": "public"
@@ -20,8 +20,8 @@
20
20
  }
21
21
  },
22
22
  "dependencies": {
23
- "@tsrx/core": "0.5.4",
24
- "@tsrx/solid": "0.3.4"
23
+ "@tsrx/core": "0.6.0",
24
+ "@tsrx/solid": "0.4.0"
25
25
  },
26
26
  "peerDependencies": {
27
27
  "vite": "*"
package/src/index.js CHANGED
@@ -3,10 +3,12 @@
3
3
  import { readFile } from 'node:fs/promises';
4
4
  import { existsSync } from 'node:fs';
5
5
  import { resolve as path_resolve, isAbsolute } from 'node:path';
6
+ import { normalizePath, parseSync } from 'vite';
6
7
  import { compile } from '@tsrx/solid';
7
8
  import { mergePlatformDefinitions, validatePlatform } from '@tsrx/core';
8
9
  import { resolveBuildPlatform } from '@tsrx/core/config';
9
10
  import { createDepScanLoadPlugin } from '@tsrx/core/vite/dep-scan';
11
+ import { findUnconfiguredPlatformFlag, restartOnPlatformChange } from '@tsrx/core/vite/platform';
10
12
  import { createWorkerEntryMiddleware, stripWorkerEntryQuery } from '@tsrx/core/vite/worker';
11
13
 
12
14
  const DEFAULT_TSRX_PATTERN = /\.tsrx$/;
@@ -16,6 +18,22 @@ const VIRTUAL_TSX_SUFFIX = '.tsx';
16
18
  // CSS from the component's directory.
17
19
  const CSS_QUERY = '?tsrx-solid-css&lang.css';
18
20
 
21
+ /**
22
+ * Whether Vite runs a hook for a server build. Vite 6+ names the environment;
23
+ * older versions only pass the `ssr` flag, which vite-plugin-solid reads and
24
+ * Vite 6+ still passes.
25
+ *
26
+ * @param {unknown} context the hook's `this`
27
+ * @param {{ ssr?: boolean } | undefined} options the hook's options
28
+ * @returns {boolean}
29
+ */
30
+ function is_server_build(context, options) {
31
+ const consumer = /** @type {{ environment?: { config?: { consumer?: string } } } | undefined} */ (
32
+ context
33
+ )?.environment?.config?.consumer;
34
+ return consumer ? consumer === 'server' : !!options?.ssr;
35
+ }
36
+
19
37
  /**
20
38
  * Vite plugin that compiles `.tsrx` files to Solid-flavoured TSX via
21
39
  * `@tsrx/solid`. It does not run Solid's JSX-DOM-expressions transform
@@ -38,16 +56,20 @@ export function tsrxSolid(options = {}) {
38
56
  const include_pattern = options.include ?? DEFAULT_TSRX_PATTERN;
39
57
  const compile_options = { runtimeImports: options.runtimeImports, platform };
40
58
 
59
+ /** @type {import('@tsrx/core/config').BuildPlatformResolutionOptions} */
60
+ let platform_resolution = {};
61
+
41
62
  /** @param {import('vite').UserConfig} config */
42
63
  function resolve_platform(config) {
43
- platform = resolveBuildPlatform({
64
+ platform_resolution = {
44
65
  root: config.root ?? process.cwd(),
45
66
  tsconfig:
46
67
  options.tsconfig ??
47
68
  /** @type {{ tsconfig?: string }} */ (/** @type {unknown} */ (config)).tsconfig,
48
69
  platform: explicit_platform,
49
70
  integration: '@tsrx/vite-plugin-solid',
50
- });
71
+ };
72
+ platform = resolveBuildPlatform(platform_resolution);
51
73
  compile_options.platform = platform;
52
74
  }
53
75
 
@@ -97,37 +119,40 @@ export function tsrxSolid(options = {}) {
97
119
  name: '@tsrx/vite-plugin-solid',
98
120
  enforce: 'pre',
99
121
 
100
- config(config = /** @type {import('vite').UserConfig} */ ({})) {
101
- resolve_platform(config);
102
- return {
103
- ...(platform === undefined
104
- ? {}
105
- : {
106
- define: mergePlatformDefinitions(config.define, platform, {
107
- integration: 'Vite',
122
+ config: {
123
+ order: 'post',
124
+ handler(config = /** @type {import('vite').UserConfig} */ ({})) {
125
+ resolve_platform(config);
126
+ return {
127
+ ...(platform === undefined
128
+ ? {}
129
+ : {
130
+ define: mergePlatformDefinitions(config.define, platform, {
131
+ integration: 'Vite',
132
+ }),
108
133
  }),
109
- }),
110
- optimizeDeps: {
111
- rolldownOptions: {
112
- // The scan runs its own jsx transform over the tsx the
113
- // dep-scan plugin hands back, and it defaults to react's
114
- // automatic runtime — which no Solid project has installed,
115
- // so the scan would fail on an unresolvable
116
- // `react/jsx-dev-runtime`. Solid's own jsx handling belongs
117
- // to `vite-plugin-solid` downstream; the scan only needs the
118
- // imports, so leave the jsx alone.
119
- transform: { jsx: 'preserve' },
120
- plugins: [
121
- createDepScanLoadPlugin({
122
- name: '@tsrx/vite-plugin-solid:dep-scan',
123
- isVirtual: is_virtual,
124
- toRealPath: to_real_path,
125
- compile: (code, id) => compile(code, id, compile_options),
126
- }),
127
- ],
134
+ optimizeDeps: {
135
+ rolldownOptions: {
136
+ // The scan runs its own jsx transform over the tsx the
137
+ // dep-scan plugin hands back, and it defaults to react's
138
+ // automatic runtime — which no Solid project has installed,
139
+ // so the scan would fail on an unresolvable
140
+ // `react/jsx-dev-runtime`. Solid's own jsx handling belongs
141
+ // to `vite-plugin-solid` downstream; the scan only needs the
142
+ // imports, so leave the jsx alone.
143
+ transform: { jsx: 'preserve' },
144
+ plugins: [
145
+ createDepScanLoadPlugin({
146
+ name: '@tsrx/vite-plugin-solid:dep-scan',
147
+ isVirtual: is_virtual,
148
+ toRealPath: to_real_path,
149
+ compile: (code, id) => compile(code, id, compile_options),
150
+ }),
151
+ ],
152
+ },
128
153
  },
129
- },
130
- };
154
+ };
155
+ },
131
156
  },
132
157
 
133
158
  configEnvironment(name, config = /** @type {import('vite').EnvironmentOptions} */ ({})) {
@@ -144,6 +169,7 @@ export function tsrxSolid(options = {}) {
144
169
  },
145
170
 
146
171
  configureServer(server) {
172
+ restartOnPlatformChange(server, platform_resolution, () => platform);
147
173
  server.middlewares.use(createWorkerEntryMiddleware(is_tsrx_source));
148
174
  },
149
175
 
@@ -155,7 +181,17 @@ export function tsrxSolid(options = {}) {
155
181
  // worker plugin reads it back to set the entry up as a worker.
156
182
  const path = stripWorkerEntryQuery(source);
157
183
  const worker_query = source.slice(path.length);
158
- if (is_virtual(path)) return source;
184
+ if (is_virtual(path)) {
185
+ // Vitest can ask for a module by its root-relative URL. Anchor the id
186
+ // to the file, or imports from the module resolve against the root.
187
+ // Vite's resolver spells it (real path, `/` separators), so the file
188
+ // has one module id however it is reached.
189
+ const real_path = to_real_path(path);
190
+ if (real_path === path.slice(0, -VIRTUAL_TSX_SUFFIX.length)) return source;
191
+ const resolved = await this.resolve(real_path, importer, { ...options, skipSelf: true });
192
+ const id = resolved && !resolved.external ? resolved.id : real_path;
193
+ return normalizePath(id) + VIRTUAL_TSX_SUFFIX + worker_query;
194
+ }
159
195
 
160
196
  // Rewrite tsrx source imports to their virtual `<path>.tsx` form
161
197
  // so downstream extension-based plugins pick the module up as TSX.
@@ -178,10 +214,24 @@ export function tsrxSolid(options = {}) {
178
214
  : path_resolve(root_dir, path.replace(/^\/+/, ''));
179
215
  return absolute_source + VIRTUAL_TSX_SUFFIX + worker_query;
180
216
  }
181
- return null;
217
+
218
+ // A package export or an extensionless import can still resolve to
219
+ // a `.tsrx` file.
220
+ if (source.startsWith('\0') || source.includes('?')) return null;
221
+ const resolved = await this.resolve(source, importer, { ...options, skipSelf: true });
222
+ if (
223
+ resolved &&
224
+ !resolved.id.startsWith('\0') &&
225
+ !resolved.id.includes('?') &&
226
+ is_tsrx_source(resolved.id)
227
+ ) {
228
+ return { ...resolved, id: resolved.id + VIRTUAL_TSX_SUFFIX };
229
+ }
230
+ // Hand the resolution over, so Vite does not resolve the import again.
231
+ return resolved;
182
232
  },
183
233
 
184
- async load(id) {
234
+ async load(id, load_options) {
185
235
  if (id.includes(CSS_QUERY)) {
186
236
  const key = id.split('?')[0];
187
237
  return css_cache.get(key) ?? '';
@@ -191,7 +241,12 @@ export function tsrxSolid(options = {}) {
191
241
 
192
242
  const real_path = to_real_path(path);
193
243
  const source = await readFile(real_path, 'utf-8');
194
- let { code, css, map } = compile(source, real_path, compile_options);
244
+ let { code, css, map } = compile(source, real_path, {
245
+ ...compile_options,
246
+ // With `ssr` on, as for `solid({ ssr: true })`, vite-plugin-solid
247
+ // compiles a server build to babel-preset-solid's SSR output.
248
+ ssr: options.ssr === true && is_server_build(this, load_options),
249
+ });
195
250
 
196
251
  if (css) {
197
252
  css_cache.set(real_path, css);
@@ -205,8 +260,28 @@ export function tsrxSolid(options = {}) {
205
260
  return { code, map };
206
261
  },
207
262
 
208
- handleHotUpdate(ctx) {
263
+ transform(code, id) {
264
+ if (platform !== undefined) return null;
265
+ const flag = findUnconfiguredPlatformFlag(code, id, parseSync);
266
+ if (flag) this.error(flag, flag.pos);
267
+ return null;
268
+ },
269
+
270
+ async handleHotUpdate(ctx) {
209
271
  if (!is_tsrx_source(ctx.file)) return;
272
+ // CSS can reload before the virtual JS, so refresh its selector hash first.
273
+ // A file that doesn't compile keeps its last good CSS, and the reloaded
274
+ // module's `load` reports the error, as before this refresh.
275
+ try {
276
+ const { css } = compile(await ctx.read(), ctx.file, compile_options);
277
+ if (css) {
278
+ css_cache.set(ctx.file, css);
279
+ } else {
280
+ css_cache.delete(ctx.file);
281
+ }
282
+ } catch {
283
+ // Keep the last good CSS.
284
+ }
210
285
  // Invalidate the virtual `<path>.tsx` modules so Vite re-runs `load`,
211
286
  // looked up by file to include a worker entry's query-suffixed id.
212
287
  // Also invalidate the virtual CSS module — Vite doesn't cascade
package/types/index.d.ts CHANGED
@@ -16,6 +16,15 @@ export interface TsrxSolidOptions {
16
16
  * set of `.tsrx` files that should go through this plugin.
17
17
  */
18
18
  include?: RegExp;
19
+ /**
20
+ * Set this as you set `vite-plugin-solid`'s own `ssr` option. With it on,
21
+ * vite-plugin-solid compiles a server build to babel-preset-solid's SSR
22
+ * output (`generate: 'ssr'`, `hydratable: true`), and TSRX2034 then also
23
+ * reports a `yield` in a template value that output moves into an arrow.
24
+ * A client build, and any build with it off, keeps the DOM output's rules.
25
+ * Defaults to `false`.
26
+ */
27
+ ssr?: boolean;
19
28
  }
20
29
 
21
30
  export function tsrxSolid(options?: TsrxSolidOptions): Plugin;