@webstudio-is/fonts 0.56.0 → 0.58.0
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.
|
@@ -22,14 +22,14 @@ __export(get_font_faces_exports, {
|
|
|
22
22
|
});
|
|
23
23
|
module.exports = __toCommonJS(get_font_faces_exports);
|
|
24
24
|
var import_constants = require("./constants");
|
|
25
|
-
const formatFace = (asset, format) => {
|
|
25
|
+
const formatFace = (asset, format, url) => {
|
|
26
26
|
if ("variationAxes" in asset.meta) {
|
|
27
27
|
const { wght, wdth } = asset.meta?.variationAxes ?? {};
|
|
28
28
|
return {
|
|
29
29
|
fontFamily: asset.meta.family,
|
|
30
30
|
fontStyle: "normal",
|
|
31
31
|
fontDisplay: "swap",
|
|
32
|
-
src: `url('${
|
|
32
|
+
src: `url('${url}') format('${format}')`,
|
|
33
33
|
fontStretch: wdth ? `${wdth.min}% ${wdth.max}%` : void 0,
|
|
34
34
|
fontWeight: wght ? `${wght.min} ${wght.max}` : void 0
|
|
35
35
|
};
|
|
@@ -39,7 +39,7 @@ const formatFace = (asset, format) => {
|
|
|
39
39
|
fontStyle: asset.meta.style,
|
|
40
40
|
fontWeight: asset.meta.weight,
|
|
41
41
|
fontDisplay: "swap",
|
|
42
|
-
src: `url('${
|
|
42
|
+
src: `url('${url}') format('${format}')`
|
|
43
43
|
};
|
|
44
44
|
};
|
|
45
45
|
const getKey = (asset) => {
|
|
@@ -48,9 +48,11 @@ const getKey = (asset) => {
|
|
|
48
48
|
}
|
|
49
49
|
return asset.meta.family + asset.meta.style + asset.meta.weight;
|
|
50
50
|
};
|
|
51
|
-
const getFontFaces = (assets) => {
|
|
51
|
+
const getFontFaces = (assets, options) => {
|
|
52
|
+
const { assetBaseUrl } = options;
|
|
52
53
|
const faces = /* @__PURE__ */ new Map();
|
|
53
54
|
for (const asset of assets) {
|
|
55
|
+
const url = `${assetBaseUrl}${asset.name}`;
|
|
54
56
|
const assetKey = getKey(asset);
|
|
55
57
|
const face = faces.get(assetKey);
|
|
56
58
|
const format = import_constants.FONT_FORMATS.get(asset.format);
|
|
@@ -58,11 +60,11 @@ const getFontFaces = (assets) => {
|
|
|
58
60
|
continue;
|
|
59
61
|
}
|
|
60
62
|
if (face === void 0) {
|
|
61
|
-
const face2 = formatFace(asset, format);
|
|
63
|
+
const face2 = formatFace(asset, format, url);
|
|
62
64
|
faces.set(assetKey, face2);
|
|
63
65
|
continue;
|
|
64
66
|
}
|
|
65
|
-
face.src += `, url('${
|
|
67
|
+
face.src += `, url('${url}') format('${format}')`;
|
|
66
68
|
}
|
|
67
69
|
return Array.from(faces.values());
|
|
68
70
|
};
|
package/lib/get-font-faces.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { FONT_FORMATS } from "./constants";
|
|
2
|
-
const formatFace = (asset, format) => {
|
|
2
|
+
const formatFace = (asset, format, url) => {
|
|
3
3
|
if ("variationAxes" in asset.meta) {
|
|
4
4
|
const { wght, wdth } = asset.meta?.variationAxes ?? {};
|
|
5
5
|
return {
|
|
6
6
|
fontFamily: asset.meta.family,
|
|
7
7
|
fontStyle: "normal",
|
|
8
8
|
fontDisplay: "swap",
|
|
9
|
-
src: `url('${
|
|
9
|
+
src: `url('${url}') format('${format}')`,
|
|
10
10
|
fontStretch: wdth ? `${wdth.min}% ${wdth.max}%` : void 0,
|
|
11
11
|
fontWeight: wght ? `${wght.min} ${wght.max}` : void 0
|
|
12
12
|
};
|
|
@@ -16,7 +16,7 @@ const formatFace = (asset, format) => {
|
|
|
16
16
|
fontStyle: asset.meta.style,
|
|
17
17
|
fontWeight: asset.meta.weight,
|
|
18
18
|
fontDisplay: "swap",
|
|
19
|
-
src: `url('${
|
|
19
|
+
src: `url('${url}') format('${format}')`
|
|
20
20
|
};
|
|
21
21
|
};
|
|
22
22
|
const getKey = (asset) => {
|
|
@@ -25,9 +25,11 @@ const getKey = (asset) => {
|
|
|
25
25
|
}
|
|
26
26
|
return asset.meta.family + asset.meta.style + asset.meta.weight;
|
|
27
27
|
};
|
|
28
|
-
const getFontFaces = (assets) => {
|
|
28
|
+
const getFontFaces = (assets, options) => {
|
|
29
|
+
const { assetBaseUrl } = options;
|
|
29
30
|
const faces = /* @__PURE__ */ new Map();
|
|
30
31
|
for (const asset of assets) {
|
|
32
|
+
const url = `${assetBaseUrl}${asset.name}`;
|
|
31
33
|
const assetKey = getKey(asset);
|
|
32
34
|
const face = faces.get(assetKey);
|
|
33
35
|
const format = FONT_FORMATS.get(asset.format);
|
|
@@ -35,11 +37,11 @@ const getFontFaces = (assets) => {
|
|
|
35
37
|
continue;
|
|
36
38
|
}
|
|
37
39
|
if (face === void 0) {
|
|
38
|
-
const face2 = formatFace(asset, format);
|
|
40
|
+
const face2 = formatFace(asset, format, url);
|
|
39
41
|
faces.set(assetKey, face2);
|
|
40
42
|
continue;
|
|
41
43
|
}
|
|
42
|
-
face.src += `, url('${
|
|
44
|
+
face.src += `, url('${url}') format('${format}')`;
|
|
43
45
|
}
|
|
44
46
|
return Array.from(faces.values());
|
|
45
47
|
};
|
|
@@ -2,7 +2,7 @@ import type { FontMeta, FontFormat, FontMetaStatic } from "./schema";
|
|
|
2
2
|
export type PartialFontAsset = {
|
|
3
3
|
format: FontFormat;
|
|
4
4
|
meta: FontMeta;
|
|
5
|
-
|
|
5
|
+
name: string;
|
|
6
6
|
};
|
|
7
7
|
export type FontFace = {
|
|
8
8
|
fontFamily: string;
|
|
@@ -12,4 +12,6 @@ export type FontFace = {
|
|
|
12
12
|
fontWeight?: number | string;
|
|
13
13
|
fontStretch?: string;
|
|
14
14
|
};
|
|
15
|
-
export declare const getFontFaces: (assets: Array<PartialFontAsset
|
|
15
|
+
export declare const getFontFaces: (assets: Array<PartialFontAsset>, options: {
|
|
16
|
+
assetBaseUrl: string;
|
|
17
|
+
}) => Array<FontFace>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webstudio-is/fonts",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.58.0",
|
|
4
4
|
"description": "Fonts utils",
|
|
5
5
|
"author": "Webstudio <github@webstudio.is>",
|
|
6
6
|
"homepage": "https://webstudio.is",
|
|
@@ -14,10 +14,10 @@
|
|
|
14
14
|
"jest": "^29.3.1",
|
|
15
15
|
"typescript": "5.0.3",
|
|
16
16
|
"zod": "^3.19.1",
|
|
17
|
-
"@webstudio-is/design-system": "^0.
|
|
18
|
-
"@webstudio-is/jest-config": "^1.0.
|
|
17
|
+
"@webstudio-is/design-system": "^0.58.0",
|
|
18
|
+
"@webstudio-is/jest-config": "^1.0.5",
|
|
19
19
|
"@webstudio-is/scripts": "^0.0.0",
|
|
20
|
-
"@webstudio-is/tsconfig": "^1.0.
|
|
20
|
+
"@webstudio-is/tsconfig": "^1.0.5"
|
|
21
21
|
},
|
|
22
22
|
"peerDependencies": {
|
|
23
23
|
"zod": "^3.19.1"
|
|
@@ -44,12 +44,12 @@
|
|
|
44
44
|
"private": false,
|
|
45
45
|
"sideEffects": false,
|
|
46
46
|
"scripts": {
|
|
47
|
-
"typecheck": "tsc --noEmit",
|
|
47
|
+
"typecheck": "tsc --noEmit --emitDeclarationOnly false",
|
|
48
48
|
"test": "NODE_OPTIONS=--experimental-vm-modules jest",
|
|
49
49
|
"checks": "pnpm typecheck && pnpm lint && pnpm test",
|
|
50
50
|
"dev": "build-package --watch",
|
|
51
51
|
"build": "build-package",
|
|
52
|
-
"dts": "tsc --
|
|
52
|
+
"dts": "tsc --declarationDir lib/types",
|
|
53
53
|
"lint": "eslint ./src --ext .ts,.tsx --max-warnings 0"
|
|
54
54
|
}
|
|
55
55
|
}
|
|
@@ -11,7 +11,7 @@ describe("getFontFaces()", () => {
|
|
|
11
11
|
style: "normal",
|
|
12
12
|
weight: 400,
|
|
13
13
|
},
|
|
14
|
-
|
|
14
|
+
name: "roboto.woff",
|
|
15
15
|
},
|
|
16
16
|
{
|
|
17
17
|
format: "ttf",
|
|
@@ -20,10 +20,10 @@ describe("getFontFaces()", () => {
|
|
|
20
20
|
style: "normal",
|
|
21
21
|
weight: 400,
|
|
22
22
|
},
|
|
23
|
-
|
|
23
|
+
name: "roboto.ttf",
|
|
24
24
|
},
|
|
25
25
|
];
|
|
26
|
-
expect(getFontFaces(assets)).toMatchSnapshot();
|
|
26
|
+
expect(getFontFaces(assets, { assetBaseUrl: "/fonts/" })).toMatchSnapshot();
|
|
27
27
|
});
|
|
28
28
|
|
|
29
29
|
test("different style", () => {
|
|
@@ -35,7 +35,7 @@ describe("getFontFaces()", () => {
|
|
|
35
35
|
style: "normal",
|
|
36
36
|
weight: 400,
|
|
37
37
|
},
|
|
38
|
-
|
|
38
|
+
name: "roboto.ttf",
|
|
39
39
|
},
|
|
40
40
|
{
|
|
41
41
|
format: "ttf",
|
|
@@ -44,10 +44,10 @@ describe("getFontFaces()", () => {
|
|
|
44
44
|
style: "italic",
|
|
45
45
|
weight: 400,
|
|
46
46
|
},
|
|
47
|
-
|
|
47
|
+
name: "roboto-italic.ttf",
|
|
48
48
|
},
|
|
49
49
|
];
|
|
50
|
-
expect(getFontFaces(assets)).toMatchSnapshot();
|
|
50
|
+
expect(getFontFaces(assets, { assetBaseUrl: "/fonts/" })).toMatchSnapshot();
|
|
51
51
|
});
|
|
52
52
|
|
|
53
53
|
test("different weight", () => {
|
|
@@ -59,7 +59,7 @@ describe("getFontFaces()", () => {
|
|
|
59
59
|
style: "normal",
|
|
60
60
|
weight: 400,
|
|
61
61
|
},
|
|
62
|
-
|
|
62
|
+
name: "roboto.ttf",
|
|
63
63
|
},
|
|
64
64
|
{
|
|
65
65
|
format: "ttf",
|
|
@@ -68,10 +68,10 @@ describe("getFontFaces()", () => {
|
|
|
68
68
|
style: "normal",
|
|
69
69
|
weight: 500,
|
|
70
70
|
},
|
|
71
|
-
|
|
71
|
+
name: "roboto-bold.ttf",
|
|
72
72
|
},
|
|
73
73
|
];
|
|
74
|
-
expect(getFontFaces(assets)).toMatchSnapshot();
|
|
74
|
+
expect(getFontFaces(assets, { assetBaseUrl: "/fonts/" })).toMatchSnapshot();
|
|
75
75
|
});
|
|
76
76
|
|
|
77
77
|
test("variable font", () => {
|
|
@@ -96,9 +96,9 @@ describe("getFontFaces()", () => {
|
|
|
96
96
|
YTFI: { name: "YTFI", min: 560, default: 738, max: 788 },
|
|
97
97
|
},
|
|
98
98
|
},
|
|
99
|
-
|
|
99
|
+
name: "inter.ttf",
|
|
100
100
|
},
|
|
101
101
|
];
|
|
102
|
-
expect(getFontFaces(assets)).toMatchSnapshot();
|
|
102
|
+
expect(getFontFaces(assets, { assetBaseUrl: "/fonts/" })).toMatchSnapshot();
|
|
103
103
|
});
|
|
104
104
|
});
|
package/src/get-font-faces.ts
CHANGED
|
@@ -4,7 +4,7 @@ import type { FontMeta, FontFormat, FontMetaStatic } from "./schema";
|
|
|
4
4
|
export type PartialFontAsset = {
|
|
5
5
|
format: FontFormat;
|
|
6
6
|
meta: FontMeta;
|
|
7
|
-
|
|
7
|
+
name: string;
|
|
8
8
|
};
|
|
9
9
|
|
|
10
10
|
export type FontFace = {
|
|
@@ -16,14 +16,18 @@ export type FontFace = {
|
|
|
16
16
|
fontStretch?: string;
|
|
17
17
|
};
|
|
18
18
|
|
|
19
|
-
const formatFace = (
|
|
19
|
+
const formatFace = (
|
|
20
|
+
asset: PartialFontAsset,
|
|
21
|
+
format: string,
|
|
22
|
+
url: string
|
|
23
|
+
): FontFace => {
|
|
20
24
|
if ("variationAxes" in asset.meta) {
|
|
21
25
|
const { wght, wdth } = asset.meta?.variationAxes ?? {};
|
|
22
26
|
return {
|
|
23
27
|
fontFamily: asset.meta.family,
|
|
24
28
|
fontStyle: "normal",
|
|
25
29
|
fontDisplay: "swap",
|
|
26
|
-
src: `url('${
|
|
30
|
+
src: `url('${url}') format('${format}')`,
|
|
27
31
|
fontStretch: wdth ? `${wdth.min}% ${wdth.max}%` : undefined,
|
|
28
32
|
fontWeight: wght ? `${wght.min} ${wght.max}` : undefined,
|
|
29
33
|
};
|
|
@@ -33,7 +37,7 @@ const formatFace = (asset: PartialFontAsset, format: string): FontFace => {
|
|
|
33
37
|
fontStyle: asset.meta.style,
|
|
34
38
|
fontWeight: asset.meta.weight,
|
|
35
39
|
fontDisplay: "swap",
|
|
36
|
-
src: `url('${
|
|
40
|
+
src: `url('${url}') format('${format}')`,
|
|
37
41
|
};
|
|
38
42
|
};
|
|
39
43
|
|
|
@@ -45,10 +49,15 @@ const getKey = (asset: PartialFontAsset) => {
|
|
|
45
49
|
};
|
|
46
50
|
|
|
47
51
|
export const getFontFaces = (
|
|
48
|
-
assets: Array<PartialFontAsset
|
|
52
|
+
assets: Array<PartialFontAsset>,
|
|
53
|
+
options: {
|
|
54
|
+
assetBaseUrl: string;
|
|
55
|
+
}
|
|
49
56
|
): Array<FontFace> => {
|
|
57
|
+
const { assetBaseUrl } = options;
|
|
50
58
|
const faces = new Map();
|
|
51
59
|
for (const asset of assets) {
|
|
60
|
+
const url = `${assetBaseUrl}${asset.name}`;
|
|
52
61
|
const assetKey = getKey(asset);
|
|
53
62
|
const face = faces.get(assetKey);
|
|
54
63
|
const format = FONT_FORMATS.get(asset.format);
|
|
@@ -58,13 +67,13 @@ export const getFontFaces = (
|
|
|
58
67
|
}
|
|
59
68
|
|
|
60
69
|
if (face === undefined) {
|
|
61
|
-
const face = formatFace(asset, format);
|
|
70
|
+
const face = formatFace(asset, format, url);
|
|
62
71
|
faces.set(assetKey, face);
|
|
63
72
|
continue;
|
|
64
73
|
}
|
|
65
74
|
|
|
66
75
|
// We already have that font face, so we need to add the new src
|
|
67
|
-
face.src += `, url('${
|
|
76
|
+
face.src += `, url('${url}') format('${format}')`;
|
|
68
77
|
}
|
|
69
78
|
return Array.from(faces.values());
|
|
70
79
|
};
|