Skip to content

Commit

Permalink
chore: optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
Jevon617 committed Dec 4, 2024
1 parent d122e37 commit e047040
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/core/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { replace, transformStyleStrToObject } from './utils'
import { LOAD_EVENT } from './constants'
import createSvgSprite from './sprite'

export async function genSpriteWidthDts(options: Options, isBuild: boolean) {
export async function genSpriteAndDts(options: Options, isBuild: boolean) {
const spriteInfo = await createSvgSprite(options, isBuild)
if (!isBuild && options?.dts)
genDts(spriteInfo.symbolIds, options)
Expand All @@ -17,7 +17,7 @@ export async function genSpriteWidthDts(options: Options, isBuild: boolean) {
}

export async function genCode(options: Options, isDev = false) {
const spriteInfo = await genSpriteWidthDts(options, !isDev)
const spriteInfo = await genSpriteAndDts(options, !isDev)
const { svgSpriteDomId } = options
const { symbolIds, sprite } = spriteInfo

Expand Down
12 changes: 5 additions & 7 deletions src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import genEtag from 'etag'
import { createUnplugin } from 'unplugin'
import type { ViteDevServer } from 'vite'
import type { Options } from '../types'
import { genCode, genSpriteWidthDts } from './generator'
import { genCode, genSpriteAndDts } from './generator'
import { MODULE_NAME, PLUGIN_NAME } from './constants'
import { resolveOptions } from './utils'
import watchIconDir from './watcher'
Expand Down Expand Up @@ -32,11 +32,9 @@ const unplugin = createUnplugin<Options>(options => ({
return id === MODULE_NAME
},
async load() {
return isBuild
return isBuild || isWebpack
? (await genCode(options))
: isWebpack
? (await genCode(options, true))
: ''
: ''
},
webpack(compiler) {
isWebpack = true
Expand All @@ -48,7 +46,7 @@ const unplugin = createUnplugin<Options>(options => ({
compiler.hooks.emit.tapAsync(PLUGIN_NAME, async (compilation, callback) => {
const assets = compilation.assets as any
const originHtml = assets['index.html']._value
const { sprite } = await genSpriteWidthDts(options, isBuild)
const { sprite } = await genSpriteAndDts(options, isBuild)
const transformedHtml = originHtml.replace(/<\/body>/, `${sprite}</body>`)
assets['index.html'] = {
source() {
Expand Down Expand Up @@ -112,7 +110,7 @@ const unplugin = createUnplugin<Options>(options => ({
return html
}
else {
const { sprite } = await genSpriteWidthDts(options, isBuild)
const { sprite } = await genSpriteAndDts(options, isBuild)
return html.replace(/<\/body>/, `${sprite}</body>`)
}
},
Expand Down
4 changes: 2 additions & 2 deletions src/core/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { ViteDevServer } from 'vite'
import colors from 'picocolors'
import type { Options } from '../types'
import { debounce } from './utils'
import { genSpriteWidthDts, genDts as updateDts } from './generator'
import { genSpriteAndDts, genDts as updateDts } from './generator'
import { LOAD_EVENT } from './constants'

export default function watchIconDir(
Expand Down Expand Up @@ -31,7 +31,7 @@ export default function watchIconDir(
async function handleIconFilesChange(path: string) {
if (!isSvgFile(path))
return
const { symbolIds, sprite } = await genSpriteWidthDts(options, false)
const { symbolIds, sprite } = await genSpriteAndDts(options, false)

if (dts)
updateDts(symbolIds, options)
Expand Down

0 comments on commit e047040

Please sign in to comment.