@teacss/vite 0.10.5 → 0.10.6

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/README.md CHANGED
@@ -33,7 +33,9 @@ Import the generated CSS once from the application entry:
33
33
  import "virtual:teacss.css";
34
34
  ```
35
35
 
36
- The dev server also serves the inspector at `/__teacss`; pass `inspector: false` to turn it off.
36
+ The dev server also serves the inspector at `/__teacss`; pass `inspector: false` to turn it off. In dev, the dev server serves the Standard preset's fonts under `<base>__teacss_fonts/`, prefixed with `server.origin` when it is set; a build emits them as assets.
37
+
38
+ Other plugins reach the project through the `api` of the plugin named `teacss:api` (`TeacssVitePluginApi`): `getProject()` returns it, and `invalidate()` updates the generated CSS on the dev client after a module was registered through `getProject().transform()`.
37
39
 
38
40
  ## Documentation
39
41
 
package/dist/plugin.d.ts CHANGED
@@ -17,6 +17,11 @@ export interface TeacssViteOptions extends ShellOptions {
17
17
  }
18
18
  export interface TeacssVitePluginApi {
19
19
  getProject(): Project;
20
+ /**
21
+ * Sends the dev client an update of the generated CSS, as a changed module does: for the modules another plugin
22
+ * registers through `getProject().transform()`. A build has no client to update.
23
+ */
24
+ invalidate(): void;
20
25
  }
21
26
  /** The dev-server path the inspector's UI and `/api` routes live under; the UI's built asset URLs assume it. */
22
27
  export declare const INSPECTOR_BASE = "/__teacss";
package/dist/plugin.js CHANGED
@@ -13,6 +13,8 @@ const ENTRY_CANDIDATES = ["package.json", "index.css", "src/index.css"];
13
13
  const INVALIDATION_DELAY_MS = 10;
14
14
  /** The dev-server path the inspector's UI and `/api` routes live under; the UI's built asset URLs assume it. */
15
15
  export const INSPECTOR_BASE = "/__teacss";
16
+ /** Where, under Vite's `base`, the dev server serves the fonts of the generated CSS; the path teacss-ts uses. */
17
+ const DEV_FONTS = "__teacss_fonts";
16
18
  const HTML_ENTRY = /\.html$/;
17
19
  /** `vite:css-post`'s transform, and the stylesheets holding the placeholder it cached, by its hook object. */
18
20
  const cssPostCaches = new WeakMap();
@@ -54,6 +56,8 @@ export function teacss(options = {}) {
54
56
  const entries = new Set();
55
57
  /** `@source` directories already reported as unreadable: Vite's builder resolves the config once per environment. */
56
58
  const unreadable = new Set();
59
+ /** The bytes of each font the dev CSS links to, by its file name under `DEV_FONTS`, which carries its sha256. */
60
+ const devFonts = new Map();
57
61
  function warnUnreadable() {
58
62
  for (const path of project.info.unreadableDirectories) {
59
63
  if (unreadable.has(path))
@@ -202,9 +206,9 @@ export function teacss(options = {}) {
202
206
  syncSources();
203
207
  }
204
208
  /**
205
- * What stands for the generated CSS: the placeholder `generateBundle` replaces in a build, the CSS itself in dev.
206
- * While a watcher event has failed, the error goes out again: Vite's own update of a changed entry stylesheet
207
- * clears the overlay before the client requests it.
209
+ * What stands for the generated CSS: the placeholder `generateBundle` replaces in a build, the CSS itself in dev,
210
+ * with its fonts linked from the dev server. While a watcher event has failed, the error goes out again: Vite's
211
+ * own update of a changed entry stylesheet clears the overlay before the client requests it.
208
212
  */
209
213
  function generated(warn) {
210
214
  if (config.command === "build")
@@ -213,7 +217,22 @@ export function teacss(options = {}) {
213
217
  server?.hot.send({ type: "error", err: failure });
214
218
  const result = project.css();
215
219
  project.reportUnmatched(result, { warn });
216
- return result.css;
220
+ return linkDevFonts(result.css);
221
+ }
222
+ /** `css` with the fonts it inlines linked from the dev server, which serves the bytes recorded here. */
223
+ function linkDevFonts(css) {
224
+ return extractBundledFonts(css, (digest, bytes) => {
225
+ const name = `teacss-${digest}.woff2`;
226
+ devFonts.set(name, bytes);
227
+ // As Vite's own dev asset URLs: a page served by another server (`server.origin`) loads them from Vite.
228
+ return `${config.server.origin ?? ""}${config.base}${DEV_FONTS}/${name}`;
229
+ }).css;
230
+ }
231
+ /** A font this instance has not linked yet, as in CSS another process generated, is looked up in the project's CSS. */
232
+ function devFont(name) {
233
+ if (!devFonts.has(name) && !disposed)
234
+ linkDevFonts(project.css().css);
235
+ return devFonts.get(name);
217
236
  }
218
237
  function dispose() {
219
238
  if (invalidation)
@@ -336,6 +355,19 @@ export function teacss(options = {}) {
336
355
  if (failure)
337
356
  client.send({ type: "error", err: failure });
338
357
  });
358
+ const serveFont = (request, response, next) => {
359
+ const bytes = devFont(request.url?.split("?", 1)[0]?.slice(1) ?? "");
360
+ if (!bytes)
361
+ return next();
362
+ response.setHeader("Content-Type", "font/woff2");
363
+ response.setHeader("Cache-Control", "public, max-age=31536000, immutable");
364
+ response.end(bytes);
365
+ };
366
+ // After Vite's CORS and host check, ahead of the middleware that strips `base`.
367
+ devServer.middlewares.use(`${config.base}${DEV_FONTS}`, serveFont);
368
+ // A parent app that mounts Vite's middlewares under `base` strips it before they run.
369
+ if (config.server.middlewareMode && config.base !== "/")
370
+ devServer.middlewares.use(`/${DEV_FONTS}`, serveFont);
339
371
  if (inspector)
340
372
  devServer.middlewares.use(INSPECTOR_BASE, createInspectorMiddleware(project));
341
373
  },
@@ -393,7 +425,7 @@ export function teacss(options = {}) {
393
425
  name: "teacss:api",
394
426
  enforce: "post",
395
427
  generateBundle: spliceGenerated,
396
- api: { getProject: () => project },
428
+ api: { getProject: () => project, invalidate: invalidateEntry },
397
429
  };
398
430
  return [main, api];
399
431
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@teacss/vite",
3
- "version": "0.10.5",
3
+ "version": "0.10.6",
4
4
  "description": "Vite plugin for TeaCSS on the Rust engine (@teacss/engine)",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -36,8 +36,8 @@
36
36
  "vite": ">=7"
37
37
  },
38
38
  "dependencies": {
39
- "@teacss/engine": "0.10.5",
40
- "@teacss/inspector": "0.10.5"
39
+ "@teacss/engine": "0.10.6",
40
+ "@teacss/inspector": "0.10.6"
41
41
  },
42
42
  "devDependencies": {
43
43
  "@types/node": "^22.20.5",