Skip to content

Commit 4b44fb8

Browse files
rxliuliaklinker1
andauthored
fix: Don't return promises from unlisted scripts that do not have an async main function (#1907)
Co-authored-by: Aaron <[email protected]>
1 parent 5d0a266 commit 4b44fb8

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

packages/wxt/src/utils/__tests__/define-unlisted-script.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,22 @@ describe('defineUnlistedScript', () => {
2121

2222
expect(actual).toEqual({ main });
2323
});
24+
25+
it('should return the result without awaiting for synchronous main functions', () => {
26+
const main = vi.fn(() => 'test');
27+
28+
const actual = defineUnlistedScript(main);
29+
30+
expect(actual).toEqual({ main });
31+
expect(actual.main()).eq('test');
32+
});
33+
34+
it('should return a promise of a result for async main functions', async () => {
35+
const main = vi.fn(() => Promise.resolve('test'));
36+
37+
const actual = defineUnlistedScript(main);
38+
39+
expect(actual).toEqual({ main });
40+
await expect(actual.main()).resolves.toEqual('test');
41+
});
2442
});

packages/wxt/src/virtual/unlisted-script-entrypoint.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,37 @@ import definition from 'virtual:user-unlisted-script-entrypoint';
22
import { logger } from '../utils/internal/logger';
33
import { initPlugins } from 'virtual:wxt-plugins';
44

5-
const result = (async () => {
5+
const result = (() => {
66
try {
77
initPlugins();
8-
return await definition.main();
8+
} catch (err) {
9+
logger.error(
10+
`Failed to initialize plugins for "${import.meta.env.ENTRYPOINT}"`,
11+
err,
12+
);
13+
throw err;
14+
}
15+
let result;
16+
try {
17+
result = definition.main();
18+
19+
if (result instanceof Promise) {
20+
result = (result as Promise<any>).catch((err) => {
21+
logger.error(
22+
`The unlisted script "${import.meta.env.ENTRYPOINT}" crashed on startup!`,
23+
err,
24+
);
25+
throw err;
26+
});
27+
}
928
} catch (err) {
1029
logger.error(
1130
`The unlisted script "${import.meta.env.ENTRYPOINT}" crashed on startup!`,
1231
err,
1332
);
1433
throw err;
1534
}
35+
return result;
1636
})();
1737

1838
// Return the main function's result to the background when executed via the

0 commit comments

Comments
 (0)