Skip to content

Commit

Permalink
fix: refine types and jsdoc
Browse files Browse the repository at this point in the history
  • Loading branch information
ntnyq committed Nov 15, 2024
1 parent fa2dff6 commit 56665d1
Show file tree
Hide file tree
Showing 10 changed files with 1,932 additions and 1,610 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"eslint.enable": true,
"prettier.enable": true,
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
Expand Down
6 changes: 3 additions & 3 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"devDependencies": {
"@goy/vuepress-plugin-svg-icons": "workspace:*",
"@vuepress/bundler-vite": "^2.0.0-rc.18",
"@vuepress/plugin-shiki": "^2.0.0-rc.57",
"@vuepress/theme-default": "^2.0.0-rc.58",
"sass-embedded": "^1.80.4"
"@vuepress/plugin-shiki": "^2.0.0-rc.60",
"@vuepress/theme-default": "^2.0.0-rc.60",
"sass-embedded": "^1.81.0"
}
}
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@goy/vuepress-plugin-svg-icons",
"type": "module",
"version": "5.6.0",
"packageManager": "pnpm@9.12.2",
"packageManager": "pnpm@9.13.2",
"description": "SVG sprite icon plugin for VuePress",
"keywords": [
"vuepress-plugin",
Expand All @@ -17,10 +17,10 @@
"email": "ntnyq13@gmail.com"
},
"homepage": "https://vp-icon.ntnyq.com",
"repository": "ntnyq/vuepress-plugin-svg-icons",
"bugs": {
"url": "https://github.com/ntnyq/vuepress-plugin-svg-icons/issues"
},
"repository": "ntnyq/vuepress-plugin-svg-icons",
"exports": {
".": "./dist/node/index.js",
"./client": "./dist/client/index.js",
Expand All @@ -32,6 +32,9 @@
"files": [
"dist"
],
"publishConfig": {
"access": "public"
},
"scripts": {
"build": "run-s clean build:lib copy",
"build:docs": "pnpm -C docs run build",
Expand All @@ -54,26 +57,23 @@
"vue": "^3.5.12"
},
"devDependencies": {
"@ntnyq/eslint-config": "^3.1.1",
"@ntnyq/eslint-config": "^3.2.2",
"@ntnyq/prettier-config": "^1.21.3",
"@types/node": "^22.8.1",
"@types/node": "^22.9.0",
"copyfiles": "^2.4.1",
"eslint": "^9.13.0",
"eslint": "^9.14.0",
"husky": "^9.1.6",
"nano-staged": "^0.8.0",
"npm-run-all2": "^7.0.1",
"pnpm": "^9.12.2",
"pnpm": "^9.13.2",
"prettier": "^3.3.3",
"rimraf": "^6.0.1",
"tsc-watch": "^6.2.0",
"tsc-watch": "^6.2.1",
"typescript": "^5.6.3",
"vuepress": "^2.0.0-rc.18"
},
"prettier": "@ntnyq/prettier-config",
"nano-staged": {
"*.{js,ts,cjs,mjs,vue,yaml,yml,md,json}": "eslint --fix"
},
"publishConfig": {
"access": "public"
}
}
3,386 changes: 1,837 additions & 1,549 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

38 changes: 22 additions & 16 deletions src/client/components/Icon.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
import { defineComponent, h } from 'vue'
import type { ExtractPropTypes } from 'vue'

