@memobit/icons 1.4.0 → 1.4.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/README.md CHANGED
@@ -41,6 +41,16 @@ importing it pulls in the whole set and defeats tree-shaking. Reach for it only
41
41
  name is not known until runtime — for example a value chosen through a picker and
42
42
  persisted to a database.
43
43
 
44
+ The same subpath exports `getIcon(name)`, `loadIcon(name)`, `loadAllIcons()` and
45
+ `canLoadMoreIcons`. With the full map they simply read it. With the Vite plugin's
46
+ `lazy: true`, only the icons found in the app's source ship up front; any other icon is
47
+ fetched as its own small chunk the first time `loadIcon` asks for it, and `loadAllIcons`
48
+ fetches the rest of the set, which is what an icon picker needs.
49
+
50
+ ```ts
51
+ memobitIcons({ root: import.meta.dirname, lazy: true });
52
+ ```
53
+
44
54
  ### Metadata
45
55
 
46
56
  ```ts
package/dist/map.d.ts CHANGED
@@ -1,3 +1,7 @@
1
1
  import { ReactElement } from 'react';
2
2
  import { IconName } from './iconNames';
3
3
  export declare const iconMap: Record<IconName, ReactElement>;
4
+ export declare const canLoadMoreIcons: boolean;
5
+ export declare const getIcon: (name: string) => ReactElement | undefined;
6
+ export declare const loadIcon: (name: string) => Promise<ReactElement | undefined>;
7
+ export declare const loadAllIcons: () => Promise<Readonly<Record<string, ReactElement>>>;
package/dist/map.js CHANGED
@@ -494,6 +494,7 @@ import { workout } from "./icons/workout.js";
494
494
  import { wrench } from "./icons/wrench.js";
495
495
  import { yoga } from "./icons/yoga.js";
496
496
  import { youtube } from "./icons/youtube.js";
497
+ import { createIconRegistry } from "./registry.js";
497
498
  //#region src/map.ts
498
499
  var iconMap = {
499
500
  "air-conditioning": airConditioning,
@@ -993,5 +994,10 @@ var iconMap = {
993
994
  uranus,
994
995
  venus
995
996
  };
997
+ var registry = createIconRegistry(iconMap);
998
+ var canLoadMoreIcons = registry.canLoadMore;
999
+ var getIcon = registry.getIcon;
1000
+ var loadIcon = registry.loadIcon;
1001
+ var loadAllIcons = registry.loadAllIcons;
996
1002
  //#endregion
997
- export { iconMap };
1003
+ export { canLoadMoreIcons, getIcon, iconMap, loadAllIcons, loadIcon };
@@ -0,0 +1,17 @@
1
+ import { ReactElement } from 'react';
2
+ export type IconLoader = () => Promise<ReactElement>;
3
+ export interface IconRegistry {
4
+ /** False when every icon the app can have is already in memory, so nothing will ever load. */
5
+ canLoadMore: boolean;
6
+ getIcon: (name: string) => ReactElement | undefined;
7
+ /** Resolves to undefined for a name that is neither in memory nor loadable. */
8
+ loadIcon: (name: string) => Promise<ReactElement | undefined>;
9
+ loadAllIcons: () => Promise<Readonly<Record<string, ReactElement>>>;
10
+ }
11
+ /**
12
+ * Lookup behind `@memobit/icons/map`. Without loaders it is a plain view over the static map,
13
+ * which is what the full map and a trimmed build give. With loaders (the Vite plugin's `lazy`
14
+ * mode) a name missing from the static map is fetched on demand and kept, so `getIcon` finds
15
+ * it synchronously from then on.
16
+ */
17
+ export declare const createIconRegistry: (staticIcons: Readonly<Record<string, ReactElement>>, loaders?: Readonly<Record<string, IconLoader>>) => IconRegistry;
@@ -0,0 +1,50 @@
1
+ //#region src/registry.ts
2
+ /**
3
+ * Lookup behind `@memobit/icons/map`. Without loaders it is a plain view over the static map,
4
+ * which is what the full map and a trimmed build give. With loaders (the Vite plugin's `lazy`
5
+ * mode) a name missing from the static map is fetched on demand and kept, so `getIcon` finds
6
+ * it synchronously from then on.
7
+ */
8
+ var createIconRegistry = (staticIcons, loaders) => {
9
+ const icons = { ...staticIcons };
10
+ const pending = /* @__PURE__ */ new Map();
11
+ let allIcons = null;
12
+ const canLoadMore = loaders !== void 0 && Object.keys(loaders).length > 0;
13
+ const loadIcon = (name) => {
14
+ const known = icons[name];
15
+ if (known !== void 0) return Promise.resolve(known);
16
+ const loader = loaders?.[name];
17
+ if (loader === void 0) return Promise.resolve(void 0);
18
+ const inFlight = pending.get(name);
19
+ if (inFlight) return inFlight;
20
+ const request = loader().then((icon) => {
21
+ icons[name] = icon;
22
+ pending.delete(name);
23
+ return icon;
24
+ }, (error) => {
25
+ pending.delete(name);
26
+ throw error;
27
+ });
28
+ pending.set(name, request);
29
+ return request;
30
+ };
31
+ const loadAllIcons = () => {
32
+ if (!canLoadMore) return Promise.resolve(icons);
33
+ if (!allIcons) {
34
+ const names = Object.keys(loaders ?? {});
35
+ allIcons = Promise.all(names.map(loadIcon)).then(() => Object.fromEntries(Object.keys(icons).sort().map((name) => [name, icons[name]])), (error) => {
36
+ allIcons = null;
37
+ throw error;
38
+ });
39
+ }
40
+ return allIcons;
41
+ };
42
+ return {
43
+ canLoadMore,
44
+ getIcon: (name) => icons[name],
45
+ loadIcon,
46
+ loadAllIcons
47
+ };
48
+ };
49
+ //#endregion
50
+ export { createIconRegistry };
package/dist/vite.d.ts CHANGED
@@ -8,9 +8,16 @@ export interface MemobitIconsOptions {
8
8
  /**
9
9
  * Names the source scan cannot see because they only exist at runtime — a value from a
10
10
  * database, an API response, or an icon picker. Pass 'all' to keep the whole set, which
11
- * is what an app with a picker needs.
11
+ * is what an app with a picker needs unless it uses `lazy`.
12
12
  */
13
13
  include?: readonly string[] | 'all';
14
+ /**
15
+ * Keep the scanned icons in the startup bundle and load any other icon the first time it
16
+ * renders, one small chunk per icon; `IconPicker` loads the whole set when it opens. For
17
+ * an app whose names come from its data, where `include: 'all'` would put every icon in
18
+ * the startup bundle. Cannot be combined with `include: 'all'`.
19
+ */
20
+ lazy?: boolean;
14
21
  /** Directories to scan, relative to `root`. Defaults to ['src']. */
15
22
  scanDirs?: readonly string[];
16
23
  /**
@@ -37,7 +44,8 @@ export interface MemobitIconsOptions {
37
44
  * invisible to any source scan, and trimming it away would break icons at runtime with no
38
45
  * build error. An app that does not use this plugin keeps the full set and behaves exactly
39
46
  * as before. An app whose names are user-chosen — anything with an icon picker — should
40
- * pass `include: 'all'`, which keeps everything and simply does nothing.
47
+ * pass `lazy: true`, which keeps the scanned icons up front and fetches the rest as they
48
+ * render, or `include: 'all'`, which keeps everything and simply does nothing.
41
49
  */
42
50
  export declare const memobitIcons: (options?: MemobitIconsOptions) => TrimPlugin;
43
51
  export {};
package/dist/vite.js CHANGED
@@ -78,8 +78,26 @@ var scanForIconNames = (root, scanDirs, scanPackages) => {
78
78
  }
79
79
  return names;
80
80
  };
81
- var buildMapModule = (names) => {
82
- return `${names.map((name) => `import { ${iconModules[name]} } from '@memobit/icons/icons/${name}';`).join("\n")}\n\nexport const iconMap = {\n${names.map((name) => ` '${name}': ${iconModules[name]},`).join("\n")}\n};\n`;
81
+ var buildMapModule = (names, lazy) => {
82
+ const imports = names.map((name) => `import { ${iconModules[name]} } from '@memobit/icons/icons/${name}';`).join("\n");
83
+ const entries = names.map((name) => ` '${name}': ${iconModules[name]},`).join("\n");
84
+ const kept = new Set(names);
85
+ const loaders = Object.keys(iconModules).filter((name) => !kept.has(name)).map((name) => ` '${name}': () => import('@memobit/icons/icons/${name}').then(module => module.${iconModules[name]}),`).join("\n");
86
+ const registry = lazy ? `createIconRegistry(iconMap, {\n${loaders}\n})` : "createIconRegistry(iconMap)";
87
+ return [
88
+ `import { createIconRegistry } from '@memobit/icons/registry';`,
89
+ imports,
90
+ "",
91
+ `export const iconMap = {\n${entries}\n};`,
92
+ "",
93
+ `const registry = ${registry};`,
94
+ "",
95
+ "export const canLoadMoreIcons = registry.canLoadMore;",
96
+ "export const getIcon = registry.getIcon;",
97
+ "export const loadIcon = registry.loadIcon;",
98
+ "export const loadAllIcons = registry.loadAllIcons;",
99
+ ""
100
+ ].join("\n");
83
101
  };
84
102
  /**
85
103
  * Replaces the full 495-icon `iconMap` with one holding only the icons an app actually
@@ -94,10 +112,12 @@ var buildMapModule = (names) => {
94
112
  * invisible to any source scan, and trimming it away would break icons at runtime with no
95
113
  * build error. An app that does not use this plugin keeps the full set and behaves exactly
96
114
  * as before. An app whose names are user-chosen — anything with an icon picker — should
97
- * pass `include: 'all'`, which keeps everything and simply does nothing.
115
+ * pass `lazy: true`, which keeps the scanned icons up front and fetches the rest as they
116
+ * render, or `include: 'all'`, which keeps everything and simply does nothing.
98
117
  */
99
118
  var memobitIcons = (options = {}) => {
100
- const { include = [], scanDirs = ["src"], scanPackages = ["@memobit/libs"], root = process.cwd(), debug = false } = options;
119
+ const { include = [], lazy = false, scanDirs = ["src"], scanPackages = ["@memobit/libs"], root = process.cwd(), debug = false } = options;
120
+ if (lazy && include === "all") throw new Error(`[memobit-icons] \`lazy\` keeps only the scanned icons up front, so it cannot be combined with include: 'all'.`);
101
121
  return {
102
122
  name: "memobit-icons-trim",
103
123
  enforce: "pre",
@@ -106,15 +126,18 @@ var memobitIcons = (options = {}) => {
106
126
  },
107
127
  load(id) {
108
128
  if (id !== VIRTUAL_ID) return null;
109
- if (include === "all") return buildMapModule(Object.keys(iconModules));
129
+ if (include === "all") return buildMapModule(Object.keys(iconModules), false);
110
130
  const names = scanForIconNames(root, scanDirs, scanPackages);
111
131
  for (const name of include) {
112
132
  if (!(name in iconModules)) throw new Error(`[memobit-icons] "${name}" was passed to include but is not an icon name.`);
113
133
  names.add(name);
114
134
  }
115
135
  const kept = [...names].sort();
116
- if (debug) console.log(`[memobit-icons] keeping ${kept.length} of ${Object.keys(iconModules).length} icons: ${kept.join(", ")}`);
117
- return buildMapModule(kept);
136
+ if (debug) {
137
+ const rest = lazy ? " up front, the rest load on demand" : "";
138
+ console.log(`[memobit-icons] keeping ${kept.length} of ${Object.keys(iconModules).length} icons${rest}: ${kept.join(", ")}`);
139
+ }
140
+ return buildMapModule(kept, lazy);
118
141
  }
119
142
  };
