Skip to content

Commit 4f6a283

Browse files
committed
fix(routes): transform path params to valid rollup chunk name
1 parent 65e047d commit 4f6a283

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { useParams } from 'react-router-dom';
2+
3+
export default function UserDetail() {
4+
const { id } = useParams<{ id: string }>();
5+
return <div>user detail page: {id}</div>;
6+
}

packages/routes/src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,13 @@ export function conventionalRoutes(userConfig?: UserConfig): Plugin {
6464
const assetsDir = viteConfig.build.assetsDir;
6565

6666
// By default, "src/pages/a/b/index.js" and "src/pages/c/index.js"
67-
// will have the same chunk name: "index", ant it's a little confusing.
67+
// will have the same chunk name: "index", and it's a little confusing.
6868
//
6969
// To avoid duplicate chunk names, we should custom the names here.
7070
// eg: src/pages/a/b/index.js -> page~a~b
7171
// src/pages/c/index.js -> page~c
72+
// src/pages/d/[id].js -> page~d~{id}
73+
// src/pages/e/[...all].js -> page~e~{all}
7274
if (chunkInfo.isDynamicEntry && chunkInfo.facadeModuleId) {
7375
const pageConfigItem = pagesService.checkPageFile(
7476
chunkInfo.facadeModuleId
@@ -86,6 +88,7 @@ export function conventionalRoutes(userConfig?: UserConfig): Plugin {
8688
const name = path
8789
.trimExt(withBase)
8890
.replace(/^(.+)\/(index|README)/, '$1') // remove trailing /index or /README
91+
.replace(/\[(?:\.{3})?(.*?)\]/g, '{$1}') // transform [id] or [...id] to {id}
8992
.split('/')
9093
.filter(Boolean) // remove leading slash
9194
.join('~'); // a/b/c -> a~b~c

0 commit comments

Comments
 (0)