This repository was archived by the owner on Oct 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
feat: unplugin-icons adder #555
Open
Joex3
wants to merge
2
commits into
svelte-add:main
Choose a base branch
from
Joex3:unplugin-icons
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'@svelte-add/config': minor | ||
'@svelte-add/adders': minor | ||
'svelte-add': minor | ||
--- | ||
|
||
feat: unplugin-icons adder |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* This file is auto generated and can be updated by running: | ||
* $ node scripts/update-unplugin-icons-collections | ||
*/ | ||
|
||
export const collections = [ | ||
{ name: 'none', label: 'None' }, | ||
{ name: '@iconify/json', version: '^2.2.242', label: 'Full Collection (~120MB)' }, | ||
{ name: '@iconify-json/carbon', version: '^1.1.37', label: 'Carbon' }, | ||
{ name: '@iconify-json/mdi', version: '^1.1.68', label: 'Material Design Icons' }, | ||
{ name: '@iconify-json/tabler', version: '^1.1.121', label: 'Tabler Icons' }, | ||
{ name: '@iconify-json/heroicons', version: '^1.1.24', label: 'HeroIcons' }, | ||
{ name: '@iconify-json/logos', version: '^1.1.44', label: 'SVG Logos' }, | ||
{ name: '@iconify-json/ri', version: '^1.1.22', label: 'Remix Icon' }, | ||
{ name: '@iconify-json/ph', version: '^1.1.14', label: 'Phosphor' }, | ||
{ name: '@iconify-json/simple-icons', version: '^1.1.115', label: 'Simple Icons' }, | ||
{ name: '@iconify-json/ic', version: '^1.1.18', label: 'Google Material Icons' }, | ||
{ name: '@iconify-json/lucide', version: '^1.1.209', label: 'Lucide' }, | ||
{ name: '@iconify-json/svg-spinners', version: '^1.1.3', label: 'SVG Spinners' }, | ||
{ name: '@iconify-json/bi', version: '^1.1.24', label: 'Bootstrap Icons' }, | ||
{ name: '@iconify-json/material-symbols', version: '^1.1.89', label: 'Material Symbols' }, | ||
{ name: '@iconify-json/fluent', version: '^1.1.63', label: 'Fluent UI System Icons' }, | ||
{ name: '@iconify-json/fa6-solid', version: '^1.1.24', label: 'Font Awesome Solid' }, | ||
{ name: '@iconify-json/vscode-icons', version: '^1.1.37', label: 'VSCode Icons' }, | ||
{ name: '@iconify-json/bx', version: '^1.1.11', label: 'BoxIcons' }, | ||
{ name: '@iconify-json/twemoji', version: '^1.1.16', label: 'Twitter Emoji' }, | ||
]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import { defineAdderConfig } from '@svelte-add/core'; | ||
import { options } from './options'; | ||
import { collections } from '../collections'; | ||
import type { PackageDefinition } from '@svelte-add/core/adder/config'; | ||
|
||
export const adder = defineAdderConfig({ | ||
metadata: { | ||
id: 'unplugin-icons', | ||
name: 'unplugin-icons ', | ||
description: 'Access thousands of icons as components on-demand universally.', | ||
environments: { svelte: true, kit: true }, | ||
website: { | ||
logo: './unplugin-icons.svg', | ||
keywords: ['unplugin-icons', 'svg', 'icons', 'iconify', 'iconify-json'], | ||
documentation: 'https://www.npmjs.com/package/unplugin-icons', | ||
}, | ||
}, | ||
options, | ||
integrationType: 'inline', | ||
packages: [ | ||
{ name: 'unplugin-icons', version: '^0.19.2', dev: true }, | ||
...collections | ||
.filter((collection) => collection.name && collection.version) | ||
.map( | ||
({ name, version }) => | ||
({ | ||
name, | ||
version, | ||
dev: true, | ||
condition: ({ options }) => options.collection === name, | ||
}) as PackageDefinition<typeof options>, | ||
), | ||
], | ||
files: [ | ||
{ | ||
name: ({ typescript }) => `vite.config.${typescript.installed ? 'ts' : 'js'}`, | ||
contentType: 'script', | ||
content: ({ ast, imports, exports, functions, array, object, common }) => { | ||
const vitePluginName = 'Icons'; | ||
imports.addDefault(ast, 'unplugin-icons/vite', vitePluginName); | ||
|
||
const { value: rootObject } = exports.defaultExport( | ||
ast, | ||
functions.call('defineConfig', []), | ||
); | ||
const param1 = functions.argumentByIndex(rootObject, 0, object.createEmpty()); | ||
|
||
const pluginsArray = object.property(param1, 'plugins', array.createEmpty()); | ||
const pluginFunctionCall = functions.call(vitePluginName, []); | ||
const pluginConfig = object.create({ | ||
compiler: common.createLiteral('svelte'), | ||
}); | ||
functions.argumentByIndex(pluginFunctionCall, 0, pluginConfig); | ||
|
||
array.push(pluginsArray, pluginFunctionCall); | ||
}, | ||
}, | ||
{ | ||
name: () => 'src/app.d.ts', | ||
contentType: 'text', | ||
content: ({ content, dependencies }) => { | ||
return addImport(content, getIconTypes(dependencies)); | ||
}, | ||
condition: ({ typescript, kit }) => kit.installed && typescript.installed, | ||
}, | ||
{ | ||
name: () => 'src/vite-env.d.ts', | ||
contentType: 'text', | ||
content: ({ content, dependencies }) => { | ||
return addTypeReferenceComment(content, getIconTypes(dependencies)); | ||
}, | ||
condition: ({ typescript, kit }) => !kit.installed && typescript.installed, | ||
}, | ||
], | ||
}); | ||
|
||
const addTypeReferenceComment = (content: string, types: string) => { | ||
if (!hasTypeReferenceComment(content, types)) { | ||
const contentTrimmed = content.trimEnd(); | ||
const trailingTrivia = content.slice(contentTrimmed.length); | ||
|
||
content = `${contentTrimmed}\n/// <reference types=${JSON.stringify(types)} />${trailingTrivia}`; | ||
} | ||
|
||
return content; | ||
}; | ||
|
||
const hasTypeReferenceComment = (content: string, types: string) => { | ||
const regex = new RegExp(`///\\s*<\\s*reference\\s*types\\s*=\\s*(['"])${types}\\1\\s*/>`); | ||
return regex.test(content); | ||
}; | ||
|
||
const addImport = (content: string, types: string) => { | ||
if (!hasImport(content, types)) { | ||
content = content.trimStart(); | ||
if (content.startsWith('//') || content.startsWith('/*')) { | ||
content = `\n${content}`; | ||
} | ||
|
||
content = `import '${types}'\n${content};`; | ||
} | ||
|
||
return content; | ||
}; | ||
|
||
const hasImport = (content: string, types: string) => { | ||
const regex = new RegExp(`import\\s+(['"])${types}\\1`); | ||
return regex.test(content); | ||
}; | ||
|
||
const getIconTypes = (dependencies: Record<string, string>) => { | ||
if ((dependencies['svelte'] ?? '').startsWith('^3.')) { | ||
return 'unplugin-icons/types/svelte3'; | ||
} | ||
|
||
return 'unplugin-icons/types/svelte'; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { defineAdderChecks } from '@svelte-add/core'; | ||
import { options } from './options'; | ||
|
||
export const checks = defineAdderChecks({ | ||
options, | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { defineAdderOptions } from '@svelte-add/core'; | ||
import { collections } from '../collections'; | ||
|
||
export const options = defineAdderOptions({ | ||
collection: { | ||
question: 'Do you want to install an icon collection?', | ||
type: 'select', | ||
default: 'none', | ||
options: collections.map((collection) => ({ | ||
value: collection.name, | ||
label: collection.label, | ||
hint: collection.name, | ||
})), | ||
}, | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { defineAdderTests } from '@svelte-add/core'; | ||
import { options } from './options'; | ||
|
||
const defaultOptionValues = { | ||
collection: options.collection.default, | ||
}; | ||
|
||
export const tests = defineAdderTests({ | ||
options, | ||
optionValues: [ | ||
{ ...defaultOptionValues }, | ||
{ ...defaultOptionValues, collection: '@iconify-json/mdi' }, | ||
], | ||
files: [ | ||
{ | ||
name: ({ kit }) => (kit.installed ? `${kit.routesDirectory}/+page.svelte` : `src/App.svelte`), | ||
contentType: 'svelte', | ||
condition: ({ options }) => options.collection !== 'none', | ||
content: ({ js, html }) => { | ||
js.imports.addDefault(js.ast, 'virtual:icons/mdi/add', 'IconAdd'); | ||
js.imports.addDefault(js.ast, '~icons/mdi/minus', 'IconMinus'); | ||
|
||
html.addFromRawHtml( | ||
html.ast.childNodes, | ||
` | ||
<div class="unplugin-icons"> | ||
<IconAdd class="mdi-icon-1" /> | ||
<IconMinus class="mdi-icon-2" /> | ||
</div> | ||
`, | ||
); | ||
}, | ||
}, | ||
], | ||
tests: [ | ||
{ | ||
name: 'icons exist', | ||
condition: ({ collection }) => collection !== 'none', | ||
run: async ({ elementExists }) => { | ||
await elementExists('.unplugin-icons .mdi-icon-1'); | ||
await elementExists('.unplugin-icons .mdi-icon-2'); | ||
}, | ||
}, | ||
], | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#!/usr/bin/env node | ||
|
||
import { defineAdder } from '@svelte-add/core'; | ||
import { adder } from './config/adder.js'; | ||
import { checks } from './config/checks.js'; | ||
import { tests } from './config/tests.js'; | ||
|
||
export default defineAdder(adder, checks, tests); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/** | ||
* This script updates all collections supported by the unplugin-icons adder. | ||
* It does this by searching all "@iconify-json/*" packages using pnpm. | ||
*/ | ||
|
||
// @ts-check | ||
import { execSync } from 'node:child_process'; | ||
import { writeFileSync } from 'node:fs'; | ||
import { format, resolveConfig } from 'prettier'; | ||
|
||
(async () => { | ||
const json = execSync('pnpm search --json "@iconify-json/"').toString('utf-8'); | ||
/** @type {Array<{ name: string, scope: string, version: string, description: string }>} */ | ||
const packages = JSON.parse(json); | ||
|
||
const allowedScopes = ['iconify', 'iconify-json']; | ||
const stripDescription = ' icon set in Iconify JSON format'; | ||
/** @type {Record<string, string | undefined>} */ | ||
const replacedDescriptions = { | ||
'@iconify/json': 'Full Collection (~120MB)', | ||
}; | ||
|
||
/** @type {Array<{ name: string, version?: string, label?: string }>} */ | ||
const collections = [ | ||
{ | ||
name: 'none', | ||
version: undefined, | ||
label: 'None', | ||
}, | ||
]; | ||
|
||
for (const pkg of packages) { | ||
if (!allowedScopes.includes(pkg.scope)) { | ||
continue; | ||
} | ||
|
||
const description = | ||
replacedDescriptions[pkg.name] ?? pkg.description.replace(stripDescription, ''); | ||
|
||
collections.push({ | ||
name: pkg.name, | ||
version: `^${pkg.version}`, | ||
label: description, | ||
}); | ||
} | ||
|
||
const codePath = 'adders/unplugin-icons/collections.ts'; | ||
const config = await resolveConfig(codePath, { editorconfig: true }); | ||
|
||
const code = await format( | ||
` | ||
/** | ||
* This file is auto generated and can be updated by running: | ||
* $ node scripts/update-unplugin-icons-collections | ||
*/ | ||
|
||
export const collections = ${JSON.stringify(collections)}; | ||
`, | ||
{ | ||
...(config ?? {}), | ||
filepath: codePath, | ||
}, | ||
); | ||
writeFileSync(codePath, code); | ||
|
||
console.log(`Wrote ${codePath}`); | ||
})().catch( | ||
/** @param {unknown} e} */ | ||
(e) => { | ||
console.error(e); | ||
process.exit(1); | ||
}, | ||
); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a very partial list, so I'm not sure how we'd decide which get listed here vs omitted. E.g. https://www.npmjs.com/package/@iconify-json/openmoji is missing. You can see all the icon sets at https://icon-sets.iconify.design/