@astroscope/boot 0.3.1 → 0.3.3

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.
@@ -0,0 +1,13 @@
1
+ // src/prepend.ts
2
+ var code = [];
3
+ function prepend(value) {
4
+ code.push(value);
5
+ }
6
+ function getPrependCode() {
7
+ return code;
8
+ }
9
+
10
+ export {
11
+ prepend,
12
+ getPrependCode
13
+ };
package/dist/index.cjs CHANGED
@@ -31,7 +31,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
31
31
  var index_exports = {};
32
32
  __export(index_exports, {
33
33
  boot: () => boot,
34
- default: () => boot
34
+ default: () => boot,
35
+ prepend: () => prepend
35
36
  });
36
37
  module.exports = __toCommonJS(index_exports);
37
38
 
@@ -118,8 +119,16 @@ async function runShutdown(boot2, context) {
118
119
  }
119
120
  }
120
121
 
122
+ // src/utils.ts
123
+ function serializeError(error) {
124
+ if (error instanceof Error) {
125
+ return error.stack ?? error.message;
126
+ }
127
+ return JSON.stringify(error);
128
+ }
129
+
121
130
  // src/hmr.ts
122
- function setupBootHmr(server, entry, logger, getBootContext) {
131
+ function setupBootHmr(server, entry, logger, getBootContext2) {
123
132
  const bootModuleId = `/${entry}`;
124
133
  const bootFilePath = import_node_path.default.resolve(server.config.root, entry);
125
134
  const getBootDependencies = () => {
@@ -147,24 +156,33 @@ function setupBootHmr(server, entry, logger, getBootContext) {
147
156
  const bootDeps = getBootDependencies();
148
157
  if (bootDeps.has(changedPath)) {
149
158
  logger.info(`boot dependency changed: ${changedPath}, rerunning hooks...`);
150
- const bootContext = getBootContext();
159
+ const bootContext = getBootContext2();
151
160
  try {
152
161
  const oldModule = await server.ssrLoadModule(bootModuleId);
153
162
  await runShutdown(oldModule, bootContext);
154
163
  } catch (error) {
155
- logger.error(`Error during boot HMR shutdown: ${error}`);
164
+ logger.error(`Error during boot HMR shutdown: ${serializeError(error)}`);
156
165
  }
157
166
  server.moduleGraph.invalidateAll();
158
167
  try {
159
168
  const newModule = await server.ssrLoadModule(bootModuleId);
160
169
  await runStartup(newModule, bootContext);
161
170
  } catch (error) {
162
- logger.error(`Error during boot HMR startup: ${error}`);
171
+ logger.error(`Error during boot HMR startup: ${serializeError(error)}`);
163
172
  }
164
173
  }
165
174
  });
166
175
  }
167
176
 
177
+ // src/prepend.ts
178
+ var code = [];
179
+ function prepend(value) {
180
+ code.push(value);
181
+ }
182
+ function getPrependCode() {
183
+ return code;
184
+ }
185
+
168
186
  // src/warmup-manifest.ts
169
187
  var import_node_fs = __toESM(require("fs"), 1);
170
188
  var import_node_path2 = __toESM(require("path"), 1);
@@ -208,6 +226,15 @@ function getServerDefaults(config) {
208
226
  port: config?.server?.port ?? 4321
209
227
  };
210
228
  }
