Skip to content

Commit

Permalink
fix: 运行时新layout
Browse files Browse the repository at this point in the history
  • Loading branch information
markthree committed Feb 9, 2022
1 parent bbb8777 commit 8e4e215
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
21 changes: 21 additions & 0 deletions presets/shared/global.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
declare global {
/**
* dev Server 第一次启动标识
*/
var $devServerFirstStarted: boolean
}

setImmediate(() => {
global.$devServerFirstStarted = true
})

/**
* 仅第一次启动服务时允许
*/
export const runOnDevServerFirstStart = (
callback: Function
) => {
if (!global.$devServerFirstStarted) {
callback()
}
}
21 changes: 21 additions & 0 deletions presets/shared/watcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { watch } from 'chokidar'
import { runOnDevServerFirstStart } from './global'
import { utimesSync, openSync, closeSync } from 'fs'

runOnDevServerFirstStart(() => {
const restart = () => {
const time = new Date()
const path = 'vite.config.ts'
try {
utimesSync(path, time, time)
} catch (error) {
closeSync(openSync(path, 'w'))
}
}

const layoutsWatcher = watch('./src/layouts', {
ignoreInitial: true
})

layoutsWatcher.on('add', restart)
})
1 change: 1 addition & 0 deletions presets/tov.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import './shared/watcher'
import { resolve } from 'path'
import Pages from 'vite-plugin-pages'
import Icons from 'unplugin-icons/vite'
Expand Down
1 change: 0 additions & 1 deletion presets/types/auto-imports.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ declare global {
const computedInject: typeof import('@vueuse/core')['computedInject']
const controlledComputed: typeof import('@vueuse/core')['controlledComputed']
const controlledRef: typeof import('@vueuse/core')['controlledRef']
const counterStore: typeof import('/@fs/src/stores/counterStore')['default']
const createApp: typeof import('vue')['createApp']
const createEventHook: typeof import('@vueuse/core')['createEventHook']
const createGlobalState: typeof import('@vueuse/core')['createGlobalState']
Expand Down

0 comments on commit 8e4e215

Please sign in to comment.