@tsrx/vite-plugin-react 0.0.120 → 0.0.121

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 +3 -3
  2. package/src/index.js +39 -14
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "Vite plugin for @tsrx/react (.tsrx modules)",
4
4
  "license": "MIT",
5
5
  "author": "Dominic Gannaway",
6
- "version": "0.0.120",
6
+ "version": "0.0.121",
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.5",
24
- "@tsrx/react": "0.4.5"
23
+ "@tsrx/core": "0.6.0",
24
+ "@tsrx/react": "0.5.0"
25
25
  },
26
26
  "peerDependencies": {
27
27
  "vite": "*"
package/src/index.js CHANGED
@@ -39,11 +39,12 @@
39
39
  * }} TsrxReactPlugin
40
40
  */
41
41
 
42
- import { transformWithOxc } from 'vite';
42
+ import { parseSync, transformWithOxc } from 'vite';
43
43
  import { compile } from '@tsrx/react';
44
44
  import { mergePlatformDefinitions, validatePlatform } from '@tsrx/core';
45
45
  import { resolveBuildPlatform } from '@tsrx/core/config';
46
46
  import { createDepScanTransformPlugin } from '@tsrx/core/vite/dep-scan';
47
+ import { findUnconfiguredPlatformFlag, restartOnPlatformChange } from '@tsrx/core/vite/platform';
47
48
  import { createWorkerEntryMiddleware, stripWorkerEntryQuery } from '@tsrx/core/vite/worker';
48
49
 
49
50
  const TSRX_EXTENSION_PATTERN = /\.tsrx$/;
@@ -67,16 +68,20 @@ export function tsrxReact(options = {}) {
67
68
  const jsxImportSource = options.jsxImportSource ?? 'react';
68
69
  const compile_options = { runtimeImports: options.runtimeImports, platform };
69
70
 
71
+ /** @type {import('@tsrx/core/config').BuildPlatformResolutionOptions} */
72
+ let platform_resolution = {};
73
+
70
74
  /** @param {import('vite').UserConfig} config */
71
75
  function resolve_platform(config) {
72
- platform = resolveBuildPlatform({
76
+ platform_resolution = {
73
77
  root: config.root ?? process.cwd(),
74
78
  tsconfig:
75
79
  options.tsconfig ??
76
80
  /** @type {{ tsconfig?: string }} */ (/** @type {unknown} */ (config)).tsconfig,
77
81
  platform: explicit_platform,
78
82
  integration: '@tsrx/vite-plugin-react',
79
- });
83
+ };
84
+ platform = resolveBuildPlatform(platform_resolution);
80
85
  compile_options.platform = platform;
81
86
  }
82
87
 
@@ -101,14 +106,17 @@ export function tsrxReact(options = {}) {
101
106
  name: '@tsrx/vite-plugin-react',
102
107
  enforce: 'pre',
103
108
 
104
- config(config = /** @type {import('vite').UserConfig} */ ({})) {
105
- resolve_platform(config);
106
- if (platform === undefined) return;
107
- return {
108
- define: mergePlatformDefinitions(config.define, platform, {
109
- integration: 'Vite',
110
- }),
111
- };
109
+ config: {
110
+ order: 'post',
111
+ handler(config = /** @type {import('vite').UserConfig} */ ({})) {
112
+ resolve_platform(config);
113
+ if (platform === undefined) return;
114
+ return {
115
+ define: mergePlatformDefinitions(config.define, platform, {
116
+ integration: 'Vite',
117
+ }),
118
+ };
119
+ },
112
120
  },
113
121
 
114
122
  configEnvironment(name, config = /** @type {import('vite').EnvironmentOptions} */ ({})) {
@@ -131,6 +139,7 @@ export function tsrxReact(options = {}) {
131
139
  },
132
140
 
133
141
  configureServer(server) {
142
+ restartOnPlatformChange(server, platform_resolution, () => platform);
134
143
  server.middlewares.use(
135
144
  createWorkerEntryMiddleware((path) => TSRX_EXTENSION_PATTERN.test(path)),
136
145
  );
@@ -148,11 +157,17 @@ export function tsrxReact(options = {}) {
148
157
  return css ?? '';
149
158
  },
150
159
 
160
+ /** @this {import('vite').Rollup.TransformPluginContext} */
151
161
  async transform(/** @type {string} */ code, /** @type {string} */ id) {
152
162
  // A dev worker entry arrives as `<path>?worker_file&type=<type>`.
153
163
  // Compile it under its file path, as a plain import would be.
154
164
  const file = stripWorkerEntryQuery(id);
155
- if (!TSRX_EXTENSION_PATTERN.test(file)) return null;
165
+ if (!TSRX_EXTENSION_PATTERN.test(file)) {
166
+ const flag =
167
+ platform === undefined ? findUnconfiguredPlatformFlag(code, id, parseSync) : null;
168
+ if (flag) this.error(flag, flag.pos);
169
+ return null;
170
+ }
156
171
 
157
172
  let { code: tsx_code, css, map } = compile(code, file, compile_options);
158
173
 
@@ -187,11 +202,21 @@ export function tsrxReact(options = {}) {
187
202
  async handleHotUpdate(ctx) {
188
203
  if (!TSRX_EXTENSION_PATTERN.test(ctx.file)) return;
189
204
 
190
- update_css_cache(await ctx.read(), ctx.file);
191
-
205
+ // Without a stylesheet module there is no css to refresh, and the file
206
+ // may only be imported as an asset (`?raw`) that must not compile.
192
207
  const css_mod = ctx.server.moduleGraph.getModuleById(ctx.file + CSS_QUERY);
193
208
  if (!css_mod) return ctx.modules;
194
209
 
210
+ const source = await ctx.read();
211
+ try {
212
+ update_css_cache(source, ctx.file);
213
+ } catch {
214
+ // Vite keeps the stylesheet module after the last component import is
215
+ // gone, so the file may now be imported only as `?raw`. Keep the last
216
+ // good css; a component import reports the error when it transforms.
217
+ return ctx.modules;
218
+ }
219
+
195
220
  ctx.server.moduleGraph.invalidateModule(css_mod);
196
221
  // Vite usually lists the CSS module already, under the component's file.
197
222
  if (ctx.modules.includes(css_mod)) return ctx.modules;