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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user