forked from QwikDev/qwik
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: qwik-city layout hierarchy top (QwikDev#809)
- Loading branch information
1 parent
4bb293d
commit e31f1a9
Showing
37 changed files
with
446 additions
and
383 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
87 changes: 87 additions & 0 deletions
87
packages/qwik-city/buildtime/routing/resolve-source-file.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { dirname } from 'path'; | ||
import type { | ||
BuildLayout, | ||
BuildRoute, | ||
NormalizedPluginOptions, | ||
ParsedLayoutId, | ||
RouteSourceFile, | ||
} from '../types'; | ||
import { createFileId, isLayoutName, normalizePath, parseLayoutId } from '../utils/fs'; | ||
import { getPathnameFromFilePath } from '../utils/pathname'; | ||
import { parseRoutePathname } from './parse-pathname'; | ||
|
||
export function resolveLayout(routesDir: string, layoutSourceFile: RouteSourceFile) { | ||
const dirName = layoutSourceFile.dirName; | ||
const filePath = layoutSourceFile.filePath; | ||
let dirPath = layoutSourceFile.dirPath; | ||
|
||
let layoutId: ParsedLayoutId; | ||
|
||
if (isLayoutName(dirName)) { | ||
dirPath = normalizePath(dirname(dirPath)); | ||
layoutId = parseLayoutId(dirName); | ||
} else { | ||
layoutId = parseLayoutId(layoutSourceFile.fileName); | ||
} | ||
|
||
const layout: BuildLayout = { | ||
id: createFileId(routesDir, filePath), | ||
filePath, | ||
dirPath, | ||
...layoutId, | ||
}; | ||
|
||
return layout; | ||
} | ||
|
||
export function resolveRoute( | ||
opts: NormalizedPluginOptions, | ||
appLayouts: BuildLayout[], | ||
sourceFile: RouteSourceFile | ||
) { | ||
const filePath = sourceFile.filePath; | ||
const layouts: BuildLayout[] = []; | ||
const routesDir = opts.routesDir; | ||
const { pathname, layoutName } = getPathnameFromFilePath(opts, filePath); | ||
const hasNamedLayout = layoutName !== ''; | ||
|
||
let currentDir = normalizePath(dirname(filePath)); | ||
let hasFoundNamedLayout = false; | ||
|
||
for (let i = 0; i < 20; i++) { | ||
let layout: BuildLayout | undefined = undefined; | ||
|
||
if (hasNamedLayout && !hasFoundNamedLayout) { | ||
layout = appLayouts.find((l) => l.dirPath === currentDir && l.layoutName === layoutName); | ||
if (layout) { | ||
hasFoundNamedLayout = true; | ||
} | ||
} else { | ||
layout = appLayouts.find((l) => l.dirPath === currentDir && l.layoutName === ''); | ||
} | ||
|
||
if (layout) { | ||
layouts.push(layout); | ||
if (layout.layoutType === 'top') { | ||
break; | ||
} | ||
} | ||
|
||
if (currentDir === routesDir) { | ||
break; | ||
} | ||
|
||
currentDir = normalizePath(dirname(currentDir)); | ||
} | ||
|
||
const buildRoute: BuildRoute = { | ||
type: sourceFile.type as any, | ||
id: createFileId(routesDir, filePath), | ||
filePath, | ||
pathname, | ||
layouts: layouts.reverse(), | ||
...parseRoutePathname(pathname), | ||
}; | ||
|
||
return buildRoute; | ||
} |
Oops, something went wrong.