mirror of
https://github.com/lucide-icons/lucide.git
synced 2026-11-16 14:18:29 +00:00
React package (#105)
* init new react package project * setup react package * Fix config for build * update package * Fix build * update package * fix package.json * refactor and cleanup * bumb beta version * fix regex * bump version * Add hashing * Add tests * bump version * Add peer dependecies * Add docs * Add readme link
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
import path from 'path';
|
||||
|
||||
import { generateComponentName, resetFile, appendFile } from '../helpers';
|
||||
import { toPascalCase, resetFile, appendFile } from '../helpers';
|
||||
|
||||
export default function(inputEntry, outputDirectory, componentGetter, iconNodes) {
|
||||
export default function(inputEntry, outputDirectory, iconNodes) {
|
||||
const fileName = path.basename(inputEntry);
|
||||
|
||||
// Reset file
|
||||
@@ -12,7 +12,7 @@ export default function(inputEntry, outputDirectory, componentGetter, iconNodes)
|
||||
|
||||
// Generate Import for Icon VNodes
|
||||
icons.forEach(iconName => {
|
||||
const componentName = generateComponentName(iconName);
|
||||
const componentName = toPascalCase(iconName);
|
||||
const importString = `export { default as ${componentName} } from './${iconName}';\n`;
|
||||
appendFile(importString, fileName, outputDirectory);
|
||||
});
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import prettier from 'prettier';
|
||||
import { generateComponentName } from '../helpers';
|
||||
import { toPascalCase } from '../helpers';
|
||||
|
||||
export default function(iconNode, outputDirectory, template) {
|
||||
const icons = Object.keys(iconNode);
|
||||
@@ -14,7 +14,7 @@ export default function(iconNode, outputDirectory, template) {
|
||||
|
||||
icons.forEach(icon => {
|
||||
const location = path.join(iconsDistDirectory, `${icon}.js`);
|
||||
const componentName = generateComponentName(icon);
|
||||
const componentName = toPascalCase(icon);
|
||||
|
||||
const node = JSON.stringify(iconNode[icon]);
|
||||
|
||||
|
||||
+18
-13
@@ -1,15 +1,19 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import getArgumentOptions from 'minimist';
|
||||
|
||||
import renderIconsObject from './render/renderIconsObject';
|
||||
import renderIconNodes from './render/renderIconNodes';
|
||||
import generateIconFiles from './build/generateIconFiles';
|
||||
import generateExportsFile from './build/generateExportsFile';
|
||||
|
||||
import { readSvgDirectory } from './helpers';
|
||||
|
||||
/* eslint-disable import/no-dynamic-require */
|
||||
|
||||
const cliArguments = getArgumentOptions(process.argv.slice(2));
|
||||
|
||||
const ICONS_DIR = path.resolve(__dirname, '../icons');
|
||||
const OUTPUT_DIR = path.resolve(__dirname, '../build');
|
||||
const OUTPUT_DIR = path.resolve(__dirname, cliArguments.output || '../build');
|
||||
const SRC_DIR = path.resolve(__dirname, '../src');
|
||||
|
||||
if (!fs.existsSync(OUTPUT_DIR)) {
|
||||
@@ -20,23 +24,24 @@ const svgFiles = readSvgDirectory(ICONS_DIR);
|
||||
|
||||
const icons = renderIconsObject(svgFiles, ICONS_DIR);
|
||||
|
||||
const iconVNodes = renderIconNodes(icons);
|
||||
const iconVNodes = renderIconNodes(icons, cliArguments);
|
||||
|
||||
const defaultIconFileTemplate = ({ componentName, node }) => `
|
||||
const ${componentName} = ${node};
|
||||
|
||||
export default ${componentName};
|
||||
`;
|
||||
|
||||
const iconFileTemplate = cliArguments.templateSrc
|
||||
? require(cliArguments.templateSrc).default
|
||||
: defaultIconFileTemplate;
|
||||
|
||||
// Generates iconsNodes files for each icon
|
||||
generateIconFiles(
|
||||
iconVNodes,
|
||||
OUTPUT_DIR,
|
||||
({ componentName, node }) => `
|
||||
const ${componentName} = ${node};
|
||||
|
||||
export default ${componentName};
|
||||
`,
|
||||
);
|
||||
generateIconFiles(iconVNodes, OUTPUT_DIR, iconFileTemplate);
|
||||
|
||||
// Generates entry files for the compiler filled with icons exports
|
||||
generateExportsFile(
|
||||
path.join(SRC_DIR, 'icons/index.js'),
|
||||
path.join(OUTPUT_DIR, 'icons'),
|
||||
'getElement',
|
||||
iconVNodes,
|
||||
);
|
||||
|
||||
+30
-6
@@ -1,15 +1,26 @@
|
||||
// eslint-disable-next-line import/no-extraneous-dependencies
|
||||
import { upperFirst, camelCase } from 'lodash/string';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
/**
|
||||
* Generates a componentName of a String.
|
||||
* Converts string to PascalCase
|
||||
*
|
||||
* @param {string} iconName
|
||||
* @param {string} string
|
||||
*/
|
||||
export const generateComponentName = iconName =>
|
||||
iconName === 'github' ? 'GitHub' : upperFirst(camelCase(iconName));
|
||||
export const toCamelCase = string =>
|
||||
string.replace(/^([A-Z])|[\s-_]+(\w)/g, (match, p1, p2) =>
|
||||
p2 ? p2.toUpperCase() : p1.toLowerCase(),
|
||||
);
|
||||
|
||||
/**
|
||||
* Converts string to PascalCase
|
||||
*
|
||||
* @param {string} string
|
||||
*/
|
||||
export const toPascalCase = string => {
|
||||
const camelCase = toCamelCase(string);
|
||||
|
||||
return camelCase.charAt(0).toUpperCase() + camelCase.slice(1);
|
||||
};
|
||||
|
||||
/**
|
||||
* Resets the file contents.
|
||||
@@ -72,3 +83,16 @@ export const readSvg = (fileName, directory) => fs.readFileSync(path.join(direct
|
||||
*/
|
||||
export const writeSvgFile = (fileName, outputDirectory, content) =>
|
||||
fs.appendFileSync(path.join(outputDirectory, fileName), content, 'utf-8');
|
||||
|
||||
// This is a djb2 hashing function
|
||||
export const hash = (string, seed = 5381) => {
|
||||
let i = string.length;
|
||||
|
||||
while (i) {
|
||||
// eslint-disable-next-line no-bitwise, no-plusplus
|
||||
seed = (seed * 33) ^ string.charCodeAt(--i);
|
||||
}
|
||||
|
||||
// eslint-disable-next-line no-bitwise
|
||||
return (seed >>> 0).toString(36).substr(0, 6);
|
||||
};
|
||||
|
||||
@@ -1,25 +1,47 @@
|
||||
/* eslint-disable import/no-extraneous-dependencies */
|
||||
import { parseDOM } from 'htmlparser2';
|
||||
import DEFAULT_ATTRS from './default-attrs.json';
|
||||
import { toCamelCase, hash } from '../helpers';
|
||||
|
||||
export default iconsObject => {
|
||||
const camelizeAttrs = attrs =>
|
||||
Object.keys(attrs).reduce((newAttrs, attr) => {
|
||||
const attrKey = toCamelCase(attr);
|
||||
|
||||
newAttrs[attrKey] = attrs[attr];
|
||||
return newAttrs;
|
||||
}, {});
|
||||
|
||||
export default (iconsObject, options) => {
|
||||
const iconNodes = {};
|
||||
|
||||
Object.keys(iconsObject).forEach(icon => {
|
||||
const svgString = iconsObject[icon];
|
||||
const dom = parseDOM(svgString);
|
||||
|
||||
const children = dom.map(element => [
|
||||
element.name,
|
||||
{
|
||||
...element.attribs,
|
||||
},
|
||||
]);
|
||||
const children = dom.map(element => {
|
||||
if (options.renderUniqueKey) {
|
||||
const hashSource = {
|
||||
name: element.name,
|
||||
...element.attribs,
|
||||
};
|
||||
|
||||
const uniqueKey = hash(JSON.stringify(hashSource));
|
||||
|
||||
element.attribs.key = uniqueKey;
|
||||
}
|
||||
|
||||
return [
|
||||
element.name,
|
||||
{
|
||||
...(options.camelizeAttrs ? camelizeAttrs(element.attribs) : element.attribs),
|
||||
},
|
||||
];
|
||||
});
|
||||
|
||||
iconNodes[icon] = [
|
||||
'svg',
|
||||
{
|
||||
...DEFAULT_ATTRS,
|
||||
...(options.camelizeAttrs ? camelizeAttrs(DEFAULT_ATTRS) : DEFAULT_ATTRS),
|
||||
},
|
||||
children,
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user