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: use async way to parse theme modules #1983

Merged
merged 1 commit into from
Dec 13, 2023
Merged
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
29 changes: 17 additions & 12 deletions src/features/theme/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
VERSION_2_LEVEL_NAV,
} from '@/constants';
import type { IApi } from '@/types';
import { parseModuleSync } from '@umijs/bundler-utils';
import { parseModule } from '@umijs/bundler-utils';
import { execSync } from 'child_process';
import fs from 'fs';
import hostedGit from 'hosted-git-info';
Expand Down Expand Up @@ -67,11 +67,13 @@ function getPkgThemePath(api: IApi) {
/**
* get exports for module
*/
function getModuleExports(modulePath: string) {
return parseModuleSync({
async function getModuleExports(modulePath: string) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [_, exports] = await parseModule({
path: modulePath,
content: fs.readFileSync(modulePath, 'utf-8'),
})[1];
});
return exports || [];
}

/**
Expand Down Expand Up @@ -329,16 +331,17 @@ export default DumiLoading;
},
});

api.onGenerateFiles(() => {
api.onGenerateFiles(async () => {
// write shadow theme files to tmp dir
themeMapKeys.forEach((key) => {
Object.values(originalThemeData[key] || {}).forEach((item) => {
const pAll = themeMapKeys.flatMap((key) =>
Object.values(originalThemeData[key] || {}).map(async (item) => {
// skip write internal components
if (item.source === 'dumi') return;

let contents = [];
// parse exports for theme module
const exports = getModuleExports(item.source);
const exports = await getModuleExports(item.source);

const contents = [];

// export default
if (exports.includes('default')) {
Expand All @@ -355,13 +358,15 @@ export default DumiLoading;
path: `dumi/theme/${key}/${item.specifier}.ts`,
content: contents.join('\n'),
});
});
});
}),
);

await Promise.all(pAll);

const entryFile =
api.config.resolve.entryFile &&
[path.resolve(api.cwd, api.config.resolve.entryFile)].find(fs.existsSync);
const entryExports = entryFile ? getModuleExports(entryFile) : [];
const entryExports = entryFile ? await getModuleExports(entryFile) : [];
const hasDefaultExport = entryExports.includes('default');
const hasNamedExport = entryExports.some((exp) => exp !== 'default');

Expand Down
Loading