Skip to content

Commit

Permalink
refactor(theme): improve theme component load logic
Browse files Browse the repository at this point in the history
  • Loading branch information
PeachScript committed Aug 14, 2020
1 parent 9dc0d40 commit f468948
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 15 deletions.
8 changes: 8 additions & 0 deletions packages/dumi/e2e/minimal/.umirc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import path from 'path';

export default {
chainWebpack(config) {
// load dumi-default-theme for testing
config.module.rule('js').include.add(path.join(__dirname, '../../../theme-default'));
},
};
9 changes: 0 additions & 9 deletions packages/preset-dumi/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import fs from 'fs';
import path from 'path';
import symlink from 'symlink-dir';
import { Service } from '@umijs/core';
import { render } from '@testing-library/react';

Expand Down Expand Up @@ -30,14 +29,6 @@ describe('preset-dumi', () => {
presets: [require.resolve('@umijs/preset-built-in'), require.resolve('./index.ts')],
});

// FIXME: find the real reason why component path missing 1 level in routes.ts
service.paths.absPagesPath += '/tmp';
// workaround for load theme
await symlink(
path.join(__dirname, '../../theme-default'),
path.join(service.paths.absNodeModulesPath, 'dumi-theme-default'),
);

await service.run({
name: 'g',
args: {
Expand Down
1 change: 1 addition & 0 deletions packages/preset-dumi/src/loader/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default async function loader(content: string) {
import React from 'react';
import { Link, AnchorLink } from 'dumi/theme';
${theme.builtins
.concat(theme.fallbacks)
.map(component => `import ${component.identifier} from '${component.source}';`)
.join('\n')}
${builtins
Expand Down
3 changes: 2 additions & 1 deletion packages/preset-dumi/src/plugins/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import getMenuFromRoutes from '../routes/getMenuFromRoutes';
import getLocaleFromRoutes from '../routes/getLocaleFromRoutes';
import getHostPkgAlias from '../utils/getHostPkgAlias';
import getRepoUrl from '../utils/getRepoUrl';
import getTheme from '../theme/loader';
import ctx, { init as setContext } from '../context';
import { IDumiOpts } from '..';

Expand Down Expand Up @@ -142,7 +143,7 @@ export default function(api: IApi) {

// pass props for layout
root.component = `(props) => require('react').createElement(require('${
root.component
getTheme().layoutPath
}').default, {
...${
// escape " to ^ to avoid umi parse error, then umi will decode them
Expand Down
5 changes: 4 additions & 1 deletion packages/preset-dumi/src/plugins/features/demos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ export default {

// add single demo render route
api.modifyRoutes(routes => {
const Previewer = getTheme().builtins.find(({ identifier }) => identifier === 'Previewer');
const theme = getTheme();
const Previewer = theme.builtins
.concat(theme.fallbacks)
.find(({ identifier }) => identifier === 'Previewer');

routes.unshift(
{
Expand Down
3 changes: 1 addition & 2 deletions packages/preset-dumi/src/routes/getRouteConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import slash from 'slash2';
import { IApi, IRoute } from '@umijs/types';
import getRouteConfigFromDir from './getRouteConfigFromDir';
import decorateRoutes from './decorator';
import getTheme from '../theme/loader';
import { IDumiOpts } from '../index';

export default (api: IApi, opts: IDumiOpts): IRoute[] => {
Expand Down Expand Up @@ -39,7 +38,7 @@ export default (api: IApi, opts: IDumiOpts): IRoute[] => {
config.push({
path: '/',
wrappers: [slash(path.join(__dirname, '../theme/layout'))],
component: slash(path.relative(path.join(paths.absTmpPath, 'core'), getTheme().layoutPath)),
// component: the real theme layout will be configure in core.ts
// decorate standard umi routes
routes: decorateRoutes(childRoutes, opts, api),
title: opts.title,
Expand Down
4 changes: 2 additions & 2 deletions packages/preset-dumi/src/theme/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function detectInstalledTheme() {
export default () => {
if (!cache) {
const [theme = FALLBACK_THEME] = detectInstalledTheme();
const modulePath = path.join(ctx.umi.paths.absNodeModulesPath, theme);
const modulePath = winPath(path.join(ctx.umi.paths.absNodeModulesPath, theme));
const builtinPath = path.join(modulePath, 'src/builtins');
const components = fs.existsSync(builtinPath)
? fs
Expand All @@ -79,7 +79,7 @@ export default () => {

cache = {
name: theme,
layoutPath: path.join(modulePath, 'src', 'layout'),
layoutPath: winPath(path.join(theme, 'src', 'layout')),
modulePath,
builtins: components,
fallbacks,
Expand Down

0 comments on commit f468948

Please sign in to comment.