@wix/origami-transforms 0.1.2 → 0.1.4

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.4",
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": "35811fffdca1725e0dd686efa31f1755a08fb3d17f32f9d20b06a808"
58
58
  }
@@ -1,18 +1,18 @@
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
4
- // catalog bundled in @wix/origami-fonts: no quotes, commas or fallbacks, the same case. A legacy font, which the
5
- // editor no longer offers, counts as not in the catalog. `origami search-font` finds the right id.
1
+ // Loads the fonts the stylesheet names. A family is only a name until a `@font-face` rule says where its files are.
2
+ // For every Wix font a font token (`--wst-heading-1-font: normal normal 700 --spx(88)/1em benzin`), `font` or
3
+ // `font-family` declaration names, the `@font-face` rules a live Wix site writes for it are appended to the
4
+ // stylesheet. They point at Wix's CDN, so no font file is downloaded into the site or its build; the browser fetches
5
+ // only the files of the fonts, weights and scripts a page uses. A family with no font faces, like a system font, adds
6
+ // nothing. The rules exist only in the served CSS, never in the site's own files. Whether a token names exactly one
7
+ // Wix font id is checked by the lint (@wix/origami-lint's theme-font-ids rule), not here.
6
8
 
7
- import { basename } from 'node:path'
8
- import { fonts } from '@wix/origami-fonts'
9
- import { LayoutError } from '../transform/layoutError.js'
9
+ import { fontFaceCss, fontFaces } from '@wix/origami-fonts'
10
+ import postcss from 'postcss'
10
11
 
11
- const FONT_TOKEN = /^--wst-[\w-]+-font$/
12
+ const FONT_DECLARATION = /^(?:--wst-[\w-]+-font|font|font-family)$/
12
13
  // The size in a `font` shorthand, with its optional line height: the family follows it. The number's quantifiers
13
14
  // never overlap, so a long run of digits that is not a size fails in linear time.
14
15
  const SIZE = /^(?:--spx\(.*\)|calc\(.*\)|(?:\d+(?:\.\d*)?|\.\d+)(?:px|em|rem|%|vw|vh|pt)?)(?:\/\S+)?$/i
15
- const FONT_IDS = new Set(fonts.filter(font => font.permissions !== 'legacy').map(font => font.id))
16
16
 
17
17
  /** A value split on top-level whitespace, so `--spx(16)/1.3em` stays one token. */
18
18
  const tokens = value => {
@@ -42,52 +42,24 @@ const familyOf = value => {
42
42
  return parts.slice(size + 1).join(' ')
43
43
  }
44
44
 
45
- /** ` (theme.css:12)`: the carrier the declaration was written in, which the bundler doesn't name. */
46
- const where = declaration => {
47
- const file = declaration.source?.input.file
48
- const line = declaration.source?.start?.line
49
- return file ? ` (${basename(file)}${line ? `:${line}` : ''})` : ''
50
- }
51
-
52
- /**
53
- * Where `source` starts, as LayoutError's location, leaving out what it doesn't know.
54
- * @param {import('postcss').Declaration['source']} source
55
- */
56
- const locationOf = source => {
57
- /** @type {{ file?: string, line?: number, column?: number }} */
58
- const location = {}
59
- if (source?.input.file !== undefined) location.file = source.input.file
60
- if (source?.start !== undefined) {
61
- location.line = source.start.line
62
- location.column = source.start.column
63
- }
64
- return location
45
+ /** The Wix font ids a declaration names, as the font faces key them: each name of its family list, lowercase. */
46
+ const fontIdsOf = declaration => {
47
+ const family = declaration.prop === 'font-family' ? declaration.value : familyOf(declaration.value)
48
+ if (family === undefined || /var\(/.test(family)) return []
49
+ return family.split(',').map(name => name.trim().replace(/^(['"])(.*)\1$/, '$2').toLowerCase())
65
50
  }
66
51
 
67
52
  /** @type {import('../transform/types.js').CssTransformPlugin} */
68
53
  export const fontsPlugin = {
69
54
  name: 'fonts',
70
- // Only checks; it reads the tokens as authored, before spx rewrites the sizes the family follows.
55
+ // Reads the declarations as authored, before spx rewrites the sizes the family follows.
71
56
  order: 5,
72
57
  transform(root) {
73
- /** @type {import('postcss').Declaration[]} */
74
- const declarations = []
75
- root.walkDecls(FONT_TOKEN, declaration => {
76
- declarations.push(declaration)
58
+ const ids = new Set()
59
+ root.walkDecls(FONT_DECLARATION, declaration => {
60
+ for (const id of fontIdsOf(declaration)) ids.add(id)
77
61
  })
78
- const violations = []
79
- let first
80
- for (const declaration of declarations) {
81
- const family = familyOf(declaration.value)
82
- if (family === undefined || FONT_IDS.has(family)) continue
83
- first ??= declaration.source
84
- violations.push(
85
- `${declaration.prop}${where(declaration)} must name exactly one Wix font id from the catalog; found ` +
86
- `${family}. Run \`origami search-font --font <name>\` for the id.`
87
- )
88
- }
89
- if (violations.length) {
90
- throw new LayoutError(violations, locationOf(first))
91
- }
62
+ const css = fontFaceCss(fontFaces, ids)
63
+ if (css) root.append(postcss.parse(css).nodes)
92
64
  },
93
65
  }
@@ -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 appends the `@font-face` rules that load the Wix fonts the stylesheet names from Wix's
12
+ // CDN.
12
13
  //
13
14
  // `@layer` needs no plugin: cascade layers have been in every browser since March 2022.
14
15