-
Notifications
You must be signed in to change notification settings - Fork 287
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reimplemented virtual routes to build paths without node libraries.
- Loading branch information
1 parent
e6ded23
commit e19ab47
Showing
2 changed files
with
47 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,57 @@ | ||
import {fileURLToPath} from 'node:url'; | ||
import path from 'node:path'; | ||
import {readdir} from 'node:fs/promises'; | ||
|
||
export const VIRTUAL_ROUTES_DIR = 'vite/virtual-routes/routes'; | ||
export const VIRTUAL_ROUTES_ROUTES_DIR_PARTS = [ | ||
'vite', | ||
'virtual-routes', | ||
'routes', | ||
]; | ||
export const VIRTUAL_ROUTES_DIR_PARTS = ['vite', 'virtual-routes']; | ||
export const VIRTUAL_ROOT = 'vite/virtual-routes/virtual-root'; | ||
|
||
export async function getVirtualRoutes() { | ||
const distPath = path.dirname(path.dirname(fileURLToPath(import.meta.url))); | ||
const virtualRoutesPath = path.join(distPath, VIRTUAL_ROUTES_DIR); | ||
|
||
const routes = await readdir(virtualRoutesPath, {recursive: true}).then( | ||
(files) => | ||
files.map((relativeFilePath) => { | ||
const absoluteFilePath = path.join(virtualRoutesPath, relativeFilePath); | ||
const id = relativeFilePath | ||
.replace(/\.[jt]sx?$/, '') | ||
.replaceAll('\\', '/'); | ||
const isIndex = /(^|\/)index$/.test(id); | ||
const routePath = id.replace(/(^|\/)index$/, ''); | ||
function getVirtualRoutesPath( | ||
pathParts: Array<string>, | ||
forFile: string, | ||
): string { | ||
const basePath = new URL('../', import.meta.url); | ||
const virtualRoutesPath = pathParts.reduce((working, dirPart) => { | ||
return new URL(`${dirPart}/`, working); | ||
}, basePath); | ||
return new URL(forFile, virtualRoutesPath).pathname; | ||
} | ||
|
||
return { | ||
id: `${VIRTUAL_ROUTES_DIR}/${id}`, | ||
path: routePath, | ||
file: absoluteFilePath, | ||
index: isIndex, | ||
}; | ||
}), | ||
); | ||
export async function getVirtualRoutes() { | ||
const routes = [ | ||
{ | ||
id: `${VIRTUAL_ROUTES_DIR}/graphiql`, | ||
path: 'graphiql', | ||
file: getVirtualRoutesPath( | ||
VIRTUAL_ROUTES_ROUTES_DIR_PARTS, | ||
'graphiql.jsx', | ||
), | ||
index: false, | ||
}, | ||
{ | ||
id: `${VIRTUAL_ROUTES_DIR}/subrequest-profiler`, | ||
path: 'subrequest-profiler', | ||
file: getVirtualRoutesPath( | ||
VIRTUAL_ROUTES_ROUTES_DIR_PARTS, | ||
'subrequest-profiler.jsx', | ||
), | ||
index: false, | ||
}, | ||
{ | ||
id: `${VIRTUAL_ROUTES_DIR}/index`, | ||
path: '', | ||
file: getVirtualRoutesPath(VIRTUAL_ROUTES_ROUTES_DIR_PARTS, 'index.jsx'), | ||
index: true, | ||
}, | ||
]; | ||
|
||
return { | ||
routes, | ||
root: { | ||
id: VIRTUAL_ROOT, | ||
path: '', | ||
file: path.join(distPath, VIRTUAL_ROOT + '.jsx'), | ||
file: getVirtualRoutesPath(VIRTUAL_ROUTES_DIR_PARTS, 'virtual-root.jsx'), | ||
}, | ||
}; | ||
} |