@tsrx/vite-plugin-preact 0.0.100 → 0.0.102

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 +28 -13
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.100",
6
+ "version": "0.0.102",
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.2.3",
24
- "@tsrx/preact": "0.2.3"
23
+ "@tsrx/core": "0.3.0",
24
+ "@tsrx/preact": "0.2.5"
25
25
  },
26
26
  "peerDependencies": {
27
27
  "vite": "*"
package/src/index.js CHANGED
@@ -6,14 +6,15 @@
6
6
  * @typedef {{ code: string, map: unknown }} TsrxPreactTransformResult
7
7
  * @typedef {{
8
8
  * (code: string, id: `${string}.tsrx`): Promise<TsrxPreactTransformResult>,
9
+ * (code: string, id: `${string}.tsrx?worker_file&type=${string}`): Promise<TsrxPreactTransformResult>,
9
10
  * (code: string, id: string): Promise<TsrxPreactTransformResult | null>,
10
11
  * }} TsrxPreactTransform
11
12
  * @typedef {{
12
- * (source: `${string}?tsrx-css&lang.css`): `\0${string}?tsrx-css&lang.css`,
13
+ * (source: `${string}?tsrx-css&lang.css`): `${string}?tsrx-css&lang.css`,
13
14
  * (source: string): string | null,
14
15
  * }} TsrxPreactResolveId
15
16
  * @typedef {{
16
- * (id: `\0${string}?tsrx-css&lang.css`): string,
17
+ * (id: `${string}?tsrx-css&lang.css`): string,
17
18
  * (id: string): string | null,
18
19
  * }} TsrxPreactLoad
19
20
  * @typedef {() => {
@@ -38,8 +39,12 @@ import { compile } from '@tsrx/preact';
38
39
  import { mergePlatformDefinitions, validatePlatform } from '@tsrx/core';
39
40
  import { resolveBuildPlatform } from '@tsrx/core/config';
40
41
  import { createDepScanTransformPlugin } from '@tsrx/core/vite/dep-scan';
42
+ import { createWorkerEntryMiddleware, stripWorkerEntryQuery } from '@tsrx/core/vite/worker';
41
43
 
42
44
  const TSRX_EXTENSION_PATTERN = /\.tsrx$/;
45
+ // CSS ids stay the component path plus this query, with no `\0` prefix, so
46
+ // Vite resolves relative `@import` and `url()` references in the extracted
47
+ // CSS from the component's directory.
43
48
  const CSS_QUERY = '?tsrx-css&lang.css';
44
49
 
45
50
  /**
@@ -138,37 +143,45 @@ export function tsrxPreact(options = {}) {
138
143
  };
139
144
  },
140
145
 
146
+ configureServer(server) {
147
+ server.middlewares.use(
148
+ createWorkerEntryMiddleware((path) => TSRX_EXTENSION_PATTERN.test(path)),
149
+ );
150
+ },
151
+
141
152
  resolveId(/** @type {string} */ source) {
142
153
  if (!source.includes(CSS_QUERY)) return null;
143
- if (source.startsWith('\0')) return source;
144
- return '\0' + source;
154
+ return source;
145
155
  },
146
156
 
147
157
  load(/** @type {string} */ id) {
148
- if (!id.startsWith('\0') || !id.includes(CSS_QUERY)) return null;
149
- const key = id.slice(1).split('?')[0];
158
+ if (!id.includes(CSS_QUERY)) return null;
159
+ const key = id.split('?')[0];
150
160
  const css = css_cache.get(key);
151
161
  return css ?? '';
152
162
  },
153
163
 
154
164
  async transform(/** @type {string} */ code, /** @type {string} */ id) {
155
- if (!TSRX_EXTENSION_PATTERN.test(id)) return null;
165
+ // A dev worker entry arrives as `<path>?worker_file&type=<type>`.
166
+ // Compile it under its file path, as a plain import would be.
167
+ const file = stripWorkerEntryQuery(id);
168
+ if (!TSRX_EXTENSION_PATTERN.test(file)) return null;
156
169
 
157
- let { code: tsx_code, css, map } = compile(code, id, compile_options);
170
+ let { code: tsx_code, css, map } = compile(code, file, compile_options);
158
171
 
159
172
  let source = tsx_code;
160
173
  if (css) {
161
- css_cache.set(id, css);
174
+ css_cache.set(file, css);
162
175
  // After existing imports so dependency sheets (imported themes)
163
176
  // evaluate first and this module's rules win at equal specificity.
164
- source = `${tsx_code}\nimport ${JSON.stringify(id + CSS_QUERY)};\n`;
177
+ source = `${tsx_code}\nimport ${JSON.stringify(file + CSS_QUERY)};\n`;
165
178
  } else {
166
- css_cache.delete(id);
179
+ css_cache.delete(file);
167
180
  }
168
181
 
169
182
  const result = await transformWithOxc(
170
183
  source,
171
- id,
184
+ file,
172
185
  {
173
186
  lang: 'tsx',
174
187
  sourcemap: true,
@@ -189,10 +202,12 @@ export function tsrxPreact(options = {}) {
189
202
 
190
203
  update_css_cache(await ctx.read(), ctx.file);
191
204
 
192
- const css_mod = ctx.server.moduleGraph.getModuleById('\0' + ctx.file + CSS_QUERY);
205
+ const css_mod = ctx.server.moduleGraph.getModuleById(ctx.file + CSS_QUERY);
193
206
  if (!css_mod) return ctx.modules;
194
207
 
195
208
  ctx.server.moduleGraph.invalidateModule(css_mod);
209
+ // Vite usually lists the CSS module already, under the component's file.
210
+ if (ctx.modules.includes(css_mod)) return ctx.modules;
196
211
  return [...ctx.modules, css_mod];
197
212
  },
198
213
  });