lucide-static (#401)

* Add lucide static

* add render files

* add read scripts

* Add sprite generator

* start with documentation

* Add docs

* adding documentation

* fix build command

* Add some extra static files

* bump version

* Update packages/lucide-static/README.md

Co-authored-by: Kathryn Reeve <67553+BinaryKitten@users.noreply.github.com>

* Fix sprite generation

* update readme

* Add lucide static workflow

* adjust readme

* Add font to release yml

* Temporary turn of new versioning for lucide-static

Co-authored-by: Kathryn Reeve <67553+BinaryKitten@users.noreply.github.com>
This commit is contained in:
Eric Fennis
2021-11-21 20:27:15 +01:00
committed by GitHub
parent 6c31ab914a
commit bb4f05e6c5
16 changed files with 430 additions and 25 deletions

5
packages/lucide-static/.gitignore vendored Normal file
View File

@@ -0,0 +1,5 @@
icons
lib
sprite.svg
tags.json
icon-nodes.json

View File

@@ -0,0 +1,10 @@
stats
node_modules
tests
scripts
build
src
babel.config.js
jest.config.js
rollup.config.js
yarn.error.log

View File

@@ -0,0 +1,15 @@
ISC License
Copyright (c) 2020, Lucide Contributors
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

View File

@@ -0,0 +1,196 @@
# Lucide Static
This package include the following lucide implementations:
- All svg files
- Javascript library containing strings of svgs.
- Icon fonts
- Svg sprite
> What is lucide? Read it [here](https://github.com/lucide-icons/lucide#what-is-lucide).
## Why lucide-static?
This package is suitable for very specific use cases for example if you want to use icon fonts, svg sprites, normal svgs or Common.js Svg strings in your javascript project.
> ⚠️ It is not recommended to use this package for svg sprites or icon fonts for web pages/applications, for prototyping it is ok. We recommend to bundlers for web applications to make sure you only bundle the used icons from this icon library (Threeshaking). Otherwise it will load all the icons, making you webpage loading slower. Threeshaking is only available in the packages: [lucide](https://github.com/lucide-icons/lucide/tree/master/packages/lucide), [lucide-react](https://github.com/lucide-icons/lucide/tree/master/packages/lucide-react), [lucide-vue](https://github.com/lucide-icons/lucide/tree/master/packages/lucide-vue), [lucide-vue-next](https://github.com/lucide-icons/lucide/tree/master/packages/lucide-vue-next), [lucide-angular](https://github.com/lucide-icons/lucide/tree/master/packages/lucide-angular), [lucide-preact](https://github.com/lucide-icons/lucide/tree/master/packages/lucide-preact)
## Installation
## Package Managers
```sh
yarn add lucide-static
# or
npm install lucide-static
```
### CDN
``` html
<!-- Svg File -->
<img src="https://unpkg.com/lucide-static@latest/icons/home.svg">
<!-- Icon Font -->
<style>
@font-face {
font-family: "LucideIcons";
src: url(https://unpkg.com/lucide-static@latest/font/Lucide.ttf) format("truetype");
}
</style>
```
## Usage
Checkout the [codesandbox examples](https://codesandbox.io/s/using-the-svg-sprite-lz1kk).
### SVG Files
#### Svg file as image
To use it in for example html:
``` html
<!-- Svg File -->
<img src="~lucide-static/icons/home.svg">
```
``` css
.home-icon {
background-image: url(~lucide-static/icons/home.svg)
}
```
Make sure you have the correct webpack loaders to make this work. [url-loader](https://v4.webpack.js.org/loaders/url-loader/)
#### Svg file Inline
You can simply import each svg by targeting `lucide-static/icons/{icon-name}.svg`.
To use svgs in your project you can for example use a [svg loader](https://v4.webpack.js.org/loaders/svg-inline-loader/).
```js
import arrowRightIcon from 'lucide-static/icons/arrow-right'
// return string of a svg
```
### SVG Sprite
You may need additional loader for this.
```html
<!-- Icon Sprite, not recommended for production! -->
<img src="lucide-static/sprite.svg#home">
<!-- or -->
<svg
width="24"
height="24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
>
<use href="#alert-triangle" />
</svg>
<svg>
...sprite svg
</svg>
```
If you'd prefer, you can use CSS to hold your base SVG properties
```css
.lucide-icon {
width: 24px;
height: 24px;
stroke: currentColor;
fill: none;
stroke-width: 2;
stroke-linecap: round;
stroke-linejoin: round;
}
```
and update the svg as follows
```svg
<svg
xmlns="http://www.w3.org/2000/svg"
class="lucide-icon"
>
<use
href="#alert-triangle"
/>
</svg>
<svg>
...sprite svg
</svg>
```
### Icon Font
```css
@import("~lucide-static/font/Lucide.css")
```
```html
<div class="icon-home"></div>
```
### Node.js
To use lucide icons in your Nodejs project you can import each icon as:
```js
const { messageSquare } = require('lucide-static/lib')
```
> Note: Each icon name is in camelCase.
#### Example in node.js project
```js
const express = require('express')
const { messageSquare } = require('lucide-static/lib')
const app = express()
const port = 3000
app.get('/', (req, res) => {
res.send(`
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<h1>Lucide Icons</h1>
<p>This is a lucide icon ${messageSquare}</p>
</body>
</html>
`)
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
```
## Contributing
For more info on how to contribute please see the [contribution guidelines](https://github.com/lucide-icons/lucide/blob/master/CONTRIBUTING.md).
Caught a mistake or want to contribute to the documentation? [Edit this page on Github](https://github.com/lucide-icons/lucide/blob/master/README.md)
## Community
Join the community on our [Discord](https://discord.gg/EH6nSts) server!
## License
Lucide is totally free for commercial use and personally use, this software is licensed under the [ISC License](https://github.com/lucide-icons/lucide/blob/master/LICENSE).

View File

@@ -0,0 +1,24 @@
{
"name": "lucide-static",
"description": "Lucide is a community-run fork of Feather Icons, open for anyone to contribute icons.",
"version": "0.16.18-rc.0",
"license": "ISC",
"homepage": "https://lucide.dev",
"bugs": "https://github.com/lucide-icons/lucide/issues",
"repository": {
"type": "git",
"url": "https://github.com/lucide-icons/lucide.git"
},
"main": "lib/index.js",
"scripts": {
"move:icons": "cp -r ../../icons icons",
"move:tags": "cp ../../tags.json tags.json",
"build": "yarn clean && yarn move:icons && yarn move:tags && yarn build:lib",
"build:lib": "yarn --cwd ../../ babel-node packages/lucide-static/scripts/buildLib.js",
"clean": "rm -rf lib && rm -rf build && rm -rf icons && rm -f sprite.svg && rm -f tags.json"
},
"devDependencies": {
"prettier": "^2.3.2",
"svgson": "^5.2.1"
}
}

View File

@@ -0,0 +1,47 @@
/* eslint-disable import/no-extraneous-dependencies */
import fs from 'fs';
import path from 'path';
import getArgumentOptions from 'minimist';
import { parseSync } from 'svgson';
// import renderIconsObject from '../../../scripts/render/renderIconsObject';
import { appendFile, readSvgDirectory, toCamelCase } from '../../../scripts/helpers';
import readSvgs from './readSvgs';
import generateSprite from './generateSprite';
import generateIconNodes from './generateIconNodes';
const cliArguments = getArgumentOptions(process.argv.slice(2));
const createDirectory = dir => {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
};
const PACKAGE_DIR = path.resolve(__dirname, '../');
const ICONS_DIR = path.join(PACKAGE_DIR, 'icons');
const LIB_DIR = path.join(PACKAGE_DIR, cliArguments.output || 'lib');
const ICON_MODULE_DIR = path.join(LIB_DIR, 'icons');
createDirectory(LIB_DIR);
createDirectory(ICON_MODULE_DIR);
const svgFiles = readSvgDirectory(ICONS_DIR);
const svgs = readSvgs(svgFiles, ICONS_DIR);
svgs.forEach(({ name, contents }) => {
const componentName = toCamelCase(name);
const importString = `module.exports.${componentName} = require('./icons/${name}');\n`;
appendFile(importString, `index.js`, LIB_DIR);
const exportString = `module.exports = \`${contents}\`;\n`;
appendFile(exportString, `${name}.js`, ICON_MODULE_DIR);
});
const parsedSvgs = svgs.map(({ name, contents }) => ({
name,
contents,
parsedSvg: parseSync(contents),
}));
generateSprite(parsedSvgs, PACKAGE_DIR);
generateIconNodes(parsedSvgs, PACKAGE_DIR);

View File

@@ -0,0 +1,13 @@
import { writeFile } from '../../../scripts/helpers';
export default function generateIconNodes(parsedSvgs, packageDir) {
const iconNodes = parsedSvgs.reduce((acc, { name, parsedSvg }) => {
acc[name] = parsedSvg.children.map(({ name, attributes }) => [name, attributes]);
return acc;
}, {});
const iconNodesStringified = JSON.stringify(iconNodes, null, 2);
writeFile(iconNodesStringified, 'icon-nodes.json', packageDir);
}

View File

@@ -0,0 +1,39 @@
/* eslint-disable import/no-extraneous-dependencies */
import { stringify } from 'svgson';
import { format } from 'prettier';
import { appendFile } from '../../../scripts/helpers';
export default function generateSprite(svgs, packageDir) {
const symbols = svgs.map(({ name, parsedSvg }) => ({
name: 'symbol',
type: 'element',
attributes: {
id: name,
},
children: parsedSvg.children,
}));
const spriteSvgObject = {
name: 'svg',
type: 'element',
attributes: {
xmlns: 'http://www.w3.org/2000/svg',
version: '1.1',
},
children: [
{
name: 'defs',
type: 'element',
children: symbols,
},
],
};
const spriteSvg = stringify(spriteSvgObject);
const prettifiedSprite = format(spriteSvg, { parser: 'babel' }).replace(/;/g, '');
const xmlMeta = `<?xml version="1.0" encoding="utf-8"?>\n`;
appendFile(xmlMeta, `sprite.svg`, packageDir);
appendFile(prettifiedSprite, `sprite.svg`, packageDir);
}

View File

@@ -0,0 +1,17 @@
/* eslint-disable import/no-extraneous-dependencies */
import { basename } from 'path';
import { readSvg } from '../../../scripts/helpers';
/**
* Build an object in the format: `{ <name>: <contents> }`.
* @param {string[]} svgFiles - A list of filenames.
* @param {Function} getSvg - A function that returns the contents of an SVG file given a filename.
* @returns {Object}
*/
export default (svgFiles, iconsDirectory) =>
svgFiles.map(svgFile => {
const name = basename(svgFile, '.svg');
const contents = readSvg(svgFile, iconsDirectory);
return { name, contents };
});