Skip to content

Commit

Permalink
perf: 根据建议修改方法
Browse files Browse the repository at this point in the history
  • Loading branch information
waset committed Oct 30, 2024
1 parent 36e0c7d commit eead711
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Iconify({
/**
* 输出目录
* @type string
* @default `process.join(process.cwd(), 'node_modules/.unplugin-iconify')`
* @default `path.join(process.cwd(), 'node_modules/.unplugin-iconify')`
*/
output: 'dist/icons',

Expand Down
16 changes: 3 additions & 13 deletions src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { cwd } from 'node:process'
import { OUTPUT } from '../env'
import { getOutputFiles, UnocssLoader } from '../loader'
import { Generateds } from './convert'
import { normalizePath } from './utils'
import { IconifyIntelliSenseSettings } from './vscode'

export class Iconify {
Expand Down Expand Up @@ -128,20 +129,9 @@ export class Iconify {
const paths = []
for (const dir of this.outputs) {
if (!existsSync(dir)) {
return
continue
}
const workspace = this.options.workspace.replace(/\\/g, '/')
let path = dir.replace(/\\/g, '/')
if (path.startsWith(workspace)) {
path = path.replace(workspace, '')
}
if (path.startsWith('/')) {
path = path.slice(1)
}
if (path.endsWith('/')) {
path = path.slice(0, -1)
}
paths.push(path)
paths.push(normalizePath(dir, this.options.workspace))
}

// 如果没有生成文件,则直接返回
Expand Down
2 changes: 1 addition & 1 deletion src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export interface Options {
/**
* 生成的类型文件的输出目录。
*
* @default process.join(process.cwd(), 'node_modules/.unplugin-iconify')
* @default path.join(process.cwd(), 'node_modules/.unplugin-iconify')
*/
output?: string

Expand Down
19 changes: 19 additions & 0 deletions src/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,22 @@ export function toArray<T>(array?: T | T[]): T[] {
array = array ?? []
return Array.isArray(array) ? array : [array]
}

/**
* 获取相对路径
* @param path 文件路径
* @param workspace 工作区路径
* @returns 相对路径
*/
export function normalizePath(path: string, workspace: string): string {
// 统一使用正斜杠
path = path.replace(/\\/g, '/')
workspace = workspace.replace(/\\/g, '/')

// 移除工作区前缀
if (path.startsWith(workspace))
path = path.replace(workspace, '')

// 清理首尾的斜杠
return path.replace(/^\/+|\/+$/g, '')
}

0 comments on commit eead711

Please sign in to comment.