@@ -2,7 +2,8 @@ import type { ResolvedOptions } from './types'
22import fs from 'node:fs'
33import fg from 'fast-glob'
44
5- import { FILE_EXTENSIONS } from './constant'
5+ import { normalizePath } from 'vite'
6+ import { EMPTY_PAGES_JSON_CONTENTS , FILE_EXTENSIONS } from './constant'
67import { extsToGlob } from './utils'
78
89/**
@@ -27,9 +28,9 @@ export function getPageFiles(path: string, options: ResolvedOptions): string[] {
2728 * @param path - 要检查的文件路径
2829 * @returns Promise<void> - 无返回值的异步函数
2930 */
30- export async function checkPagesJsonFile ( path : fs . PathLike ) : Promise < boolean > {
31+ export async function checkPagesJsonFile ( path : fs . PathLike , contents : string = EMPTY_PAGES_JSON_CONTENTS ) : Promise < boolean > {
3132 const createEmptyFile = ( path : fs . PathLike ) => {
32- return fs . promises . writeFile ( path , JSON . stringify ( { pages : [ { path : '' } ] } , null , 2 ) , { encoding : 'utf-8' } ) . then ( ( ) => true ) . catch ( ( ) => false )
33+ return fs . promises . writeFile ( path , contents , { encoding : 'utf-8' } ) . then ( ( ) => true ) . catch ( ( ) => false )
3334 }
3435
3536 const unlink = ( path : fs . PathLike ) => {
@@ -65,3 +66,18 @@ export async function checkPagesJsonFile(path: fs.PathLike): Promise<boolean> {
6566 return createEmptyFile ( path ) // 创建空文件
6667 }
6768}
69+
70+ export function setupPagesJsonFile ( path : string ) {
71+ const _readFileSync = fs . readFileSync
72+ fs . readFileSync = new Proxy ( fs . readFileSync , {
73+ apply ( target , thisArg , argArray ) {
74+ if ( typeof argArray [ 0 ] === 'string' && normalizePath ( argArray [ 0 ] ) === normalizePath ( path ) ) {
75+ checkPagesJsonFile ( path ) . then ( ( ) => {
76+ fs . readFileSync = _readFileSync
77+ } )
78+ return EMPTY_PAGES_JSON_CONTENTS
79+ }
80+ return Reflect . apply ( target , thisArg , argArray )
81+ } ,
82+ } )
83+ }
0 commit comments