diff --git a/src/context.ts b/src/context.ts index ab7eaf9..1195dcd 100644 --- a/src/context.ts +++ b/src/context.ts @@ -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' @@ -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 }) @@ -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"') } diff --git a/src/index.ts b/src/index.ts index 3f97820..f02b8b2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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' @@ -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)