@goauthentik/esbuild-plugin-live-reload 1.6.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,231 +0,0 @@
1
- /**
2
- * @file Client-side observer for ESBuild events.
3
- *
4
- * @import { Logger } from "@goauthentik/esbuild-plugin-live-reload/shared";
5
- * @import { Message as ESBuildMessage } from "esbuild";
6
- */
7
-
8
- /// <reference types="./types.js" />
9
-
10
- import { createLogger } from "@goauthentik/esbuild-plugin-live-reload/shared";
11
-
12
- if (typeof EventSource === "undefined") {
13
- throw new TypeError("Environment doesn't appear to have an EventSource constructor");
14
- }
15
-
16
- /**
17
- * @template {unknown} [Data=unknown]
18
- * @typedef {(event: MessageEvent) => void} BuildEventListener
19
- */
20
-
21
- /**
22
- * A client-side watcher for ESBuild.
23
- *
24
- * Note that this should be conditionally imported in your code, so that
25
- * ESBuild may tree-shake it out of production builds.
26
- *
27
- * ```ts
28
- * if (process.env.NODE_ENV === "development") {
29
- * await import("@goauthentik/esbuild-plugin-live-reload/client")
30
- * .catch(() => console.warn("Failed to import watcher"))
31
- * }
32
- * ```
33
- *
34
- * @implements {Disposable}
35
- * @category Plugin
36
- * runtime browser
37
- */
38
- export class ESBuildObserver extends EventSource {
39
- /**
40
- * @type {Logger}
41
- */
42
- #logger;
43
-
44
- /**
45
- * Whether the watcher has a recent connection to the server.
46
- */
47
- alive = true;
48
-
49
- /**
50
- * The number of errors that have occurred since the watcher started.
51
- */
52
- errorCount = 0;
53
-
54
- /**
55
- * Whether a reload has been requested while offline.
56
- */
57
- deferredReload = false;
58
-
59
- /**
60
- * The last time a message was received from the server.
61
- */
62
- lastUpdatedAt = Date.now();
63
-
64
- /**
65
- * Whether the browser considers itself online.
66
- */
67
- online = true;
68
-
69
- /**
70
- * The ID of the animation frame for the reload.
71
- */
72
- #reloadFrameID = -1;
73
-
74
- /**
75
- * The interval for the keep-alive check.
76
- * @type {ReturnType<typeof setInterval> | undefined}
77
- */
78
- #keepAliveInterval;
79
-
80
- #trackActivity = () => {
81
- this.lastUpdatedAt = Date.now();
82
- this.alive = true;
83
- };
84
-
85
- /**
86
- * @type {BuildEventListener}
87
- */
88
- #startListener = () => {
89
- this.#trackActivity();
90
- this.#logger.info("⏰ Build started...");
91
- };
92
-
93
- #internalErrorListener = () => {
94
- this.errorCount += 1;
95
-
96
- if (this.errorCount > 100) {
97
- clearTimeout(this.#keepAliveInterval);
98
-
99
- this.close();
100
- this.#logger.info("⛔️ Closing connection");
101
- }
102
- };
103
-
104
- /**
105
- * @type {BuildEventListener<string>}
106
- */
107
- #errorListener = (event) => {
108
- this.#trackActivity();
109
-
110
- this.#logger.warn("⛔️⛔️⛔️ Build error...");
111
-
112
- /**
113
- * @type {ESBuildMessage[]}
114
- */
115
- const esbuildErrorMessages = JSON.parse(event.data);
116
-
117
- for (const error of esbuildErrorMessages) {
118
- this.#logger.warn(error.text);
119
-
120
- if (error.location) {
121
- this.#logger.debug(
122
- `file://${error.location.file}:${error.location.line}:${error.location.column}`,
123
- );
124
- this.#logger.debug(error.location.lineText);
125
- }
126
- }
127
- };
128
-
129
- /**
130
- * @type {BuildEventListener}
131
- */
132
- #endListener = () => {
133
- cancelAnimationFrame(this.#reloadFrameID);
134
-
135
- this.#trackActivity();
136
-
137
- if (!this.online) {
138
- this.#logger.info("🚫 Build finished while offline.");
139
- this.deferredReload = true;
140
-
141
- return;
142
- }
143
-
144
- this.#logger.info("🛎️ Build completed! Reloading...");
145
-
146
- // We use an animation frame to keep the reload from happening before the
147
- // event loop has a chance to process the message.
148
- this.#reloadFrameID = requestAnimationFrame(() => {
149
- window.location.reload();
150
- });
151
- };
152
-
153
- /**
154
- * @type {BuildEventListener}
155
- */
156
- #keepAliveListener = () => {
157
- this.#trackActivity();
158
- this.#logger.info("🏓 Keep-alive");
159
- };
160
-
161
- /**
162
- * Initialize the ESBuild observer.
163
- * This should be called once in your application.
164
- *
165
- * @param {string | URL} [url]
166
- * @returns {ESBuildObserver}
167
- */
168
- static initialize = (url) => {
169
- const esbuildObserver = new ESBuildObserver(url);
170
-
171
- return esbuildObserver;
172
- };
173
-
174
- /**
175
- *
176
- * @param {string | URL} [url]
177
- * @param {Logger} [logger]
178
- */
179
- constructor(url, logger = createLogger()) {
180
- if (!url) {
181
- throw new TypeError("ESBuildObserver: Cannot construct without a URL");
182
- }
183
-
184
- super(url);
185
-
186
- this.#logger = logger;
187
-
188
- this.addEventListener("esbuild:start", this.#startListener);
189
- this.addEventListener("esbuild:end", this.#endListener);
190
- this.addEventListener("esbuild:error", this.#errorListener);
191
- this.addEventListener("esbuild:keep-alive", this.#keepAliveListener);
192
-
193
- this.addEventListener("error", this.#internalErrorListener);
194
-
195
- window.addEventListener("offline", () => {
196
- this.online = false;
197
- });
198
-
199
- window.addEventListener("online", () => {
200
- this.online = true;
201
-
202
- if (!this.deferredReload) return;
203
-
204
- this.#logger.info("🛎️ Reloading after offline build...");
205
- this.deferredReload = false;
206
-
207
- window.location.reload();
208
- });
209
-
210
- this.#logger.info("🛎️ Listening for build changes...");
211
-
212
- this.#keepAliveInterval = setInterval(() => {
213
- const now = Date.now();
214
-
215
- if (now - this.lastUpdatedAt < 10_000) return;
216
-
217
- this.alive = false;
218
- this.#logger.info("👋 Waiting for build to start...");
219
- }, 15_000);
220
- }
221
-
222
- [Symbol.dispose]() {
223
- return this.close();
224
- }
225
-
226
- dispose() {
227
- return this[Symbol.dispose]();
228
- }
229
- }
230
-
231
- export default ESBuildObserver;
@@ -1,64 +0,0 @@
1
- /**
2
- * @template {unknown} [Data=unknown]
3
- * @typedef {(event: MessageEvent) => void} BuildEventListener
4
- */
5
- /**
6
- * A client-side watcher for ESBuild.
7
- *
8
- * Note that this should be conditionally imported in your code, so that
9
- * ESBuild may tree-shake it out of production builds.
10
- *
11
- * ```ts
12
- * if (process.env.NODE_ENV === "development") {
13
- * await import("@goauthentik/esbuild-plugin-live-reload/client")
14
- * .catch(() => console.warn("Failed to import watcher"))
15
- * }
16
- * ```
17
- *
18
- * @implements {Disposable}
19
- * @category Plugin
20
- * runtime browser
21
- */
22
- export class ESBuildObserver extends EventSource implements Disposable {
23
- /**
24
- * Initialize the ESBuild observer.
25
- * This should be called once in your application.
26
- *
27
- * @param {string | URL} [url]
28
- * @returns {ESBuildObserver}
29
- */
30
- static initialize: (url?: string | URL) => ESBuildObserver;
31
- /**
32
- *
33
- * @param {string | URL} [url]
34
- * @param {Logger} [logger]
35
- */
36
- constructor(url?: string | URL, logger?: Logger);
37
- /**
38
- * Whether the watcher has a recent connection to the server.
39
- */
40
- alive: boolean;
41
- /**
42
- * The number of errors that have occurred since the watcher started.
43
- */
44
- errorCount: number;
45
- /**
46
- * Whether a reload has been requested while offline.
47
- */
48
- deferredReload: boolean;
49
- /**
50
- * The last time a message was received from the server.
51
- */
52
- lastUpdatedAt: number;
53
- /**
54
- * Whether the browser considers itself online.
55
- */
56
- online: boolean;
57
- dispose(): void;
58
- [Symbol.dispose](): void;
59
- #private;
60
- }
61
- export default ESBuildObserver;
62
- export type BuildEventListener<Data extends unknown = unknown> = (event: MessageEvent) => void;
63
- import type { Logger } from "@goauthentik/esbuild-plugin-live-reload/shared";
64
- //# sourceMappingURL=ESBuildObserver.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ESBuildObserver.d.ts","sourceRoot":"","sources":["../../client/ESBuildObserver.js"],"names":[],"mappings":"AAeA;;;GAGG;AAEH;;;;;;;;;;;;;;;;GAgBG;AACH,4DAJgB,UAAU;IA+HtB;;;;;;OAMG;IACH,oBAAqB,MAHV,MAAM,GAAG,GAGI,KAFX,eAAe,CAM1B;IAEF;;;;OAIG;IACH,kBAHW,MAAM,GAAG,GAAG,WACZ,MAAM,EA2ChB;IAhLD;;OAEG;IACH,eAAa;IAEb;;OAEG;IACH,mBAAe;IAEf;;OAEG;IACH,wBAAuB;IAEvB;;OAEG;IACH,sBAA2B;IAE3B;;OAEG;IACH,gBAAc;IA+Jd,gBAEC;IAND,yBAEC;;CAKJ;;+BApNuB,IAAI,SAAf,OAAS,cACT,CAAC,KAAK,EAAE,YAAY,KAAK,IAAI;4BAdf,gDAAgD"}