"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { default: () => src_default, unplugin: () => unplugin, unpluginFactory: () => unpluginFactory }); module.exports = __toCommonJS(src_exports); var import_svgson3 = require("svgson"); var import_unplugin = require("unplugin"); // src/frameworks/react.ts var import_magic_string = __toESM(require("magic-string"), 1); var import_svgson = require("svgson"); // src/frameworks/utils.ts var import_node_fs = __toESM(require("fs"), 1); var import_node_module = require("module"); var import_meta = {}; if (!("require" in globalThis)) { globalThis.require = (0, import_node_module.createRequire)(import_meta.url); } function isModuleDeclaration(node) { return node.type === "ImportDeclaration"; } function readIconSource(kebabName, weight) { const url = require.resolve(`@phosphor-icons/core/assets/${weight}/${kebabName}${weight === "regular" ? "" : `-${weight}`}.svg`); return import_node_fs.default.readFileSync(url); } async function findImports(ast, packageName) { const { walk } = await import("estree-walker"); const declarations = []; walk(ast, { enter(node) { if (isModuleDeclaration(node) && node.source.value.startsWith(packageName)) { declarations.push(node); } } }); return declarations; } function specifierNames(specifiers) { return specifiers.filter( (node) => node.specifiers.every((s) => s.type === "ImportSpecifier") ).map((node) => node.specifiers).flat().map((spec) => ({ local: spec.local.name, imported: spec.imported.name, kebab: spec.imported.name.replace(/([a-z0–9])([A-Z])/g, "$1-$2").toLowerCase() })); } // src/frameworks/react.ts var TARGET_MODULE_ID = "@phosphor-icons/react"; var transformInclude = (id) => { return /\.(tsx?|jsx)$/.test(id); }; function transformer(spriteSheet, assetPath, packageName = TARGET_MODULE_ID) { return async function(code, _id) { var _a, _b, _c, _d, _e, _f, _g; const ast = this.parse(code); const phosphorImports = await findImports( ast, packageName ); if (phosphorImports.length === 0) return; const source = new import_magic_string.default(code); for (const name of specifierNames(phosphorImports)) { const uses = await findIcons(ast, name.local); for (const expr of uses) { const { weight, color, size, mirrored, props } = extractIconProps(expr); const iconId = expr.arguments[0]; const propsExpr = expr.arguments[1]; source.update(iconId.start, iconId.end, `"svg"`); const colorValue = (_b = (_a = color == null ? void 0 : color.value) == null ? void 0 : _a.value) != null ? _b : "currentColor"; if (color) source.update( color.start, color.end, `color: "${colorValue}"` ); const sizeValue = (_d = (_c = size == null ? void 0 : size.value) == null ? void 0 : _c.value) != null ? _d : "1em"; if (size) { source.update( size.start, size.end, `width: "${sizeValue}", height: "${sizeValue}"` ); } else { source.appendRight( propsExpr.start + 1, `width: "1em", height: "1em", ` ); } const weightValue = (_g = (_f = (_e = weight == null ? void 0 : weight.value) == null ? void 0 : _e.value) == null ? void 0 : _f.toString()) != null ? _g : "regular"; if (weight) { source.remove(weight.start, weight.end + 1); } const svg = readIconSource(name.kebab, weightValue).toString(); const svgObj = await (0, import_svgson.parse)(svg); svgObj.name = "symbol"; svgObj.attributes.id = `${name.kebab}-${weightValue}`; delete svgObj.attributes.xmlns; spriteSheet.children.push(svgObj); source.update( propsExpr.start, propsExpr.start + 1, `{ children: /* @__PURE__ */ jsx("use", { href: "${assetPath}#${name.kebab}-${weightValue}" }),` ); } } for (const ii of phosphorImports) { source.remove(ii.start, ii.end); } return { code: source.toString(), map: source.generateMap({}) }; }; } function extractIconProps(ce) { var _a, _b; const props = (_b = (_a = ce.arguments.find( (node) => node.type === "ObjectExpression" )) == null ? void 0 : _a.properties) != null ? _b : []; const weight = props.find( (node) => node.type === "Property" && node.key.type === "Identifier" && node.key.name === "weight" ); const color = props.find( (node) => node.type === "Property" && node.key.type === "Identifier" && node.key.name === "color" ); const size = props.find( (node) => node.type === "Property" && node.key.type === "Identifier" && node.key.name === "size" ); const mirrored = props.find( (node) => node.type === "Property" && node.key.type === "Identifier" && node.key.name === "mirrored" ); return { weight, color, size, mirrored, props }; } async function findIcons(ast, name) { const { walk } = await import("estree-walker"); const icons = []; walk(ast, { enter(node, _parent, _key, _index) { if (node.type === "CallExpression") { if (node.callee.type === "Identifier" && (node.callee.name === "jsx" || node.callee.name === "jsxDEV")) { if (node.arguments.some( (node2) => node2.type === "Identifier" && node2.name === name )) { icons.push(node); } } } } }); return icons; } // src/frameworks/svelte.ts var import_magic_string2 = __toESM(require("magic-string"), 1); var import_svgson2 = require("svgson"); var TARGET_MODULE_ID2 = "phosphor-svelte"; var transformInclude2 = (id) => { return /\.svelte$/.test(id); }; function transformer2(spriteSheet, assetPath, packageName = TARGET_MODULE_ID2) { return async function(code, _id) { var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w; const loaded = { regular: /* @__PURE__ */ new Set(), thin: /* @__PURE__ */ new Set(), light: /* @__PURE__ */ new Set(), bold: /* @__PURE__ */ new Set(), fill: /* @__PURE__ */ new Set(), duotone: /* @__PURE__ */ new Set() }; const ast = this.parse(code); const phosphorImports = await findImports( ast, packageName ); if (phosphorImports.length === 0) return; const svelteInternals = new Set( specifierNames( await findImports(ast, "svelte/internal") ).map((name) => name.imported) ); const source = new import_magic_string2.default(code); for (const name of specifierNames(phosphorImports)) { const locs = await findSourceLocations( ast, name.local ); if (!locs.length) continue; for (const icon of locs) { if (icon.assignment) { let useVar = `${icon.varName}use`; source.appendLeft(icon.assignment.start, `let ${useVar}; `); source.remove( icon.assignment.start, icon.assignment.end ); for (const c of icon.create) { const { weight, color, size, mirrored, rest } = extractIconProps2( icon.assignment.expression.right.arguments[0].properties[0].value.properties ); const colorValue = (_b = (_a = color == null ? void 0 : color.value) == null ? void 0 : _a.value) != null ? _b : "currentColor"; const sizeValue = (_d = (_c = size == null ? void 0 : size.value) == null ? void 0 : _c.value) != null ? _d : "1em"; const mirrorValue = (_f = (_e = mirrored == null ? void 0 : mirrored.value) == null ? void 0 : _e.value) != null ? _f : false; const weightValue = (_i = (_h = (_g = weight == null ? void 0 : weight.value) == null ? void 0 : _g.value) == null ? void 0 : _h.toString()) != null ? _i : "regular"; const useUrl = `${assetPath}#${name.kebab}-${weightValue}`; if (!svelteInternals.has("element")) { source.prepend(`import { element } from "svelte/internal"; `); svelteInternals.add("element"); } if (!svelteInternals.has("attr")) { source.prepend(`import { attr } from "svelte/internal"; `); svelteInternals.add("attr"); } if (!svelteInternals.has("svg_element")) { source.prepend(`import { svg_element } from "svelte/internal"; `); svelteInternals.add("svg_element"); } source.update( c.start, c.end, `${icon.varName} = svg_element("svg"); ${useVar} = svg_element("use"); attr(${icon.varName}, "color", "${colorValue}"); attr(${icon.varName}, "width", "${sizeValue}"); attr(${icon.varName}, "height", "${sizeValue}"); ${mirrorValue ? `attr(${icon.varName}, "style", "transform: scale(-1, 1)");` : ""} ${rest.map( (prop) => `attr(${icon.varName}, "${prop.key.name}", "${prop.value.value}");` ).join("\n")} attr(${useVar}, "href", "${useUrl}"); ` ); if (!((_j = loaded[weightValue]) == null ? void 0 : _j.has(name.kebab))) { const svg = readIconSource(name.kebab, weightValue).toString(); const svgObj = await (0, import_svgson2.parse)(svg); svgObj.name = "symbol"; svgObj.attributes.id = `${name.kebab}-${weightValue}`; delete svgObj.attributes.xmlns; spriteSheet.children.push(svgObj); } } for (const claim of icon.claim) { if (!svelteInternals.has("claim_element")) { source.prepend( `import { claim_element } from "svelte/internal"; ` ); svelteInternals.add("claim_element"); } const parentLocationName = (_k = claim.expression.arguments[1]) == null ? void 0 : _k.name; if (parentLocationName) { source.update( claim.start, claim.end, `${icon.varName} = claim_element(${parentLocationName}, "SVG", { xmlns: true });` ); } } for (const h of icon.hydrate) { } for (const mount of icon.mount) { if (!svelteInternals.has("append")) { source.prepend(`import { append } from "svelte/internal"; `); svelteInternals.add("append"); } if (!svelteInternals.has("append_hydration")) { source.prepend(`import { append_hydration } from "svelte/internal"; `); svelteInternals.add("append"); } const parentLocationName = (_l = mount.expression.arguments[1]) == null ? void 0 : _l.name; if (parentLocationName) { source.update( mount.start, mount.end, `append(${parentLocationName}, ${icon.varName}); append(${icon.varName}, ${useVar}); ` ); } for (const u of icon.update) { } for (const i of icon.in) { source.remove(i.start, i.end); } for (const o of icon.out) { source.remove(o.start, o.end); } for (const d of icon.destroy) { source.remove(d.start, d.end); } } } else { for (const call of icon.ssr) { const { weight, size, color, mirrored, rest } = extractIconProps2( ((_m = call.arguments[0]) == null ? void 0 : _m.type) === "Identifier" && call.arguments[0].name === "$$result" ? call.arguments[1] : null ); const colorValue = (_o = (_n = color == null ? void 0 : color.value) == null ? void 0 : _n.value) != null ? _o : "currentColor"; const sizeValue = (_q = (_p = size == null ? void 0 : size.value) == null ? void 0 : _p.value) != null ? _q : "1em"; const mirrorValue = (_s = (_r = mirrored == null ? void 0 : mirrored.value) == null ? void 0 : _r.value) != null ? _s : false; const weightValue = (_v = (_u = (_t = weight == null ? void 0 : weight.value) == null ? void 0 : _t.value) == null ? void 0 : _u.toString()) != null ? _v : "regular"; const useUrl = `${assetPath}#${name.kebab}-${weightValue}`; const markup = ` `${prop.key.name}="${prop.value.value}"`).join(" ")}> `; source.update(call.start - 2, call.end + 1, markup); if (!((_w = loaded[weightValue]) == null ? void 0 : _w.has(name.kebab))) { const svg = readIconSource(name.kebab, weightValue).toString(); const svgObj = await (0, import_svgson2.parse)(svg); svgObj.name = "symbol"; svgObj.attributes.id = `${name.kebab}-${weightValue}`; delete svgObj.attributes.xmlns; spriteSheet.children.push(svgObj); } } } } } for (const ii of phosphorImports) { source.remove(ii.start, ii.end); } console.log(code, source.toString(), JSON.stringify(ast)); return { code: source.toString(), map: source.generateMap({}) }; }; } async function findSourceLocations(ast, name) { const { walk } = await import("estree-walker"); const icons = []; function findCreateNodes(ast2, varName) { const createNodes = []; walk(ast2, { enter(node, _parent, _key, _index) { if (node.type !== "ExpressionStatement") return; const expression = node.expression; if (expression.type !== "CallExpression") return; if (expression.callee.type !== "Identifier" || expression.callee.name !== "create_component") return; const arg = expression.arguments[0]; if (arg.type !== "MemberExpression") return; if (!(arg.object.type === "MemberExpression" && arg.object.object.type === "Identifier" && arg.object.object.name === varName && arg.object.property.type === "Identifier" && arg.object.property.name === "$$" && arg.property.type === "Identifier" && arg.property.name === "fragment")) return; createNodes.push(node); } }); return createNodes; } function findClaimNode(ast2, varName) { const claimNodes = []; walk(ast2, { enter(node, _parent, _key, _index) { if (node.type !== "ExpressionStatement") return; const expression = node.expression; if (expression.type !== "CallExpression") return; if (expression.callee.type !== "Identifier" || expression.callee.name !== "claim_component") return; const arg = expression.arguments[0]; if (arg.type !== "MemberExpression") return; if (!(arg.object.type === "MemberExpression" && arg.object.object.type === "Identifier" && arg.object.object.name === varName && arg.object.property.type === "Identifier" && arg.object.property.name === "$$" && arg.property.type === "Identifier" && arg.property.name === "fragment")) return; claimNodes.push(node); } }); return claimNodes; } function findMountNode(ast2, varName) { const mountNodes = []; walk(ast2, { enter(node, _parent, _key, _index) { if (node.type !== "ExpressionStatement") return; const expression = node.expression; if (expression.type !== "CallExpression") return; if (expression.callee.type !== "Identifier" || expression.callee.name !== "mount_component") return; if (expression.arguments[0].type !== "Identifier" || expression.arguments[0].name !== varName) return; mountNodes.push(node); } }); return mountNodes; } function findtransitionInNode(ast2, varName) { const transitionInNodes = []; walk(ast2, { enter(node, _parent, _key, _index) { if (node.type !== "ExpressionStatement") return; const expression = node.expression; if (expression.type !== "CallExpression") return; if (expression.callee.type !== "Identifier" || expression.callee.name !== "transition_in") return; const arg = expression.arguments[0]; if (arg.type !== "MemberExpression") return; if (!(arg.object.type === "MemberExpression" && arg.object.object.type === "Identifier" && arg.object.object.name === varName && arg.object.property.type === "Identifier" && arg.object.property.name === "$$" && arg.property.type === "Identifier" && arg.property.name === "fragment")) return; transitionInNodes.push(node); } }); return transitionInNodes; } function findtransitionOutNode(ast2, varName) { const transitionOutNodes = []; walk(ast2, { enter(node, _parent, _key, _index) { if (node.type !== "ExpressionStatement") return; const expression = node.expression; if (expression.type !== "CallExpression") return; if (expression.callee.type !== "Identifier" || expression.callee.name !== "transition_out") return; const arg = expression.arguments[0]; if (arg.type !== "MemberExpression") return; if (!(arg.object.type === "MemberExpression" && arg.object.object.type === "Identifier" && arg.object.object.name === varName && arg.object.property.type === "Identifier" && arg.object.property.name === "$$" && arg.property.type === "Identifier" && arg.property.name === "fragment")) return; transitionOutNodes.push(node); } }); return transitionOutNodes; } function findDestroyNode(ast2, varName) { const destroyNodes = []; walk(ast2, { enter(node, _parent, _key, _index) { if (node.type !== "ExpressionStatement") return; const expression = node.expression; if (expression.type !== "CallExpression") return; if (expression.callee.type !== "Identifier" || expression.callee.name !== "destroy_component") return; const arg = expression.arguments[0]; if (arg.type !== "Identifier" || arg.name !== varName) return; destroyNodes.push(node); } }); return destroyNodes; } async function findSSRLocations(ast2) { const ssrNodes = []; walk(ast2, { enter(node, _parent, _key, _index) { if (node.type !== "TemplateLiteral") return; const exprs = node.expressions.filter( (expr) => expr.type === "CallExpression" && expr.callee.type === "MemberExpression" && expr.callee.object.type === "CallExpression" && expr.callee.object.callee.type === "Identifier" && expr.callee.object.callee.name === "validate_component" && expr.callee.object.arguments.some( (arg) => arg.type === "Identifier" && arg.name === name ) ); if (!exprs.length) return; ssrNodes.push(...exprs); } }); icons.push({ varName: name, assignment: null, create: [], claim: [], mount: [], hydrate: [], update: [], in: [], out: [], destroy: [], ssr: ssrNodes }); } walk(ast, { enter(node, _parent, _key, _index) { if (node.type === "CallExpression" && node.callee.type === "Identifier" && node.callee.name === "create_ssr_component") { findSSRLocations(node); return; } if (node.type !== "ExpressionStatement") return; if (node.expression.type !== "AssignmentExpression") return; if (node.expression.right.type !== "NewExpression") return; if (node.expression.right.callee.type === "Identifier" && node.expression.right.callee.name === name) { const varName = node.expression.left.name; icons.push({ varName, assignment: node, create: findCreateNodes(ast, varName), claim: findClaimNode(ast, varName), hydrate: [], mount: findMountNode(ast, varName), update: [], in: findtransitionInNode(ast, varName), out: findtransitionOutNode(ast, varName), destroy: findDestroyNode(ast, varName), ssr: [] }); } } }); return icons; } function extractIconProps2(oe) { var _a; const props = (_a = oe == null ? void 0 : oe.properties) != null ? _a : []; const weight = props.find( (node) => node.type === "Property" && node.key.type === "Identifier" && node.key.name === "weight" ); const color = props.find( (node) => node.type === "Property" && node.key.type === "Identifier" && node.key.name === "color" ); const size = props.find( (node) => node.type === "Property" && node.key.type === "Identifier" && node.key.name === "size" ); const mirrored = props.find( (node) => node.type === "Property" && node.key.type === "Identifier" && node.key.name === "mirrored" ); const rest = props.filter( (prop) => ![weight, color, size, mirrored].includes(prop) ); return { weight, color, size, mirrored, rest }; } // src/index.ts var transformer3 = { react: transformer, svelte: transformer2, vue: () => () => void 0 }; var transformInclude3 = { react: transformInclude, svelte: transformInclude2, vue: (id) => /\.vue$/.test(id) }; var unpluginFactory = ({ framework = "react", assetPath = "phosphor.svg", packageName } = {}) => { const spriteSheet = { name: "svg", type: "element", value: "", attributes: { xmlns: "http://www.w3.org/2000/svg" }, children: [] }; if (assetPath.startsWith("/")) assetPath = assetPath.slice(1); return { name: "@phosphor-icons/unplugin", apply: "build", transformInclude: transformInclude3[framework], transform: transformer3[framework].bind(void 0)( spriteSheet, assetPath, packageName ), buildEnd() { this.emitFile({ fileName: assetPath, needsCodeReference: false, source: (0, import_svgson3.stringify)(spriteSheet), type: "asset" }); } }; }; var unplugin = /* @__PURE__ */ (0, import_unplugin.createUnplugin)(unpluginFactory); var src_default = unplugin; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { unplugin, unpluginFactory }); exports.default = module.exports;