版博士V2.0程序
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

414 wiersze
12 KiB

  1. "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  2. var _chunkJUWXSDKJjs = require('./chunk-JUWXSDKJ.js');
  3. // src/index.ts
  4. var _unplugin = require('unplugin');
  5. // src/core/options.ts
  6. var _localpkg = require('local-pkg');
  7. var _debug = require('debug'); var _debug2 = _interopRequireDefault(_debug);
  8. var debug = _debug2.default.call(void 0, "unplugin-icons:options");
  9. async function resolveOptions(options) {
  10. const {
  11. scale = 1.2,
  12. defaultStyle = "",
  13. defaultClass = "",
  14. compiler = await guessCompiler(),
  15. jsx = guessJSX(),
  16. customCollections = {},
  17. iconCustomizer = () => {
  18. },
  19. transform,
  20. autoInstall = false
  21. } = options;
  22. const webComponents = Object.assign({
  23. autoDefine: false,
  24. iconPrefix: "icon"
  25. }, options.webComponents);
  26. debug("compiler", compiler);
  27. return {
  28. scale,
  29. defaultStyle,
  30. defaultClass,
  31. customCollections,
  32. iconCustomizer,
  33. compiler,
  34. jsx,
  35. webComponents,
  36. transform,
  37. autoInstall
  38. };
  39. }
  40. async function guessCompiler() {
  41. return await getVueVersion() || (_localpkg.isPackageExists.call(void 0, "@svgr/core") ? "jsx" : "vue3");
  42. }
  43. function guessJSX() {
  44. if (_localpkg.isPackageExists.call(void 0, "preact"))
  45. return "preact";
  46. return "react";
  47. }
  48. async function getVueVersion() {
  49. try {
  50. const result = await _localpkg.getPackageInfo.call(void 0, "vue");
  51. if (!result)
  52. return null;
  53. return result.version.startsWith("2.") ? "vue2" : "vue3";
  54. } catch (e) {
  55. return null;
  56. }
  57. }
  58. // src/core/loader.ts
  59. var _nodeloader = require('@iconify/utils/lib/loader/node-loader');
  60. // src/core/compilers/astro.ts
  61. var AstroCompiler = (svg) => {
  62. const svgWithProps = svg.replace("<svg", "<svg {...props}");
  63. return `---
  64. interface Props extends astroHTML.JSX.SVGAttributes {};
  65. const props = Astro.props;
  66. ---
  67. ${svgWithProps}`;
  68. };
  69. // src/core/compilers/jsx.ts
  70. var _strings = require('@iconify/utils/lib/misc/strings');
  71. var JSXCompiler = async (svg, collection, icon, options) => {
  72. var _a;
  73. const svgrCore = await _localpkg.importModule.call(void 0, "@svgr/core");
  74. const svgr = svgrCore.transform || (svgrCore.default ? (_a = svgrCore.default.transform) != null ? _a : svgrCore.default : svgrCore.default) || svgrCore;
  75. let res = await svgr(
  76. svg,
  77. {
  78. plugins: ["@svgr/plugin-jsx"]
  79. },
  80. { componentName: _strings.camelize.call(void 0, `${collection}-${icon}`) }
  81. );
  82. if (options.jsx !== "react")
  83. res = res.replace('import * as React from "react";', "");
  84. return res;
  85. };
  86. // src/core/compilers/marko.ts
  87. var MarkoCompiler = (svg) => {
  88. const openTagEnd = svg.indexOf(">", svg.indexOf("<svg "));
  89. const closeTagStart = svg.lastIndexOf("</svg");
  90. const openTag = `${svg.slice(0, openTagEnd)} ...input>`;
  91. const content = `$!{\`${escapeTemplateLiteral(svg.slice(openTagEnd + 1, closeTagStart))}\`}`;
  92. const closeTag = svg.slice(closeTagStart);
  93. return `${openTag}${content}${closeTag}`;
  94. };
  95. function escapeTemplateLiteral(str) {
  96. return str.replace(/\\.|[$`]/g, (m) => {
  97. switch (m) {
  98. case "$":
  99. return "&#36";
  100. case "`":
  101. return "&#96;";
  102. default:
  103. return m;
  104. }
  105. });
  106. }
  107. // src/core/compilers/none.ts
  108. var NoneCompiler = (svg) => {
  109. return svg;
  110. };
  111. // src/core/compilers/qwik.ts
  112. var QwikCompiler = async (svg, collection, icon, options) => {
  113. const defaultOptions = {
  114. importSource: "@builder.io/qwik",
  115. runtime: "automatic",
  116. componentName: _strings.camelize.call(void 0, `${collection}-${icon}`)
  117. };
  118. const mergedOptions = Object.assign({}, defaultOptions, options);
  119. const svgx = await _localpkg.importModule.call(void 0, "@svgx/core");
  120. const toJsComponent = svgx.toJsComponent || svgx.default || svgx;
  121. const res = toJsComponent(svg, _chunkJUWXSDKJjs.__spreadProps.call(void 0, _chunkJUWXSDKJjs.__spreadValues.call(void 0, {}, mergedOptions), {
  122. defaultExport: true
  123. }));
  124. return res;
  125. };
  126. // src/core/compilers/raw.ts
  127. var RawCompiler = (svg) => {
  128. return `export default ${JSON.stringify(svg)}`;
  129. };
  130. // src/core/compilers/solid.ts
  131. var SolidCompiler = (svg) => {
  132. const svgWithProps = svg.replace(/([{}])/g, "{'$1'}").replace(new RegExp("(?<=<svg.*?)(>)", "i"), "{...props}>");
  133. return `export default (props = {}) => ${svgWithProps}`;
  134. };
  135. // src/core/compilers/svelte.ts
  136. var SvelteCompiler = (svg) => {
  137. const openTagEnd = svg.indexOf(">", svg.indexOf("<svg "));
  138. const closeTagStart = svg.lastIndexOf("</svg");
  139. const openTag = `${svg.slice(0, openTagEnd)} {...$$props}>`;
  140. const content = `{@html \`${escapeSvelte(svg.slice(openTagEnd + 1, closeTagStart))}\`}`;
  141. const closeTag = svg.slice(closeTagStart);
  142. return `${openTag}${content}${closeTag}`;
  143. };
  144. function escapeSvelte(str) {
  145. return str.replace(/{/g, "&#123;").replace(/}/g, "&#125;").replace(/`/g, "&#96;").replace(/\\([trn])/g, " ");
  146. }
  147. // src/core/compilers/vue2.ts
  148. // src/core/svgId.ts
  149. var randIdFn = "const __randId = () => Math.random().toString(36).substr(2, 10);";
  150. function handleSVGId(svg) {
  151. const hasID = /="url\(#/.test(svg);
  152. const idMap = {};
  153. let injectScripts = "";
  154. if (hasID) {
  155. svg = svg.replace(/\b([\w-]+?)="url\(#(.+?)\)"/g, (_, s, id) => {
  156. idMap[id] = `'${id}':'uicons-'+__randId()`;
  157. return `:${s}="'url(#'+idMap['${id}']+')'"`;
  158. }).replace(/\bid="(.+?)"/g, (full, id) => {
  159. if (idMap[id])
  160. return `:id="idMap['${id}']"`;
  161. return full;
  162. });
  163. injectScripts = `${randIdFn}const idMap = {${Object.values(idMap).join(",")}};`;
  164. }
  165. return {
  166. hasID,
  167. svg,
  168. injectScripts
  169. };
  170. }
  171. // src/core/compilers/vue2.ts
  172. var Vue2Compiler = async (svg, collection, icon) => {
  173. const { compile } = await _localpkg.importModule.call(void 0, "vue-template-compiler");
  174. const transpile = (await _localpkg.importModule.call(void 0, "vue-template-es2015-compiler")).default;
  175. const { injectScripts, svg: handled } = handleSVGId(svg);
  176. const { render } = compile(handled);
  177. const toFunction = (code2) => {
  178. return `function () {${code2}}`;
  179. };
  180. let code = transpile(`var __render__ = ${toFunction(render)}
  181. `, {});
  182. code = code.replace(/\s__(render|staticRenderFns)__\s/g, " $1 ");
  183. code += `
  184. /* vite-plugin-components disabled */
  185. export default {
  186. render: render,
  187. ${injectScripts ? `data() {${injectScripts};return { idMap }},` : ""}
  188. name: '${collection}-${icon}',
  189. }
  190. `;
  191. return code;
  192. };
  193. // src/core/compilers/vue3.ts
  194. var Vue3Compiler = async (svg, collection, icon) => {
  195. const { compileTemplate } = await _localpkg.importModule.call(void 0, "@vue/compiler-sfc");
  196. const { injectScripts, svg: handled } = handleSVGId(svg);
  197. let { code } = compileTemplate({
  198. source: handled,
  199. id: `${collection}:${icon}`,
  200. filename: `${collection}-${icon}.vue`
  201. });
  202. code = code.replace(/^export /gm, "");
  203. code += `
  204. export default { name: '${collection}-${icon}', render${injectScripts ? `, data() {${injectScripts};return { idMap }}` : ""} }`;
  205. code += "\n/* vite-plugin-components disabled */";
  206. return code;
  207. };
  208. // src/core/compilers/web-components.ts
  209. var WebComponentsCompiler = (svg, collection, icon, { webComponents: options }) => {
  210. let id = `${collection}-${icon}`;
  211. if (options.iconPrefix)
  212. id = `${options.iconPrefix}-${id}`;
  213. const name = _strings.camelize.call(void 0, id);
  214. let code = `export default class ${name} extends HTMLElement {`;
  215. if (options.shadow) {
  216. code += `constructor() {
  217. super()
  218. this.attachShadow({ mode: 'open' }).innerHTML = ${JSON.stringify(svg)}
  219. }`;
  220. } else {
  221. code += `connectedCallback() { this.innerHTML = ${JSON.stringify(svg)} }`;
  222. }
  223. code += "}";
  224. if (options.autoDefine)
  225. code += `
  226. customElements.define('${id}', ${name})`;
  227. return code;
  228. };
  229. // src/core/compilers/index.ts
  230. var compilers = {
  231. "astro": AstroCompiler,
  232. "jsx": JSXCompiler,
  233. "marko": MarkoCompiler,
  234. "none": NoneCompiler,
  235. "raw": RawCompiler,
  236. "solid": SolidCompiler,
  237. "svelte": SvelteCompiler,
  238. "vue2": Vue2Compiler,
  239. "vue3": Vue3Compiler,
  240. "web-components": WebComponentsCompiler,
  241. "qwik": QwikCompiler
  242. };
  243. // src/core/loader.ts
  244. var URL_PREFIXES = ["/~icons/", "~icons/", "virtual:icons/", "virtual/icons/"];
  245. var iconPathRE = new RegExp(`${URL_PREFIXES.map((v) => `^${v}`).join("|")}`);
  246. function isIconPath(path) {
  247. return iconPathRE.test(path);
  248. }
  249. function normalizeIconPath(path) {
  250. return path.replace(iconPathRE, URL_PREFIXES[0]);
  251. }
  252. function resolveIconsPath(path) {
  253. if (!isIconPath(path))
  254. return null;
  255. path = path.replace(iconPathRE, "");
  256. const query = {};
  257. const queryIndex = path.indexOf("?");
  258. if (queryIndex !== -1) {
  259. const queryRaw = path.slice(queryIndex + 1);
  260. path = path.slice(0, queryIndex);
  261. new URLSearchParams(queryRaw).forEach((value, key) => {
  262. if (key === "raw")
  263. query.raw = value === "" || value === "true" ? "true" : "false";
  264. else
  265. query[key] = value;
  266. });
  267. }
  268. path = path.replace(/\.\w+$/, "");
  269. const [collection, icon] = path.split("/");
  270. return {
  271. collection,
  272. icon,
  273. query
  274. };
  275. }
  276. async function generateComponent({ collection, icon, query }, options) {
  277. const warn = `${collection}/${icon}`;
  278. const {
  279. scale,
  280. defaultStyle,
  281. defaultClass,
  282. customCollections,
  283. iconCustomizer: providedIconCustomizer,
  284. transform,
  285. autoInstall = false
  286. } = options;
  287. const iconifyLoaderOptions = {
  288. addXmlNs: false,
  289. scale,
  290. customCollections,
  291. autoInstall,
  292. defaultClass,
  293. defaultStyle,
  294. // there is no need to warn since we throw an error below
  295. warn: void 0,
  296. customizations: {
  297. transform,
  298. async iconCustomizer(collection2, icon2, props) {
  299. await (providedIconCustomizer == null ? void 0 : providedIconCustomizer(collection2, icon2, props));
  300. Object.keys(query).forEach((p) => {
  301. const v = query[p];
  302. if (p !== "raw" && v !== void 0 && v !== null)
  303. props[p] = v;
  304. });
  305. }
  306. }
  307. };
  308. const svg = await _nodeloader.loadNodeIcon.call(void 0, collection, icon, iconifyLoaderOptions);
  309. if (!svg)
  310. throw new Error(`Icon \`${warn}\` not found`);
  311. const _compiler = query.raw === "true" ? "raw" : options.compiler;
  312. if (_compiler) {
  313. const compiler = typeof _compiler === "string" ? compilers[_compiler] : await _compiler.compiler;
  314. if (compiler)
  315. return compiler(svg, collection, icon, options);
  316. }
  317. throw new Error(`Unknown compiler: ${_compiler}`);
  318. }
  319. async function generateComponentFromPath(path, options) {
  320. const resolved = resolveIconsPath(path);
  321. if (!resolved)
  322. return null;
  323. return generateComponent(resolved, options);
  324. }
  325. // src/index.ts
  326. var unplugin = _unplugin.createUnplugin.call(void 0, (options = {}) => {
  327. const resolved = resolveOptions(options);
  328. return {
  329. name: "unplugin-icons",
  330. enforce: "pre",
  331. resolveId(id) {
  332. var _a;
  333. if (isIconPath(id)) {
  334. const res = normalizeIconPath(id).replace(/\.\w+$/i, "").replace(/^\//, "");
  335. const resolved2 = resolveIconsPath(res);
  336. const compiler = ((_a = resolved2 == null ? void 0 : resolved2.query) == null ? void 0 : _a.raw) === "true" ? "raw" : options.compiler;
  337. if (compiler && typeof compiler !== "string") {
  338. const ext = compiler.extension;
  339. if (ext)
  340. return `${res}.${ext.startsWith(".") ? ext.slice(1) : ext}`;
  341. } else {
  342. switch (compiler) {
  343. case "astro":
  344. return `${res}.astro`;
  345. case "jsx":
  346. return `${res}.jsx`;
  347. case "qwik":
  348. return `${res}.tsx`;
  349. case "marko":
  350. return `${res}.marko`;
  351. case "svelte":
  352. return `${res}.svelte`;
  353. case "solid":
  354. return `${res}.tsx`;
  355. }
  356. }
  357. return res;
  358. }
  359. return null;
  360. },
  361. loadInclude(id) {
  362. return isIconPath(id);
  363. },
  364. async load(id) {
  365. const config = await resolved;
  366. const code = await generateComponentFromPath(id, config) || null;
  367. if (code) {
  368. return {
  369. code,
  370. map: { version: 3, mappings: "", sources: [] }
  371. };
  372. }
  373. },
  374. rollup: {
  375. api: {
  376. config: options
  377. }
  378. }
  379. };
  380. });
  381. var src_default = unplugin;
  382. exports.src_default = src_default;