mirror of
https://github.com/stackblitz/bolt.new
synced 2024-11-27 22:42:21 +00:00
36 lines
868 B
TypeScript
36 lines
868 B
TypeScript
import { WebContainer } from '@webcontainer/api';
|
|
import { WORK_DIR_NAME } from '~/utils/constants';
|
|
|
|
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(() => {
|
|
return WebContainer.boot({ workdirName: WORK_DIR_NAME });
|
|
})
|
|
.then((webcontainer) => {
|
|
webcontainerContext.loaded = true;
|
|
return webcontainer;
|
|
});
|
|
|
|
if (import.meta.hot) {
|
|
import.meta.hot.data.webcontainer = webcontainer;
|
|
}
|
|
}
|