Skip to content

Commit

Permalink
feat: add hmr option, enable hmr in buildStart hook
Browse files Browse the repository at this point in the history
  • Loading branch information
Jevon617 committed Dec 3, 2024
1 parent a3859d4 commit 61da38d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
16 changes: 12 additions & 4 deletions src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import cors from 'cors'
import genEtag from 'etag'
import { createUnplugin } from 'unplugin'
import type { ViteDevServer } from 'vite'
import type { Options, SvgSpriteInfo } from '../types'
import { genCode, genDts } from './generator'
import { MODULE_NAME, PLUGIN_NAME } from './constants'
Expand All @@ -12,18 +13,25 @@ import watchIconDir from './watcher'
let isBuild = false
let isWebpack = false
let isDynamicStrategy = false

let spriteInfo: SvgSpriteInfo
let transformPluginContext: any
let viteDevServer: ViteDevServer

const unplugin = createUnplugin<Options>(options => ({
name: PLUGIN_NAME,
async buildStart() {
options = resolveOptions(options)
spriteInfo = await createSvgSprite(options, isBuild)
isDynamicStrategy = options.domInsertionStrategy === 'dynamic'
// only generate dts in serve
if (options?.dts && !isBuild)

// dts generator
if (options?.dts)
genDts(spriteInfo.symbolIds, options)

// svg hmr
if (!isBuild && viteDevServer && options.hmr)
watchIconDir(options, viteDevServer, spriteInfo)
},
resolveId(id: string) {
if (id === MODULE_NAME)
Expand Down Expand Up @@ -67,15 +75,15 @@ const unplugin = createUnplugin<Options>(options => ({
transformPluginContext = this
},
configureServer(server) {
viteDevServer = server
server.middlewares.use(cors({ origin: '*' }))
server.middlewares.use(async (req, res, next) => {
// close #22
const { pathname } = req.url
? new URL(req.url, 'https://example.com')
: { pathname: '' }
if (pathname.endsWith(`/@id/${MODULE_NAME}`)) {
watchIconDir(options, server, spriteInfo)

spriteInfo = await createSvgSprite(options, isBuild)
const code = await genCode(options, spriteInfo, true)
const etag = genEtag(code, { weak: true })
const noneMatch = req.headers['if-none-match'] || req.headers['If-None-Match']
Expand Down
1 change: 1 addition & 0 deletions src/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export function replace(dts: string, symbolIds: Set<string>, componentName: stri

export function resolveOptions(options: Options): Options {
const defaultOptions: Partial<Options> = {
hmr: true,
componentName: 'SvgIcon',
dtsDir: process.cwd(),
projectType: 'auto',
Expand Down
16 changes: 8 additions & 8 deletions src/core/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,6 @@ export default function watchIconDir(
}
isWatched = true

function notifyClientUpdate(sprite: string, { ws, config }: ViteDevServer) {
config.logger.info(
colors.green(`unplugin-svg-component reload`),
{ clear: false, timestamp: true },
)
ws.send(LOAD_EVENT, { sprite })
}

function isSvgFile(path: string) {
const isSvgDir = iconDirs.some(dir => path.startsWith(dir + sep))
return isSvgDir && path.endsWith('.svg')
Expand All @@ -57,3 +49,11 @@ export default function watchIconDir(
notifyClientUpdate(svgSpriteInfo.sprite, server)
}
}

function notifyClientUpdate(sprite: string, { ws, config }: ViteDevServer) {
config.logger.info(
colors.green(`unplugin-svg-component reload`),
{ clear: false, timestamp: true },
)
ws.send(LOAD_EVENT, { sprite })
}
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Config as OptimizeOptions } from 'svgo'
export interface Options {
iconDir: string | string[]
prefix?: string
hmr?: boolean
dts?: boolean
dtsDir?: string
svgSpriteDomId?: string
Expand Down

0 comments on commit 61da38d

Please sign in to comment.