Skip to content

Commit

Permalink
捕获modules缺失时import失败的报错
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaozhuobin committed Mar 16, 2023
1 parent 630adf8 commit 2f4e82f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lisa-plugin/zephyr",
"version": "1.7.1-beta.5",
"version": "1.7.1-beta.6",
"description": "LISA plugin for Zephyr",
"main": "./lib/main.js",
"engines": {
Expand Down
12 changes: 10 additions & 2 deletions src/env/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,19 @@ export async function loadBinaries(bundles?: Bundle[]): Promise<Record<string, B
const binaries: Record<string, Binary> = {};
for (const name of BUILTIN_BINARIES) {
const unprefixedName = name.split('/').slice(1).join('/');
binaries[unprefixedName] = await typedImport<Binary>(name);
try {
binaries[unprefixedName] = await typedImport<Binary>(name);
} catch (error) {
binaries[unprefixedName] = <Binary>{}
}
}
for (const bundle of bundles || []) {
for (const name of bundle.binaries || []) {
binaries[name] = await typedImport<Binary>(`${PACKAGE_MODULES_DIR}/@binary/${name}`);
try {
binaries[name] = await typedImport<Binary>(`${PACKAGE_MODULES_DIR}/@binary/${name}`);
} catch (error) {
binaries[name] = <Binary>{};
}
}
}
return binaries;
Expand Down
8 changes: 2 additions & 6 deletions src/utils/typedImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ interface Module<T> {
}

export default async function typedImport<T>(name: string): Promise<T> {
try {
const mod = await import(name) as Module<T>;
return mod.default;
} catch (error) {
return <T>{}
}
const mod = await import(name) as Module<T>;
return mod.default;
}

0 comments on commit 2f4e82f

Please sign in to comment.