Skip to content

Commit

Permalink
feat: support define route with absolute path (#6467)
Browse files Browse the repository at this point in the history
* feat: support define route with absolute path

* fix: test case
  • Loading branch information
ClarkXia authored Aug 21, 2023
1 parent 669a801 commit 3d72131
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 8 deletions.
6 changes: 6 additions & 0 deletions .changeset/famous-bottles-lick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@ice/route-manifest': patch
'@ice/app': patch
---

feat: support define route with absolute path
9 changes: 6 additions & 3 deletions packages/ice/src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export async function generateRoutesInfo(
// add exports filed for route manifest
routeItem.exports = await getFileExports({
rootDir,
file: formatPath(path.join('./src/pages', routeItem.file)),
file: formatPath(path.isAbsolute(routeItem.file) ? routeItem.file : path.join('./src/pages', routeItem.file)),
});
});
await Promise.all(analyzeTasks);
Expand Down Expand Up @@ -55,12 +55,15 @@ export default {
};
}

function getFilePath(file: string) {
return formatPath(path.isAbsolute(file) ? file : `@/pages/${file}`.replace(new RegExp(`${path.extname(file)}$`), ''));
}

export function getRoutesDefinition(nestRouteManifest: NestedRouteManifest[], lazy = false, depth = 0) {
const routeImports: string[] = [];
const routeDefinition = nestRouteManifest.reduce((prev, route) => {
const { children, path: routePath, index, componentName, file, id, layout, exports } = route;

const componentPath = id.startsWith('__') ? file : `@/pages/${file}`.replace(new RegExp(`${path.extname(file)}$`), '');
const componentPath = id.startsWith('__') ? file : getFilePath(file);

let loadStatement = '';
if (lazy) {
Expand Down
2 changes: 1 addition & 1 deletion packages/ice/src/service/analyze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ type CachedRouteExports = { hash: string; exports: string[] };
// Exports for other plugin to get exports info.
export async function getFileExports(options: FileOptions): Promise<CachedRouteExports['exports']> {
const { rootDir, file } = options;
const filePath = path.join(rootDir, file);
const filePath = path.isAbsolute(file) ? file : path.join(rootDir, file);
let cached: CachedRouteExports | null = null;
try {
cached = await getCache(rootDir, filePath);
Expand Down
3 changes: 2 additions & 1 deletion packages/route-manifest/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export function generateRouteManifest(
defineExtraRoutesQueue.forEach((defineExtraRoutes) => {
if (defineExtraRoutes) {
const extraRoutes = defineRoutes(
rootDir,
defineExtraRoutes,
{
routeManifest,
Expand Down Expand Up @@ -244,7 +245,7 @@ function defineConventionalRoutes(
}
}

return defineRoutes(defineNestedRoutes, options);
return defineRoutes(rootDir, defineNestedRoutes, options);
}

const escapeStart = '[';
Expand Down
11 changes: 8 additions & 3 deletions packages/route-manifest/src/routes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// based on https://github.com/remix-run/remix/blob/main/packages/remix-dev/config/routes.ts

import { win32, join } from 'path';
import { win32, join, isAbsolute, relative, sep } from 'path';

export interface ConfigRoute {
/**
Expand Down Expand Up @@ -92,14 +92,19 @@ export type DefineExtraRoutes = (
options: DefineRoutesOptions,
) => void;


export function formatPath(pathStr: string): string {
return process.platform === 'win32' ? pathStr.split(sep).join('/') : pathStr;
}

export function defineRoutes(
rootDir: string,
callback: (defineRoute: DefineRouteFunction, options: DefineRoutesOptions) => void,
options: DefineRoutesOptions,
) {
const routes: RouteManifest = Object.create(null);
const parentRoutes: ConfigRoute[] = [];
let alreadyReturned = false;

const defineRoute: DefineRouteFunction = (path, file, optionsOrChildren, children) => {
if (alreadyReturned) {
throw new Error(
Expand Down Expand Up @@ -131,7 +136,7 @@ export function defineRoutes(
id,
parentId: parentRoute ? parentRoute.id : undefined,
file,
componentName: createComponentName(file),
componentName: createComponentName(isAbsolute(file) ? formatPath(relative(rootDir, file)) : file),
layout: id.endsWith('layout'),
};

Expand Down
Empty file.
13 changes: 13 additions & 0 deletions packages/route-manifest/tests/generateRouteManifest.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,19 @@ describe('generateRouteManifest function', () => {
expect(routeManifest).toMatchSnapshot();
});

test('define-absolute-route', () => {
const rootDir = path.join(fixturesDir, 'define-absolute-route');
const routeManifest = generateRouteManifest(
rootDir,
['About/index.tsx'],
[(defineRoute) => {
defineRoute('/about-me', path.join(rootDir, 'src/index.tsx'));
}],
);
expect(path.isAbsolute(routeManifest['/about-me'].file)).toBeTruthy();
expect(routeManifest['/about-me'].componentName).toBe('src-index');
});

test('escape-routes', () => {
const routeManifest = generateRouteManifest(
path.join(fixturesDir, 'escape-routes'),
Expand Down

0 comments on commit 3d72131

Please sign in to comment.