@tsrx/vite-plugin-preact 0.0.112 → 0.0.113

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 +56 -31
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.112",
6
+ "version": "0.0.113",
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/preact": "0.3.5"
23
+ "@tsrx/core": "0.6.0",
24
+ "@tsrx/preact": "0.4.0"
25
25
  },
26
26
  "peerDependencies": {
27
27
  "vite": "*"
package/src/index.js CHANGED
@@ -27,18 +27,19 @@
27
27
  * },
28
28
  * }} TsrxPreactConfigHook
29
29
  * @typedef {Omit<Plugin, 'config' | 'transform' | 'resolveId' | 'load'> & {
30
- * config: TsrxPreactConfigHook,
30
+ * config: { order: 'post', handler: TsrxPreactConfigHook },
31
31
  * transform: TsrxPreactTransform,
32
32
  * resolveId: TsrxPreactResolveId,
33
33
  * load: TsrxPreactLoad,
34
34
  * }} TsrxPreactPlugin
35
35
  */
36
36
 
37
- import { transformWithOxc } from 'vite';
37
+ import { parseSync, transformWithOxc } from 'vite';
38
38
  import { compile } from '@tsrx/preact';
39
39
  import { mergePlatformDefinitions, validatePlatform } from '@tsrx/core';
40
40
  import { resolveBuildPlatform } from '@tsrx/core/config';
41
41
  import { createDepScanTransformPlugin } from '@tsrx/core/vite/dep-scan';
42
+ import { findUnconfiguredPlatformFlag, restartOnPlatformChange } from '@tsrx/core/vite/platform';
42
43
  import { createWorkerEntryMiddleware, stripWorkerEntryQuery } from '@tsrx/core/vite/worker';
43
44
 
44
45
  const TSRX_EXTENSION_PATTERN = /\.tsrx$/;
@@ -72,16 +73,20 @@ export function tsrxPreact(options = {}) {
72
73
  platform,
73
74
  };
74
75
 
76
+ /** @type {import('@tsrx/core/config').BuildPlatformResolutionOptions} */
77
+ let platform_resolution = {};
78
+
75
79
  /** @param {import('vite').UserConfig} config */
76
80
  function resolve_platform(config) {
77
- platform = resolveBuildPlatform({
81
+ platform_resolution = {
78
82
  root: config.root ?? process.cwd(),
79
83
  tsconfig:
80
84
  options.tsconfig ??
81
85
  /** @type {{ tsconfig?: string }} */ (/** @type {unknown} */ (config)).tsconfig,
82
86
  platform: explicit_platform,
83
87
  integration: '@tsrx/vite-plugin-preact',
84
- });
88
+ };
89
+ platform = resolveBuildPlatform(platform_resolution);
85
90
  compile_options.platform = platform;
86
91
  }
87
92
 
