Skip to content

Commit

Permalink
feat: reduce the chance of regenerating conventional routes (umijs#6220)
Browse files Browse the repository at this point in the history
* feat: reduce the change of regenerating conventional routes

* fix: build
  • Loading branch information
sorrycc authored Mar 7, 2021
1 parent f510614 commit d17ceca
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 18 deletions.
9 changes: 8 additions & 1 deletion packages/core/src/Config/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { IRoute } from '..';
import { BaseIConfig } from '@umijs/types';

export interface IConfig {
type WithFalse<T> = {
[P in keyof T]?: T[P] | false;
};

export interface BaseIConfig {
singular?: boolean;
outputPath?: string;
publicPath?: string;
Expand All @@ -12,3 +17,5 @@ export interface IConfig {
dynamicRoot?: boolean;
};
}

export type IConfig = WithFalse<BaseIConfig>;
14 changes: 10 additions & 4 deletions packages/core/src/Html/Html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ class Html {
path = path.replace(/^\//, '');
path = path.replace(/\/$/, '');

if (this.config.exportStatic?.htmlSuffix || path === 'index.html') {
if (
(this.config.exportStatic && this.config.exportStatic.htmlSuffix) ||
path === 'index.html'
) {
return `${path}`;
} else {
return `${path}/index.html`;
Expand All @@ -36,8 +39,11 @@ class Html {
const htmlPath = this.getHtmlPath(path);
const len = htmlPath.split('/').length;
return (
Array(this.config.exportStatic?.htmlSuffix ? len : len - 1).join('../') ||
'./'
Array(
this.config.exportStatic && this.config.exportStatic.htmlSuffix
? len
: len - 1,
).join('../') || './'
);
}

Expand All @@ -46,7 +52,7 @@ class Html {
return opts.file;
}
const file = opts.file.charAt(0) === '/' ? opts.file.slice(1) : opts.file;
if (this.config.exportStatic?.dynamicRoot) {
if (this.config.exportStatic && this.config.exportStatic.dynamicRoot) {
return `${this.getRelPathToPublicPath(opts.path || '/')}${file}`;
} else {
return `${this.config.publicPath}${file}`;
Expand Down
27 changes: 18 additions & 9 deletions packages/preset-built-in/src/plugins/commands/generateFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import { join } from 'path';
export default async ({ api, watch }: { api: IApi; watch?: boolean }) => {
const { paths } = api;

async function generate() {
api.logger.debug('generate files');
async function generate(files?: { event: string; path: string }[]) {
api.logger.debug('generate files', files);
await api.applyPlugins({
key: 'onGenerateFiles',
type: api.ApplyPluginsType.event,
args: {
files: files || [],
},
});
}

Expand Down Expand Up @@ -53,13 +56,19 @@ export default async ({ api, watch }: { api: IApi; watch?: boolean }) => {
ignored: /(^|[\/\\])(_mock.js$|\..)/,
ignoreInitial: true,
});
watcher.on(
'all',
lodash.throttle(async (event, path) => {
// debug(`${event} ${path}`);
await generate();
}, 100),
);
let timer: any = null;
let files: { event: string; path: string }[] = [];
watcher.on('all', (event: string, path: string) => {
if (timer) {
clearTimeout(timer);
}
files.push({ event, path: winPath(path) });
timer = setTimeout(async () => {
timer = null;
await generate(files);
files = [];
}, 2000);
});
watchers.push(watcher);
}

Expand Down
26 changes: 23 additions & 3 deletions packages/preset-built-in/src/plugins/generateFiles/core/routes.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,36 @@
import { readFileSync } from 'fs';
import { join } from 'path';
import { extname, join } from 'path';
import { IApi } from '@umijs/types';
import { winPath } from '@umijs/utils';
import { Route } from '@umijs/core';
import { runtimePath } from '../constants';

const ROUTE_FILE_EXT_LIST = ['.js', '.jsx', '.tsx'];

export default function (api: IApi) {
const {
cwd,
utils: { Mustache },
} = api;

api.onGenerateFiles(async (args) => {
if (
// conventional routes
!api.config.routes &&
// from watch
args.files.length &&
// no files is valid route component
!args.files.some(({ path }) => {
return (
path.startsWith(api.paths.absPagesPath!) &&
ROUTE_FILE_EXT_LIST.includes(extname(path))
);
})
) {
return;
}

api.logger.debug('generate core/routes.ts');
const routesTpl = readFileSync(join(__dirname, 'routes.tpl'), 'utf-8');
const routes = await api.getRoutes();
api.writeTmpFile({
Expand All @@ -21,8 +40,9 @@ export default function (api: IApi) {
runtimePath,
config: api.config,
loadingComponent:
api.config.dynamicImport?.loading &&
winPath(api.config.dynamicImport?.loading),
api.config.dynamicImport &&
api.config.dynamicImport.loading &&
winPath(api.config.dynamicImport.loading),
}),
});
});
Expand Down
2 changes: 1 addition & 1 deletion packages/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export interface IApi extends PluginAPI {
onPluginReady: IEvent<null>;
onStart: IEvent<{ args: object }>;
onExit: IEvent<{ signal: 'SIGINT' | 'SIGQUIT' | 'SIGTERM' }>;
onGenerateFiles: IEvent<{ isRebuild?: boolean }>;
onGenerateFiles: IEvent<{ files: { event: string; path: string }[] }>;
onPatchRoute: IEvent<{ route: IRoute; parentRoute?: IRoute }>;
onPatchRouteBefore: IEvent<{ route: IRoute; parentRoute?: IRoute }>;
onPatchRoutes: IEvent<{ routes: IRoute[]; parentRoute?: IRoute }>;
Expand Down

0 comments on commit d17ceca

Please sign in to comment.