@unocss/preset-web-fonts 0.21.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Anthony Fu <https://github.com/antfu>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,82 @@
1
+ # @unocss/preset-web-fonts
2
+
3
+ Web fonts support for [UnoCSS](https://github.com/antfu/unocss).
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm i -D @unocss/preset-web-fonts
9
+ ```
10
+
11
+ ```ts
12
+ import presetWebFonts from '@unocss/preset-web-fonts'
13
+ import presetUno from '@unocss/preset-uno'
14
+
15
+ Unocss({
16
+ presets: [
17
+ presetUno(),
18
+ presetWebFonts({
19
+ provider: 'google', // default provider
20
+ fonts: {
21
+ // these will extend the default theme
22
+ sans: 'Roboto',
23
+ mono: ['Fira Code', 'Fira Mono:400,700'],
24
+ // custom ones
25
+ lobster: 'Lobster',
26
+ lato: [
27
+ {
28
+ name: 'Lato',
29
+ weights: ['400', '700'],
30
+ italic: true
31
+ },
32
+ {
33
+ name: 'sans-serif',
34
+ provider: 'none'
35
+ }
36
+ ]
37
+ },
38
+ })
39
+ ]
40
+ })
41
+ ```
42
+
43
+ The following CSS will be generated
44
+
45
+ ```css
46
+ @import url('https://fonts.googleapis.com/css2?family=Roboto&family=Fira+Code&family=Fira+Mono:wght@400;700&family=Lobster&family=Lato:ital,wght@0,400;0,700;1,400;1,700&display=swap');
47
+
48
+ /* layer: default */
49
+ .font-lato {
50
+ font-family: "Lato", sans-serif;
51
+ }
52
+ .font-lobster {
53
+ font-family: "Lobster";
54
+ }
55
+ .font-mono {
56
+ font-family: "Fira Code", "Fira Mono", ui-monospace, SFMono-Regular, Menlo,
57
+ Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
58
+ }
59
+ .font-sans {
60
+ font-family: "Roboto", ui-sans-serif, system-ui, -apple-system,
61
+ BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans",
62
+ sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol",
63
+ "Noto Color Emoji";
64
+ }
65
+ ```
66
+
67
+ ## Providers
68
+
69
+ Currently supported Providers:
70
+
71
+ - `none` - do nothing, treat the font as system font
72
+ - `google` - [Google Fonts](https://fonts.google.com/)
73
+
74
+ PR welcome to add more providers 🙌
75
+
76
+ ## Configuration
77
+
78
+ Refer to the [type definition](https://github.com/antfu/unocss/blob/main/packages/preset-web-fonts/src/types.ts#L4) for all configurations avaliable.
79
+
80
+ ## License
81
+
82
+ MIT License © 2022-PRESENT [Anthony Fu](https://github.com/antfu)
package/dist/index.cjs ADDED
@@ -0,0 +1,92 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ const core = require('@unocss/core');
6
+
7
+ const GoogleFontsProvider = {
8
+ name: "google",
9
+ getPreflight(fonts) {
10
+ const strings = fonts.filter((i) => i.provider === "google").map((i) => {
11
+ let name = i.name.replace(/\s+/g, "+");
12
+ if (i.weights?.length) {
13
+ name += i.italic ? `:ital,wght@${i.weights.flatMap((i2) => [`0,${i2}`, `1,${i2}`]).sort().join(";")}` : `:wght@${i.weights.sort().join(";")}`;
14
+ }
15
+ return `family=${name}`;
16
+ }).join("&");
17
+ return `@import url('https://fonts.googleapis.com/css2?${strings}&display=swap');`;
18
+ },
19
+ getFontName(font) {
20
+ return `"${font.name}"`;
21
+ }
22
+ };
23
+
24
+ const NoneProvider = {
25
+ name: "none",
26
+ getPreflight() {
27
+ return "";
28
+ },
29
+ getFontName(font) {
30
+ return font.name;
31
+ }
32
+ };
33
+
34
+ function normalizedFontMeta(meta, defaultProvider) {
35
+ if (typeof meta !== "string") {
36
+ meta.provider = meta.provider ?? defaultProvider;
37
+ return meta;
38
+ }
39
+ const [name, weights = ""] = meta.split(":");
40
+ return {
41
+ name,
42
+ weights: weights.split(/[,;]\s*/).filter(Boolean),
43
+ provider: defaultProvider
44
+ };
45
+ }
46
+ const providers = {
47
+ google: GoogleFontsProvider,
48
+ none: NoneProvider
49
+ };
50
+ const preset = (options = {}) => {
51
+ const {
52
+ provider: defaultProvider = "google",
53
+ extendTheme = true,
54
+ themeKey = "fontFamily"
55
+ } = options;
56
+ const fontObject = Object.fromEntries(Object.entries(options.fonts || {}).map(([name, meta]) => [name, core.toArray(meta).map((m) => normalizedFontMeta(m, defaultProvider))]));
57
+ const fonts = Object.values(fontObject).flatMap((i) => i);
58
+ const preset2 = {
59
+ name: "@unocss/preset-web-fonts",
60
+ preflights: [
61
+ {
62
+ getCSS() {
63
+ const names = new Set(fonts.map((i) => i.provider || defaultProvider));
64
+ const preflights = [];
65
+ for (const name of names) {
66
+ const provider = providers[name];
67
+ preflights.push(provider.getPreflight(fonts.filter((i) => i.provider === name)));
68
+ }
69
+ return preflights.filter(Boolean).join("\n");
70
+ },
71
+ layer: "_webfonts"
72
+ }
73
+ ]
74
+ };
75
+ if (extendTheme) {
76
+ preset2.extendTheme = (theme) => {
77
+ if (!theme[themeKey])
78
+ theme[themeKey] = {};
79
+ const obj = Object.fromEntries(Object.entries(fontObject).map(([name, fonts2]) => [name, fonts2.map((f) => providers[f.provider || defaultProvider].getFontName(f))]));
80
+ for (const key of Object.keys(obj)) {
81
+ if (typeof theme[themeKey][key] === "string")
82
+ theme[themeKey][key] = obj[key].map((i) => `${i},`).join("") + theme[themeKey][key];
83
+ else
84
+ theme[themeKey][key] = obj[key].join(",");
85
+ }
86
+ };
87
+ }
88
+ return preset2;
89
+ };
90
+
91
+ exports["default"] = preset;
92
+ exports.normalizedFontMeta = normalizedFontMeta;
@@ -0,0 +1,45 @@
1
+ import { Preset } from '@unocss/core';
2
+
3
+ declare type WebFontsProviders = 'google' | 'none';
4
+ interface WebFontMeta {
5
+ name: string;
6
+ weights?: (string | number)[];
7
+ italic?: boolean;
8
+ /**
9
+ * Override the provider
10
+ * @default <matches root config>
11
+ */
12
+ provider?: WebFontsProviders;
13
+ }
14
+ interface WebFontsOptions {
15
+ /**
16
+ * Provider service of the web fonts
17
+ * @default 'google'
18
+ */
19
+ provider?: WebFontsProviders;
20
+ /**
21
+ * The fonts
22
+ */
23
+ fonts?: Record<string, WebFontMeta | string | (WebFontMeta | string)[]>;
24
+ /**
25
+ * Extend the theme object
26
+ * @default true
27
+ */
28
+ extendTheme?: boolean;
29
+ /**
30
+ * Key for the theme object
31
+ *
32
+ * @default 'fontFamily'
33
+ */
34
+ themeKey?: string;
35
+ }
36
+ interface Provider {
37
+ name: WebFontsProviders;
38
+ getPreflight(fonts: WebFontMeta[]): string;
39
+ getFontName(font: WebFontMeta): string;
40
+ }
41
+
42
+ declare function normalizedFontMeta(meta: WebFontMeta | string, defaultProvider: WebFontsProviders): WebFontMeta;
43
+ declare const preset: (options?: WebFontsOptions) => Preset<any>;
44
+
45
+ export { Provider, WebFontMeta, WebFontsOptions, WebFontsProviders, preset as default, normalizedFontMeta };
package/dist/index.mjs ADDED
@@ -0,0 +1,87 @@
1
+ import { toArray } from '@unocss/core';
2
+
3
+ const GoogleFontsProvider = {
4
+ name: "google",
5
+ getPreflight(fonts) {
6
+ const strings = fonts.filter((i) => i.provider === "google").map((i) => {
7
+ let name = i.name.replace(/\s+/g, "+");
8
+ if (i.weights?.length) {
9
+ name += i.italic ? `:ital,wght@${i.weights.flatMap((i2) => [`0,${i2}`, `1,${i2}`]).sort().join(";")}` : `:wght@${i.weights.sort().join(";")}`;
10
+ }
11
+ return `family=${name}`;
12
+ }).join("&");
13
+ return `@import url('https://fonts.googleapis.com/css2?${strings}&display=swap');`;
14
+ },
15
+ getFontName(font) {
16
+ return `"${font.name}"`;
17
+ }
18
+ };
19
+
20
+ const NoneProvider = {
21
+ name: "none",
22
+ getPreflight() {
23
+ return "";
24
+ },
25
+ getFontName(font) {
26
+ return font.name;
27
+ }
28
+ };
29
+
30
+ function normalizedFontMeta(meta, defaultProvider) {
31
+ if (typeof meta !== "string") {
32
+ meta.provider = meta.provider ?? defaultProvider;
33
+ return meta;
34
+ }
35
+ const [name, weights = ""] = meta.split(":");
36
+ return {
37
+ name,
38
+ weights: weights.split(/[,;]\s*/).filter(Boolean),
39
+ provider: defaultProvider
40
+ };
41
+ }
42
+ const providers = {
43
+ google: GoogleFontsProvider,
44
+ none: NoneProvider
45
+ };
46
+ const preset = (options = {}) => {
47
+ const {
48
+ provider: defaultProvider = "google",
49
+ extendTheme = true,
50
+ themeKey = "fontFamily"
51
+ } = options;
52
+ const fontObject = Object.fromEntries(Object.entries(options.fonts || {}).map(([name, meta]) => [name, toArray(meta).map((m) => normalizedFontMeta(m, defaultProvider))]));
53
+ const fonts = Object.values(fontObject).flatMap((i) => i);
54
+ const preset2 = {
55
+ name: "@unocss/preset-web-fonts",
56
+ preflights: [
57
+ {
58
+ getCSS() {
59
+ const names = new Set(fonts.map((i) => i.provider || defaultProvider));
60
+ const preflights = [];
61
+ for (const name of names) {
62
+ const provider = providers[name];
63
+ preflights.push(provider.getPreflight(fonts.filter((i) => i.provider === name)));
64
+ }
65
+ return preflights.filter(Boolean).join("\n");
66
+ },
67
+ layer: "_webfonts"
68
+ }
69
+ ]
70
+ };
71
+ if (extendTheme) {
72
+ preset2.extendTheme = (theme) => {
73
+ if (!theme[themeKey])
74
+ theme[themeKey] = {};
75
+ const obj = Object.fromEntries(Object.entries(fontObject).map(([name, fonts2]) => [name, fonts2.map((f) => providers[f.provider || defaultProvider].getFontName(f))]));
76
+ for (const key of Object.keys(obj)) {
77
+ if (typeof theme[themeKey][key] === "string")
78
+ theme[themeKey][key] = obj[key].map((i) => `${i},`).join("") + theme[themeKey][key];
79
+ else
80
+ theme[themeKey][key] = obj[key].join(",");
81
+ }
82
+ };
83
+ }
84
+ return preset2;
85
+ };
86
+
87
+ export { preset as default, normalizedFontMeta };
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "@unocss/preset-web-fonts",
3
+ "version": "0.21.1",
4
+ "description": "Web Fonts support for Uno CSS",
5
+ "keywords": [
6
+ "unocss",
7
+ "unocss-preset",
8
+ "fonts",
9
+ "web-fonts",
10
+ "google-fonts"
11
+ ],
12
+ "homepage": "https://github.com/antfu/unocss/tree/main/packages/preset-web-fonts#readme",
13
+ "bugs": {
14
+ "url": "https://github.com/antfu/unocss/issues"
15
+ },
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/antfu/unocss.git",
19
+ "directory": "packages/preset-web-fonts"
20
+ },
21
+ "funding": "https://github.com/sponsors/antfu",
22
+ "license": "MIT",
23
+ "author": "Anthony Fu <anthonyfu117@hotmail.com>",
24
+ "sideEffects": false,
25
+ "exports": {
26
+ ".": {
27
+ "require": "./dist/index.cjs",
28
+ "import": "./dist/index.mjs",
29
+ "types": "./dist/index.d.ts"
30
+ },
31
+ "./fs": {
32
+ "require": "./dist/fs.cjs",
33
+ "import": "./dist/fs.mjs",
34
+ "types": "./dist/fs.d.ts"
35
+ }
36
+ },
37
+ "main": "dist/index.cjs",
38
+ "module": "dist/index.mjs",
39
+ "types": "dist/index.d.ts",
40
+ "files": [
41
+ "dist",
42
+ "*.css"
43
+ ],
44
+ "dependencies": {
45
+ "@unocss/core": "0.21.1"
46
+ },
47
+ "scripts": {
48
+ "build": "unbuild",
49
+ "stub": "unbuild --stub"
50
+ }
51
+ }