|
| 1 | +import { readdirSync, statSync, readFileSync } from 'fs'; |
| 2 | +import { join, extname, basename, relative } from 'path'; |
| 3 | +import { winPath, findJS } from 'umi-utils'; |
| 4 | +import assert from 'assert'; |
| 5 | + |
| 6 | +const JS_EXTNAMES = ['.md']; |
| 7 | + |
| 8 | +export function sortRoutes(routes) { |
| 9 | + const paramsRoutes = []; |
| 10 | + const exactRoutes = []; |
| 11 | + const layoutRoutes = []; |
| 12 | + |
| 13 | + routes.forEach(route => { |
| 14 | + const { _isParamsRoute, exact } = route; |
| 15 | + if (_isParamsRoute) { |
| 16 | + paramsRoutes.push(route); |
| 17 | + } else if (exact) { |
| 18 | + exactRoutes.push(route); |
| 19 | + } else { |
| 20 | + layoutRoutes.push(route); |
| 21 | + } |
| 22 | + }); |
| 23 | + |
| 24 | + assert(paramsRoutes.length <= 1, `We should not have multiple dynamic routes under a directory.`); |
| 25 | + |
| 26 | + return [...exactRoutes, ...layoutRoutes, ...paramsRoutes].reduce((memo, route) => { |
| 27 | + if (route._toMerge) { |
| 28 | + memo = memo.concat(route.routes); |
| 29 | + } else { |
| 30 | + delete route._isParamsRoute; |
| 31 | + memo.push(route); |
| 32 | + } |
| 33 | + return memo; |
| 34 | + }, []); |
| 35 | +} |
| 36 | + |
| 37 | +export default function getRouteConfigFromDir(paths) { |
| 38 | + const { cwd, absPagesPath, absSrcPath, dirPath = '' } = paths; |
| 39 | + const absPath = join(absPagesPath, dirPath); |
| 40 | + const files = readdirSync(absPath); |
| 41 | + |
| 42 | + const absLayoutFile = findJS(absPagesPath, '_layout'); |
| 43 | + if (absLayoutFile) { |
| 44 | + throw new Error('root _layout.js is not supported, use layouts/index.js instead'); |
| 45 | + } |
| 46 | + |
| 47 | + const routes = sortRoutes( |
| 48 | + files |
| 49 | + .filter(file => { |
| 50 | + if ( |
| 51 | + file.charAt(0) === '.' || |
| 52 | + file.charAt(0) === '_' || |
| 53 | + /\.(test|spec)\.(j|t)sx?$/.test(file) |
| 54 | + ) { |
| 55 | + return false; |
| 56 | + } |
| 57 | + return true; |
| 58 | + }) |
| 59 | + .reduce(handleFile.bind(null, paths, absPath), []), |
| 60 | + ); |
| 61 | + |
| 62 | + if (dirPath === '' && absSrcPath) { |
| 63 | + const globalLayoutFile = |
| 64 | + findJS(absSrcPath, 'layouts/index') || findJS(absSrcPath, 'layout/index'); |
| 65 | + if (globalLayoutFile) { |
| 66 | + const wrappedRoutes = []; |
| 67 | + addRoute( |
| 68 | + wrappedRoutes, |
| 69 | + { |
| 70 | + path: '/', |
| 71 | + component: `./${winPath(relative(cwd, globalLayoutFile))}`, |
| 72 | + routes, |
| 73 | + }, |
| 74 | + { |
| 75 | + componentFile: globalLayoutFile, |
| 76 | + }, |
| 77 | + ); |
| 78 | + return wrappedRoutes; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + return routes; |
| 83 | +} |
| 84 | + |
| 85 | +function handleFile(paths, absPath, memo, file) { |
| 86 | + const { cwd, absPagesPath, dirPath = '' } = paths; |
| 87 | + const absFilePath = join(absPath, file); |
| 88 | + const stats = statSync(absFilePath); |
| 89 | + const isParamsRoute = file.charAt(0) === '$'; |
| 90 | + |
| 91 | + if (stats.isDirectory()) { |
| 92 | + const newDirPath = join(dirPath, file); |
| 93 | + // routes & _layout |
| 94 | + const routes = getRouteConfigFromDir({ |
| 95 | + ...paths, |
| 96 | + dirPath: newDirPath, |
| 97 | + }); |
| 98 | + const absLayoutFile = findJS(join(absPagesPath, newDirPath), '_layout'); |
| 99 | + if (absLayoutFile) { |
| 100 | + addRoute( |
| 101 | + memo, |
| 102 | + { |
| 103 | + path: normalizePath(newDirPath), |
| 104 | + exact: false, |
| 105 | + component: `./${winPath(relative(cwd, absLayoutFile))}`, |
| 106 | + routes, |
| 107 | + _isParamsRoute: isParamsRoute, |
| 108 | + }, |
| 109 | + { |
| 110 | + componentFile: absLayoutFile, |
| 111 | + }, |
| 112 | + ); |
| 113 | + } else { |
| 114 | + memo.push({ |
| 115 | + _toMerge: true, |
| 116 | + path: normalizePath(newDirPath), |
| 117 | + exact: true, |
| 118 | + _isParamsRoute: isParamsRoute, |
| 119 | + routes, |
| 120 | + }); |
| 121 | + } |
| 122 | + } else if (stats.isFile() && isValidJS(file)) { |
| 123 | + const bName = basename(file, extname(file)); |
| 124 | + const path = normalizePath(join(dirPath, bName)); |
| 125 | + addRoute( |
| 126 | + memo, |
| 127 | + { |
| 128 | + path, |
| 129 | + exact: true, |
| 130 | + component: `./${winPath(relative(cwd, absFilePath))}`, |
| 131 | + _isParamsRoute: isParamsRoute, |
| 132 | + }, |
| 133 | + { |
| 134 | + componentFile: absFilePath, |
| 135 | + }, |
| 136 | + ); |
| 137 | + } |
| 138 | + |
| 139 | + return memo; |
| 140 | +} |
| 141 | + |
| 142 | +function normalizePath(path) { |
| 143 | + let newPath = `/${winPath(path) |
| 144 | + .split('/') |
| 145 | + .map(_ => _.replace(/^\$/, ':').replace(/\$$/, '?')) |
| 146 | + .join('/')}`; |
| 147 | + |
| 148 | + // /index/index -> / |
| 149 | + if (newPath === '/index/index') { |
| 150 | + newPath = '/'; |
| 151 | + } |
| 152 | + // /xxxx/index -> /xxxx/ |
| 153 | + newPath = newPath.replace(/\/index$/, '/'); |
| 154 | + |
| 155 | + // remove the last slash |
| 156 | + // e.g. /abc/ -> /abc |
| 157 | + if (newPath !== '/' && newPath.slice(-1) === '/') { |
| 158 | + newPath = newPath.slice(0, -1); |
| 159 | + } |
| 160 | + |
| 161 | + return newPath; |
| 162 | +} |
| 163 | + |
| 164 | +function addRoute(memo, route, { componentFile }) { |
| 165 | + memo.push({ |
| 166 | + ...route, |
| 167 | + }); |
| 168 | +} |
| 169 | + |
| 170 | +function isValidJS(file) { |
| 171 | + return JS_EXTNAMES.includes(extname(file)); |
| 172 | +} |
0 commit comments