Skip to content

Commit

Permalink
feat: parseModuleSync to parseModule
Browse files Browse the repository at this point in the history
  • Loading branch information
xierenyuan committed Dec 12, 2023
1 parent fdc9672 commit 3735415
Showing 1 changed file with 17 additions and 12 deletions.
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

0 comments on commit 3735415

Please sign in to comment.