Skip to content

Commit

Permalink
feat: add vueVersion prop
Browse files Browse the repository at this point in the history
  • Loading branch information
Jevon617 committed Dec 12, 2022
1 parent e93d138 commit aa0f95c
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 14 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ function App() {
| symbolIdFormatter | `(svgName:string, prefix: string)=>string` | [code](./src/core/utils.ts/#L33) | the symbolId's formatter |
| optimizeOptions | `SvgoOptions` | - | svgo optimize [Options](https://github.com/svg/svgo) |
| svgSpriteDomId | `string` | __svg_sprite__dom__ | Customize the ID of the svgDom |
| vueVersion | `2 | 3 | auto` | auto | Vue version |


## Typescript support
```json
Expand Down
1 change: 1 addition & 0 deletions README.zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ function App() {
| symbolIdFormatter | `(svgName:string, prefix: string)=>string` | [code](./src/core/utils.ts/#L33) | 可以通过这个参数来设置symbolId的格式 |
| optimizeOptions | `SvgoOptions` | - | [svgo 的优化参数](https://github.com/svg/svgo) |
| svgSpriteDomId | `string` | __svg_sprite__dom__ | 自定义生成的svg元素的id |
| vueVersion | `2 | 3 | auto` | auto | Vue 版本, 默认会自动检测 |


## Typescript 支持
Expand Down
33 changes: 19 additions & 14 deletions src/core/generator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'path'
import fs from 'fs/promises'
import { getPackageInfo, importModule, isPackageExists } from 'local-pkg'
import type { Options } from '../types'
import type { Options, VueVersion } from '../types'
import { dts, golbalDts, reactDts, reactTemplate, template } from './snippets'
import { replace, transformStyleStrToObject } from './utils'
import createSvgSprite from './sprite'
Expand Down Expand Up @@ -91,13 +91,13 @@ export function genDts(symbolIds: Set<string>, options: Options, isVueProject: b
}

async function genComponent(options: Options, isVueProject: boolean) {
const { componentStyle, componentName } = options
const { componentStyle, componentName, vueVersion } = options
if (!isVueProject) {
return reactTemplate.replace(/\$component_name/, componentName!)
.replace(/\$component_style/, JSON.stringify(transformStyleStrToObject(componentStyle!)))
}

const vueVerison = await getVueVersion()
const vueVerison = await getVueVersion(vueVersion!)
if (!vueVerison)
return 'export default {}'

Expand Down Expand Up @@ -148,23 +148,25 @@ async function compileVue2Template(template: string): Promise<string> {
return code
}

async function getVueVersion() {
try {
const result = await getPackageInfo('vue')
if (!result)
async function getVueVersion(vueVerison: VueVersion) {
if (vueVerison === 'auto') {
try {
const result = await getPackageInfo('vue')
if (!result)
return null
return result.version.startsWith('2.') ? 'vue2' : 'vue3'
}
catch {
return null
return result.version.startsWith('2.') ? 'vue2' : 'vue3'
}
}
catch {
return null
else {
return `vue${vueVerison}`
}
}

function detectProjectType(options: Options) {
if (options.projectType) {
return options.projectType === 'vue'
}
else {
if (options.projectType === 'auto') {
try {
if (isPackageExists('vue'))
return true
Expand All @@ -177,4 +179,7 @@ function detectProjectType(options: Options) {
throw new Error('[unpluign-svg-component] can\'t detect your project type, please set options.projectType.')
}
}
else {
return options.projectType === 'vue'
}
}
1 change: 1 addition & 0 deletions src/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export function resolveOptions(options: Options): Options {
componentName: 'SvgIcon',
dtsDir: process.cwd(),
projectType: 'auto',
vueVersion: 'auto',
svgSpriteDomId: '__svg_sprite__dom__',
componentStyle: 'width: 1em; height: 1em; fill:currentColor;',
symbolIdFormatter(svgName: string, prefix: string) {
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ export interface Options {
symbolIdFormatter?: (name: string, prefix: string) => string
optimizeOptions?: OptimizeOptions
projectType?: 'vue' | 'react' | 'auto'
vueVersion?: VueVersion
}

export type VueVersion = 2 | 3 | 'auto'
8 changes: 8 additions & 0 deletions test/gen-module-code.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@ import { genModuleCode } from '../src/core/generator'

test('createSprite', async () => {
const { code } = await genModuleCode({
projectType: 'vue',
vueVersion: 3,
iconDir: path.resolve(__dirname, './icons'),
componentName: 'SvgIcon',
svgSpriteDomId: 'sprite-id',
symbolIdFormatter(svgName: string, prefix: string) {
const nameArr = svgName.split('/')
if (prefix)
nameArr.unshift(prefix)
return nameArr.join('-').replace(/\.svg$/, '')
},
}, true)
expect(code).matchSnapshot()
})

0 comments on commit aa0f95c

Please sign in to comment.