120
143
  };
package/package.json CHANGED
@@ -1,95 +1,99 @@
1
- {
2
- "name": "@memobit/icons",
3
- "version": "1.4.0",
4
- "description": "495 hand-built SVG icons for the Memobit apps — individually exported and tree-shakeable, with categories and search aliases. TypeScript-first, ESM-only, React 19.",
5
- "keywords": [
6
- "react",
7
- "icons",
8
- "svg",
9
- "svg-icons",
10
- "icon-set",
11
- "typescript",
12
- "tree-shakeable",
13
- "memobit"
14
- ],
15
- "author": "Iulian Cristea <iulianx@gmail.com>",
16
- "license": "Apache-2.0",
17
- "homepage": "https://react.memobit.ro/",
18
- "repository": {
19
- "type": "git",
20
- "url": "git+https://github.com/CristeaIulian/memobit-react-library-icons.git"
21
- },
22
- "bugs": {
23
- "url": "https://github.com/CristeaIulian/memobit-react-library-icons/issues"
24
- },
25
- "publishConfig": {
26
- "access": "public"
27
- },
28
- "sideEffects": false,
29
- "type": "module",
30
- "main": "dist/index.js",
31
- "module": "dist/index.js",
32
- "types": "dist/index.d.ts",
33
- "exports": {
34
- ".": {
35
- "types": "./dist/index.d.ts",
36
- "import": "./dist/index.js"
37
- },
38
- "./map": {
39
- "types": "./dist/map.d.ts",
40
- "import": "./dist/map.js"
41
- },
42
- "./vite": {
43
- "types": "./dist/vite.d.ts",
44
- "import": "./dist/vite.js"
45
- },
46
- "./icons/*": {
47
- "types": "./dist/icons/*.d.ts",
48
- "import": "./dist/icons/*.js"
49
- },
50
- "./package.json": "./package.json"
51
- },
52
- "files": [
53
- "dist"
54
- ],
55
- "scripts": {
56
- "build": "vite build && npm pack",
57
- "release": "npm run build && npm publish",
58
- "ship": "npm version patch && npm run release && git push --follow-tags",
59
- "ship:minor": "npm version minor && npm run release && git push --follow-tags",
60
- "ship:major": "npm version major && npm run release && git push --follow-tags",
61
- "prepublishOnly": "vite build",
62
- "generate:icon-names": "node scripts/generate-icon-names.mjs",
63
- "dev": "vite build --watch",
64
- "lint": "eslint src --ext .ts,.tsx",
65
- "lint:fix": "eslint src --ext .ts,.tsx --fix",
66
- "format": "prettier --write \"src/**/*.{ts,tsx,json}\"",
67
- "format:check": "prettier --check \"src/**/*.{ts,tsx,json}\""
68
- },
69
- "peerDependencies": {
70
- "react": "19.3.0"
71
- },
72
- "devDependencies": {
73
- "@eslint/js": "9.39.4",
74
- "@types/node": "25.9.8",
75
- "@types/react": "19.3.0",
76
- "@vitejs/plugin-react": "6.1.1",
77
- "esbuild": "0.28.2",
78
- "eslint": "9.39.4",
79
- "eslint-config-prettier": "10.1.8",
80
- "eslint-plugin-import-x": "4.17.1",
81
- "eslint-plugin-prettier": "5.5.6",
82
- "eslint-plugin-react": "7.37.5",
83
- "eslint-plugin-simple-import-sort": "14.0.0",
84
- "globals": "17.12.0",
85
- "prettier": "3.9.9",
86
- "react": "19.3.0",
87
- "typescript": "6.0.3",
88
- "typescript-eslint": "8.70.1",
89
- "vite": "8.3.1",
90
- "vite-plugin-dts": "5.1.1"
91
- },
92
- "overrides": {
93
- "source-map-js": "1.2.2"
94
- }
95
- }
1
+ {
2
+ "name": "@memobit/icons",
3
+ "version": "1.4.1",
4
+ "description": "495 hand-built SVG icons for the Memobit apps — individually exported and tree-shakeable, with categories and search aliases. TypeScript-first, ESM-only, React 19.",
5
+ "keywords": [
6
+ "react",
7
+ "icons",
8
+ "svg",
9
+ "svg-icons",
10
+ "icon-set",
11
+ "typescript",
12
+ "tree-shakeable",
13
+ "memobit"
14
+ ],
15
+ "author": "Iulian Cristea <iulianx@gmail.com>",
16
+ "license": "Apache-2.0",
17
+ "homepage": "https://react.memobit.ro/",
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/CristeaIulian/memobit-react-library-icons.git"
21
+ },
22
+ "bugs": {
23
+ "url": "https://github.com/CristeaIulian/memobit-react-library-icons/issues"
24
+ },
25
+ "publishConfig": {
26
+ "access": "public"
27
+ },
28
+ "sideEffects": false,
29
+ "type": "module",
30
+ "main": "dist/index.js",
31
+ "module": "dist/index.js",
32
+ "types": "dist/index.d.ts",
33
+ "exports": {
34
+ ".": {
35
+ "types": "./dist/index.d.ts",
36
+ "import": "./dist/index.js"
37
+ },
38
+ "./map": {
39
+ "types": "./dist/map.d.ts",
40
+ "import": "./dist/map.js"
41
+ },
42
+ "./registry": {
43
+ "types": "./dist/registry.d.ts",
44
+ "import": "./dist/registry.js"
45
+ },
46
+ "./vite": {
47
+ "types": "./dist/vite.d.ts",
48
+ "import": "./dist/vite.js"
49
+ },
50
+ "./icons/*": {
51
+ "types": "./dist/icons/*.d.ts",
52
+ "import": "./dist/icons/*.js"
53
+ },
54
+ "./package.json": "./package.json"
55
+ },
56
+ "files": [
57
+ "dist"
58
+ ],
59
+ "scripts": {
60
+ "build": "vite build && npm pack",
61
+ "release": "npm run build && npm publish",
62
+ "ship": "npm version patch && npm run release && git push --follow-tags",
63
+ "ship:minor": "npm version minor && npm run release && git push --follow-tags",
64
+ "ship:major": "npm version major && npm run release && git push --follow-tags",
65
+ "prepublishOnly": "vite build",
66
+ "generate:icon-names": "node scripts/generate-icon-names.mjs",
67
+ "dev": "vite build --watch",
68
+ "lint": "eslint src --ext .ts,.tsx",
69
+ "lint:fix": "eslint src --ext .ts,.tsx --fix",
70
+ "format": "prettier --write \"src/**/*.{ts,tsx,json}\"",
71
+ "format:check": "prettier --check \"src/**/*.{ts,tsx,json}\""
72
+ },
73
+ "peerDependencies": {
74
+ "react": "19.3.0"
75
+ },
76
+ "devDependencies": {
77
+ "@eslint/js": "9.39.4",
78
+ "@types/node": "25.9.8",
79
+ "@types/react": "19.3.0",
80
+ "@vitejs/plugin-react": "6.1.1",
81
+ "esbuild": "0.28.2",
82
+ "eslint": "9.39.4",
83
+ "eslint-config-prettier": "10.1.8",
84
+ "eslint-plugin-import-x": "4.17.1",
85
+ "eslint-plugin-prettier": "5.5.6",
86
+ "eslint-plugin-react": "7.37.5",
87
+ "eslint-plugin-simple-import-sort": "14.0.0",
88
+ "globals": "17.12.0",
89
+ "prettier": "3.9.9",
90
+ "react": "19.3.0",
91
+ "typescript": "6.0.3",
92
+ "typescript-eslint": "8.70.1",
93
+ "vite": "8.3.1",
94
+ "vite-plugin-dts": "5.1.1"
95
+ },
96
+ "overrides": {
97
+ "source-map-js": "1.2.2"
98
+ }
99
+ }