229
+ function getBootContext(server, config) {
230
+ const addr = server.httpServer?.address();
231
+ if (addr && typeof addr === "object" && "address" in addr && "port" in addr) {
232
+ const host2 = addr.address === "::" || addr.address === "0.0.0.0" ? "localhost" : addr.address;
233
+ return { dev: true, host: host2, port: addr.port };
234
+ }
235
+ const { host, port } = getServerDefaults(config);
236
+ return { dev: true, host, port };
237
+ }
211
238
  function boot(options = {}) {
212
239
  const entry = resolveEntry(options.entry);
213
240
  const hmr = options.hmr ?? false;
@@ -225,40 +252,9 @@ function boot(options = {}) {
225
252
  updateConfig({
226
253
  vite: {
227
254
  plugins: [
255
+ // build plugin: handles entry.mjs injection, warmup manifest
228
256
  {
229
257
  name: "@astroscope/boot",
230
- enforce: "pre",
231
- async configureServer(server) {
232
- if (isBuild) return;
233
- const getBootContext = () => {
234
- const addr = server.httpServer?.address();
235
- if (addr && typeof addr === "object") {
236
- const host2 = addr.address === "::" || addr.address === "0.0.0.0" ? "localhost" : addr.address;
237
- return { dev: true, host: host2, port: addr.port };
238
- }
239
- const { host, port } = getServerDefaults(astroConfig);
240
- return { dev: true, host, port };
241
- };
242
- try {
243
- const bootContext = getBootContext();
244
- const module2 = await server.ssrLoadModule(`/${entry}`);
245
- await runStartup(module2, bootContext);
246
- } catch (error) {
247
- logger.error(`Error running startup script: ${error}`);
248
- }
249
- server.httpServer?.once("close", async () => {
250
- try {
251
- const bootContext = getBootContext();
252
- const module2 = await server.ssrLoadModule(`/${entry}`);
253
- await runShutdown(module2, bootContext);
254
- } catch (error) {
255
- logger.error(`Error running shutdown script: ${error}`);
256
- }
257
- });
258
- if (hmr) {
259
- setupBootHmr(server, entry, logger, getBootContext);
260
- }
261
- },
262
258
  configResolved(config2) {
263
259
  isSSR = !!config2.build?.ssr;
264
260
  },
@@ -283,13 +279,16 @@ function boot(options = {}) {
283
279
  }
284
280
  warmupModules = collectWarmupModules(bundle);
285
281
  const { host, port } = getServerDefaults(astroConfig);
286
- const bootImport = `globalThis.__astroscope_server_url = import.meta.url;
282
+ const prependCode = getPrependCode();
283
+ const prefix = prependCode.length ? `${prependCode.join("\n")}
284
+ ` : "";
285
+ const injection = `${prefix}globalThis.__astroscope_server_url = import.meta.url;
287
286
  import * as __astroscope_boot from './${bootChunkName}';
288
287
  import { setup as __astroscope_bootSetup } from '@astroscope/boot/setup';
289
288
  await __astroscope_bootSetup(__astroscope_boot, ${JSON.stringify({ host, port })});
290
289
  `;
291
290
  const s = new import_magic_string.default(entryChunk.code);
292
- s.prepend(bootImport);
291
+ s.prepend(injection);
293
292
  entryChunk.code = s.toString();
294
293
  if (entryChunk.map) {
295
294
  entryChunk.map = s.generateMap({ hires: true });
@@ -302,6 +301,33 @@ await __astroscope_bootSetup(__astroscope_boot, ${JSON.stringify({ host, port })
302
301
  if (!outDir) return;
303
302
  writeWarmupManifest(outDir, warmupModules, logger);
304
303
  }
304
+ },
305
+ // startup plugin: runs after all other configureServer hooks
306
+ {
307
+ name: "@astroscope/boot/startup",
308
+ enforce: "post",
309
+ async configureServer(server) {
310
+ if (isBuild) return;
311
+ try {
312
+ const bootContext = getBootContext(server, astroConfig);
313
+ const module2 = await server.ssrLoadModule(`/${entry}`);
314
+ await runStartup(module2, bootContext);
315
+ } catch (error) {
316
+ logger.error(`Error running startup script: ${serializeError(error)}`);
317
+ }
318
+ server.httpServer?.once("close", async () => {
319
+ try {
320
+ const bootContext = getBootContext(server, astroConfig);
321
+ const module2 = await server.ssrLoadModule(`/${entry}`);
322
+ await runShutdown(module2, bootContext);
323
+ } catch (error) {
324
+ logger.error(`Error running shutdown script: ${serializeError(error)}`);
325
+ }
326
+ });
327
+ if (hmr) {
328
+ setupBootHmr(server, entry, logger, () => getBootContext(server, astroConfig));
329
+ }
330
+ }
305
331
  }
306
332
  ]
307
333
  }
@@ -312,5 +338,6 @@ await __astroscope_bootSetup(__astroscope_boot, ${JSON.stringify({ host, port })
312
338
  }
313
339
  // Annotate the CommonJS export names for ESM import in node:
314
340
  0 && (module.exports = {
315
- boot
341
+ boot,
342
+ prepend
316
343
  });
package/dist/index.d.cts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { AstroIntegration } from 'astro';
2
2
  export { B as BootContext, W as WarmupResult } from './types-CxpusND2.cjs';
3
3
  export { BootEventHandler, BootEventName } from './events.cjs';
4
+ export { prepend } from './prepend.cjs';
4
5
 
5
6
  interface BootOptions {
6
7
  /**
package/dist/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { AstroIntegration } from 'astro';
2
2
  export { B as BootContext, W as WarmupResult } from './types-CxpusND2.js';
3
3
  export { BootEventHandler, BootEventName } from './events.js';
4
+ export { prepend } from './prepend.js';
4
5
 
5
6
  interface BootOptions {
6
7
  /**
package/dist/index.js CHANGED
@@ -1,3 +1,7 @@
1
+ import {
2
+ getPrependCode,
3
+ prepend
4
+ } from "./chunk-4PQTRKEQ.js";
1
5
  import {
2
6
  runShutdown,
3
7
  runStartup
@@ -54,8 +58,16 @@ var ignoredSuffixes = [
54
58
  ".less"
55
59
  ];
56
60
 
61
+ // src/utils.ts
62
+ function serializeError(error) {
63
+ if (error instanceof Error) {
64
+ return error.stack ?? error.message;
65
+ }
66
+ return JSON.stringify(error);
67
+ }
68
+
57
69
  // src/hmr.ts
58
- function setupBootHmr(server, entry, logger, getBootContext) {
70
+ function setupBootHmr(server, entry, logger, getBootContext2) {
59
71
  const bootModuleId = `/${entry}`;
60
72
  const bootFilePath = path.resolve(server.config.root, entry);
61
73
  const getBootDependencies = () => {
@@ -83,19 +95,19 @@ function setupBootHmr(server, entry, logger, getBootContext) {
83
95
  const bootDeps = getBootDependencies();
84
96
  if (bootDeps.has(changedPath)) {
85
97
  logger.info(`boot dependency changed: ${changedPath}, rerunning hooks...`);
86
- const bootContext = getBootContext();
98
+ const bootContext = getBootContext2();
87
99
  try {
88
100
  const oldModule = await server.ssrLoadModule(bootModuleId);
89
101
  await runShutdown(oldModule, bootContext);
90
102
  } catch (error) {
91
- logger.error(`Error during boot HMR shutdown: ${error}`);
103
+ logger.error(`Error during boot HMR shutdown: ${serializeError(error)}`);
92
104
  }
93
105
  server.moduleGraph.invalidateAll();
94
106
  try {
95
107
  const newModule = await server.ssrLoadModule(bootModuleId);
96
108
  await runStartup(newModule, bootContext);
97
109
  } catch (error) {
98
- logger.error(`Error during boot HMR startup: ${error}`);
110
+ logger.error(`Error during boot HMR startup: ${serializeError(error)}`);
99
111
  }
100
112
  }
101
113
  });
@@ -144,6 +156,15 @@ function getServerDefaults(config) {
144
156
  port: config?.server?.port ?? 4321
145
157
  };
146
158
  }
159
+ function getBootContext(server, config) {
160
+ const addr = server.httpServer?.address();
161
+ if (addr && typeof addr === "object" && "address" in addr && "port" in addr) {
162
+ const host2 = addr.address === "::" || addr.address === "0.0.0.0" ? "localhost" : addr.address;
163
+ return { dev: true, host: host2, port: addr.port };
164
+ }
165
+ const { host, port } = getServerDefaults(config);
166
+ return { dev: true, host, port };
167
+ }
147
168
  function boot(options = {}) {
148
169
  const entry = resolveEntry(options.entry);
149
170
  const hmr = options.hmr ?? false;
@@ -161,40 +182,9 @@ function boot(options = {}) {
161
182
  updateConfig({
162
183
  vite: {
163
184
  plugins: [
185
+ // build plugin: handles entry.mjs injection, warmup manifest
164
186
  {
165
187
  name: "@astroscope/boot",
166
- enforce: "pre",
167
- async configureServer(server) {
168
- if (isBuild) return;
169
- const getBootContext = () => {
170
- const addr = server.httpServer?.address();
171
- if (addr && typeof addr === "object") {
172
- const host2 = addr.address === "::" || addr.address === "0.0.0.0" ? "localhost" : addr.address;
173
- return { dev: true, host: host2, port: addr.port };
174
- }
175
- const { host, port } = getServerDefaults(astroConfig);
176
- return { dev: true, host, port };
177
- };
178
- try {
179
- const bootContext = getBootContext();
180
- const module = await server.ssrLoadModule(`/${entry}`);
181
- await runStartup(module, bootContext);
182
- } catch (error) {
183
- logger.error(`Error running startup script: ${error}`);
184
- }
185
- server.httpServer?.once("close", async () => {
186
- try {
187
- const bootContext = getBootContext();
188
- const module = await server.ssrLoadModule(`/${entry}`);
189
- await runShutdown(module, bootContext);
190
- } catch (error) {
191
- logger.error(`Error running shutdown script: ${error}`);
192
- }
193
- });
194
- if (hmr) {
195
- setupBootHmr(server, entry, logger, getBootContext);
196
- }
197
- },
198
188
  configResolved(config2) {
199
189
  isSSR = !!config2.build?.ssr;
200
190
  },
@@ -219,13 +209,16 @@ function boot(options = {}) {
219
209
  }
220
210
  warmupModules = collectWarmupModules(bundle);
221
211
  const { host, port } = getServerDefaults(astroConfig);
222
- const bootImport = `globalThis.__astroscope_server_url = import.meta.url;
212
+ const prependCode = getPrependCode();
213
+ const prefix = prependCode.length ? `${prependCode.join("\n")}
214
+ ` : "";
215
+ const injection = `${prefix}globalThis.__astroscope_server_url = import.meta.url;
223
216
  import * as __astroscope_boot from './${bootChunkName}';
224
217
  import { setup as __astroscope_bootSetup } from '@astroscope/boot/setup';
225
218
  await __astroscope_bootSetup(__astroscope_boot, ${JSON.stringify({ host, port })});
226
219
  `;
227
220
  const s = new MagicString(entryChunk.code);
228
- s.prepend(bootImport);
221
+ s.prepend(injection);
229
222
  entryChunk.code = s.toString();
230
223
  if (entryChunk.map) {
231
224
  entryChunk.map = s.generateMap({ hires: true });
@@ -238,6 +231,33 @@ await __astroscope_bootSetup(__astroscope_boot, ${JSON.stringify({ host, port })
238
231
  if (!outDir) return;
239
232
  writeWarmupManifest(outDir, warmupModules, logger);
240
233
  }
234
+ },
235
+ // startup plugin: runs after all other configureServer hooks
236
+ {
237
+ name: "@astroscope/boot/startup",
238
+ enforce: "post",
239
+ async configureServer(server) {
240
+ if (isBuild) return;
241
+ try {
242
+ const bootContext = getBootContext(server, astroConfig);
243
+ const module = await server.ssrLoadModule(`/${entry}`);
244
+ await runStartup(module, bootContext);
245
+ } catch (error) {
246
+ logger.error(`Error running startup script: ${serializeError(error)}`);
247
+ }
248
+ server.httpServer?.once("close", async () => {
249
+ try {
250
+ const bootContext = getBootContext(server, astroConfig);
251
+ const module = await server.ssrLoadModule(`/${entry}`);
252
+ await runShutdown(module, bootContext);
253
+ } catch (error) {
254
+ logger.error(`Error running shutdown script: ${serializeError(error)}`);
255
+ }
256
+ });
257
+ if (hmr) {
258
+ setupBootHmr(server, entry, logger, () => getBootContext(server, astroConfig));
259
+ }
260
+ }
241
261
  }
242
262
  ]
243
263
  }
@@ -248,5 +268,6 @@ await __astroscope_bootSetup(__astroscope_boot, ${JSON.stringify({ host, port })
248
268
  }
249
269
  export {
250
270
  boot,
251
- boot as default
271
+ boot as default,
272
+ prepend
252
273
  };
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/prepend.ts
21
+ var prepend_exports = {};
22
+ __export(prepend_exports, {
23
+ getPrependCode: () => getPrependCode,
24
+ prepend: () => prepend
25
+ });
26
+ module.exports = __toCommonJS(prepend_exports);
27
+ var code = [];
28
+ function prepend(value) {
29
+ code.push(value);
30
+ }
31
+ function getPrependCode() {
32
+ return code;
33
+ }
34
+ // Annotate the CommonJS export names for ESM import in node:
35
+ 0 && (module.exports = {
36
+ getPrependCode,
37
+ prepend
38
+ });
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Register code to prepend to `entry.mjs` before boot's startup.
3
+ *
4
+ * Used by integrations (e.g. `@astroscope/health`) to ensure their
5
+ * setup runs before `onStartup` in production builds.
6
+ */
7
+ declare function prepend(value: string): void;
8
+ /**
9
+ * Get all registered prepend code strings.
10
+ * Used internally by boot's generateBundle.
11
+ */
12
+ declare function getPrependCode(): string[];
13
+
14
+ export { getPrependCode, prepend };
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Register code to prepend to `entry.mjs` before boot's startup.
3
+ *
4
+ * Used by integrations (e.g. `@astroscope/health`) to ensure their
5
+ * setup runs before `onStartup` in production builds.
6
+ */
7
+ declare function prepend(value: string): void;
8
+ /**
9
+ * Get all registered prepend code strings.
10
+ * Used internally by boot's generateBundle.
11
+ */
12
+ declare function getPrependCode(): string[];
13
+
14
+ export { getPrependCode, prepend };
@@ -0,0 +1,8 @@
1
+ import {
2
+ getPrependCode,
3
+ prepend
4
+ } from "./chunk-4PQTRKEQ.js";
5
+ export {
6
+ getPrependCode,
7
+ prepend
8
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astroscope/boot",
3
- "version": "0.3.1",
3
+ "version": "0.3.3",
4
4
  "description": "Startup and graceful shutdown hooks for Astro SSR",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -19,6 +19,10 @@
19
19
  "types": "./dist/events.d.ts",
20
20
  "import": "./dist/events.js"
21
21
  },
22
+ "./prepend": {
23
+ "types": "./dist/prepend.d.ts",
24
+ "import": "./dist/prepend.js"
25
+ },
22
26
  "./setup": {
23
27
  "types": "./dist/setup.d.ts",
24
28
  "import": "./dist/setup.js"
@@ -49,7 +53,7 @@
49
53
  },
50
54
  "homepage": "https://github.com/smnbbrv/astroscope/tree/main/packages/boot#readme",
51
55
  "scripts": {
52
- "build": "tsup src/index.ts src/warmup.ts src/events.ts src/lifecycle.ts src/setup.ts --format esm,cjs --dts",
56
+ "build": "tsup src/index.ts src/warmup.ts src/events.ts src/lifecycle.ts src/setup.ts src/prepend.ts --format esm,cjs --dts",
53
57
  "typecheck": "tsc --noEmit",
54
58
  "lint": "eslint 'src/**/*.{ts,tsx}'",
55
59
  "lint:fix": "eslint 'src/**/*.{ts,tsx}' --fix"