Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions packages/wxt/src/utils/__tests__/define-unlisted-script.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,22 @@ describe('defineUnlistedScript', () => {

expect(actual).toEqual({ main });
});

it('should return the object definition when given a main function that returns a value', () => {
const main = vi.fn(() => 'test');

const actual = defineUnlistedScript(main);

expect(actual).toEqual({ main });
expect(actual.main()).eq('test');
});

it('should return the object definition when given a main function that returns a promise', async () => {
const main = vi.fn(() => Promise.resolve('test'));

const actual = defineUnlistedScript(main);

expect(actual).toEqual({ main });
await expect(actual.main()).resolves.toEqual('test');
});
});
24 changes: 22 additions & 2 deletions packages/wxt/src/virtual/unlisted-script-entrypoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,37 @@ 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 && typeof result === 'object' && 'then' in result) {
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!`,
err,
);
throw err;
}
return result;
})();

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