Skip to content
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

Merged
merged 11 commits into from
May 16, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand Down Expand Up @@ -205,7 +206,7 @@ async function getRoutePathFilesMap(

export default (api: IApi) => {
let routeChunkFilesMap: IRouteChunkFilesMap;

let hash = '';
api.describe({
enableBy: () =>
// enable when package name available
Expand All @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -306,6 +292,35 @@ export default (api: IApi) => {
.value() as any,
};
}
if (api.name === 'build' && routeChunkFilesMap && !api.config.tern) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  api.name === 'build' && routeChunkFilesMap

↑ 这个条件,在上面的代码也有,最好把它提取出来复用,比如叫 isEnableRoutePreload()

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';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这是怎么漂移了。

在本地 commit 提交代码 → GitHub 发起 PR → CI 跑到绿色 这个流程中,建议一定要做两次代码检查:

  1. 第一次是改好了代码,在本地 commit 要编写 commit msg 前,先在 vscode 或者某些 git 可视化代码管理工具里,看一下你 change 部分的代码有没有问题,一定要浏览一遍,比如这个时候,那些明显的错误或者 console.log 等的遗留就可以避免。

  2. 第二次是在 GitHub 打开 PR 后,在 files 的 tab 里,再人工浏览过一遍代码,看看有没有上一次检查没发现的问题,同时再想一下,这个 PR 里的代码有没有 edge case ,有的话就再补充或修订,从而可以避免再返工的绝大多数问题。

在业务项目的代码里没必要做 2 次 check ,往往做 1 次本地 vscode git 可视化 change 浏览就可以了;你要提到 umi 仓库里的话,一定要谨慎再谨慎,本来 umi 的名声就不是特别好,不期望代码里漏洞百出被抓到把柄被黑,希望能逐渐提升锻炼到一次 PR 即成功,不存在明显的错误。

}
}
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface IPreloadRouteFile {
}

export const PRELOAD_ROUTE_MAP_SCP_TYPE = 'umi-route-chunk-files-map';
export const PRELOAD_ROUTE_HELPER = '_umi_route_preload_helper';
Jinbao1001 marked this conversation as resolved.
Show resolved Hide resolved

export function getPreloadRouteFiles(
path: string,
Expand Down
Loading