-
Notifications
You must be signed in to change notification settings - Fork 2.7k
feat: 约定式路由支持配置额外属性 #10527
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: 约定式路由支持配置额外属性 #10527
Changes from 3 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
016483b
feat: 约定式路由支持配置额外属性
AdoKevin ad3b723
wip: 增加esbuild抽取routeProps
AdoKevin 309b9ee
fix: 修复由于tempFiles onGenerateFiles先执行导致clientLoader以及routeProps获取属性不正确
AdoKevin 41023eb
refactor: 抽取clientLoadery和routeProps公用逻辑
AdoKevin a3330c5
refactor: route props
fz6m 44d79a0
chore: remove refresh route data method
fz6m a05a81a
example: add route props example
fz6m 621a49a
feat: add `useRouteProps` and type
fz6m a0f9b54
docs: `useRouteProps`
fz6m ae8143a
chore: improve type
fz6m File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
packages/preset-umi/src/features/routeProps/routeProps.ts
This file contains hidden or 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,113 @@ | ||
| import esbuild from '@umijs/bundler-utils/compiled/esbuild'; | ||
| import { join, resolve } from 'path'; | ||
| import type { IApi } from '../../types'; | ||
|
|
||
| export default (api: IApi) => { | ||
| api.describe({ | ||
| config: { | ||
| schema(Joi) { | ||
| return Joi.object({}); | ||
| }, | ||
| }, | ||
| enableBy: api.EnableBy.config, | ||
| }); | ||
|
|
||
| api.onGenerateFiles(() => { | ||
| const routePropsImports: string[] = []; | ||
| const routePropsDefines: string[] = []; | ||
| const routeIds = Object.keys(api.appData.routes); | ||
| let index = 0; | ||
| for (const id of routeIds) { | ||
| const route = api.appData.routes[id]; | ||
| if (route.routeProps) { | ||
| index += 1; | ||
| routePropsImports.push( | ||
| `import { routeProps as routeProps_${index} } from '${route.__absFile}';`, | ||
fz6m marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ); | ||
| routePropsDefines.push(` '${id}': routeProps_${index},`); | ||
| } | ||
| } | ||
| api.writeTmpFile({ | ||
| noPluginDir: true, | ||
| path: 'core/routeProps.ts', | ||
| content: ` | ||
| ${routePropsImports.join('\n')} | ||
| export default { | ||
| ${routePropsDefines.join('\n')} | ||
| }; | ||
| `, | ||
| }); | ||
| }); | ||
|
|
||
| api.onBeforeCompiler(async () => { | ||
| await esbuild.build({ | ||
| format: 'esm', | ||
| platform: 'browser', | ||
| target: 'esnext', | ||
| loader, | ||
| watch: api.env === 'development' && {}, | ||
| bundle: true, | ||
| logLevel: 'error', | ||
| entryPoints: [join(api.paths.absTmpPath, 'core/routeProps.ts')], | ||
| outfile: join(api.paths.absTmpPath, 'core/routeProps.js'), | ||
| plugins: [ | ||
| { | ||
| name: 'imports', | ||
| setup(build) { | ||
| let entry: string | undefined; | ||
| build.onResolve({ filter: /.*/ }, (args) => { | ||
| if (args.kind === 'entry-point') entry = args.path; | ||
| if (args.kind === 'entry-point' || args.importer === entry) { | ||
| return { path: resolve(args.resolveDir, args.path) }; | ||
| } | ||
| return { | ||
| path: | ||
| !args.path.startsWith('.') && !args.path.startsWith('/') | ||
| ? args.path | ||
| : resolve(args.resolveDir, args.path), | ||
| external: true, | ||
| sideEffects: false, | ||
| }; | ||
| }); | ||
| }, | ||
| }, | ||
| ], | ||
| }); | ||
| }); | ||
| }; | ||
|
|
||
| const loader: { [ext: string]: esbuild.Loader } = { | ||
| '.aac': 'file', | ||
| '.css': 'text', | ||
| '.less': 'text', | ||
| '.sass': 'text', | ||
| '.scss': 'text', | ||
| '.eot': 'file', | ||
| '.flac': 'file', | ||
| '.gif': 'file', | ||
| '.htm': 'file', | ||
| '.html': 'file', | ||
| '.ico': 'file', | ||
| '.icon': 'file', | ||
| '.jpeg': 'file', | ||
| '.jpg': 'file', | ||
| '.js': 'jsx', | ||
| '.jsx': 'jsx', | ||
| '.json': 'json', | ||
| '.md': 'jsx', | ||
| '.mdx': 'jsx', | ||
| '.mp3': 'file', | ||
| '.mp4': 'file', | ||
| '.ogg': 'file', | ||
| '.otf': 'file', | ||
| '.png': 'file', | ||
| '.svg': 'file', | ||
| '.ts': 'ts', | ||
| '.tsx': 'tsx', | ||
| '.ttf': 'file', | ||
| '.wav': 'file', | ||
| '.webm': 'file', | ||
| '.webp': 'file', | ||
| '.woff': 'file', | ||
| '.woff2': 'file', | ||
| }; | ||
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.