Skip to content

Commit 784cefe

Browse files
committedNov 13, 2024
Fix dynamic import of atmosphere
Fixes #2867
1 parent dfca9e8 commit 784cefe

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed
 

‎packages/ts/frontend/src/FluxConnection.ts

+10-20
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import type { ReactiveControllerHost } from '@lit/reactive-element';
2-
32
import type Atmosphere from 'atmosphere.js';
4-
53
import type { Subscription } from './Connect.js';
64
import { getCsrfTokenHeadersForEndpointRequest } from './CsrfUtils.js';
75
import {
@@ -75,14 +73,6 @@ type EndpointInfo = {
7573
reconnect?(): ActionOnLostSubscription | void;
7674
};
7775

78-
interface ImportMetaEnv {
79-
readonly SW_CONTEXT: boolean;
80-
}
81-
82-
interface ImportMeta {
83-
readonly env: ImportMetaEnv;
84-
}
85-
8676
/**
8777
* A representation of the underlying persistent network connection used for subscribing to Flux type endpoint methods.
8878
*/
@@ -101,16 +91,16 @@ export class FluxConnection extends EventTarget {
10191

10292
constructor(connectPrefix: string, atmosphereOptions?: Partial<Atmosphere.Request>) {
10393
super();
104-
// @ts-expect-error - vite environment variable
105-
const meta: ImportMeta = import.meta;
106-
if (!meta.env.SW_CONTEXT) {
107-
(async () => {
108-
atmosphere = await import('atmosphere.js');
109-
this.#connectWebsocket(connectPrefix.replace(/connect$/u, ''), atmosphereOptions ?? {});
110-
})().catch((e) => {
111-
// eslint-disable-next-line no-console
112-
console.error('Failed to load atmosphere.js', e);
113-
});
94+
if (!import.meta.env['VITE_SW_CONTEXT']) {
95+
import('atmosphere.js')
96+
.then((module) => {
97+
atmosphere = module.default;
98+
this.#connectWebsocket(connectPrefix.replace(/connect$/u, ''), atmosphereOptions ?? {});
99+
})
100+
.catch((error) => {
101+
// eslint-disable-next-line no-console
102+
console.error('Failed to load atmosphere', error);
103+
});
114104
}
115105
}
116106

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// / <reference types="vite/client" />
2+
3+
interface ImportMetaEnv {
4+
readonly VITE_SW_CONTEXT: boolean;
5+
}
6+
7+
interface ImportMeta {
8+
readonly env: ImportMetaEnv;
9+
}
10+
11+
export {};

0 commit comments

Comments
 (0)