Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 优化 convert 通过文件夹转换的编译方式 #30

Merged
merged 5 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ export default antfu({
warnOnUnsupportedTypeScriptVersion: false,
},
},
vue: true,
rules: {
'no-unused-vars': 'warn',
},
})
6 changes: 3 additions & 3 deletions playground/icons/cat.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 2 additions & 8 deletions playground/icons/daiban.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions playground/icons/gongzuotai.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions playground/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
<div class="i-svg:cat"></div>
<div class="i-icon-daiban"></div>
<div class="i-svg-daiban"></div>
<div class="i-icon:gongzuotai"></div>
<div class="i-svg:gongzuotai"></div>
</div>
<a href="/__inspect/#/module?id=./main.ts&index=2" target="_blank" style="text-decoration: none; margin-top:10px; display: block;">visit /__inspect/ to inspect the intermediate state</a>
<script type="module" src="./main.ts"></script>
Expand Down
1 change: 1 addition & 0 deletions playground/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default defineConfig({
},
svg: './icons',
},
output: './playground/node_modules/.unplugin-iconify',
iconifyIntelliSense: true,
}),
],
Expand Down
8 changes: 5 additions & 3 deletions src/core/convert.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Convert, Options } from './types'
import { existsSync, mkdirSync, writeFileSync } from 'node:fs'
import { join } from 'node:path'
import { removeFigmaClipPathFromSVG, scaleSVG } from '@iconify/tools'
import { isEmptyColor, parseColors } from '@iconify/tools/lib/colors/parse'
import { importDirectory } from '@iconify/tools/lib/import/directory'
import { runSVGO } from '@iconify/tools/lib/optimise/svgo'
Expand Down Expand Up @@ -36,7 +37,6 @@ export async function Generateds(options: Required<Options>): Promise<void> {
export async function Generated(name: string, setting: string | Convert, output: string): Promise<void> {
const convert = typeof setting === 'string' ? { path: setting } : { ...setting }
const { path, noColor, suffix } = convert

if (!existsSync(path)) {
throw new Error(`Path ${path} does not exist`)
}
Expand All @@ -51,7 +51,7 @@ export async function Generated(name: string, setting: string | Convert, output:
})

// Validate, clean up, fix palette and optimise
iconSet.forEach(async (name, type) => {
iconSet.forEachSync((name, type) => {
waset marked this conversation as resolved.
Show resolved Hide resolved
if (type !== 'icon')
return

Expand All @@ -65,6 +65,8 @@ export async function Generated(name: string, setting: string | Convert, output:
// Clean up and optimise icons
try {
cleanupSVG(svg)
removeFigmaClipPathFromSVG(svg)
scaleSVG(svg, 1 / 48)
noColor && parseColors(svg, {
defaultColor: 'currentColor',
callback: (attr, colorStr, color) => {
Expand All @@ -81,9 +83,9 @@ export async function Generated(name: string, setting: string | Convert, output:
iconSet.remove(name)
return
}

// Update icon
iconSet.fromSVG(name, svg)
iconSet.resolve(name)
})

// Export as IconifyJSON
Expand Down
10 changes: 3 additions & 7 deletions src/loader.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { CustomIconLoader } from './core/types'
import { existsSync, readdirSync, readFileSync } from 'node:fs'
import { basename, join } from 'node:path'
import { cwd } from 'node:process'
import { OUTPUT } from './env'

/**
Expand All @@ -24,14 +23,11 @@ export function UnocssLoader(dir: string = OUTPUT): Record<string, CustomIconLoa
* 获取所有输出文件
*/
export function getOutputFiles(dir: string = OUTPUT): string[] {
// 构建 manifest 文件路径
const srcDir = join(cwd(), dir)

if (!existsSync(srcDir)) {
if (!existsSync(dir)) {
return []
}

const files = readdirSync(srcDir).filter(file => file.endsWith('.json'))
const files = readdirSync(dir).filter(file => file.endsWith('.json'))

return files.map(file => join(srcDir, file))
return files.map(file => join(dir, file))
}
Loading