Skip to content

Commit 19af9dd

Browse files
committed
feat: 为 H5 实现初始化 pages.json 的能力
1 parent b4dfdcc commit 19af9dd

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

packages/core/src/constant.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ export const RESOLVED_MODULE_ID_VIRTUAL = `\0${MODULE_ID_VIRTUAL}`
44
export const OUTPUT_NAME = 'pages.json'
55

66
export const FILE_EXTENSIONS = ['vue', 'nvue', 'uvue']
7+
export const EMPTY_PAGES_JSON_CONTENTS = '{ "pages": [{ "path": "" }] }'

packages/core/src/files.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import type { ResolvedOptions } from './types'
22
import fs from 'node:fs'
33
import 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'
67
import { 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+
}

packages/core/src/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { Plugin } from 'vite'
33
import type { UserOptions } from './types'
44
import path from 'node:path'
55
import process from 'node:process'
6+
import { isH5 } from '@uni-helper/uni-env'
67
import { babelParse } from 'ast-kit'
78
import chokidar from 'chokidar'
89
import { bold, dim, lightYellow, link } from 'kolorist'
@@ -15,7 +16,7 @@ import {
1516
RESOLVED_MODULE_ID_VIRTUAL,
1617
} from './constant'
1718
import { PageContext } from './context'
18-
import { checkPagesJsonFile } from './files'
19+
import { setupPagesJsonFile } from './files'
1920
import { findMacro, parseSFC } from './page'
2021

2122
export * from './config'
@@ -37,7 +38,9 @@ export async function VitePluginUniPages(userOptions: UserOptions = {}): Promise
3738
userOptions.outDir ?? 'src',
3839
OUTPUT_NAME,
3940
)
40-
await checkPagesJsonFile(resolvedPagesJSONPath)
41+
if (isH5) {
42+
setupPagesJsonFile(resolvedPagesJSONPath)
43+
}
4144

4245
return {
4346
name: 'vite-plugin-uni-pages',

0 commit comments

Comments
 (0)