Skip to content

Commit

Permalink
fix: temp routes file should use alias intead of absolute path (#5065)
Browse files Browse the repository at this point in the history
  • Loading branch information
yimingjfe authored Dec 13, 2023
1 parent eda89cb commit b79ba29
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 36 deletions.
6 changes: 6 additions & 0 deletions .changeset/chilled-pigs-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@modern-js/app-tools': patch
---

fix: temp routes file should use alias intead of absolute path
fix: 临时路由文件应该使用 alias 代替路径
49 changes: 29 additions & 20 deletions packages/solutions/app-tools/src/analyze/nestedRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@ export const walk = async (

for (const item of items) {
const itemPath = path.join(dirname, item);
const itemPathWithAlias = replaceWithAlias(
alias.basename,
itemPath,
alias.name,
);
const extname = path.extname(item);
const itemWithoutExt = item.slice(0, -extname.length);

Expand Down Expand Up @@ -194,54 +199,54 @@ export const walk = async (

if (itemWithoutExt === NESTED_ROUTE.LAYOUT_LOADER_FILE) {
if (!route.loader) {
route.loader = itemPath;
route.loader = itemPathWithAlias;
}
}

if (itemWithoutExt === NESTED_ROUTE.LAYOUT_CLIENT_LOADER) {
route.clientData = itemPath;
route.clientData = itemPathWithAlias;
}

if (itemWithoutExt === NESTED_ROUTE.LAYOUT_DATA_FILE) {
route.data = itemPath;
route.data = itemPathWithAlias;
if (await hasAction(itemPath)) {
route.action = itemPath;
route.action = itemPathWithAlias;
}
}

if (itemWithoutExt === NESTED_ROUTE.LAYOUT_CONFIG_FILE) {
if (!route.config) {
route.config = itemPath;
route.config = itemPathWithAlias;
}
}

if (itemWithoutExt === NESTED_ROUTE.LAYOUT_FILE) {
route._component = replaceWithAlias(alias.basename, itemPath, alias.name);
route._component = itemPathWithAlias;
}

if (itemWithoutExt === NESTED_ROUTE.PAGE_LOADER_FILE) {
pageLoaderFile = itemPath;
pageLoaderFile = itemPathWithAlias;
}

if (itemWithoutExt === NESTED_ROUTE.PAGE_CLIENT_LOADER) {
pageClientData = itemPath;
pageClientData = itemPathWithAlias;
}

if (itemWithoutExt === NESTED_ROUTE.PAGE_DATA_FILE) {
pageData = itemPath;
pageData = itemPathWithAlias;
if (await hasAction(itemPath)) {
pageAction = itemPath;
pageAction = itemPathWithAlias;
}
}

if (itemWithoutExt === NESTED_ROUTE.PAGE_CONFIG_FILE) {
pageConfigFile = itemPath;
pageConfigFile = itemPathWithAlias;
}

if (itemWithoutExt === NESTED_ROUTE.PAGE_FILE) {
pageRoute = createIndexRoute(
{
_component: replaceWithAlias(alias.basename, itemPath, alias.name),
_component: itemPathWithAlias,
} as NestedRouteForCli,
rootDir,
itemPath,
Expand All @@ -268,30 +273,34 @@ export const walk = async (
}

if (itemWithoutExt === NESTED_ROUTE.SPLATE_LOADER_FILE) {
splatLoaderFile = itemPath;
splatLoaderFile = itemPathWithAlias;
}

if (itemWithoutExt === NESTED_ROUTE.SPLATE_CLIENT_DATA) {
splatClientData = itemPath;
splatClientData = itemPathWithAlias;
}

if (itemWithoutExt === NESTED_ROUTE.SPLATE_CONFIG_FILE) {
if (!route.config) {
splatConfigFile = itemPath;
splatConfigFile = replaceWithAlias(
alias.basename,
itemPath,
alias.name,
);
}
}

if (itemWithoutExt === NESTED_ROUTE.SPLATE_DATA_FILE) {
splatData = itemPath;
splatData = itemPathWithAlias;
if (await hasAction(itemPath)) {
splatAction = itemPath;
splatAction = itemPathWithAlias;
}
}

if (itemWithoutExt === NESTED_ROUTE.SPLATE_FILE) {
splatRoute = createRoute(
{
_component: replaceWithAlias(alias.basename, itemPath, alias.name),
_component: itemPathWithAlias,
path: '*',
},
rootDir,
Expand Down Expand Up @@ -319,11 +328,11 @@ export const walk = async (
}

if (itemWithoutExt === NESTED_ROUTE.LOADING_FILE) {
route.loading = replaceWithAlias(alias.basename, itemPath, alias.name);
route.loading = itemPathWithAlias;
}

if (itemWithoutExt === NESTED_ROUTE.ERROR_FILE) {
route.error = replaceWithAlias(alias.basename, itemPath, alias.name);
route.error = itemPathWithAlias;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,40 +31,40 @@ exports[`nested routes walk 1`] = `
"type": "nested",
},
],
"config": "<ROOT>/tests/analyze/fixtures/nested-routes/__auth/layout.config.ts",
"config": "@_modern_js_src/__auth/layout.config.ts",
"id": "__auth/layout",
"isRoot": false,
"loader": "<ROOT>/tests/analyze/fixtures/nested-routes/__auth/layout.loader.tsx",
"loader": "@_modern_js_src/__auth/layout.loader.tsx",
"type": "nested",
},
{
"_component": "@_modern_js_src/user/layout.tsx",
"children": [
{
"_component": "@_modern_js_src/user/$.tsx",
"action": "<ROOT>/tests/analyze/fixtures/nested-routes/user/$.data.ts",
"clientData": "<ROOT>/tests/analyze/fixtures/nested-routes/user/$.data.client.ts",
"config": "<ROOT>/tests/analyze/fixtures/nested-routes/user/$.config.ts",
"data": "<ROOT>/tests/analyze/fixtures/nested-routes/user/$.data.ts",
"action": "@_modern_js_src/user/$.data.ts",
"clientData": "@_modern_js_src/user/$.data.client.ts",
"config": "@_modern_js_src/user/$.config.ts",
"data": "@_modern_js_src/user/$.data.ts",
"id": "user/$",
"path": "*",
"type": "nested",
},
{
"_component": "@_modern_js_src/user/[id]/page.tsx",
"children": undefined,
"data": "<ROOT>/tests/analyze/fixtures/nested-routes/user/[id]/page.data.ts",
"data": "@_modern_js_src/user/[id]/page.data.ts",
"id": "user/(id)/page",
"path": ":id",
"type": "nested",
},
{
"_component": "@_modern_js_src/user/page.tsx",
"children": undefined,
"config": "<ROOT>/tests/analyze/fixtures/nested-routes/user/page.config.ts",
"config": "@_modern_js_src/user/page.config.ts",
"id": "user/page",
"index": true,
"loader": "<ROOT>/tests/analyze/fixtures/nested-routes/user/page.loader.ts",
"loader": "@_modern_js_src/user/page.loader.ts",
"type": "nested",
},
{
Expand All @@ -75,7 +75,7 @@ exports[`nested routes walk 1`] = `
"children": undefined,
"id": "user/profile/page",
"index": true,
"loader": "<ROOT>/tests/analyze/fixtures/nested-routes/user/profile/page.loader.ts",
"loader": "@_modern_js_src/user/profile/page.loader.ts",
"type": "nested",
},
],
Expand All @@ -85,10 +85,10 @@ exports[`nested routes walk 1`] = `
"type": "nested",
},
],
"config": "<ROOT>/tests/analyze/fixtures/nested-routes/user/layout.config.ts",
"config": "@_modern_js_src/user/layout.config.ts",
"id": "user/layout",
"isRoot": false,
"loader": "<ROOT>/tests/analyze/fixtures/nested-routes/user/layout.loader.ts",
"loader": "@_modern_js_src/user/layout.loader.ts",
"path": "user",
"type": "nested",
},
Expand All @@ -98,23 +98,23 @@ exports[`nested routes walk 1`] = `
{
"_component": "@_modern_js_src/user.profile.name/page.tsx",
"children": undefined,
"config": "<ROOT>/tests/analyze/fixtures/nested-routes/user.profile.name/page.config.ts",
"config": "@_modern_js_src/user.profile.name/page.config.ts",
"id": "user.profile.name/page",
"index": true,
"type": "nested",
},
],
"config": "<ROOT>/tests/analyze/fixtures/nested-routes/user.profile.name/layout.config.ts",
"config": "@_modern_js_src/user.profile.name/layout.config.ts",
"id": "user.profile.name/layout",
"isRoot": false,
"path": "user/profile/name",
"type": "nested",
},
],
"config": "<ROOT>/tests/analyze/fixtures/nested-routes/layout.config.ts",
"config": "@_modern_js_src/layout.config.ts",
"id": "layout",
"isRoot": true,
"loader": "<ROOT>/tests/analyze/fixtures/nested-routes/layout.loader.ts",
"loader": "@_modern_js_src/layout.loader.ts",
"path": "/",
"type": "nested",
},
Expand Down

0 comments on commit b79ba29

Please sign in to comment.