-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
packages/preset-umi/src/features/transitionRouter/transitionRouter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { winPath } from '@umijs/utils'; | ||
import { join } from 'path'; | ||
import type { IApi } from '../../types'; | ||
|
||
export default (api: IApi) => { | ||
api.describe({ | ||
config: { | ||
schema({ zod }) { | ||
return zod.object({}); | ||
}, | ||
}, | ||
enableBy: api.EnableBy.config, | ||
}); | ||
|
||
api.onGenerateFiles(() => { | ||
if (api.userConfig.mpa || api.config.mpa) { | ||
throw new Error('transitionRouter plugin does not support mpa mode'); | ||
} | ||
if (api.appData.framework !== 'react') { | ||
throw new Error('transitionRouter plugin only support react framework'); | ||
} | ||
const isReact18 = ( | ||
api.appData.react?.version as string | undefined | ||
)?.startsWith('18'); | ||
if (!isReact18) { | ||
throw new Error('transitionRouter plugin only support react@18'); | ||
} | ||
|
||
// https://github.com/remix-run/remix/issues/5763 | ||
api.writeTmpFile({ | ||
path: 'runtime.ts', | ||
content: ` | ||
import { startTransition } from 'react'; | ||
export const modifyClientRenderOpts = (context) => { | ||
const h = context.history | ||
const originPush = h.push | ||
const originReplace = h.replace | ||
h.push = (...args) => { | ||
startTransition(() => originPush.apply(h, args)) | ||
} | ||
h.replace = (...args) => { | ||
startTransition(() => originReplace.apply(h, args)) | ||
} | ||
return context | ||
} | ||
`, | ||
}); | ||
}); | ||
|
||
const pluginDir = `plugin-${api.plugin.key}`; | ||
api.addRuntimePlugin(() => [ | ||
winPath(join(api.paths.absTmpPath, `${pluginDir}/runtime.ts`)), | ||
]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters