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

fix: default theme lost bug if major version mixed in monorepo #1601

Merged
merged 2 commits into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions src/features/routes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { SP_ROUTE_PREFIX } from '@/constants';
import type { IApi } from '@/types';
import { getClientDistFile } from '@/utils';
import { getConventionRoutes } from '@umijs/core';
import { createRouteId } from '@umijs/core/dist/route/utils';
import path from 'path';
Expand All @@ -9,6 +8,8 @@ import type { IRoute } from 'umi';
import { glob, winPath } from 'umi/plugin-utils';

const CTX_LAYOUT_ID = 'dumi-context-layout';
const ALIAS_THEME_TMP = '@/dumi__theme';
const ALIAS_INTERNAL_PAGES = '@/dumi__pages';

/**
* normalize item of `resolve.docDirs` to object
Expand Down Expand Up @@ -103,9 +104,9 @@ export default (api: IApi) => {
// watch docs paths to re-generate routes
api.addTmpGenerateWatcherPaths(() => extraWatchPaths);

// support to disable docDirs & atomDirs by empty array
// because the empty array will be ignored by config merge logic
api.modifyDefaultConfig((memo) => {
// support to disable docDirs & atomDirs by empty array
// because the empty array will be ignored by config merge logic
if (api.userConfig.resolve) {
const keys: ['docDirs', 'atomDirs'] = ['docDirs', 'atomDirs'];

Expand All @@ -114,6 +115,16 @@ export default (api: IApi) => {
});
}

// set alias for internal pages and layouts rather than use absolute path
// to avoid umi generate chunk name with long path
// ref: https://github.com/umijs/umi/blob/30a11c60b1be9066ea0162fe279aaf62b70b0b14/packages/preset-umi/src/features/tmpFiles/routes.ts#L229
memo.alias[ALIAS_THEME_TMP] = winPath(
path.join(api.paths.absTmpPath!, 'dumi/theme'),
);
memo.alias[ALIAS_INTERNAL_PAGES] = winPath(
path.join(__dirname, '../client/pages'),
);

return memo;
});

Expand Down Expand Up @@ -149,7 +160,10 @@ export default (api: IApi) => {
routes[DocLayout.specifier] = {
id: DocLayout.specifier,
path: '/',
file: DocLayout.source,
// why not use DocLayout.source?
// because umi will generate chunk name from file path
// but source may too long in pnpm/monorepo project
file: `${ALIAS_THEME_TMP}/layouts/DocLayout`,
parentId: lastLayoutId,
absPath: '/',
isLayout: true,
Expand All @@ -162,7 +176,7 @@ export default (api: IApi) => {
routes[DemoLayout.specifier] = {
id: DemoLayout.specifier,
path: '/',
file: DemoLayout.source,
file: `${ALIAS_THEME_TMP}/layouts/DemoLayout`,
parentId: lastLayoutId,
absPath: '/',
isLayout: true,
Expand Down Expand Up @@ -262,7 +276,7 @@ export default (api: IApi) => {
path: '*',
absPath: '/*',
parentId: docLayoutId,
file: getClientDistFile('dist/client/pages/404', api.cwd),
file: `${ALIAS_INTERNAL_PAGES}/404`,
};
}

Expand All @@ -272,7 +286,7 @@ export default (api: IApi) => {
path: `${SP_ROUTE_PREFIX}demos/:id`,
absPath: `/${SP_ROUTE_PREFIX}demos/:id`,
parentId: demoLayoutId,
file: getClientDistFile('dist/client/pages/Demo', api.cwd),
file: `${ALIAS_INTERNAL_PAGES}/Demo`,
};

return routes;
Expand All @@ -283,15 +297,15 @@ export default (api: IApi) => {
const layouts = [
{
id: CTX_LAYOUT_ID,
file: `${api.paths.absTmpPath}/dumi/theme/ContextWrapper.tsx`,
file: `${ALIAS_THEME_TMP}/ContextWrapper`,
},
];
const { GlobalLayout } = api.service.themeData.layouts;

if (GlobalLayout) {
layouts.unshift({
id: GlobalLayout.specifier,
file: GlobalLayout.source,
file: `${ALIAS_THEME_TMP}/layouts/GlobalLayout`,
});
}

Expand Down
7 changes: 2 additions & 5 deletions src/features/theme/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import {
THEME_PREFIX,
} from '@/constants';
import type { IApi } from '@/types';
import { getClientDistFile } from '@/utils';
import { parseModuleSync } from '@umijs/bundler-utils';
import fs from 'fs';
import path from 'path';
import { deepmerge, lodash, resolve, winPath } from 'umi/plugin-utils';
import { safeExcludeInMFSU } from '../derivative';
import loadTheme, { IThemeLoadResult } from './loader';

const DEFAULT_THEME_PATH = path.join(__dirname, '../../../theme-default');

/**
* get pkg theme name
*/
Expand Down Expand Up @@ -57,10 +58,6 @@ function getModuleExports(modulePath: string) {
}

export default (api: IApi) => {
const DEFAULT_THEME_PATH = path.join(
getClientDistFile('package.json', api.cwd),
'../theme-default',
);
// load default theme
const defaultThemeData = loadTheme(DEFAULT_THEME_PATH);
// try to load theme package
Expand Down
24 changes: 1 addition & 23 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Cache from 'file-system-cache';
import fs from 'fs';
import yaml from 'js-yaml';
import path from 'path';
import { lodash, logger, resolve, winPath } from 'umi/plugin-utils';
import { lodash, logger, winPath } from 'umi/plugin-utils';

/**
* get route path from file-system path
Expand Down Expand Up @@ -158,25 +158,3 @@ export function getProjectRoot(cwd: string) {

return winPath(cwd);
}

/**
* get dumi client dist file and preserve symlink(pnpm, tnpm & etc.) to make chunk name clean
*/
export function getClientDistFile(file: string, cwd: string) {
let clientFile: string;

try {
// why use `resolve`?
// because `require.resolve` will use the final path of symlink file
// and in tnpm or pnpm project, the long realpath make chunk name unexpected
clientFile = resolve.sync(`dumi/${file}`, {
basedir: cwd,
preserveSymlinks: true,
});
} catch {
// fallback to use `require.resolve`, for dumi self docs & examples
clientFile = require.resolve(`../${file}`);
}

return winPath(clientFile);
}