@teacss/vite 0.10.5 → 0.10.7

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. The generated CSS writes these URLs as `URL(`, as teacss-ts does: CSS function names are case-insensitive, and Vite skips PostCSS for a stylesheet with no lowercase `url(`. A build emits the fonts 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,27 @@ 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
+ // As Vite's own dev asset URLs: a page served by another server (`server.origin`) loads them from Vite.
225
+ const prefix = `${config.server.origin ?? ""}${config.base}${DEV_FONTS}/`;
226
+ const linked = extractBundledFonts(css, (digest, bytes) => {
227
+ const name = `teacss-${digest}.woff2`;
228
+ devFonts.set(name, bytes);
229
+ return `${prefix}${name}`;
230
+ }).css;
231
+ // Vite runs PostCSS on a stylesheet with a lowercase `url(`, and these URLs are final. CSS function names
232
+ // are case-insensitive, so writing them `URL(`, as teacss-ts does, lets the sheet skip PostCSS.
233
+ const quoted = JSON.stringify(prefix).slice(0, -1);
234
+ return linked.replaceAll(`url(${quoted}`, `URL(${quoted}`);
235
+ }
236
+ /** A font this instance has not linked yet, as in CSS another process generated, is looked up in the project's CSS. */
237
+ function devFont(name) {
238
+ if (!devFonts.has(name) && !disposed)
239
+ linkDevFonts(project.css().css);
240
+ return devFonts.get(name);
217
241
  }
218
242
  function dispose() {
219
243
  if (invalidation)
@@ -336,6 +360,19 @@ export function teacss(options = {}) {
336
360
  if (failure)
337
361
  client.send({ type: "error", err: failure });
338
362
  });
363
+ const serveFont = (request, response, next) => {
364
+ const bytes = devFont(request.url?.split("?", 1)[0]?.slice(1) ?? "");
365
+ if (!bytes)
366
+ return next();
367
+ response.setHeader("Content-Type", "font/woff2");
368
+ response.setHeader("Cache-Control", "public, max-age=31536000, immutable");
369
+ response.end(bytes);
370
+ };
371
+ // After Vite's CORS and host check, ahead of the middleware that strips `base`.
372
+ devServer.middlewares.use(`${config.base}${DEV_FONTS}`, serveFont);
373
+ // A parent app that mounts Vite's middlewares under `base` strips it before they run.
374
+ if (config.server.middlewareMode && config.base !== "/")
375
+ devServer.middlewares.use(`/${DEV_FONTS}`, serveFont);
339
376
  if (inspector)
340
377
  devServer.middlewares.use(INSPECTOR_BASE, createInspectorMiddleware(project));
341
378
  },
@@ -393,7 +430,7 @@ export function teacss(options = {}) {
393
430
  name: "teacss:api",
394
431
  enforce: "post",
395
432
  generateBundle: spliceGenerated,
396
- api: { getProject: () => project },
433
+ api: { getProject: () => project, invalidate: invalidateEntry },
397
434
  };
398
435
  return [main, api];
399
436
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@teacss/vite",
3
- "version": "0.10.5",
3
+ "version": "0.10.7",
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.7",
40
+ "@teacss/inspector": "0.10.7"
41
41
  },
42
42
  "devDependencies": {
43
43
  "@types/node": "^22.20.5",