From b79ba291dd5856852a86bc73a57f72ae61c862fc Mon Sep 17 00:00:00 2001 From: Ming Date: Wed, 13 Dec 2023 11:34:40 +0800 Subject: [PATCH] fix: temp routes file should use alias intead of absolute path (#5065) --- .changeset/chilled-pigs-turn.md | 6 +++ .../app-tools/src/analyze/nestedRoutes.ts | 49 +++++++++++-------- .../__snapshots__/nestedRoutes.test.ts.snap | 32 ++++++------ 3 files changed, 51 insertions(+), 36 deletions(-) create mode 100644 .changeset/chilled-pigs-turn.md diff --git a/.changeset/chilled-pigs-turn.md b/.changeset/chilled-pigs-turn.md new file mode 100644 index 000000000000..8f99ff0362d5 --- /dev/null +++ b/.changeset/chilled-pigs-turn.md @@ -0,0 +1,6 @@ +--- +'@modern-js/app-tools': patch +--- + +fix: temp routes file should use alias intead of absolute path +fix: 临时路由文件应该使用 alias 代替路径 diff --git a/packages/solutions/app-tools/src/analyze/nestedRoutes.ts b/packages/solutions/app-tools/src/analyze/nestedRoutes.ts index 7765bd0172aa..af7381231095 100644 --- a/packages/solutions/app-tools/src/analyze/nestedRoutes.ts +++ b/packages/solutions/app-tools/src/analyze/nestedRoutes.ts @@ -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); @@ -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, @@ -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, @@ -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; } } diff --git a/packages/solutions/app-tools/tests/analyze/__snapshots__/nestedRoutes.test.ts.snap b/packages/solutions/app-tools/tests/analyze/__snapshots__/nestedRoutes.test.ts.snap index 036599af60fb..b441f5003e33 100644 --- a/packages/solutions/app-tools/tests/analyze/__snapshots__/nestedRoutes.test.ts.snap +++ b/packages/solutions/app-tools/tests/analyze/__snapshots__/nestedRoutes.test.ts.snap @@ -31,10 +31,10 @@ exports[`nested routes walk 1`] = ` "type": "nested", }, ], - "config": "/tests/analyze/fixtures/nested-routes/__auth/layout.config.ts", + "config": "@_modern_js_src/__auth/layout.config.ts", "id": "__auth/layout", "isRoot": false, - "loader": "/tests/analyze/fixtures/nested-routes/__auth/layout.loader.tsx", + "loader": "@_modern_js_src/__auth/layout.loader.tsx", "type": "nested", }, { @@ -42,10 +42,10 @@ exports[`nested routes walk 1`] = ` "children": [ { "_component": "@_modern_js_src/user/$.tsx", - "action": "/tests/analyze/fixtures/nested-routes/user/$.data.ts", - "clientData": "/tests/analyze/fixtures/nested-routes/user/$.data.client.ts", - "config": "/tests/analyze/fixtures/nested-routes/user/$.config.ts", - "data": "/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", @@ -53,7 +53,7 @@ exports[`nested routes walk 1`] = ` { "_component": "@_modern_js_src/user/[id]/page.tsx", "children": undefined, - "data": "/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", @@ -61,10 +61,10 @@ exports[`nested routes walk 1`] = ` { "_component": "@_modern_js_src/user/page.tsx", "children": undefined, - "config": "/tests/analyze/fixtures/nested-routes/user/page.config.ts", + "config": "@_modern_js_src/user/page.config.ts", "id": "user/page", "index": true, - "loader": "/tests/analyze/fixtures/nested-routes/user/page.loader.ts", + "loader": "@_modern_js_src/user/page.loader.ts", "type": "nested", }, { @@ -75,7 +75,7 @@ exports[`nested routes walk 1`] = ` "children": undefined, "id": "user/profile/page", "index": true, - "loader": "/tests/analyze/fixtures/nested-routes/user/profile/page.loader.ts", + "loader": "@_modern_js_src/user/profile/page.loader.ts", "type": "nested", }, ], @@ -85,10 +85,10 @@ exports[`nested routes walk 1`] = ` "type": "nested", }, ], - "config": "/tests/analyze/fixtures/nested-routes/user/layout.config.ts", + "config": "@_modern_js_src/user/layout.config.ts", "id": "user/layout", "isRoot": false, - "loader": "/tests/analyze/fixtures/nested-routes/user/layout.loader.ts", + "loader": "@_modern_js_src/user/layout.loader.ts", "path": "user", "type": "nested", }, @@ -98,23 +98,23 @@ exports[`nested routes walk 1`] = ` { "_component": "@_modern_js_src/user.profile.name/page.tsx", "children": undefined, - "config": "/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": "/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": "/tests/analyze/fixtures/nested-routes/layout.config.ts", + "config": "@_modern_js_src/layout.config.ts", "id": "layout", "isRoot": true, - "loader": "/tests/analyze/fixtures/nested-routes/layout.loader.ts", + "loader": "@_modern_js_src/layout.loader.ts", "path": "/", "type": "nested", },