UNPKG

2.1 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.loadFontAwesomeIcons = exports.generateSVG = void 0;
4const path_1 = require("path");
5function camelCase(source) {
6 // tslint:disable-next-line strict-type-predicates
7 if (typeof source !== 'string' || !source.length) {
8 return source;
9 }
10 return source
11 .toLowerCase()
12 .replace(/[^a-z0-9\s]/g, ' ')
13 .replace(/(^.|\s.)/g, (...t) => t[1].toUpperCase());
14}
15function generateSVG(icon, tag) {
16 const { width, height, svgPathData } = icon;
17 return `
18 <svg id="${tag}" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${width} ${height}">
19 <path fill="currentColor" d="${svgPathData}"></path>
20 </svg>
21 `;
22}
23exports.generateSVG = generateSVG;
24async function loadFontAwesomeIcons(icons, toLoad) {
25 // eslint-disable-next-line @typescript-eslint/no-var-requires
26 const dependencies = require(path_1.resolve(process.cwd(), './package.json')).dependencies;
27 icons.tags = toLoad.reduce((accu, entry, index) => {
28 // Manipulate the icon name - Syntax: [alias@]<icon>[:section]
29 const [alias, rawName] = entry.includes('@') ? entry.split('@') : [entry.replace(/:.+/, ''), entry];
30 const [name, section] = rawName.includes(':') ? rawName.split(':') : [rawName, 'solid'];
31 const tag = `i${index}`;
32 const iconPackage = `@fortawesome/free-${section}-svg-icons`;
33 // Check font-awesome exists in dependencies
34 if (!(iconPackage in dependencies)) {
35 throw new Error(`In order to load the "${entry}" icon, please add ${iconPackage} to the package.json dependencies.`);
36 }
37 // Load the icon then add to the definitions
38 // eslint-disable-next-line @typescript-eslint/no-var-requires
39 const icon = require(path_1.resolve(process.cwd(), `node_modules/${iconPackage}/fa${camelCase(`${name}`).replace(/\s/g, '')}`));
40 icons.definitions += generateSVG(icon, tag);
41 accu[alias] = tag;
42 return accu;
43 }, {});
44}
45exports.loadFontAwesomeIcons = loadFontAwesomeIcons;