Skip to content

Commit

Permalink
feat: custom export history name, default is history
Browse files Browse the repository at this point in the history
  • Loading branch information
baozouai committed Jun 17, 2023
1 parent beeb4c0 commit 9c0c320
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/genetate-history-method-webpack-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ interface GenerateHistoryMethodWebpackPluginOptions {
* @default 'history'
*/
originHistoryModuleName?: string
/**
* @description the history name you want to custom
* @default history
* @example
* { exportHistoryName: 'myHistory' }
* import { myHistory } from '~history'
* */
exportHistoryName?: string
/**
* your pages path root
* @example
Expand All @@ -56,13 +64,16 @@ class GenerateHistoryMethodWebpackPlugin {
mode: HistoryMode
contents: string[] = []
reactRouterVersion: AllowReactRouterVersion
exportHistoryName: string

constructor(options: GenerateHistoryMethodWebpackPluginOptions) {
const {
paramsName = 'index.params',
pageName = 'index.page',
historyModuleName = '~history',
originHistoryModuleName = 'history',
mode = 'browser',
exportHistoryName = 'history',
pagesRootPath,
reactRouterVersion,
} = options || {}
Expand All @@ -73,6 +84,7 @@ class GenerateHistoryMethodWebpackPlugin {
this.pagesRootPath = pagesRootPath
this.mode = mode
this.reactRouterVersion = reactRouterVersion
this.exportHistoryName = exportHistoryName
}

get isHash() {
Expand Down Expand Up @@ -130,6 +142,7 @@ class GenerateHistoryMethodWebpackPlugin {

addTopImport() {
this.contents.push(
'// @eslint-ignored',
'import React, { useLayoutEffect, useRef, useState, ReactNode } from \'react\'',
'import { useLocation, Router as BaseRouter } from \'react-router-dom\'',
`import originHistory from '${this.originHistoryModuleName}'`,
Expand Down Expand Up @@ -158,7 +171,7 @@ export const useRouterHistory = () => {

generateUseSearchParamsHook(isExistTS: boolean) {
this.contents.push(`
export const useSearchParams = ${isExistTS ? '<T = any>' : ''}() => {
export function useSearchParams${isExistTS ? '<T = any>' : ''}() {
const { search } = useLocation()
return qs.parse(search, { ignoreQueryPrefix: true }) ${isExistTS ? 'as T' : ''}
}
Expand Down Expand Up @@ -278,10 +291,9 @@ export function Router({ children, basename }${isExistTS ? ': RouterProps' : ''}
)
}
this.contents.unshift(...PathParamsTypeArrs)
this.contents.push('const history = {')
this.contents.push(`export const ${this.exportHistoryName} = {`)
this.contents.push(...methods)
this.contents.push('}\n')
this.contents.push('export default history')
}
}

Expand Down

0 comments on commit 9c0c320

Please sign in to comment.