@pikacss/plugin-icons 0.0.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) 2023 DevilTea
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/dist/index.cjs ADDED
@@ -0,0 +1,158 @@
1
+ 'use strict';
2
+
3
+ const utils = require('@iconify/utils');
4
+ const core = require('@pikacss/core');
5
+ const presetIcons = require('@unocss/preset-icons');
6
+ const ofetch = require('ofetch');
7
+
8
+ function createCDNLoader(cdnBase) {
9
+ return presetIcons.createCDNFetchLoader(ofetch.$fetch, cdnBase);
10
+ }
11
+ async function createIconsLoader(config) {
12
+ const {
13
+ cdn
14
+ } = config;
15
+ const loaders = [];
16
+ const {
17
+ isNode,
18
+ isVSCode,
19
+ isESLint
20
+ } = presetIcons.getEnvFlags();
21
+ if (isNode && !isVSCode && !isESLint) {
22
+ const nodeLoader = await presetIcons.createNodeLoader();
23
+ if (nodeLoader !== void 0)
24
+ loaders.push(nodeLoader);
25
+ }
26
+ if (cdn)
27
+ loaders.push(createCDNLoader(cdn));
28
+ loaders.push(utils.loadIcon);
29
+ return presetIcons.combineLoaders(loaders);
30
+ }
31
+ const globalColonRE = /:/g;
32
+ function createIconsPlugin(lookupIconLoader) {
33
+ let engine;
34
+ let enginePrefix = "";
35
+ const registeredIconVariables = /* @__PURE__ */ new Map();
36
+ return core.defineEnginePlugin({
37
+ name: "icons",
38
+ config: async (config) => {
39
+ const iconsConfig = config.icons || {};
40
+ const {
41
+ scale = 1,
42
+ mode = "auto",
43
+ prefix = "i-",
44
+ iconifyCollectionsNames,
45
+ collections: customCollections,
46
+ customizations = {},
47
+ autoInstall = false,
48
+ collectionsNodeResolvePath,
49
+ unit,
50
+ extraProperties = {},
51
+ processor,
52
+ autocomplete: _autocomplete
53
+ } = iconsConfig;
54
+ const loaderOptions = {
55
+ addXmlNs: true,
56
+ scale,
57
+ customCollections,
58
+ autoInstall,
59
+ cwd: collectionsNodeResolvePath,
60
+ // avoid warn from @iconify/loader: we'll warn below if not found
61
+ warn: void 0,
62
+ customizations: {
63
+ ...customizations,
64
+ additionalProps: { ...extraProperties },
65
+ trimCustomSvg: true,
66
+ async iconCustomizer(collection, icon, props) {
67
+ await customizations.iconCustomizer?.(collection, icon, props);
68
+ if (unit) {
69
+ if (!props.width)
70
+ props.width = `${scale}${unit}`;
71
+ if (!props.height)
72
+ props.height = `${scale}${unit}`;
73
+ }
74
+ }
75
+ }
76
+ };
77
+ const prefixRE = new RegExp(`^(${[prefix].flat().join("|")})`);
78
+ const autocompletePrefix = [prefix].flat();
79
+ const autocomplete = [
80
+ ...autocompletePrefix,
81
+ ...autocompletePrefix.flatMap((p) => _autocomplete?.map((a) => `${p}${a.replace(prefixRE, "")}`) || [])
82
+ ];
83
+ let iconLoader;
84
+ config.preflights ||= [];
85
+ config.preflights.push(() => {
86
+ const iconVariables = [...registeredIconVariables.entries()].map(([name, value]) => `${name}: ${value};`).join("\n");
87
+ return `:root { ${iconVariables} }`;
88
+ });
89
+ config.shortcuts ||= [];
90
+ config.shortcuts.push({
91
+ shortcut: new RegExp(`^(?:${[prefix].flat().join("|")})([\\w:-]+)(?:\\?(mask|bg|auto))?$`),
92
+ value: async (match) => {
93
+ let [full, body, _mode = mode] = match;
94
+ iconLoader = iconLoader || await lookupIconLoader(iconsConfig);
95
+ const usedProps = {};
96
+ const parsed = await presetIcons.parseIconWithLoader(
97
+ body,
98
+ iconLoader,
99
+ { ...loaderOptions, usedProps },
100
+ iconifyCollectionsNames
101
+ );
102
+ if (parsed == null) {
103
+ console.warn(`failed to load icon "${full}"`);
104
+ return {};
105
+ }
106
+ const url = `url("data:image/svg+xml;utf8,${utils.encodeSvgForCss(parsed.svg)}")`;
107
+ const varName = `--${enginePrefix}svg-icon-${body.replace(globalColonRE, "-")}`;
108
+ if (registeredIconVariables.has(varName) === false) {
109
+ registeredIconVariables.set(varName, url);
110
+ engine.notifyPreflightUpdated();
111
+ }
112
+ if (_mode === "auto")
113
+ _mode = parsed.svg.includes("currentColor") ? "mask" : "bg";
114
+ let styleItem;
115
+ if (_mode === "mask") {
116
+ styleItem = {
117
+ "--svg-icon": `var(${varName})`,
118
+ "-webkit-mask": "var(--svg-icon) no-repeat",
119
+ "mask": "var(--svg-icon) no-repeat",
120
+ "-webkit-mask-size": "100% 100%",
121
+ "mask-size": "100% 100%",
122
+ "background-color": "currentColor",
123
+ // for Safari https://github.com/elk-zone/elk/pull/264
124
+ "color": "inherit",
125
+ ...usedProps
126
+ };
127
+ } else {
128
+ styleItem = {
129
+ "--svg-icon": `var(${varName})`,
130
+ "background": "var(--svg-icon) no-repeat",
131
+ "background-size": "100% 100%",
132
+ "background-color": "transparent",
133
+ ...usedProps
134
+ };
135
+ }
136
+ processor?.(
137
+ styleItem,
138
+ {
139
+ ...parsed,
140
+ mode: _mode
141
+ }
142
+ );
143
+ return styleItem;
144
+ },
145
+ autocomplete
146
+ });
147
+ },
148
+ engineInitialized: (_engine) => {
149
+ engine = _engine;
150
+ enginePrefix = _engine.config.prefix;
151
+ }
152
+ });
153
+ }
154
+ function icons() {
155
+ return createIconsPlugin(createIconsLoader);
156
+ }
157
+
158
+ exports.icons = icons;
@@ -0,0 +1,26 @@
1
+ import { Simplify, StyleItem, EnginePlugin } from '@pikacss/core';
2
+ import { IconsOptions } from '@unocss/preset-icons';
3
+
4
+ interface IconMeta {
5
+ collection: string;
6
+ name: string;
7
+ svg: string;
8
+ mode?: IconsConfig['mode'];
9
+ }
10
+ type IconsConfig = Simplify<Omit<IconsOptions, 'warn' | 'layer' | 'processor' | 'customFetcher'> & {
11
+ /**
12
+ * Processor for the CSS object before stringify
13
+ */
14
+ processor?: (styleItem: StyleItem, meta: Required<IconMeta>) => void;
15
+ /**
16
+ * Specify the icons for auto-completion.
17
+ */
18
+ autocomplete?: string[];
19
+ }>;
20
+ interface IconsPluginConfig {
21
+ icons?: IconsConfig;
22
+ }
23
+ type IconsPlugin = EnginePlugin<IconsPluginConfig>;
24
+ declare function icons(): IconsPlugin;
25
+
26
+ export { type IconsConfig, type IconsPlugin, icons };
@@ -0,0 +1,26 @@
1
+ import { Simplify, StyleItem, EnginePlugin } from '@pikacss/core';
2
+ import { IconsOptions } from '@unocss/preset-icons';
3
+
4
+ interface IconMeta {
5
+ collection: string;
6
+ name: string;
7
+ svg: string;
8
+ mode?: IconsConfig['mode'];
9
+ }
10
+ type IconsConfig = Simplify<Omit<IconsOptions, 'warn' | 'layer' | 'processor' | 'customFetcher'> & {
11
+ /**
12
+ * Processor for the CSS object before stringify
13
+ */
14
+ processor?: (styleItem: StyleItem, meta: Required<IconMeta>) => void;
15
+ /**
16
+ * Specify the icons for auto-completion.
17
+ */
18
+ autocomplete?: string[];
19
+ }>;
20
+ interface IconsPluginConfig {
21
+ icons?: IconsConfig;
22
+ }
23
+ type IconsPlugin = EnginePlugin<IconsPluginConfig>;
24
+ declare function icons(): IconsPlugin;
25
+
26
+ export { type IconsConfig, type IconsPlugin, icons };
@@ -0,0 +1,26 @@
1
+ import { Simplify, StyleItem, EnginePlugin } from '@pikacss/core';
2
+ import { IconsOptions } from '@unocss/preset-icons';
3
+
4
+ interface IconMeta {
5
+ collection: string;
6
+ name: string;
7
+ svg: string;
8
+ mode?: IconsConfig['mode'];
9
+ }
10
+ type IconsConfig = Simplify<Omit<IconsOptions, 'warn' | 'layer' | 'processor' | 'customFetcher'> & {
11
+ /**
12
+ * Processor for the CSS object before stringify
13
+ */
14
+ processor?: (styleItem: StyleItem, meta: Required<IconMeta>) => void;
15
+ /**
16
+ * Specify the icons for auto-completion.
17
+ */
18
+ autocomplete?: string[];
19
+ }>;
20
+ interface IconsPluginConfig {
21
+ icons?: IconsConfig;
22
+ }
23
+ type IconsPlugin = EnginePlugin<IconsPluginConfig>;
24
+ declare function icons(): IconsPlugin;
25
+
26
+ export { type IconsConfig, type IconsPlugin, icons };
package/dist/index.mjs ADDED
@@ -0,0 +1,156 @@
1
+ import { encodeSvgForCss, loadIcon } from '@iconify/utils';
2
+ import { defineEnginePlugin } from '@pikacss/core';
3
+ import { parseIconWithLoader, getEnvFlags, createNodeLoader, combineLoaders, createCDNFetchLoader } from '@unocss/preset-icons';
4
+ import { $fetch } from 'ofetch';
5
+
6
+ function createCDNLoader(cdnBase) {
7
+ return createCDNFetchLoader($fetch, cdnBase);
8
+ }
9
+ async function createIconsLoader(config) {
10
+ const {
11
+ cdn
12
+ } = config;
13
+ const loaders = [];
14
+ const {
15
+ isNode,
16
+ isVSCode,
17
+ isESLint
18
+ } = getEnvFlags();
19
+ if (isNode && !isVSCode && !isESLint) {
20
+ const nodeLoader = await createNodeLoader();
21
+ if (nodeLoader !== void 0)
22
+ loaders.push(nodeLoader);
23
+ }
24
+ if (cdn)
25
+ loaders.push(createCDNLoader(cdn));
26
+ loaders.push(loadIcon);
27
+ return combineLoaders(loaders);
28
+ }
29
+ const globalColonRE = /:/g;
30
+ function createIconsPlugin(lookupIconLoader) {
31
+ let engine;
32
+ let enginePrefix = "";
33
+ const registeredIconVariables = /* @__PURE__ */ new Map();
34
+ return defineEnginePlugin({
35
+ name: "icons",
36
+ config: async (config) => {
37
+ const iconsConfig = config.icons || {};
38
+ const {
39
+ scale = 1,
40
+ mode = "auto",
41
+ prefix = "i-",
42
+ iconifyCollectionsNames,
43
+ collections: customCollections,
44
+ customizations = {},
45
+ autoInstall = false,
46
+ collectionsNodeResolvePath,
47
+ unit,
48
+ extraProperties = {},
49
+ processor,
50
+ autocomplete: _autocomplete
51
+ } = iconsConfig;
52
+ const loaderOptions = {
53
+ addXmlNs: true,
54
+ scale,
55
+ customCollections,
56
+ autoInstall,
57
+ cwd: collectionsNodeResolvePath,
58
+ // avoid warn from @iconify/loader: we'll warn below if not found
59
+ warn: void 0,
60
+ customizations: {
61
+ ...customizations,
62
+ additionalProps: { ...extraProperties },
63
+ trimCustomSvg: true,
64
+ async iconCustomizer(collection, icon, props) {
65
+ await customizations.iconCustomizer?.(collection, icon, props);
66
+ if (unit) {
67
+ if (!props.width)
68
+ props.width = `${scale}${unit}`;
69
+ if (!props.height)
70
+ props.height = `${scale}${unit}`;
71
+ }
72
+ }
73
+ }
74
+ };
75
+ const prefixRE = new RegExp(`^(${[prefix].flat().join("|")})`);
76
+ const autocompletePrefix = [prefix].flat();
77
+ const autocomplete = [
78
+ ...autocompletePrefix,
79
+ ...autocompletePrefix.flatMap((p) => _autocomplete?.map((a) => `${p}${a.replace(prefixRE, "")}`) || [])
80
+ ];
81
+ let iconLoader;
82
+ config.preflights ||= [];
83
+ config.preflights.push(() => {
84
+ const iconVariables = [...registeredIconVariables.entries()].map(([name, value]) => `${name}: ${value};`).join("\n");
85
+ return `:root { ${iconVariables} }`;
86
+ });
87
+ config.shortcuts ||= [];
88
+ config.shortcuts.push({
89
+ shortcut: new RegExp(`^(?:${[prefix].flat().join("|")})([\\w:-]+)(?:\\?(mask|bg|auto))?$`),
90
+ value: async (match) => {
91
+ let [full, body, _mode = mode] = match;
92
+ iconLoader = iconLoader || await lookupIconLoader(iconsConfig);
93
+ const usedProps = {};
94
+ const parsed = await parseIconWithLoader(
95
+ body,
96
+ iconLoader,
97
+ { ...loaderOptions, usedProps },
98
+ iconifyCollectionsNames
99
+ );
100
+ if (parsed == null) {
101
+ console.warn(`failed to load icon "${full}"`);
102
+ return {};
103
+ }
104
+ const url = `url("data:image/svg+xml;utf8,${encodeSvgForCss(parsed.svg)}")`;
105
+ const varName = `--${enginePrefix}svg-icon-${body.replace(globalColonRE, "-")}`;
106
+ if (registeredIconVariables.has(varName) === false) {
107
+ registeredIconVariables.set(varName, url);
108
+ engine.notifyPreflightUpdated();
109
+ }
110
+ if (_mode === "auto")
111
+ _mode = parsed.svg.includes("currentColor") ? "mask" : "bg";
112
+ let styleItem;
113
+ if (_mode === "mask") {
114
+ styleItem = {
115
+ "--svg-icon": `var(${varName})`,
116
+ "-webkit-mask": "var(--svg-icon) no-repeat",
117
+ "mask": "var(--svg-icon) no-repeat",
118
+ "-webkit-mask-size": "100% 100%",
119
+ "mask-size": "100% 100%",
120
+ "background-color": "currentColor",
121
+ // for Safari https://github.com/elk-zone/elk/pull/264
122
+ "color": "inherit",
123
+ ...usedProps
124
+ };
125
+ } else {
126
+ styleItem = {
127
+ "--svg-icon": `var(${varName})`,
128
+ "background": "var(--svg-icon) no-repeat",
129
+ "background-size": "100% 100%",
130
+ "background-color": "transparent",
131
+ ...usedProps
132
+ };
133
+ }
134
+ processor?.(
135
+ styleItem,
136
+ {
137
+ ...parsed,
138
+ mode: _mode
139
+ }
140
+ );
141
+ return styleItem;
142
+ },
143
+ autocomplete
144
+ });
145
+ },
146
+ engineInitialized: (_engine) => {
147
+ engine = _engine;
148
+ enginePrefix = _engine.config.prefix;
149
+ }
150
+ });
151
+ }
152
+ function icons() {
153
+ return createIconsPlugin(createIconsLoader);
154
+ }
155
+
156
+ export { icons };
package/package.json ADDED
@@ -0,0 +1,53 @@
1
+ {
2
+ "name": "@pikacss/plugin-icons",
3
+ "type": "module",
4
+ "publishConfig": {
5
+ "access": "public"
6
+ },
7
+ "version": "0.0.1",
8
+ "author": "DevilTea <ch19980814@gmail.com>",
9
+ "license": "MIT",
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/DevilTea/pikacss.git",
13
+ "directory": "packages/plugin-icons"
14
+ },
15
+ "bugs": {
16
+ "url": "https://github.com/DevilTea/pikacss/issues"
17
+ },
18
+ "keywords": [],
19
+ "exports": {
20
+ ".": {
21
+ "import": {
22
+ "types": "./dist/index.d.mts",
23
+ "default": "./dist/index.mjs"
24
+ },
25
+ "require": {
26
+ "types": "./dist/index.d.cts",
27
+ "default": "./dist/index.cjs"
28
+ }
29
+ }
30
+ },
31
+ "main": "dist/index.cjs",
32
+ "module": "dist/index.mjs",
33
+ "types": "dist/index.d.ts",
34
+ "files": [
35
+ "dist"
36
+ ],
37
+ "dependencies": {
38
+ "@iconify/utils": "^2.3.0",
39
+ "@unocss/preset-icons": "^65.4.3",
40
+ "ofetch": "^1.4.1",
41
+ "@pikacss/core": "0.0.1"
42
+ },
43
+ "scripts": {
44
+ "build": "unbuild",
45
+ "build:pack": "pnpm build && pnpm pack",
46
+ "stub": "unbuild --stub",
47
+ "typecheck": "pnpm typecheck:package && pnpm typecheck:test",
48
+ "typecheck:package": "tsc --project ./tsconfig.package.json --noEmit",
49
+ "typecheck:test": "tsc --project ./tsconfig.tests.json --noEmit",
50
+ "test": "vitest run",
51
+ "test:watch": "vitest"
52
+ }
53
+ }