Skip to content

Commit 0ef5802

Browse files
Add icon web component (#3)
* Add static icon collection file * Add icon web component
1 parent 9808cfe commit 0ef5802

29 files changed

Lines changed: 163 additions & 72 deletions

example/app/src/main.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import './style.css'
2-
import '@alexanderweigelt/icon-library/dist/styles/main.css'
3-
import IconSprite from '@alexanderweigelt/icon-library/dist/icons/icons.sprite.svg'
2+
import '@alexanderweigelt/icon-library/lib/styles/main.css'
3+
import '@alexanderweigelt/icon-library/lib/IconComponent'
4+
import IconSprite from '@alexanderweigelt/icon-library/lib/icons/icons.sprite.svg'
45

56
document.querySelector<HTMLDivElement>('#app')!.innerHTML = `
67
<header>
@@ -22,6 +23,13 @@ document.querySelector<HTMLDivElement>('#app')!.innerHTML = `
2223
<use xlink:href="#user"></use>
2324
</svg>
2425
</div>
26+
<div class="card">
27+
<p>Get your icons from web component. &lt;icon-component name="home"&gt;&lt;/icon-component&gt;</p>
28+
<icon-component name="home"></icon-component>
29+
<icon-component name="user"></icon-component>
30+
<!-- Check the fallback icon for an unassigned symbol name -->
31+
<icon-component name="fallback"></icon-component>
32+
</div>
2533
</article>
2634
`
2735
document.querySelector<HTMLDivElement>('#sprite')!.innerHTML = IconSprite

packages/icon-library/build/compile.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,21 @@ class IconLibraryCompiler {
1414
constructor() {
1515
this.rootDir = join(__dirname, '..');
1616
this.paths = {
17+
collection: {
18+
file: 'collection.generated.json',
19+
destination: 'lib',
20+
},
1721
svg: {
1822
source: 'src/icons',
19-
destination: 'dist/icons',
23+
destination: 'lib/icons',
2024
},
2125
sass: {
2226
source: 'src/styles/main.scss',
23-
destination: 'dist/styles/main.css',
27+
destination: 'lib/styles/main.css',
2428
},
2529
sprite: {
2630
file: 'icons.sprite.svg',
27-
destination: 'dist/icons',
31+
destination: 'lib/icons',
2832
}
2933
};
3034
}
@@ -38,9 +42,9 @@ class IconLibraryCompiler {
3842
let items = [];
3943

4044
try {
41-
await readdir(directory, (err, files) => {
45+
items = await readdir(directory, (err, files) => {
46+
console.log('Files:',files)
4247
if (err) throw Error(err.message);
43-
items = files;
4448
});
4549
} catch {
4650
return await mkdir(directory, {
@@ -51,6 +55,19 @@ class IconLibraryCompiler {
5155
return Promise.all(items.map((item) => rm(join(directory, item), {recursive: true})));
5256
}
5357

58+
/**
59+
* Writes the collected icons to a json file
60+
* @param svgData
61+
* @returns {Promise<void>}
62+
*/
63+
async createIconCollection(svgData) {
64+
let map = {};
65+
const dist = join(this.rootDir, this.paths.collection.destination, this.paths.collection.file);
66+
svgData.map((data) => (map[data.key] = data.optimizedSvg));
67+
68+
return await writeFile(dist, JSON.stringify(map));
69+
}
70+
5471
/**
5572
* Returns the SVG data from source directory
5673
* @returns {Promise<Awaited<{optimizedSvg: string, iconName: *, title: string, iconFileName: *, key: *}>[]>}
@@ -176,6 +193,7 @@ class IconLibraryCompiler {
176193
await this.emptyDirectory(join(this.rootDir, this.paths.svg.destination));
177194
await this.emptyDirectory(join(this.rootDir, this.paths.sass.destination.substring(0, this.paths.sass.destination.lastIndexOf('/'))));
178195
const svgData = await this.getSvgsData();
196+
await this.createIconCollection(svgData);
179197
await this.createSvgIconFiles(svgData);
180198
await this.createSassStyles(svgData);
181199
await this.createSvgSprite(svgData);
@@ -187,4 +205,4 @@ class IconLibraryCompiler {
187205
}
188206

189207
const iconCompiler = new IconLibraryCompiler();
190-
iconCompiler.compileIcons().then(() => console.log("Successful compiled!"));
208+
iconCompiler.compileIcons().then(() => console.log("Successful compiled Icon library static assets!"));

packages/icon-library/config/svgo.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module.exports = {
77
{
88
name: 'removeAttrs',
99
params: {
10-
attrs: ['path:(fill|stroke)', 'fill'],
10+
attrs: ['path:(fill|stroke)', 'fill', 'data.*'],
1111
},
1212
},
1313
{

packages/icon-library/dist/IconComponent.d.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

packages/icon-library/dist/IconComponent.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

packages/icon-library/dist/IconComponent.js.map

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/icon-library/dist/icons/icons.sprite.svg

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/icon-library/dist/styles/main.css

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {};

packages/icon-library/lib/IconComponent.js

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)