Skip to content
This repository has been archived by the owner on Aug 9, 2022. It is now read-only.

Commit

Permalink
feat: add routes and hasSrcDir to api.appData
Browse files Browse the repository at this point in the history
  • Loading branch information
sorrycc committed Oct 20, 2021
1 parent 8445771 commit 10fadde
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 14 deletions.
7 changes: 6 additions & 1 deletion packages/core/src/route/routesConvention.ts
@@ -1,5 +1,6 @@
import { winPath } from '@umijs/utils';
import { lstatSync, readdirSync } from 'fs';
import assert from 'assert';
import { existsSync, lstatSync, readdirSync, statSync } from 'fs';
import { join, relative, resolve } from 'path';
import { defineRoutes } from './defineRoutes';
import {
Expand All @@ -12,6 +13,10 @@ import {
// opts.base: path of pages
export function getConventionRoutes(opts: { base: string }) {
const files: { [routeId: string]: string } = {};
assert(
existsSync(opts.base) && statSync(opts.base).isDirectory(),
`Convention routes base not found.`,
);
visitFiles({
dir: opts.base,
visitor: (file) => {
Expand Down
11 changes: 7 additions & 4 deletions packages/preset-built-in/src/commands/dev/dev.ts
Expand Up @@ -37,15 +37,18 @@ PORT=8888 umi dev
clearTmp(api.paths.absTmpPath);

// generate files
async function generate(files?: any) {
async function generate(opts: { isFirstTime?: boolean; files?: any }) {
api.applyPlugins({
key: 'onGenerateFiles',
args: {
files: files || null,
files: opts.files || null,
isFirstTime: opts.isFirstTime,
},
});
}
await generate();
await generate({
isFirstTime: true,
});
const { absPagesPath, absSrcPath } = api.paths;
const watcherPaths: string[] = await api.applyPlugins({
key: 'addTmpGenerateWatcherPaths',
Expand All @@ -62,7 +65,7 @@ PORT=8888 umi dev
onChange: createDebouncedHandler({
timeout: 2000,
async onChange(opts) {
await generate(opts.files);
await generate({ files: opts.files, isFirstTime: false });
},
}),
});
Expand Down
16 changes: 16 additions & 0 deletions packages/preset-built-in/src/features/appData/appData.ts
@@ -0,0 +1,16 @@
import { IApi } from '../../types';
import { getRoutes } from '../tmpFiles/routes';

export default (api: IApi) => {
api.modifyAppData(async (memo) => {
// routes
memo.routes = await getRoutes({
config: api.config,
absSrcPage: api.paths.absSrcPath,
absPagesPath: api.paths.absPagesPath,
});
// hasSrcDir
memo.hasSrcDir = api.paths.absSrcPath.endsWith('/src');
return memo;
});
};
12 changes: 12 additions & 0 deletions packages/preset-built-in/src/features/check/check.ts
@@ -0,0 +1,12 @@
import assert from 'assert';
import { IApi } from '../../types';

export default (api: IApi) => {
api.onCheck(() => {
// routes
assert(
api.appData.routes,
`routes not found, you may be run umi on the wrong directory.`,
);
});
};
19 changes: 12 additions & 7 deletions packages/preset-built-in/src/features/tmpFiles/tmpFiles.ts
Expand Up @@ -13,7 +13,7 @@ export default (api: IApi) => {
},
});

api.onGenerateFiles(async () => {
api.onGenerateFiles(async (opts) => {
// umi.ts
api.writeTmpFile({
path: 'umi.ts',
Expand All @@ -24,12 +24,17 @@ export default (api: IApi) => {
});

// routes.ts
const routes = await getRoutes({
config: api.config,
absSrcPage: api.paths.absSrcPath,
absPagesPath: api.paths.absPagesPath,
});
const hasSrc = api.paths.absSrcPath.endsWith('/src');
let routes;
if (opts.isFirstTime) {
routes = api.appData.routes;
} else {
routes = await getRoutes({
config: api.config,
absSrcPage: api.paths.absSrcPath,
absPagesPath: api.paths.absPagesPath,
});
}
const hasSrc = api.appData.hasSrcDir;
// @/pages/
const prefix = hasSrc ? '../../src/pages/' : '../../pages/';
api.writeTmpFile({
Expand Down
4 changes: 3 additions & 1 deletion packages/preset-built-in/src/index.ts
Expand Up @@ -5,8 +5,10 @@ export default () => {
require.resolve('./registerMethods'),

// features
require.resolve('./features/tmpFiles/tmpFiles'),
require.resolve('./features/appData/appData'),
require.resolve('./features/check/check'),
require.resolve('./features/configPlugins/configPlugins'),
require.resolve('./features/tmpFiles/tmpFiles'),

// commands
require.resolve('./commands/build'),
Expand Down
3 changes: 2 additions & 1 deletion packages/preset-built-in/src/types.ts
Expand Up @@ -12,7 +12,8 @@ export type IApi = PluginAPI &
}) => void;
addTmpGenerateWatcherPaths: IAdd<null, string[]>;
onGenerateFiles: IEvent<{
files: { event: string; path: string } | null;
isFirstTime?: boolean;
files?: { event: string; path: string } | null;
}>;
onPkgJSONChanged: IEvent<{
origin: Record<string, any>;
Expand Down

0 comments on commit 10fadde

Please sign in to comment.