storybook-addon-sdc 0.24.3 → 0.24.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/dist/preset.js +141 -11
- package/package.json +1 -1
package/dist/preset.js
CHANGED
|
@@ -862,8 +862,10 @@ var DEFAULT_ADDON_OPTIONS = {
|
|
|
862
862
|
import { merge as lodashMerge } from "lodash-es";
|
|
863
863
|
|
|
864
864
|
// src/vite-plugin-sdc-icon-packs.ts
|
|
865
|
-
import { existsSync as existsSync4 } from "fs";
|
|
865
|
+
import { existsSync as existsSync4, readdirSync as readdirSync4, readFileSync as readFileSync4 } from "fs";
|
|
866
866
|
import { join as join4 } from "path";
|
|
867
|
+
import { parse as parseYaml3 } from "yaml";
|
|
868
|
+
import fetch3 from "node-fetch";
|
|
867
869
|
|
|
868
870
|
// src/icon-packs.ts
|
|
869
871
|
import { existsSync as existsSync3, readFileSync as readFileSync3, readdirSync as readdirSync3, statSync } from "fs";
|
|
@@ -930,7 +932,7 @@ function resolveSourceEntry(rawSource, nsRoot) {
|
|
|
930
932
|
if (!existsSync3(baseDir)) return [];
|
|
931
933
|
return scanDir(baseDir, baseDir, filterExt);
|
|
932
934
|
}
|
|
933
|
-
function loadIconPackFile(iconsFilePath) {
|
|
935
|
+
function loadIconPackFile(iconsFilePath, resolveIconSource) {
|
|
934
936
|
const watchFiles = [iconsFilePath];
|
|
935
937
|
const packs = {};
|
|
936
938
|
if (!existsSync3(iconsFilePath)) return { packs, watchFiles };
|
|
@@ -949,9 +951,10 @@ function loadIconPackFile(iconsFilePath) {
|
|
|
949
951
|
if (packDef.enabled === false) continue;
|
|
950
952
|
const extractor = packDef.extractor ?? "svg";
|
|
951
953
|
const rawSources = packDef.config?.sources ?? [];
|
|
954
|
+
const resolvedSources = resolveIconSource ? rawSources.map((src) => resolveIconSource(src, { packId, namespace: ns })) : rawSources;
|
|
952
955
|
const sources = [];
|
|
953
956
|
const sourceUrls = [];
|
|
954
|
-
for (const src of
|
|
957
|
+
for (const src of resolvedSources) {
|
|
955
958
|
if (/^https?:\/\//.test(src)) {
|
|
956
959
|
sources.push(src);
|
|
957
960
|
sourceUrls.push(src);
|
|
@@ -971,7 +974,7 @@ function loadIconPackFile(iconsFilePath) {
|
|
|
971
974
|
const svgIcons = {};
|
|
972
975
|
const pathIcons = {};
|
|
973
976
|
if (extractor === "svg") {
|
|
974
|
-
for (const src of
|
|
977
|
+
for (const src of resolvedSources) {
|
|
975
978
|
for (const file of resolveSourceEntry(src, nsRoot)) {
|
|
976
979
|
if (file.iconId === "*") continue;
|
|
977
980
|
watchFiles.push(file.absPath);
|
|
@@ -990,7 +993,7 @@ function loadIconPackFile(iconsFilePath) {
|
|
|
990
993
|
}
|
|
991
994
|
}
|
|
992
995
|
} else if (extractor === "path") {
|
|
993
|
-
for (const src of
|
|
996
|
+
for (const src of resolvedSources) {
|
|
994
997
|
for (const file of resolveSourceEntry(src, nsRoot)) {
|
|
995
998
|
if (file.iconId === "*") continue;
|
|
996
999
|
pathIcons[file.iconId] = {
|
|
@@ -1014,8 +1017,95 @@ function loadIconPackFile(iconsFilePath) {
|
|
|
1014
1017
|
}
|
|
1015
1018
|
return { packs, watchFiles };
|
|
1016
1019
|
}
|
|
1020
|
+
function collectIconIdsFromData(data, packId, out) {
|
|
1021
|
+
if (Array.isArray(data)) {
|
|
1022
|
+
for (const item of data) collectIconIdsFromData(item, packId, out);
|
|
1023
|
+
return;
|
|
1024
|
+
}
|
|
1025
|
+
if (data && typeof data === "object") {
|
|
1026
|
+
const obj = data;
|
|
1027
|
+
if (obj.pack_id === packId && typeof obj.icon_id === "string") {
|
|
1028
|
+
out.add(obj.icon_id);
|
|
1029
|
+
}
|
|
1030
|
+
if (obj.pack === packId && typeof obj.id === "string") {
|
|
1031
|
+
out.add(obj.id);
|
|
1032
|
+
}
|
|
1033
|
+
for (const value of Object.values(obj)) {
|
|
1034
|
+
collectIconIdsFromData(value, packId, out);
|
|
1035
|
+
}
|
|
1036
|
+
}
|
|
1037
|
+
}
|
|
1038
|
+
async function fetchRemoteSvgIcons(pack, usedIconIds, fetchSvg) {
|
|
1039
|
+
const httpSources = pack.sources.filter((s) => /^https?:\/\//.test(s));
|
|
1040
|
+
if (httpSources.length === 0) return;
|
|
1041
|
+
for (const iconId of usedIconIds) {
|
|
1042
|
+
if (pack.svgIcons[iconId]) continue;
|
|
1043
|
+
for (const src of httpSources) {
|
|
1044
|
+
const url = src.includes("{icon_id}") ? src.replace(/\{icon_id\}/g, iconId) : `${src.replace(/\/$/, "")}/${iconId}.svg`;
|
|
1045
|
+
const raw = await fetchSvg(url);
|
|
1046
|
+
if (raw) {
|
|
1047
|
+
const { content, attrs } = parseSvgFile(raw);
|
|
1048
|
+
pack.svgIcons[iconId] = { content, attrs, sourceUrl: url, group: "" };
|
|
1049
|
+
break;
|
|
1050
|
+
}
|
|
1051
|
+
}
|
|
1052
|
+
}
|
|
1053
|
+
}
|
|
1054
|
+
async function fetchRemoteSprite(pack, fetchSvg) {
|
|
1055
|
+
if (pack.extractor !== "svg_sprite") return null;
|
|
1056
|
+
const remote = pack.sources.find((s) => /^https?:\/\//.test(s));
|
|
1057
|
+
if (!remote) return null;
|
|
1058
|
+
const content = await fetchSvg(remote);
|
|
1059
|
+
if (!content) return null;
|
|
1060
|
+
pack.sources = [""];
|
|
1061
|
+
pack.sourceUrls = [""];
|
|
1062
|
+
return content;
|
|
1063
|
+
}
|
|
1017
1064
|
|
|
1018
1065
|
// src/vite-plugin-sdc-icon-packs.ts
|
|
1066
|
+
var remoteSvgCache = /* @__PURE__ */ new Map();
|
|
1067
|
+
async function fetchSvgFromUrl(url) {
|
|
1068
|
+
if (remoteSvgCache.has(url)) return remoteSvgCache.get(url) ?? null;
|
|
1069
|
+
try {
|
|
1070
|
+
const res = await fetch3(url);
|
|
1071
|
+
if (!res.ok) {
|
|
1072
|
+
remoteSvgCache.set(url, null);
|
|
1073
|
+
return null;
|
|
1074
|
+
}
|
|
1075
|
+
const text = await res.text();
|
|
1076
|
+
remoteSvgCache.set(url, text);
|
|
1077
|
+
return text;
|
|
1078
|
+
} catch {
|
|
1079
|
+
remoteSvgCache.set(url, null);
|
|
1080
|
+
return null;
|
|
1081
|
+
}
|
|
1082
|
+
}
|
|
1083
|
+
function readNamespaceYamlData(namespaces) {
|
|
1084
|
+
const out = [];
|
|
1085
|
+
const walk = (dir) => {
|
|
1086
|
+
let entries;
|
|
1087
|
+
try {
|
|
1088
|
+
entries = readdirSync4(dir, { withFileTypes: true });
|
|
1089
|
+
} catch {
|
|
1090
|
+
return;
|
|
1091
|
+
}
|
|
1092
|
+
for (const entry of entries) {
|
|
1093
|
+
const full = join4(dir, entry.name);
|
|
1094
|
+
if (entry.isDirectory()) {
|
|
1095
|
+
walk(full);
|
|
1096
|
+
} else if (entry.name.endsWith(".story.yml") || entry.name.endsWith(".component.yml")) {
|
|
1097
|
+
try {
|
|
1098
|
+
out.push(parseYaml3(readFileSync4(full, "utf8")));
|
|
1099
|
+
} catch {
|
|
1100
|
+
}
|
|
1101
|
+
}
|
|
1102
|
+
}
|
|
1103
|
+
};
|
|
1104
|
+
for (const [, root] of namespaces.entries()) {
|
|
1105
|
+
walk(join4(root, "components"));
|
|
1106
|
+
}
|
|
1107
|
+
return out;
|
|
1108
|
+
}
|
|
1019
1109
|
var VIRTUAL_TWIG = "virtual:sdc-icon-packs:twig";
|
|
1020
1110
|
var RESOLVED_TWIG = "\0" + VIRTUAL_TWIG;
|
|
1021
1111
|
var VIRTUAL_TWING = "virtual:sdc-icon-packs:twing";
|
|
@@ -1061,7 +1151,11 @@ function _sdcBuildIconContext(DrupalAttribute, pack, iconId, settings) {
|
|
|
1061
1151
|
source = pathData.sourceUrl || '';
|
|
1062
1152
|
group = pathData.group || '';
|
|
1063
1153
|
} else if (pack.sourceUrls && pack.sourceUrls[0]) {
|
|
1064
|
-
|
|
1154
|
+
var base = pack.sourceUrls[0];
|
|
1155
|
+
source =
|
|
1156
|
+
base.indexOf('{icon_id}') !== -1
|
|
1157
|
+
? base.replace(/{icon_id}/g, iconId)
|
|
1158
|
+
: base + '/' + iconId;
|
|
1065
1159
|
}
|
|
1066
1160
|
}
|
|
1067
1161
|
|
|
@@ -1161,7 +1255,7 @@ var TWIG_JS_INJECT_AFTER = "addDrupalExtensions(Twig);";
|
|
|
1161
1255
|
var TWING_MARKER = "createSynchronousEnvironment";
|
|
1162
1256
|
var TWING_INJECT_AFTER = "addDrupalExtensions(env);";
|
|
1163
1257
|
var INJECTED_GUARD = "_sdcRegisterIcon";
|
|
1164
|
-
function iconPacksPlugin(namespaces) {
|
|
1258
|
+
function iconPacksPlugin(namespaces, resolveIconSource) {
|
|
1165
1259
|
return {
|
|
1166
1260
|
name: "vite-plugin-sdc-icon-packs",
|
|
1167
1261
|
resolveId(id) {
|
|
@@ -1169,16 +1263,52 @@ function iconPacksPlugin(namespaces) {
|
|
|
1169
1263
|
if (id === VIRTUAL_TWING) return RESOLVED_TWING;
|
|
1170
1264
|
if (id.startsWith(PACK_PREFIX)) return id;
|
|
1171
1265
|
},
|
|
1172
|
-
load(id) {
|
|
1266
|
+
async load(id) {
|
|
1173
1267
|
if (id === RESOLVED_TWIG || id === RESOLVED_TWING) {
|
|
1174
1268
|
const packFilePaths = namespaces.entries().map(([ns, root]) => join4(root, `${ns}.icons.yml`)).filter((p) => existsSync4(p));
|
|
1175
1269
|
return id === RESOLVED_TWIG ? generateTwigModule(packFilePaths) : generateTwingModule(packFilePaths);
|
|
1176
1270
|
}
|
|
1177
1271
|
if (id.startsWith(PACK_PREFIX)) {
|
|
1178
1272
|
const filePath = id.slice(PACK_PREFIX.length);
|
|
1179
|
-
const { packs, watchFiles } = loadIconPackFile(filePath);
|
|
1273
|
+
const { packs, watchFiles } = loadIconPackFile(filePath, resolveIconSource);
|
|
1180
1274
|
watchFiles.forEach((f) => this.addWatchFile(f));
|
|
1181
|
-
|
|
1275
|
+
const remotePacks = Object.values(packs).filter(
|
|
1276
|
+
(p) => p.extractor === "svg" && p.sources.some((s) => /^https?:\/\//.test(s))
|
|
1277
|
+
);
|
|
1278
|
+
if (remotePacks.length > 0) {
|
|
1279
|
+
const yamlData = readNamespaceYamlData(namespaces);
|
|
1280
|
+
for (const pack of remotePacks) {
|
|
1281
|
+
const used = /* @__PURE__ */ new Set();
|
|
1282
|
+
for (const data of yamlData) {
|
|
1283
|
+
collectIconIdsFromData(data, pack.packId, used);
|
|
1284
|
+
}
|
|
1285
|
+
await fetchRemoteSvgIcons(pack, used, fetchSvgFromUrl);
|
|
1286
|
+
}
|
|
1287
|
+
}
|
|
1288
|
+
const sprites = {};
|
|
1289
|
+
for (const pack of Object.values(packs)) {
|
|
1290
|
+
if (pack.extractor === "svg_sprite" && pack.sources.some((s) => /^https?:\/\//.test(s))) {
|
|
1291
|
+
const content = await fetchRemoteSprite(pack, fetchSvgFromUrl);
|
|
1292
|
+
if (content) sprites[pack.packId] = content;
|
|
1293
|
+
}
|
|
1294
|
+
}
|
|
1295
|
+
const injection = Object.keys(sprites).length > 0 ? `
|
|
1296
|
+
if (typeof document !== 'undefined') {
|
|
1297
|
+
var _sdcSprites = ${JSON.stringify(sprites)};
|
|
1298
|
+
for (var _k in _sdcSprites) {
|
|
1299
|
+
var _elId = 'sdc-sprite-' + _k;
|
|
1300
|
+
if (!document.getElementById(_elId)) {
|
|
1301
|
+
var _d = document.createElement('div');
|
|
1302
|
+
_d.id = _elId;
|
|
1303
|
+
_d.style.display = 'none';
|
|
1304
|
+
_d.setAttribute('aria-hidden', 'true');
|
|
1305
|
+
_d.innerHTML = _sdcSprites[_k];
|
|
1306
|
+
document.body.appendChild(_d);
|
|
1307
|
+
}
|
|
1308
|
+
}
|
|
1309
|
+
}
|
|
1310
|
+
` : "";
|
|
1311
|
+
return `${injection}export default ${JSON.stringify(packs)};`;
|
|
1182
1312
|
}
|
|
1183
1313
|
},
|
|
1184
1314
|
handleHotUpdate({ modules, server }) {
|
|
@@ -1284,7 +1414,7 @@ async function viteFinal(config, options) {
|
|
|
1284
1414
|
}),
|
|
1285
1415
|
...twigPlugin ? [twigPlugin] : [],
|
|
1286
1416
|
vite_plugin_storybook_yaml_stories_default({ ...options, globalDefs, namespaces }),
|
|
1287
|
-
iconPacksPlugin(namespaces),
|
|
1417
|
+
iconPacksPlugin(namespaces, sdcStorybookOptions.resolveIconSource),
|
|
1288
1418
|
...headTags.length > 0 ? [{ name: "vite-plugin-sdc-head", transformIndexHtml: () => headTags }] : []
|
|
1289
1419
|
],
|
|
1290
1420
|
optimizeDeps: {
|