Skip to content

Commit

Permalink
perf: avoid double file read
Browse files Browse the repository at this point in the history
Ref #292
  • Loading branch information
posva committed Jun 18, 2024
1 parent 2794bc8 commit 07f2777
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
9 changes: 5 additions & 4 deletions src/core/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,11 @@ export function createRoutesContext(options: ResolvedOptions) {
const content = await fs.readFile(filePath, 'utf8')
// TODO: cache the result of parsing the SFC so the transform can reuse the parsing
node.hasDefinePage ||= content.includes('definePage')
const [definedPageNameAndPath, routeBlock] = await Promise.all([
extractDefinePageNameAndPath(content, filePath),
getRouteBlock(filePath, options),
])
const definedPageNameAndPath = extractDefinePageNameAndPath(
content,
filePath
)
const routeBlock = getRouteBlock(filePath, content, options)
// TODO: should warn if hasDefinePage and customRouteBlock
// if (routeBlock) logger.log(routeBlock)
node.setCustomRouteBlock(filePath, {
Expand Down
11 changes: 6 additions & 5 deletions src/core/customBlock.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { SFCBlock, parse } from '@vue/compiler-sfc'
import fs from 'node:fs/promises'
import { ResolvedOptions } from '../options'
import JSON5 from 'json5'
import { parse as YAMLParser } from 'yaml'
import { RouteRecordRaw } from 'vue-router'
import { warn } from './utils'

export async function getRouteBlock(path: string, options: ResolvedOptions) {
const content = await fs.readFile(path, 'utf8')

const parsedSFC = await parse(content, { pad: 'space' }).descriptor
export function getRouteBlock(
path: string,
content: string,
options: ResolvedOptions
) {
const parsedSFC = parse(content, { pad: 'space' }).descriptor
const blockStr = parsedSFC?.customBlocks.find((b) => b.type === 'route')

if (!blockStr) return
Expand Down

0 comments on commit 07f2777

Please sign in to comment.