Skip to content

Commit

Permalink
fix: support windows separator
Browse files Browse the repository at this point in the history
  • Loading branch information
baozouai committed Mar 20, 2024
1 parent 7866b6b commit 4621cbd
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/generate-history-method-webpack-plugin.ts
Expand Up @@ -106,6 +106,10 @@ class GenerateHistoryMethodWebpackPlugin {
this.historyModuleName = historyModuleName
this.originHistoryModuleName = originHistoryModuleName
this.pagesRootPath = pagesRootPath
if (this.pagesRootPath.includes('\\'))
this.pagesRootPath = this.pagesRootPath.replace(/\\/g, '\/')


this.mode = mode
this.reactRouterVersion = reactRouterVersion
this.exportHistoryName = exportHistoryName
Expand Down Expand Up @@ -243,7 +247,7 @@ export function Router({ children, basename }${isExistTS ? ': RouterProps' : ''}
getParamsMapAndUrlObj(files: string[]) {
// { formatPath => paramsPath }
const paramsMap: Record<string, string> = {}
const regExp = new RegExp(`\/${this.pageName.replace(/(?=\.)/g, '\\')}\.(tsx|jsx?)$`)
const regExp = new RegExp(`[\/\\\\]${this.pageName.replace(/(?=\.)/g, '\\')}\.(tsx|jsx?)$`)
// { formatPath => urlPath }
const urlObj = files.reduce<Record<string, string>>((pre, path) => {
// eg: path: Users/xxx/project/src/pages/order/ ~ q/index.page.tsx
Expand All @@ -263,15 +267,14 @@ export function Router({ children, basename }${isExistTS ? ': RouterProps' : ''}
.replace(/[^\w]/g, '_')
// eg: ORDER__Q => ORDER_Q
.replace(/_{2,}/g, '_') || '$INDEX' // 首页

pre[formatPath] = urlPath
const dir = dirname(path)
const possibleParamsPath = resolve(
dir,
`${this.paramsName}.ts`,
)
if (existsSync(possibleParamsPath))
paramsMap[formatPath] = possibleParamsPath.replace('.ts', '')
paramsMap[formatPath] = possibleParamsPath.replace(/\\/g, '\/').replace('.ts', '')
return pre
}, {})
return { paramsMap, urlObj }
Expand Down Expand Up @@ -353,4 +356,4 @@ export function Router({ children, basename }${isExistTS ? ': RouterProps' : ''}
}
}

export default GenerateHistoryMethodWebpackPlugin
export default GenerateHistoryMethodWebpackPlugin

0 comments on commit 4621cbd

Please sign in to comment.