@tsrx/vite-plugin-react 0.0.108 → 0.0.110
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 +3 -3
- package/src/index.js +28 -13
- package/types/index.d.ts +6 -2
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.
|
|
6
|
+
"version": "0.0.110",
|
|
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.
|
|
24
|
-
"@tsrx/react": "0.3.
|
|
23
|
+
"@tsrx/core": "0.3.0",
|
|
24
|
+
"@tsrx/react": "0.3.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 }} TsrxReactTransformResult
|
|
7
7
|
* @typedef {{
|
|
8
8
|
* (code: string, id: `${string}.tsrx`): Promise<TsrxReactTransformResult>,
|
|
9
|
+
* (code: string, id: `${string}.tsrx?worker_file&type=${string}`): Promise<TsrxReactTransformResult>,
|
|
9
10
|
* (code: string, id: string): Promise<TsrxReactTransformResult | null>,
|
|
10
11
|
* }} TsrxReactTransform
|
|
11
12
|
* @typedef {{
|
|
12
|
-
* (source: `${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
|
* }} TsrxReactResolveId
|
|
15
16
|
* @typedef {{
|
|
16
|
-
* (id:
|
|
17
|
+
* (id: `${string}?tsrx-css&lang.css`): string,
|
|
17
18
|
* (id: string): string | null,
|
|
18
19
|
* }} TsrxReactLoad
|
|
19
20
|
* @typedef {{
|
|
@@ -43,8 +44,12 @@ import { compile } from '@tsrx/react';
|
|
|
43
44
|
import { mergePlatformDefinitions, validatePlatform } from '@tsrx/core';
|
|
44
45
|
import { resolveBuildPlatform } from '@tsrx/core/config';
|
|
45
46
|
import { createDepScanTransformPlugin } from '@tsrx/core/vite/dep-scan';
|
|
47
|
+
import { createWorkerEntryMiddleware, stripWorkerEntryQuery } from '@tsrx/core/vite/worker';
|
|
46
48
|
|
|
47
49
|
const TSRX_EXTENSION_PATTERN = /\.tsrx$/;
|
|
50
|
+
// CSS ids stay the component path plus this query, with no `\0` prefix, so
|
|
51
|
+
// Vite resolves relative `@import` and `url()` references in the extracted
|
|
52
|
+
// CSS from the component's directory.
|
|
48
53
|
const CSS_QUERY = '?tsrx-css&lang.css';
|
|
49
54
|
|
|
50
55
|
/**
|
|
@@ -125,37 +130,45 @@ export function tsrxReact(options = {}) {
|
|
|
125
130
|
};
|
|
126
131
|
},
|
|
127
132
|
|
|
133
|
+
configureServer(server) {
|
|
134
|
+
server.middlewares.use(
|
|
135
|
+
createWorkerEntryMiddleware((path) => TSRX_EXTENSION_PATTERN.test(path)),
|
|
136
|
+
);
|
|
137
|
+
},
|
|
138
|
+
|
|
128
139
|
resolveId(/** @type {string} */ source) {
|
|
129
140
|
if (!source.includes(CSS_QUERY)) return null;
|
|
130
|
-
|
|
131
|
-
return '\0' + source;
|
|
141
|
+
return source;
|
|
132
142
|
},
|
|
133
143
|
|
|
134
144
|
load(/** @type {string} */ id) {
|
|
135
|
-
if (!id.
|
|
136
|
-
const key = id.
|
|
145
|
+
if (!id.includes(CSS_QUERY)) return null;
|
|
146
|
+
const key = id.split('?')[0];
|
|
137
147
|
const css = css_cache.get(key);
|
|
138
148
|
return css ?? '';
|
|
139
149
|
},
|
|
140
150
|
|
|
141
151
|
async transform(/** @type {string} */ code, /** @type {string} */ id) {
|
|
142
|
-
|
|
152
|
+
// A dev worker entry arrives as `<path>?worker_file&type=<type>`.
|
|
153
|
+
// Compile it under its file path, as a plain import would be.
|
|
154
|
+
const file = stripWorkerEntryQuery(id);
|
|
155
|
+
if (!TSRX_EXTENSION_PATTERN.test(file)) return null;
|
|
143
156
|
|
|
144
|
-
let { code: tsx_code, css, map } = compile(code,
|
|
157
|
+
let { code: tsx_code, css, map } = compile(code, file, compile_options);
|
|
145
158
|
|
|
146
159
|
let source = tsx_code;
|
|
147
160
|
if (css) {
|
|
148
|
-
css_cache.set(
|
|
161
|
+
css_cache.set(file, css);
|
|
149
162
|
// After existing imports so dependency sheets (imported themes)
|
|
150
163
|
// evaluate first and this module's rules win at equal specificity.
|
|
151
|
-
source = `${tsx_code}\nimport ${JSON.stringify(
|
|
164
|
+
source = `${tsx_code}\nimport ${JSON.stringify(file + CSS_QUERY)};\n`;
|
|
152
165
|
} else {
|
|
153
|
-
css_cache.delete(
|
|
166
|
+
css_cache.delete(file);
|
|
154
167
|
}
|
|
155
168
|
|
|
156
169
|
const result = await transformWithOxc(
|
|
157
170
|
source,
|
|
158
|
-
|
|
171
|
+
file,
|
|
159
172
|
{
|
|
160
173
|
lang: 'tsx',
|
|
161
174
|
sourcemap: true,
|
|
@@ -176,10 +189,12 @@ export function tsrxReact(options = {}) {
|
|
|
176
189
|
|
|
177
190
|
update_css_cache(await ctx.read(), ctx.file);
|
|
178
191
|
|
|
179
|
-
const css_mod = ctx.server.moduleGraph.getModuleById(
|
|
192
|
+
const css_mod = ctx.server.moduleGraph.getModuleById(ctx.file + CSS_QUERY);
|
|
180
193
|
if (!css_mod) return ctx.modules;
|
|
181
194
|
|
|
182
195
|
ctx.server.moduleGraph.invalidateModule(css_mod);
|
|
196
|
+
// Vite usually lists the CSS module already, under the component's file.
|
|
197
|
+
if (ctx.modules.includes(css_mod)) return ctx.modules;
|
|
183
198
|
return [...ctx.modules, css_mod];
|
|
184
199
|
},
|
|
185
200
|
});
|
package/types/index.d.ts
CHANGED
|
@@ -38,14 +38,18 @@ export interface TsrxReactPlugin extends Omit<
|
|
|
38
38
|
| undefined;
|
|
39
39
|
transform: {
|
|
40
40
|
(code: string, id: `${string}.tsrx`): Promise<TsrxReactTransformResult>;
|
|
41
|
+
(
|
|
42
|
+
code: string,
|
|
43
|
+
id: `${string}.tsrx?worker_file&type=${string}`,
|
|
44
|
+
): Promise<TsrxReactTransformResult>;
|
|
41
45
|
(code: string, id: string): Promise<TsrxReactTransformResult | null>;
|
|
42
46
|
};
|
|
43
47
|
resolveId: {
|
|
44
|
-
(source: `${string}?tsrx-css&lang.css`):
|
|
48
|
+
(source: `${string}?tsrx-css&lang.css`): `${string}?tsrx-css&lang.css`;
|
|
45
49
|
(source: string): string | null;
|
|
46
50
|
};
|
|
47
51
|
load: {
|
|
48
|
-
(id:
|
|
52
|
+
(id: `${string}?tsrx-css&lang.css`): string;
|
|
49
53
|
(id: string): string | null;
|
|
50
54
|
};
|
|
51
55
|
}
|