@astroscope/boot 0.3.2 → 0.3.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.
@@ -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
 
@@ -127,7 +128,7 @@ function serializeError(error) {
127
128
  }
128
129
 
129
130
  // src/hmr.ts
130
- function setupBootHmr(server, entry, logger, getBootContext) {
131
+ function setupBootHmr(server, entry, logger, getBootContext2) {
131
132
  const bootModuleId = `/${entry}`;
132
133
  const bootFilePath = import_node_path.default.resolve(server.config.root, entry);
133
134
  const getBootDependencies = () => {
@@ -155,7 +156,7 @@ function setupBootHmr(server, entry, logger, getBootContext) {
155
156
  const bootDeps = getBootDependencies();
156
157
  if (bootDeps.has(changedPath)) {
157
158
  logger.info(`boot dependency changed: ${changedPath}, rerunning hooks...`);
158
- const bootContext = getBootContext();
159
+ const bootContext = getBootContext2();
159
160
  try {
160
161
  const oldModule = await server.ssrLoadModule(bootModuleId);
161
162
  await runShutdown(oldModule, bootContext);
@@ -173,6 +174,15 @@ function setupBootHmr(server, entry, logger, getBootContext) {
173
174
  });
174
175
  }
175
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
+
176
186
  // src/warmup-manifest.ts
177
187
  var import_node_fs = __toESM(require("fs"), 1);
178
188
  var import_node_path2 = __toESM(require("path"), 1);
@@ -216,10 +226,18 @@ function getServerDefaults(config) {
216
226
  port: config?.server?.port ?? 4321
217
227
  };
218
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
+ }
219
238
  function boot(options = {}) {
220
239
  const entry = resolveEntry(options.entry);
221
240
  const hmr = options.hmr ?? false;
222
- let isBuild = false;
223
241
  let isSSR = false;
224
242
  let bootChunkRef = null;
225
243
  let astroConfig = null;
@@ -228,45 +246,13 @@ function boot(options = {}) {
228
246
  name: "@astroscope/boot",
229
247
  hooks: {
230
248
  "astro:config:setup": ({ command, updateConfig, logger, config }) => {
231
- isBuild = command === "build";
232
249
  astroConfig = config;
233
250
  updateConfig({
234
251
  vite: {
235
252
  plugins: [
253
+ // build plugin: handles entry.mjs injection, warmup manifest
236
254
  {
237
255
  name: "@astroscope/boot",
238
- enforce: "pre",
239
- async configureServer(server) {
240
- if (isBuild) return;
241
- const getBootContext = () => {
242
- const addr = server.httpServer?.address();
243
- if (addr && typeof addr === "object") {
244
- const host2 = addr.address === "::" || addr.address === "0.0.0.0" ? "localhost" : addr.address;
245
- return { dev: true, host: host2, port: addr.port };
246
- }
247
- const { host, port } = getServerDefaults(astroConfig);
248
- return { dev: true, host, port };
249
- };
250
- try {
251
- const bootContext = getBootContext();
252
- const module2 = await server.ssrLoadModule(`/${entry}`);
253
- await runStartup(module2, bootContext);
254
- } catch (error) {
255
- logger.error(`Error running startup script: ${serializeError(error)}`);
256
- }
257
- server.httpServer?.once("close", async () => {
258
- try {
259
- const bootContext = getBootContext();
260
- const module2 = await server.ssrLoadModule(`/${entry}`);
261
- await runShutdown(module2, bootContext);
262
- } catch (error) {
263
- logger.error(`Error running shutdown script: ${serializeError(error)}`);
264
- }
265
- });
266
- if (hmr) {
267
- setupBootHmr(server, entry, logger, getBootContext);
268
- }
269
- },
270
256
  configResolved(config2) {
271
257
  isSSR = !!config2.build?.ssr;
272
258
  },
@@ -291,13 +277,16 @@ function boot(options = {}) {
291
277
  }
292
278
  warmupModules = collectWarmupModules(bundle);
293
279
  const { host, port } = getServerDefaults(astroConfig);
294
- const bootImport = `globalThis.__astroscope_server_url = import.meta.url;
280
+ const prependCode = getPrependCode();
281
+ const prefix = prependCode.length ? `${prependCode.join("\n")}
282
+ ` : "";
283
+ const injection = `${prefix}globalThis.__astroscope_server_url = import.meta.url;
295
284
  import * as __astroscope_boot from './${bootChunkName}';
296
285
  import { setup as __astroscope_bootSetup } from '@astroscope/boot/setup';
297
286
  await __astroscope_bootSetup(__astroscope_boot, ${JSON.stringify({ host, port })});
298
287
  `;
299
288
  const s = new import_magic_string.default(entryChunk.code);
300
- s.prepend(bootImport);
289
+ s.prepend(injection);
301
290
  entryChunk.code = s.toString();
302
291
  if (entryChunk.map) {
303
292
  entryChunk.map = s.generateMap({ hires: true });
@@ -310,6 +299,33 @@ await __astroscope_bootSetup(__astroscope_boot, ${JSON.stringify({ host, port })
310
299
  if (!outDir) return;
311
300
  writeWarmupManifest(outDir, warmupModules, logger);
312
301
  }
302
+ },
303
+ // startup plugin: runs after all other configureServer hooks
304
+ {
305
+ name: "@astroscope/boot/startup",
306
+ enforce: "post",
307
+ async configureServer(server) {
308
+ if (command !== "dev") return;
309
+ try {
310
+ const bootContext = getBootContext(server, astroConfig);
311
+ const module2 = await server.ssrLoadModule(`/${entry}`);
312
+ await runStartup(module2, bootContext);
313
+ } catch (error) {
314
+ logger.error(`Error running startup script: ${serializeError(error)}`);
315
+ }
316
+ server.httpServer?.once("close", async () => {
317
+ try {
318
+ const bootContext = getBootContext(server, astroConfig);
319
+ const module2 = await server.ssrLoadModule(`/${entry}`);
320
+ await runShutdown(module2, bootContext);
321
+ } catch (error) {
322
+ logger.error(`Error running shutdown script: ${serializeError(error)}`);
323
+ }
324
+ });
325
+ if (hmr) {
326
+ setupBootHmr(server, entry, logger, () => getBootContext(server, astroConfig));
327
+ }
328
+ }
313
329
  }
314
330
  ]
315
331
  }
@@ -320,5 +336,6 @@ await __astroscope_bootSetup(__astroscope_boot, ${JSON.stringify({ host, port })
320
336
  }
321
337
  // Annotate the CommonJS export names for ESM import in node:
322
338
  0 && (module.exports = {
323
- boot
339
+ boot,
340
+ prepend
324
341
  });
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
@@ -63,7 +67,7 @@ function serializeError(error) {
63
67
  }
64
68
 
65
69
  // src/hmr.ts
66
- function setupBootHmr(server, entry, logger, getBootContext) {
70
+ function setupBootHmr(server, entry, logger, getBootContext2) {
67
71
  const bootModuleId = `/${entry}`;
68
72
  const bootFilePath = path.resolve(server.config.root, entry);
69
73
  const getBootDependencies = () => {
@@ -91,7 +95,7 @@ function setupBootHmr(server, entry, logger, getBootContext) {
91
95
  const bootDeps = getBootDependencies();
92
96
  if (bootDeps.has(changedPath)) {
93
97
  logger.info(`boot dependency changed: ${changedPath}, rerunning hooks...`);
94
- const bootContext = getBootContext();
98
+ const bootContext = getBootContext2();
95
99
  try {
96
100
  const oldModule = await server.ssrLoadModule(bootModuleId);
97
101
  await runShutdown(oldModule, bootContext);
@@ -152,10 +156,18 @@ function getServerDefaults(config) {
152
156
  port: config?.server?.port ?? 4321
153
157
  };
154
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
+ }
155
168
  function boot(options = {}) {
156
169
  const entry = resolveEntry(options.entry);
157
170
  const hmr = options.hmr ?? false;
158
- let isBuild = false;
159
171
  let isSSR = false;
160
172
  let bootChunkRef = null;
161
173
  let astroConfig = null;
@@ -164,45 +176,13 @@ function boot(options = {}) {
164
176
  name: "@astroscope/boot",
165
177
  hooks: {
166
178
  "astro:config:setup": ({ command, updateConfig, logger, config }) => {
167
- isBuild = command === "build";
168
179
  astroConfig = config;
169
180
  updateConfig({
170
181
  vite: {
171
182
  plugins: [
183
+ // build plugin: handles entry.mjs injection, warmup manifest
172
184
  {
173
185
  name: "@astroscope/boot",
174
- enforce: "pre",
175
- async configureServer(server) {
176
- if (isBuild) return;
177
- const getBootContext = () => {
178
- const addr = server.httpServer?.address();
179
- if (addr && typeof addr === "object") {
180
- const host2 = addr.address === "::" || addr.address === "0.0.0.0" ? "localhost" : addr.address;
181
- return { dev: true, host: host2, port: addr.port };
182
- }
183
- const { host, port } = getServerDefaults(astroConfig);
184
- return { dev: true, host, port };
185
- };
186
- try {
187
- const bootContext = getBootContext();
188
- const module = await server.ssrLoadModule(`/${entry}`);
189
- await runStartup(module, bootContext);
190
- } catch (error) {
191
- logger.error(`Error running startup script: ${serializeError(error)}`);
192
- }
193
- server.httpServer?.once("close", async () => {
194
- try {
195
- const bootContext = getBootContext();
196
- const module = await server.ssrLoadModule(`/${entry}`);
197
- await runShutdown(module, bootContext);
198
- } catch (error) {
199
- logger.error(`Error running shutdown script: ${serializeError(error)}`);
200
- }
201
- });
202
- if (hmr) {
203
- setupBootHmr(server, entry, logger, getBootContext);
204
- }
205
- },
206
186
  configResolved(config2) {
207
187
  isSSR = !!config2.build?.ssr;
208
188
  },
@@ -227,13 +207,16 @@ function boot(options = {}) {
227
207
  }
228
208
  warmupModules = collectWarmupModules(bundle);
229
209
  const { host, port } = getServerDefaults(astroConfig);
230
- const bootImport = `globalThis.__astroscope_server_url = import.meta.url;
210
+ const prependCode = getPrependCode();
211
+ const prefix = prependCode.length ? `${prependCode.join("\n")}
212
+ ` : "";
213
+ const injection = `${prefix}globalThis.__astroscope_server_url = import.meta.url;
231
214
  import * as __astroscope_boot from './${bootChunkName}';
232
215
  import { setup as __astroscope_bootSetup } from '@astroscope/boot/setup';
233
216
  await __astroscope_bootSetup(__astroscope_boot, ${JSON.stringify({ host, port })});
234
217
  `;
235
218
  const s = new MagicString(entryChunk.code);
236
- s.prepend(bootImport);
219
+ s.prepend(injection);
237
220
  entryChunk.code = s.toString();
238
221
  if (entryChunk.map) {
239
222
  entryChunk.map = s.generateMap({ hires: true });
@@ -246,6 +229,33 @@ await __astroscope_bootSetup(__astroscope_boot, ${JSON.stringify({ host, port })
246
229
  if (!outDir) return;
247
230
  writeWarmupManifest(outDir, warmupModules, logger);
248
231
  }
232
+ },
233
+ // startup plugin: runs after all other configureServer hooks
234
+ {
235
+ name: "@astroscope/boot/startup",
236
+ enforce: "post",
237
+ async configureServer(server) {
238
+ if (command !== "dev") return;
239
+ try {
240
+ const bootContext = getBootContext(server, astroConfig);
241
+ const module = await server.ssrLoadModule(`/${entry}`);
242
+ await runStartup(module, bootContext);
243
+ } catch (error) {
244
+ logger.error(`Error running startup script: ${serializeError(error)}`);
245
+ }
246
+ server.httpServer?.once("close", async () => {
247
+ try {
248
+ const bootContext = getBootContext(server, astroConfig);
249
+ const module = await server.ssrLoadModule(`/${entry}`);
250
+ await runShutdown(module, bootContext);
251
+ } catch (error) {
252
+ logger.error(`Error running shutdown script: ${serializeError(error)}`);
253
+ }
254
+ });
255
+ if (hmr) {
256
+ setupBootHmr(server, entry, logger, () => getBootContext(server, astroConfig));
257
+ }
258
+ }
249
259
  }
250
260
  ]
251
261
  }
@@ -256,5 +266,6 @@ await __astroscope_bootSetup(__astroscope_boot, ${JSON.stringify({ host, port })
256
266
  }
257
267
  export {
258
268
  boot,
259
- boot as default
269
+ boot as default,
270
+ prepend
260
271
  };
@@ -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.2",
3
+ "version": "0.3.4",
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,10 +53,10 @@
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
- "lint": "eslint 'src/**/*.{ts,tsx}'",
55
- "lint:fix": "eslint 'src/**/*.{ts,tsx}' --fix"
58
+ "lint": "eslint src",
59
+ "lint:fix": "eslint src --fix"
56
60
  },
57
61
  "devDependencies": {
58
62
  "astro": "^5.17.1",