export const Icon = defineComponent({
name: 'VpIcon',
export const iconProps = {
name: {
type: String,
required: true,
},

props: {
name: {
type: String,
required: true,
},
color: {
type: String,
},

color: {
type: String,
},
size: {
type: String,
},

size: {
type: String,
},
prefix: {
type: String,
required: true,
},
} as const

export type IconProps = ExtractPropTypes<typeof iconProps>

prefix: {
type: String,
},
export const Icon = defineComponent({
props: {
...iconProps,
},

setup(props) {
Expand Down
22 changes: 8 additions & 14 deletions src/client/components/Sprites.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import { SVGIconsData } from '@vuepress/plugin-svg-icons/data'
import { defineComponent, h } from 'vue'

export const Sprites = defineComponent({
name: 'VpIconSprites',

setup() {
return () =>
h('div', {
style: 'display: none;',
'data-name': '__VUEPRESS_PLUGIN_SVG_ICONS__',
innerHTML: SVGIconsData,
})
},
})

export default Sprites
export const Sprites = defineComponent(
() => () =>
h('div', {
style: 'display: none;',
'data-name': '__VUEPRESS_PLUGIN_SVG_ICONS__',
innerHTML: SVGIconsData,
}),
)
8 changes: 7 additions & 1 deletion src/client/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,22 @@ import { h } from 'vue'
import { defineClientConfig } from 'vuepress/client'
import { Icon, Sprites } from './components/index.js'
import type { SvgIconPropsOptions } from '../shared/index.js'
import type { IconProps } from './components/index.js'

declare const __SVG_ICON_ID_PREFIX__: string
declare const __SVG_ICON_COMPONENT_NAME__: string
declare const __SVG_ICON_DEFAULT_PROPS_OPTIONS__: SvgIconPropsOptions

const defaultPropsOptions = __SVG_ICON_DEFAULT_PROPS_OPTIONS__

/**
* Prefix is a constant value
*/
export type IconPropsWithoutPrefix = Omit<IconProps, 'prefix'>

export default defineClientConfig({
enhance: ({ app }) => {
app.component(__SVG_ICON_COMPONENT_NAME__, (props: SvgIconPropsOptions) =>
app.component(__SVG_ICON_COMPONENT_NAME__, (props: IconPropsWithoutPrefix) =>
h(Icon, {
...defaultPropsOptions,
...props,
Expand Down
26 changes: 25 additions & 1 deletion src/node/svgIconPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,38 @@
import { fs, getDirname, path } from 'vuepress/utils'
import { getSVGIconsData } from './utils.js'
import type { Plugin } from 'vuepress'
import type { SvgIconPropsOptions } from '../shared/index.js'

const __dirname = getDirname(import.meta.url)

export interface SvgIconPluginOptions {
/**
* svg files directory based on source dir
*
* @default `icons`
*/
svgsDir?: string

/**
* icon id prefix
*
* @default `vp_icon_`
*/
iconIdPrefix?: string

/**
* icon component name
*
* @default `VpIcon`
*/
componentName?: string
defaultPropsOptions?: Record<string, any>

/**
* default props for icon component
*
* @default {}
*/
defaultPropsOptions?: SvgIconPropsOptions
}

export const svgIconPlugin = (options: SvgIconPluginOptions = {}): Plugin => {
Expand Down
26 changes: 12 additions & 14 deletions src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import path from 'node:path'
import svgMixer from 'svg-mixer'

interface SvgSpritesOptions {
prefix?: string
/**
* Svg symbol's id prefix
*/
prefix: string
}

/**
Expand All @@ -15,16 +18,6 @@ interface SvgSpritesOptions {
*/
const generateSymbolId = (file: string, prefix: string) => prefix + path.basename(file, '.svg')

/**
* Config to generate svg sprite data
*/
const spriteConfig = {
filename: '',
usages: false,
spacing: 0,
attrs: { 'arial-hidden': 'true' },
}

/**
* Generate svg sprite data
*
Expand All @@ -33,11 +26,16 @@ const spriteConfig = {
*
* @returns svg sprite data
*/
export async function getSVGIconsData(path: string, options: SvgSpritesOptions = {}) {
export async function getSVGIconsData(path: string, options: SvgSpritesOptions) {
// @ts-expect-error svg-mixer is not typed properly
const result = await svgMixer(`${path}/*.svg`, {
generateSymbolId: path => generateSymbolId(path, options.prefix!),
spriteConfig,
generateSymbolId: path => generateSymbolId(path, options.prefix),
spriteConfig: {
filename: '',
usages: false,
spacing: 0,
attrs: { 'arial-hidden': 'true' },
},
})

return result.content
Expand Down
9 changes: 7 additions & 2 deletions src/shared/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
export interface SvgIconPropsOptions {
name: string
/**
* icon svg font-size
*/
size?: string

/**
* icon svg fill color
*/
color?: string
prefix?: string
}

0 comments on commit 56665d1

Please sign in to comment.