fix: Don't return promises from unlisted scripts that do not have an async main function (#1907)

Co-authored-by: Aaron <aaronklinker1@gmail.com>
This commit is contained in:
rxliuli
2025-12-17 06:58:46 +08:00
committed by GitHub
parent 5d0a266e9c
commit 4b44fb849d
2 changed files with 40 additions and 2 deletions
@@ -21,4 +21,22 @@ describe('defineUnlistedScript', () => {
expect(actual).toEqual({ main });
});
it('should return the result without awaiting for synchronous main functions', () => {
const main = vi.fn(() => 'test');
const actual = defineUnlistedScript(main);
expect(actual).toEqual({ main });
expect(actual.main()).eq('test');
});
it('should return a promise of a result for async main functions', async () => {
const main = vi.fn(() => Promise.resolve('test'));
const actual = defineUnlistedScript(main);
expect(actual).toEqual({ main });
await expect(actual.main()).resolves.toEqual('test');
});
});
@@ -2,10 +2,29 @@ import definition from 'virtual:user-unlisted-script-entrypoint';
import { logger } from '../utils/internal/logger';
import { initPlugins } from 'virtual:wxt-plugins';
const result = (async () => {
const result = (() => {
try {
initPlugins();
return await definition.main();
} catch (err) {
logger.error(
`Failed to initialize plugins for "${import.meta.env.ENTRYPOINT}"`,
err,
);
throw err;
}
let result;
try {
result = definition.main();
if (result instanceof Promise) {
result = (result as Promise<any>).catch((err) => {
logger.error(
`The unlisted script "${import.meta.env.ENTRYPOINT}" crashed on startup!`,
err,
);
throw err;
});
}
} catch (err) {
logger.error(
`The unlisted script "${import.meta.env.ENTRYPOINT}" crashed on startup!`,
@@ -13,6 +32,7 @@ const result = (async () => {
);
throw err;
}
return result;
})();
// Return the main function's result to the background when executed via the