@astroscope/boot 0.6.3 → 0.6.4

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.
package/dist/index.cjs CHANGED
@@ -142,7 +142,7 @@ async function ssrImport(server, moduleId) {
142
142
  }
143
143
 
144
144
  // src/hmr.ts
145
- function setupBootHmr(server, entry, logger, getBootContext2) {
145
+ function setupBootHmr(server, entry, logger, getBootContext2, initialModule) {
146
146
  const bootModuleId = `/${entry}`;
147
147
  const bootFilePath = import_node_path.default.resolve(server.config.root, entry);
148
148
  const getBootDependencies = () => {
@@ -165,8 +165,10 @@ function setupBootHmr(server, entry, logger, getBootContext2) {
165
165
  const p = filePath.toLowerCase();
166
166
  return ignoredSuffixes.some((suffix) => p.endsWith(suffix));
167
167
  };
168
+ let currentBootModule = initialModule;
168
169
  let running = false;
169
170
  let pendingReason;
171
+ let rerunPromise = null;
170
172
  const rerunBoot = async (reason) => {
171
173
  if (running) {
172
174
  pendingReason = reason;
@@ -177,8 +179,7 @@ function setupBootHmr(server, entry, logger, getBootContext2) {
177
179
  logger.info(`${reason}, rerunning hooks...`);
178
180
  const bootContext = getBootContext2();
179
181
  try {
180
- const oldModule = await ssrImport(server, bootModuleId);
181
- await runShutdown(oldModule, bootContext);
182
+ await runShutdown(currentBootModule, bootContext);
182
183
  } catch (error) {
183
184
  logger.error(`Error during boot HMR shutdown: ${serializeError(error)}`);
184
185
  }
@@ -186,6 +187,7 @@ function setupBootHmr(server, entry, logger, getBootContext2) {
186
187
  try {
187
188
  const newModule = await ssrImport(server, bootModuleId);
188
189
  await runStartup(newModule, bootContext);
190
+ currentBootModule = newModule;
189
191
  } catch (error) {
190
192
  logger.error(`Error during boot HMR startup: ${serializeError(error)}`);
191
193
  }
@@ -202,11 +204,38 @@ function setupBootHmr(server, entry, logger, getBootContext2) {
202
204
  if (shouldIgnore(changedPath)) return;
203
205
  const bootDeps = getBootDependencies();
204
206
  if (bootDeps.has(changedPath)) {
205
- await rerunBoot(`boot dependency changed: ${changedPath}`);
207
+ rerunPromise = rerunBoot(`boot dependency changed: ${changedPath}`);
208
+ await rerunPromise;
209
+ rerunPromise = null;
206
210
  }
207
211
  });
208
- server.hot.on("vite:beforeFullReload", async () => {
209
- await rerunBoot("full reload detected");
212
+ let handleFullReloads = false;
213
+ if (server.httpServer) {
214
+ server.httpServer.once("listening", () => {
215
+ handleFullReloads = true;
216
+ });
217
+ } else {
218
+ handleFullReloads = true;
219
+ }
220
+ const scheduleFullReloadRerun = (payload) => {
221
+ if (!handleFullReloads) return;
222
+ if (payload.type !== "full-reload") return;
223
+ const triggeredBy = "triggeredBy" in payload ? payload.triggeredBy : void 0;
224
+ if (triggeredBy && getBootDependencies().has(triggeredBy)) return;
225
+ rerunPromise = rerunBoot("full reload detected");
226
+ void rerunPromise.then(() => {
227
+ rerunPromise = null;
228
+ });
229
+ };
230
+ const ssrOutsideEmitter = server.environments["ssr"]?.hot?.api?.outsideEmitter;
231
+ if (ssrOutsideEmitter) {
232
+ ssrOutsideEmitter.on("send", scheduleFullReloadRerun);
233
+ }
234
+ server.middlewares.use(async (_req, _res, next) => {
235
+ if (rerunPromise) {
236
+ await rerunPromise;
237
+ }
238
+ next();
210
239
  });
211
240
  }
212
241
 
@@ -372,24 +401,25 @@ ${warmupEnd}`;
372
401
  enforce: "post",
373
402
  async configureServer(server) {
374
403
  if (command !== "dev") return;
404
+ let bootModule;
375
405
  try {
376
406
  const bootContext = getBootContext(server, astroConfig);
377
- const module2 = await ssrImport(server, `/${entry}`);
378
- await runStartup(module2, bootContext);
407
+ bootModule = await ssrImport(server, `/${entry}`);
408
+ await runStartup(bootModule, bootContext);
379
409
  } catch (error) {
380
410
  logger.error(`Error running startup script: ${serializeError(error)}`);
381
411
  }
382
412
  server.httpServer?.once("close", async () => {
383
413
  try {
384
414
  const bootContext = getBootContext(server, astroConfig);
385
- const module2 = await ssrImport(server, `/${entry}`);
386
- await runShutdown(module2, bootContext);
415
+ const mod = await ssrImport(server, `/${entry}`);
416
+ await runShutdown(mod, bootContext);
387
417
  } catch (error) {
388
418
  logger.error(`Error running shutdown script: ${serializeError(error)}`);
389
419
  }
390
420
  });
391
- if (hmr) {
392
- setupBootHmr(server, entry, logger, () => getBootContext(server, astroConfig));
421
+ if (hmr && bootModule) {
422
+ setupBootHmr(server, entry, logger, () => getBootContext(server, astroConfig), bootModule);
393
423
  }
394
424
  }
395
425
  }
package/dist/index.js CHANGED
@@ -79,7 +79,7 @@ async function ssrImport(server, moduleId) {
79
79
  }
80
80
 
81
81
  // src/hmr.ts
82
- function setupBootHmr(server, entry, logger, getBootContext2) {
82
+ function setupBootHmr(server, entry, logger, getBootContext2, initialModule) {
83
83
  const bootModuleId = `/${entry}`;
84
84
  const bootFilePath = path.resolve(server.config.root, entry);
85
85
  const getBootDependencies = () => {
@@ -102,8 +102,10 @@ function setupBootHmr(server, entry, logger, getBootContext2) {
102
102
  const p = filePath.toLowerCase();
103
103
  return ignoredSuffixes.some((suffix) => p.endsWith(suffix));
104
104
  };
105
+ let currentBootModule = initialModule;
105
106
  let running = false;
106
107
  let pendingReason;
108
+ let rerunPromise = null;
107
109
  const rerunBoot = async (reason) => {
108
110
  if (running) {
109
111
  pendingReason = reason;
@@ -114,8 +116,7 @@ function setupBootHmr(server, entry, logger, getBootContext2) {
114
116
  logger.info(`${reason}, rerunning hooks...`);
115
117
  const bootContext = getBootContext2();
116
118
  try {
117
- const oldModule = await ssrImport(server, bootModuleId);
118
- await runShutdown(oldModule, bootContext);
119
+ await runShutdown(currentBootModule, bootContext);
119
120
  } catch (error) {
120
121
  logger.error(`Error during boot HMR shutdown: ${serializeError(error)}`);
121
122
  }
@@ -123,6 +124,7 @@ function setupBootHmr(server, entry, logger, getBootContext2) {
123
124
  try {
124
125
  const newModule = await ssrImport(server, bootModuleId);
125
126
  await runStartup(newModule, bootContext);
127
+ currentBootModule = newModule;
126
128
  } catch (error) {
127
129
  logger.error(`Error during boot HMR startup: ${serializeError(error)}`);
128
130
  }
@@ -139,11 +141,38 @@ function setupBootHmr(server, entry, logger, getBootContext2) {
139
141
  if (shouldIgnore(changedPath)) return;
140
142
  const bootDeps = getBootDependencies();
141
143
  if (bootDeps.has(changedPath)) {
142
- await rerunBoot(`boot dependency changed: ${changedPath}`);
144
+ rerunPromise = rerunBoot(`boot dependency changed: ${changedPath}`);
145
+ await rerunPromise;
146
+ rerunPromise = null;
143
147
  }
144
148
  });
145
- server.hot.on("vite:beforeFullReload", async () => {
146
- await rerunBoot("full reload detected");
149
+ let handleFullReloads = false;
150
+ if (server.httpServer) {
151
+ server.httpServer.once("listening", () => {
152
+ handleFullReloads = true;
153
+ });
154
+ } else {
155
+ handleFullReloads = true;
156
+ }
157
+ const scheduleFullReloadRerun = (payload) => {
158
+ if (!handleFullReloads) return;
159
+ if (payload.type !== "full-reload") return;
160
+ const triggeredBy = "triggeredBy" in payload ? payload.triggeredBy : void 0;
161
+ if (triggeredBy && getBootDependencies().has(triggeredBy)) return;
162
+ rerunPromise = rerunBoot("full reload detected");
163
+ void rerunPromise.then(() => {
164
+ rerunPromise = null;
165
+ });
166
+ };
167
+ const ssrOutsideEmitter = server.environments["ssr"]?.hot?.api?.outsideEmitter;
168
+ if (ssrOutsideEmitter) {
169
+ ssrOutsideEmitter.on("send", scheduleFullReloadRerun);
170
+ }
171
+ server.middlewares.use(async (_req, _res, next) => {
172
+ if (rerunPromise) {
173
+ await rerunPromise;
174
+ }
175
+ next();
147
176
  });
148
177
  }
149
178
 
@@ -300,24 +329,25 @@ ${warmupEnd}`;
300
329
  enforce: "post",
301
330
  async configureServer(server) {
302
331
  if (command !== "dev") return;
332
+ let bootModule;
303
333
  try {
304
334
  const bootContext = getBootContext(server, astroConfig);
305
- const module = await ssrImport(server, `/${entry}`);
306
- await runStartup(module, bootContext);
335
+ bootModule = await ssrImport(server, `/${entry}`);
336
+ await runStartup(bootModule, bootContext);
307
337
  } catch (error) {
308
338
  logger.error(`Error running startup script: ${serializeError(error)}`);
309
339
  }
310
340
  server.httpServer?.once("close", async () => {
311
341
  try {
312
342
  const bootContext = getBootContext(server, astroConfig);
313
- const module = await ssrImport(server, `/${entry}`);
314
- await runShutdown(module, bootContext);
343
+ const mod = await ssrImport(server, `/${entry}`);
344
+ await runShutdown(mod, bootContext);
315
345
  } catch (error) {
316
346
  logger.error(`Error running shutdown script: ${serializeError(error)}`);
317
347
  }
318
348
  });
319
- if (hmr) {
320
- setupBootHmr(server, entry, logger, () => getBootContext(server, astroConfig));
349
+ if (hmr && bootModule) {
350
+ setupBootHmr(server, entry, logger, () => getBootContext(server, astroConfig), bootModule);
321
351
  }
322
352
  }
323
353
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astroscope/boot",
3
- "version": "0.6.3",
3
+ "version": "0.6.4",
4
4
  "description": "Startup and graceful shutdown hooks for Astro SSR",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",