-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
refactor: change route preonload inline to ref #12398
Changes from 10 commits
c68415c
45d7e4e
a264a33
c4768bc
724143d
2a5f031
56e88d2
127bf4f
f7dac18
af3226d
2abbc9b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,12 +3,13 @@ import type { | |
StatsCompilation, | ||
} from '@umijs/bundler-webpack/compiled/webpack'; | ||
import { lodash, logger, winPath } from '@umijs/utils'; | ||
import { readFileSync } from 'fs'; | ||
import { createHash } from 'crypto'; | ||
import { readFileSync, writeFileSync } from 'fs'; | ||
import { dirname, isAbsolute, join, relative } from 'path'; | ||
import { TEMPLATES_DIR } from '../../constants'; | ||
import { createResolver } from '../../libs/scan'; | ||
import type { IApi, IRoute } from '../../types'; | ||
import { PRELOAD_ROUTE_MAP_SCP_TYPE } from './utils'; | ||
import { PRELOAD_ROUTE_HELPER, PRELOAD_ROUTE_MAP_SCP_TYPE } from './utils'; | ||
|
||
export interface IRouteChunkFilesMap { | ||
/** | ||
|
@@ -205,7 +206,7 @@ async function getRoutePathFilesMap( | |
|
||
export default (api: IApi) => { | ||
let routeChunkFilesMap: IRouteChunkFilesMap; | ||
|
||
let hash = ''; | ||
api.describe({ | ||
enableBy: () => | ||
// enable when package name available | ||
|
@@ -222,6 +223,9 @@ export default (api: IApi) => { | |
api.addHTMLHeadScripts({ | ||
fn: () => { | ||
if (api.name === 'build' && routeChunkFilesMap) { | ||
const { publicPath } = api.config; | ||
const hashedPart = hash ? `.${hash}.js` : '.js'; | ||
const displayPublicPath = publicPath === 'auto' ? '/' : publicPath; | ||
// internal tern app use map mode | ||
return api.config.tern | ||
? // map mode | ||
|
@@ -234,29 +238,11 @@ export default (api: IApi) => { | |
: // script mode | ||
[ | ||
{ | ||
content: readFileSync( | ||
join( | ||
TEMPLATES_DIR, | ||
'routePreloadOnLoad/preloadRouteFilesScp.js', | ||
), | ||
'utf-8', | ||
) | ||
.replace( | ||
'"{{routeChunkFilesMap}}"', | ||
JSON.stringify(routeChunkFilesMap), | ||
) | ||
.replace('{{basename}}', api.config.base) | ||
.replace( | ||
'"{{publicPath}}"', | ||
`${ | ||
// handle runtimePublicPath | ||
api.config.runtimePublicPath ? 'window.publicPath||' : '' | ||
}"${api.config.publicPath}"`, | ||
), | ||
src: `${displayPublicPath}${PRELOAD_ROUTE_HELPER}${hashedPart}`, | ||
}, | ||
]; | ||
} | ||
|
||
return []; | ||
}, | ||
stage: Infinity, | ||
|
@@ -306,6 +292,35 @@ export default (api: IApi) => { | |
.value() as any, | ||
}; | ||
} | ||
if (api.name === 'build' && routeChunkFilesMap && !api.config.tern) { | ||
const content = readFileSync( | ||
join(TEMPLATES_DIR, 'routePreloadOnLoad/preloadRouteFilesScp.js'), | ||
'utf-8', | ||
) | ||
.replace( | ||
'"{{routeChunkFilesMap}}"', | ||
JSON.stringify(routeChunkFilesMap), | ||
) | ||
.replace('{{basename}}', api.config.base) | ||
.replace( | ||
'"{{publicPath}}"', | ||
`${ | ||
// handle runtimePublicPath | ||
api.config.runtimePublicPath ? 'window.publicPath||' : '' | ||
}"${api.config.publicPath}"`, | ||
); | ||
if (api.config.hash) { | ||
hash = createHash('md5') | ||
.update(content) | ||
.digest('hex') | ||
.substring(0, 8); | ||
} | ||
writeFileSync( | ||
join(api.paths.absOutputPath, `${PRELOAD_ROUTE_MAP_SCP_TYPE}.js`), | ||
content, | ||
), | ||
'utf-8'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这是怎么漂移了。 在本地 commit 提交代码 → GitHub 发起 PR → CI 跑到绿色 这个流程中,建议一定要做两次代码检查:
在业务项目的代码里没必要做 2 次 check ,往往做 1 次本地 vscode git 可视化 change 浏览就可以了;你要提到 umi 仓库里的话,一定要谨慎再谨慎,本来 umi 的名声就不是特别好,不期望代码里漏洞百出被抓到把柄被黑,希望能逐渐提升锻炼到一次 PR 即成功,不存在明显的错误。 |
||
} | ||
} | ||
}); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
↑ 这个条件,在上面的代码也有,最好把它提取出来复用,比如叫
isEnableRoutePreload()
。