Skip to content

Commit 07f2777

Browse files
committed
perf: avoid double file read
Ref #292
1 parent 2794bc8 commit 07f2777

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

src/core/context.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,11 @@ export function createRoutesContext(options: ResolvedOptions) {
105105
const content = await fs.readFile(filePath, 'utf8')
106106
// TODO: cache the result of parsing the SFC so the transform can reuse the parsing
107107
node.hasDefinePage ||= content.includes('definePage')
108-
const [definedPageNameAndPath, routeBlock] = await Promise.all([
109-
extractDefinePageNameAndPath(content, filePath),
110-
getRouteBlock(filePath, options),
111-
])
108+
const definedPageNameAndPath = extractDefinePageNameAndPath(
109+
content,
110+
filePath
111+
)
112+
const routeBlock = getRouteBlock(filePath, content, options)
112113
// TODO: should warn if hasDefinePage and customRouteBlock
113114
// if (routeBlock) logger.log(routeBlock)
114115
node.setCustomRouteBlock(filePath, {

src/core/customBlock.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import { SFCBlock, parse } from '@vue/compiler-sfc'
2-
import fs from 'node:fs/promises'
32
import { ResolvedOptions } from '../options'
43
import JSON5 from 'json5'
54
import { parse as YAMLParser } from 'yaml'
65
import { RouteRecordRaw } from 'vue-router'
76
import { warn } from './utils'
87

9-
export async function getRouteBlock(path: string, options: ResolvedOptions) {
10-
const content = await fs.readFile(path, 'utf8')
11-
12-
const parsedSFC = await parse(content, { pad: 'space' }).descriptor
8+
export function getRouteBlock(
9+
path: string,
10+
content: string,
11+
options: ResolvedOptions
12+
) {
13+
const parsedSFC = parse(content, { pad: 'space' }).descriptor
1314
const blockStr = parsedSFC?.customBlocks.find((b) => b.type === 'route')
1415

1516
if (!blockStr) return

0 commit comments

Comments
 (0)