Skip to content

Commit

Permalink
refactor:热更新控制的相关功能整理为函数
Browse files Browse the repository at this point in the history
  • Loading branch information
build-admin committed Jun 27, 2024
1 parent d219616 commit e5e77b4
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 33 deletions.
5 changes: 3 additions & 2 deletions web/src/stores/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { timeFormat } from '/@/utils/common'
import { taskStatus } from '/@/components/terminal/constant'
import { ElNotification } from 'element-plus'
import { i18n } from '/@/lang/index'
import { closeHotUpdate, openHotUpdate } from '/@/utils/vite'

export const useTerminal = defineStore(
'terminal',
Expand Down Expand Up @@ -81,7 +82,7 @@ export const useTerminal = defineStore(

function taskCompleted(idx: number) {
// 命令执行完毕,重新打开热更新
if (import.meta.hot) import.meta.hot.send('custom:open-hot', { type: 'terminal' })
openHotUpdate('terminal')

if (typeof state.taskList[idx].callback != 'function') return

Expand Down Expand Up @@ -182,7 +183,7 @@ export const useTerminal = defineStore(

function startEventSource(taskKey: number) {
// 命令执行期间禁用热更新
if (import.meta.hot) import.meta.hot.send('custom:close-hot', { type: 'terminal' })
closeHotUpdate('terminal')

window.eventSource = new EventSource(
buildTerminalUrl(state.taskList[taskKey].command, state.taskList[taskKey].uuid, state.taskList[taskKey].extend)
Expand Down
64 changes: 50 additions & 14 deletions web/src/utils/vite.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ref } from 'vue'
import { Plugin } from 'vite'

/**
Expand All @@ -14,43 +15,78 @@ export function isProd(mode: string | undefined): boolean {
return mode === 'production'
}

/**
* 调试模式下的热更新开关状态
*/
export const hotUpdateSwitch = ref(true)

/**
* 调试模式下关闭热更新
*/
export const closeHotUpdate = (type: string) => {
if (!hotUpdateSwitch.value) return
hotUpdateSwitch.value = false

if (import.meta.hot) {
import.meta.hot.send('custom:close-hot', { type })
}
}

/**
* 调试模式下开启热更新
*/
export const openHotUpdate = (type: string) => {
if (hotUpdateSwitch.value) return
hotUpdateSwitch.value = true

if (import.meta.hot) {
import.meta.hot.send('custom:open-hot', { type })
}
}

/**
* 调试模式下重启服务并刷新网页
*/
export const reloadServer = (type: string) => {
hotUpdateSwitch.value = true
if (import.meta.hot) {
import.meta.hot.send('custom:reload-server', { type })
}
}

/**
* 自定义热更新处理插件
*/
export const customHotUpdate = (): Plugin => {
const closeHmr = new Map<string, boolean>()

let hotUpdateSwitch = true
return {
name: 'vite-plugin-custom-hot-update',
apply: 'serve',
configureServer(server) {
server.ws.on('custom:close-hot', (data) => {
closeHmr.set(data.type, true)

server.ws.on('custom:close-hot', () => {
hotUpdateSwitch = false
// 关闭文件添加和删除的监听
server.watcher.removeAllListeners('add')
server.watcher.removeAllListeners('unlink')
})
server.ws.on('custom:open-hot', (data) => {
closeHmr.set(data.type, false)

server.ws.on('custom:open-hot', () => {
hotUpdateSwitch = true
server.watcher.on('add', () => {
server.restart()
})
server.watcher.on('unlink', () => {
server.restart()
})
})
server.ws.on('custom:reload-hot', () => {
server.ws.on('custom:reload-server', () => {
hotUpdateSwitch = true
server.restart()
})
},
handleHotUpdate() {
const closeHmrs = Array.from(closeHmr.values())
let closeHmrsBool = false
for (const key in closeHmrs) {
closeHmrsBool = closeHmrsBool || closeHmrs[key]
if (!hotUpdateSwitch) {
return []
}
if (closeHmrsBool) return []
},
}
}
3 changes: 2 additions & 1 deletion web/src/views/backend/crud/design.vue
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,7 @@ import { getTableFieldList, getTableListUrl, getDatabaseConnectionListUrl } from
import { buildValidatorData, regularVarName } from '/@/utils/validate'
import { getArrayKey } from '/@/utils/common'
import { useI18n } from 'vue-i18n'
import { reloadServer } from '/@/utils/vite'
const { t } = useI18n()
const designWindowRef = ref()
Expand Down Expand Up @@ -978,7 +979,7 @@ const startGenerate = () => {
setTimeout(() => {
// 要求 Vite 服务端重启
if (import.meta.hot) {
import.meta.hot.send('custom:reload-hot', { type: 'crud' })
reloadServer('crud')
} else {
ElNotification({
type: 'error',
Expand Down
13 changes: 9 additions & 4 deletions web/src/views/backend/crud/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,31 @@
</template>

<script setup lang="ts">
import { onActivated, onDeactivated, onUnmounted } from 'vue'
import { onActivated, onDeactivated, onUnmounted, onMounted } from 'vue'
import Start from '/@/views/backend/crud/start.vue'
import Design from '/@/views/backend/crud/design.vue'
import { state } from '/@/views/backend/crud/index'
import { closeHotUpdate, openHotUpdate } from '/@/utils/vite'
defineOptions({
name: 'crud/crud',
components: { Start, Design },
})
onMounted(() => {
closeHotUpdate('crud')
})
onActivated(() => {
if (import.meta.hot) import.meta.hot.send('custom:close-hot', { type: 'crud' })
closeHotUpdate('crud')
})
onDeactivated(() => {
if (import.meta.hot) import.meta.hot.send('custom:open-hot', { type: 'crud' })
openHotUpdate('crud')
})
onUnmounted(() => {
if (import.meta.hot) import.meta.hot.send('custom:open-hot', { type: 'crud' })
openHotUpdate('crud')
})
</script>

Expand Down
9 changes: 5 additions & 4 deletions web/src/views/backend/module/components/commonDone.vue
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ import { taskStatus } from '/@/components/terminal/constant'
import { ElMessageBox } from 'element-plus'
import { useI18n } from 'vue-i18n'
import { dependentInstallComplete } from '/@/api/backend/module'
import { reloadServer } from '/@/utils/vite'
const { t } = useI18n()
const terminal = useTerminal()
Expand All @@ -119,13 +120,13 @@ const onSubmitInstallDone = () => {
terminal.addTaskPM('web-build', false, '', (res: number) => {
if (res == taskStatus.Success) {
terminal.toggle(false)
if (import.meta.hot && state.common.moduleState != moduleInstallState.DISABLE) {
import.meta.hot.send('custom:reload-hot', { type: 'modules' })
if (state.common.moduleState != moduleInstallState.DISABLE) {
reloadServer('modules')
}
}
})
} else if (import.meta.hot && state.common.moduleState != moduleInstallState.DISABLE) {
import.meta.hot.send('custom:reload-hot', { type: 'modules' })
} else if (state.common.moduleState != moduleInstallState.DISABLE) {
reloadServer('modules')
}
}
Expand Down
9 changes: 5 additions & 4 deletions web/src/views/backend/module/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,24 @@ import BaAccount from './components/baAccount.vue'
import Tabs from './components/tabs.vue'
import GoodsInfo from './components/goodsInfo.vue'
import CommonDialog from './components/commonDialog.vue'
import { closeHotUpdate, openHotUpdate } from '/@/utils/vite'
defineOptions({
name: 'moduleStore/moduleStore',
})
onMounted(() => {
loadData()
if (import.meta.hot) import.meta.hot.send('custom:close-hot', { type: 'modules' })
closeHotUpdate('modules')
})
onActivated(() => {
if (import.meta.hot) import.meta.hot.send('custom:close-hot', { type: 'modules' })
closeHotUpdate('modules')
})
onDeactivated(() => {
if (import.meta.hot) import.meta.hot.send('custom:open-hot', { type: 'modules' })
openHotUpdate('modules')
})
onUnmounted(() => {
if (import.meta.hot) import.meta.hot.send('custom:open-hot', { type: 'modules' })
openHotUpdate('modules')
})
</script>

Expand Down
9 changes: 5 additions & 4 deletions web/src/views/backend/routine/config/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ import { useSiteConfig } from '/@/stores/siteConfig'
import type { SiteConfig } from '/@/stores/interface'
import { useI18n } from 'vue-i18n'
import { uuid } from '/@/utils/random'
import { closeHotUpdate, openHotUpdate } from '/@/utils/vite'
defineOptions({
name: 'routine/config',
Expand Down Expand Up @@ -277,16 +278,16 @@ const onTestSendMail = () => {
onMounted(() => {
getIndex()
if (import.meta.hot) import.meta.hot.send('custom:close-hot', { type: 'config' })
closeHotUpdate('config')
})
onActivated(() => {
if (import.meta.hot) import.meta.hot.send('custom:close-hot', { type: 'config' })
closeHotUpdate('config')
})
onDeactivated(() => {
if (import.meta.hot) import.meta.hot.send('custom:open-hot', { type: 'config' })
openHotUpdate('config')
})
onUnmounted(() => {
if (import.meta.hot) import.meta.hot.send('custom:open-hot', { type: 'config' })
openHotUpdate('config')
})
</script>

Expand Down

0 comments on commit e5e77b4

Please sign in to comment.