bolt.new/packages/bolt/app/lib/webcontainer/index.ts

34 lines
842 B
TypeScript
Raw Normal View History

2024-07-10 16:44:39 +00:00
import { WebContainer } from '@webcontainer/api';
import { WORK_DIR_NAME } from '~/utils/constants';
2024-07-10 16:44:39 +00:00
interface WebContainerContext {
loaded: boolean;
}
export const webcontainerContext: WebContainerContext = import.meta.hot?.data.webcontainerContext ?? {
loaded: false,
};
if (import.meta.hot) {
import.meta.hot.data.webcontainerContext = webcontainerContext;
}
export let webcontainer: Promise<WebContainer> = new Promise(() => {
// noop for ssr
});
if (!import.meta.env.SSR) {
webcontainer =
import.meta.hot?.data.webcontainer ??
Promise.resolve()
.then(() => WebContainer.boot({ workdirName: WORK_DIR_NAME }))
.then((webcontainer) => {
2024-07-10 16:44:39 +00:00
webcontainerContext.loaded = true;
return webcontainer;
2024-07-10 16:44:39 +00:00
});
if (import.meta.hot) {
import.meta.hot.data.webcontainer = webcontainer;
}
}