@wix/origami-transforms 0.1.2 → 0.1.3

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wix/origami-transforms",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "Babel and PostCSS checks and transforms for Wix origami page files.",
5
5
  "main": "./src/index.js",
6
6
  "files": [
@@ -15,7 +15,7 @@
15
15
  },
16
16
  "dependencies": {
17
17
  "@babel/core": "^7.26.0",
18
- "@wix/origami-fonts": "1.0.0",
18
+ "@wix/origami-fonts": "1.0.1",
19
19
  "@wix/origami-system-components": "1.7.0",
20
20
  "postcss": "^8.5.3",
21
21
  "postcss-custom-media": "^12.0.1"
@@ -54,5 +54,5 @@
54
54
  },
55
55
  "type": "module",
56
56
  "types": "./dist/index.d.ts",
57
- "falconPackageHash": "bfbe3e19687be417c93cd5eb07f45cc936aa654b83b1cb5958af7a59"
57
+ "falconPackageHash": "373242c70dd5b71460d9daba8068a88149f756889c2c74db1709e9b1"
58
58
  }
@@ -1,14 +1,24 @@
1
- // Checks the theme's font tokens (`--wst-heading-1-font: normal normal 700 --spx(88)/1em benzin`). It changes
2
- // nothing. The editor reads the family at the end of each one as one Wix font id and fails the import otherwise, so
3
- // the build fails, with every such token listed, unless the family is exactly the id of a font in the Wix font
1
+ // Checks the theme's font tokens (`--wst-heading-1-font: normal normal 700 --spx(88)/1em benzin`), then loads the
2
+ // fonts the stylesheet names.
3
+ //
4
+ // The check: the editor reads the family at the end of each token as one Wix font id and fails the import otherwise,
5
+ // so the build fails, with every such token listed, unless the family is exactly the id of a font in the Wix font
4
6
  // catalog bundled in @wix/origami-fonts: no quotes, commas or fallbacks, the same case. A legacy font, which the
5
7
  // editor no longer offers, counts as not in the catalog. `origami search-font` finds the right id.
8
+ //
9
+ // The loading: a family is only a name until a `@font-face` rule says where its files are. For every Wix font a
10
+ // font token, `font` or `font-family` declaration names, the `@font-face` rules a live Wix site writes for it are
11
+ // appended to the stylesheet. They point at Wix's CDN, so no font file is downloaded into the site or its build;
12
+ // the browser fetches only the files of the fonts, weights and scripts a page uses. A family with no font faces,
13
+ // like a system font, adds nothing. The rules exist only in the served CSS, never in the site's own files.
6
14
 
7
15
  import { basename } from 'node:path'
8
- import { fonts } from '@wix/origami-fonts'
16
+ import { fontFaceCss, fontFaces, fonts } from '@wix/origami-fonts'
17
+ import postcss from 'postcss'
9
18
  import { LayoutError } from '../transform/layoutError.js'
10
19
 
11
20
  const FONT_TOKEN = /^--wst-[\w-]+-font$/
21
+ const FONT_DECLARATION = /^(?:--wst-[\w-]+-font|font|font-family)$/
12
22
  // The size in a `font` shorthand, with its optional line height: the family follows it. The number's quantifiers
13
23
  // never overlap, so a long run of digits that is not a size fails in linear time.
14
24
  const SIZE = /^(?:--spx\(.*\)|calc\(.*\)|(?:\d+(?:\.\d*)?|\.\d+)(?:px|em|rem|%|vw|vh|pt)?)(?:\/\S+)?$/i
@@ -42,6 +52,13 @@ const familyOf = value => {
42
52
  return parts.slice(size + 1).join(' ')
43
53
  }
44
54
 
55
+ /** The Wix font ids a declaration names, as the font faces key them: each name of its family list, lowercase. */
56
+ const fontIdsOf = declaration => {
57
+ const family = declaration.prop === 'font-family' ? declaration.value : familyOf(declaration.value)
58
+ if (family === undefined || /var\(/.test(family)) return []
59
+ return family.split(',').map(name => name.trim().replace(/^(['"])(.*)\1$/, '$2').toLowerCase())
60
+ }
61
+
45
62
  /** ` (theme.css:12)`: the carrier the declaration was written in, which the bundler doesn't name. */
46
63
  const where = declaration => {
47
64
  const file = declaration.source?.input.file
@@ -67,9 +84,9 @@ const locationOf = source => {
67
84
  /** @type {import('../transform/types.js').CssTransformPlugin} */
68
85
  export const fontsPlugin = {
69
86
  name: 'fonts',
70
- // Only checks; it reads the tokens as authored, before spx rewrites the sizes the family follows.
87
+ // Reads the declarations as authored, before spx rewrites the sizes the family follows.
71
88
  order: 5,
72
- transform(root) {
89
+ validate(root) {
73
90
  /** @type {import('postcss').Declaration[]} */
74
91
  const declarations = []
75
92
  root.walkDecls(FONT_TOKEN, declaration => {
@@ -90,4 +107,12 @@ export const fontsPlugin = {
90
107
  throw new LayoutError(violations, locationOf(first))
91
108
  }
92
109
  },
110
+ transform(root) {
111
+ const ids = new Set()
112
+ root.walkDecls(FONT_DECLARATION, declaration => {
113
+ for (const id of fontIdsOf(declaration)) ids.add(id)
114
+ })
115
+ const css = fontFaceCss(fontFaces, ids)
116
+ if (css) root.append(postcss.parse(css).nodes)
117
+ },
93
118
  }
@@ -8,7 +8,8 @@
8
8
  // layout routes declarations to wrappers and SPX converts calls using the browser's active settings. stack-children
9
9
  // only checks, and runs between mixins and layout: it needs the mixins' declarations and the stack rules as authored,
10
10
  // before layout compiles them away. presets runs after mixins too, so a `--preset` a mixin body carries is moved
11
- // like an authored one. fonts only checks the theme's font tokens, as authored.
11
+ // like an authored one. fonts checks the theme's font tokens, as authored, and appends the `@font-face` rules that
12
+ // load the Wix fonts the stylesheet names from Wix's CDN.
12
13
  //
13
14
  // `@layer` needs no plugin: cascade layers have been in every browser since March 2022.
14
15