Skip to content

Commit

Permalink
fix: pages.json watcher
Browse files Browse the repository at this point in the history
fixed #23
  • Loading branch information
KeJunMao committed Dec 11, 2023
1 parent e27022d commit 2282e78
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
14 changes: 5 additions & 9 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { isMp } from '@uni-helper/uni-env'
import type { AttributeNode, DirectiveNode, ElementNode, SimpleExpressionNode } from '@vue/compiler-core'
import { babelParse, walkAST } from 'ast-kit'
import MagicString from 'magic-string'
import { resolve } from 'node:path'
import { kebabCase } from 'scule'
import type { FSWatcher, ResolvedConfig, ViteDevServer } from 'vite'
import { normalizePath } from 'vite'
Expand Down Expand Up @@ -33,10 +32,7 @@ export class Context {

async setupWatcher(watcher: FSWatcher) {
watcher.on('change', async (path) => {
if (
normalizePath(path)
=== normalizePath(resolve(this.options.cwd, 'src/pages.json'))
)
if (path.includes('pages.json'))
this.pages = loadPagesJson('src/pages.json', this.options.cwd)
// TODO: auto reload
})
Expand Down Expand Up @@ -89,11 +85,11 @@ export class Context {
enter(node) {
if (node.type === 'VariableDeclarator') {
const hasUniLayoutVar
= node.id.type === 'Identifier' && node.id.name === 'uniLayout'
= node.id.type === 'Identifier' && node.id.name === 'uniLayout'
const isRef
= node.init?.type === 'CallExpression'
&& node.init.callee.type === 'Identifier'
&& node.init.callee.name === 'ref'
= node.init?.type === 'CallExpression'
&& node.init.callee.type === 'Identifier'
&& node.init.callee.name === 'ref'
if (hasUniLayoutVar && isRef)
pageLayoutProps.push('ref="uniLayout"')
}
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Plugin } from 'vite'
import { createFilter } from 'vite'
import chokidar from 'chokidar'
import { virtualModuleId } from './constant'
import { Context } from './context'
import type { UserOptions } from './types'
Expand All @@ -13,6 +14,8 @@ export function VitePluginUniLayouts(userOptions: UserOptions = {}): Plugin {
enforce: 'pre',
configResolved(config) {
ctx.config = config
if (config.build.watch)
ctx.setupWatcher(chokidar.watch(['src/pages.json', 'pages.json']))
},
configureServer(server) {
ctx.setupViteServer(server)
Expand Down

0 comments on commit 2282e78

Please sign in to comment.