@@ -106,32 +111,35 @@ export function tsrxPreact(options = {}) {
106
111
  name: '@tsrx/vite-plugin-preact',
107
112
  enforce: 'pre',
108
113
 
109
- config(config = /** @type {import('vite').UserConfig} */ ({})) {
110
- resolve_platform(config);
111
- return {
112
- ...(platform === undefined
113
- ? {}
114
- : {
115
- define: mergePlatformDefinitions(config.define, platform, {
116
- integration: 'Vite',
114
+ config: {
115
+ order: 'post',
116
+ handler(config = /** @type {import('vite').UserConfig} */ ({})) {
117
+ resolve_platform(config);
118
+ return {
119
+ ...(platform === undefined
120
+ ? {}
121
+ : {
122
+ define: mergePlatformDefinitions(config.define, platform, {
123
+ integration: 'Vite',
124
+ }),
117
125
  }),
118
- }),
119
- optimizeDeps: {
120
- // The scanner externalizes anything that is not a known JS
121
- // type unless its extension is listed here, so without this
122
- // entry the dep-scan plugin below never runs.
123
- extensions: ['.tsrx'],
124
- rolldownOptions: {
125
- // The scan runs its own jsx transform over the tsx the
126
- // dep-scan plugin hands back, and that transform defaults to
127
- // react's runtime. Point it at the configured source, or the
128
- // scan fails outright on an unresolvable
129
- // `react/jsx-dev-runtime` in a project that has no react.
130
- transform: { jsx: { importSource: jsxImportSource } },
131
- plugins: [create_dep_scan_plugin(jsxImportSource, compile_options)],
126
+ optimizeDeps: {
127
+ // The scanner externalizes anything that is not a known JS
128
+ // type unless its extension is listed here, so without this
129
+ // entry the dep-scan plugin below never runs.
130
+ extensions: ['.tsrx'],
131
+ rolldownOptions: {
132
+ // The scan runs its own jsx transform over the tsx the
133
+ // dep-scan plugin hands back, and that transform defaults to
134
+ // react's runtime. Point it at the configured source, or the
135
+ // scan fails outright on an unresolvable
136
+ // `react/jsx-dev-runtime` in a project that has no react.
137
+ transform: { jsx: { importSource: jsxImportSource } },
138
+ plugins: [create_dep_scan_plugin(jsxImportSource, compile_options)],
139
+ },
132
140
  },
133
- },
134
- };
141
+ };
142
+ },
135
143
  },
136
144
 
137
145
  configEnvironment(name, config = /** @type {import('vite').EnvironmentOptions} */ ({})) {
@@ -144,6 +152,7 @@ export function tsrxPreact(options = {}) {
144
152
  },
145
153
 
146
154
  configureServer(server) {
155
+ restartOnPlatformChange(server, platform_resolution, () => platform);
147
156
  server.middlewares.use(
148
157
  createWorkerEntryMiddleware((path) => TSRX_EXTENSION_PATTERN.test(path)),
149
158
  );
@@ -161,11 +170,17 @@ export function tsrxPreact(options = {}) {
161
170
  return css ?? '';
162
171
  },
163
172
 
173
+ /** @this {import('vite').Rollup.TransformPluginContext} */
164
174
  async transform(/** @type {string} */ code, /** @type {string} */ id) {
165
175
  // A dev worker entry arrives as `<path>?worker_file&type=<type>`.
166
176
  // Compile it under its file path, as a plain import would be.
167
177
  const file = stripWorkerEntryQuery(id);
168
- if (!TSRX_EXTENSION_PATTERN.test(file)) return null;
178
+ if (!TSRX_EXTENSION_PATTERN.test(file)) {
179
+ const flag =
180
+ platform === undefined ? findUnconfiguredPlatformFlag(code, id, parseSync) : null;
181
+ if (flag) this.error(flag, flag.pos);
182
+ return null;
183
+ }
169
184
 
170
185
  let { code: tsx_code, css, map } = compile(code, file, compile_options);
171
186
 
@@ -200,11 +215,21 @@ export function tsrxPreact(options = {}) {
200
215
  async handleHotUpdate(ctx) {
201
216
  if (!TSRX_EXTENSION_PATTERN.test(ctx.file)) return;
202
217
 
203
- update_css_cache(await ctx.read(), ctx.file);
204
-
218
+ // Without a stylesheet module there is no css to refresh, and the file
219
+ // may only be imported as an asset (`?raw`) that must not compile.
205
220
  const css_mod = ctx.server.moduleGraph.getModuleById(ctx.file + CSS_QUERY);
206
221
  if (!css_mod) return ctx.modules;
207
222
 
223
+ const source = await ctx.read();
224
+ try {
225
+ update_css_cache(source, ctx.file);
226
+ } catch {
227
+ // Vite keeps the stylesheet module after the last component import is
228
+ // gone, so the file may now be imported only as `?raw`. Keep the last
229
+ // good css; a component import reports the error when it transforms.
230
+ return ctx.modules;
231
+ }
232
+
208
233
  ctx.server.moduleGraph.invalidateModule(css_mod);
209
234
  // Vite usually lists the CSS module already, under the component's file.
210
235
  if (ctx.modules.includes(css_mod)) return ctx.modules;