Skip to content

Commit 12ff9e9

Browse files
committed
fix(load-component): improve error handling in getEntryFile function
- Enhance the getEntryFile function to include a logger for better error tracking. - Wrap the file system operations in a try-catch block to log errors before throwing a descriptive error message. - Update the call to getEntryFile in buildComponentInstance to pass the logger from params. Signed-off-by: zxypro1 <1018995004@qq.com> Signed-off-by: Zone Tome <43384183+zxypro1@users.noreply.github.com>
1 parent 437086b commit 12ff9e9

1 file changed

Lines changed: 26 additions & 21 deletions

File tree

  • packages/load-component/src/utils

packages/load-component/src/utils/index.ts

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,35 @@ export function readJsonFile(filePath: string) {
1818
}
1919
}
2020

21-
const getEntryFile = async (componentPath: string) => {
22-
const fsStat = await fs.stat(componentPath);
23-
if (fsStat.isFile() || !fsStat.isDirectory()) return componentPath;
24-
const packageInfo: any = readJsonFile(path.resolve(componentPath, 'package.json'));
25-
// First look for main under the package json file
26-
let entry = get(packageInfo, 'main');
27-
if (entry) return path.resolve(componentPath, entry);
28-
// Second check the out dir under the tsconfig json file
29-
const tsconfigPath = path.resolve(componentPath, 'tsconfig.json');
30-
const tsconfigInfo = readJsonFile(tsconfigPath);
31-
entry = get(tsconfigInfo, 'compilerOptions.outDir');
32-
if (entry) return path.resolve(componentPath, entry);
33-
// Third look for src index js
34-
const srcIndexPath = path.resolve(componentPath, './src/index.js');
35-
if (fs.existsSync(srcIndexPath)) return srcIndexPath;
36-
const indexPath = path.resolve(componentPath, './index.js');
37-
if (fs.existsSync(indexPath)) return indexPath;
38-
throw new Error(
39-
'The component cannot be required. Please check whether the setting of the component entry file is correct. In the current directory, first look for main under the package json file, secondly look for compiler options out dir under the tsconfig json file, thirdly look for src index js, and finally look for index js',
40-
);
21+
const getEntryFile = async (componentPath: string, logger: any) => {
22+
try {
23+
const fsStat = await fs.stat(componentPath);
24+
if (fsStat.isFile() || !fsStat.isDirectory()) return componentPath;
25+
const packageInfo: any = readJsonFile(path.resolve(componentPath, 'package.json'));
26+
// First look for main under the package json file
27+
let entry = get(packageInfo, 'main');
28+
if (entry) return path.resolve(componentPath, entry);
29+
// Second check the out dir under the tsconfig json file
30+
const tsconfigPath = path.resolve(componentPath, 'tsconfig.json');
31+
const tsconfigInfo = readJsonFile(tsconfigPath);
32+
entry = get(tsconfigInfo, 'compilerOptions.outDir');
33+
if (entry) return path.resolve(componentPath, entry);
34+
// Third look for src index js
35+
const srcIndexPath = path.resolve(componentPath, './src/index.js');
36+
if (fs.existsSync(srcIndexPath)) return srcIndexPath;
37+
const indexPath = path.resolve(componentPath, './index.js');
38+
if (fs.existsSync(indexPath)) return indexPath;
39+
} catch (error) {
40+
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
41+
logger.debug(errorMessage);
42+
throw new Error(
43+
'The component cannot be required. Please check whether the setting of the component entry file is correct. In the current directory, first look for main under the package json file, secondly look for compiler options out dir under the tsconfig json file, thirdly look for src index js, and finally look for index js',
44+
);
45+
}
4146
};
4247

4348
export const buildComponentInstance = async (componentPath: string, params?: any, cleanCache: boolean = false) => {
44-
const requirePath = await getEntryFile(componentPath);
49+
const requirePath = await getEntryFile(componentPath, params.logger || console);
4550
// bug: `- component: fc invoke` timeout. Delete require cache
4651
if (cleanCache && require.cache[requirePath]) {
4752
try {

0 commit comments

Comments
 (0)