@astroscope/boot 0.3.4 → 0.4.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.
package/dist/index.cjs CHANGED
@@ -39,6 +39,7 @@ module.exports = __toCommonJS(index_exports);
39
39
  // src/integration.ts
40
40
  var import_node_fs2 = __toESM(require("fs"), 1);
41
41
  var import_magic_string = __toESM(require("magic-string"), 1);
42
+ var import_vite2 = require("vite");
42
43
 
43
44
  // src/hmr.ts
44
45
  var import_node_path = __toESM(require("path"), 1);
@@ -127,6 +128,16 @@ function serializeError(error) {
127
128
  return JSON.stringify(error);
128
129
  }
129
130
 
131
+ // src/vite-env.ts
132
+ var import_vite = require("vite");
133
+ async function ssrImport(server, moduleId) {
134
+ const ssr = server.environments["ssr"];
135
+ if (!(0, import_vite.isRunnableDevEnvironment)(ssr)) {
136
+ throw new Error("SSR environment is not runnable");
137
+ }
138
+ return ssr.runner.import(moduleId);
139
+ }
140
+
130
141
  // src/hmr.ts
131
142
  function setupBootHmr(server, entry, logger, getBootContext2) {
132
143
  const bootModuleId = `/${entry}`;
@@ -158,14 +169,14 @@ function setupBootHmr(server, entry, logger, getBootContext2) {
158
169
  logger.info(`boot dependency changed: ${changedPath}, rerunning hooks...`);
159
170
  const bootContext = getBootContext2();
160
171
  try {
161
- const oldModule = await server.ssrLoadModule(bootModuleId);
172
+ const oldModule = await ssrImport(server, bootModuleId);
162
173
  await runShutdown(oldModule, bootContext);
163
174
  } catch (error) {
164
175
  logger.error(`Error during boot HMR shutdown: ${serializeError(error)}`);
165
176
  }
166
177
  server.moduleGraph.invalidateAll();
167
178
  try {
168
- const newModule = await server.ssrLoadModule(bootModuleId);
179
+ const newModule = await ssrImport(server, bootModuleId);
169
180
  await runStartup(newModule, bootContext);
170
181
  } catch (error) {
171
182
  logger.error(`Error during boot HMR startup: ${serializeError(error)}`);
@@ -235,13 +246,11 @@ function getBootContext(server, config) {
235
246
  const { host, port } = getServerDefaults(config);
236
247
  return { dev: true, host, port };
237
248
  }
249
+ var getState = (0, import_vite2.perEnvironmentState)(() => ({ bootChunkRef: null, warmupModules: null }));
238
250
  function boot(options = {}) {
239
251
  const entry = resolveEntry(options.entry);
240
252
  const hmr = options.hmr ?? false;
241
- let isSSR = false;
242
- let bootChunkRef = null;
243
253
  let astroConfig = null;
244
- let warmupModules = null;
245
254
  return {
246
255
  name: "@astroscope/boot",
247
256
  hooks: {
@@ -253,19 +262,18 @@ function boot(options = {}) {
253
262
  // build plugin: handles entry.mjs injection, warmup manifest
254
263
  {
255
264
  name: "@astroscope/boot",
256
- configResolved(config2) {
257
- isSSR = !!config2.build?.ssr;
258
- },
259
265
  buildStart() {
260
- if (!isSSR) return;
266
+ if (this.environment.name !== "ssr") return;
267
+ const state = getState(this);
261
268
  try {
262
- bootChunkRef = this.emitFile({ type: "chunk", id: entry, name: "boot" });
269
+ state.bootChunkRef = this.emitFile({ type: "chunk", id: entry, name: "boot" });
263
270
  } catch {
264
271
  }
265
272
  },
266
273
  generateBundle(_, bundle) {
267
- if (!isSSR || !bootChunkRef) return;
268
- const bootChunkName = this.getFileName(bootChunkRef);
274
+ const state = getState(this);
275
+ if (!state.bootChunkRef) return;
276
+ const bootChunkName = this.getFileName(state.bootChunkRef);
269
277
  if (!bootChunkName) {
270
278
  logger.warn("boot chunk not found");
271
279
  return;
@@ -275,7 +283,7 @@ function boot(options = {}) {
275
283
  logger.warn("entry.mjs not found - boot injection skipped");
276
284
  return;
277
285
  }
278
- warmupModules = collectWarmupModules(bundle);
286
+ state.warmupModules = collectWarmupModules(bundle);
279
287
  const { host, port } = getServerDefaults(astroConfig);
280
288
  const prependCode = getPrependCode();
281
289
  const prefix = prependCode.length ? `${prependCode.join("\n")}
@@ -294,10 +302,11 @@ await __astroscope_bootSetup(__astroscope_boot, ${JSON.stringify({ host, port })
294
302
  logger.info(`injected ${bootChunkName} into entry.mjs`);
295
303
  },
296
304
  writeBundle(outputOptions) {
297
- if (!isSSR || !warmupModules) return;
305
+ const state = getState(this);
306
+ if (!state.warmupModules) return;
298
307
  const outDir = outputOptions.dir;
299
308
  if (!outDir) return;
300
- writeWarmupManifest(outDir, warmupModules, logger);
309
+ writeWarmupManifest(outDir, state.warmupModules, logger);
301
310
  }
302
311
  },
303
312
  // startup plugin: runs after all other configureServer hooks
@@ -308,7 +317,7 @@ await __astroscope_bootSetup(__astroscope_boot, ${JSON.stringify({ host, port })
308
317
  if (command !== "dev") return;
309
318
  try {
310
319
  const bootContext = getBootContext(server, astroConfig);
311
- const module2 = await server.ssrLoadModule(`/${entry}`);
320
+ const module2 = await ssrImport(server, `/${entry}`);
312
321
  await runStartup(module2, bootContext);
313
322
  } catch (error) {
314
323
  logger.error(`Error running startup script: ${serializeError(error)}`);
@@ -316,7 +325,7 @@ await __astroscope_bootSetup(__astroscope_boot, ${JSON.stringify({ host, port })
316
325
  server.httpServer?.once("close", async () => {
317
326
  try {
318
327
  const bootContext = getBootContext(server, astroConfig);
319
- const module2 = await server.ssrLoadModule(`/${entry}`);
328
+ const module2 = await ssrImport(server, `/${entry}`);
320
329
  await runShutdown(module2, bootContext);
321
330
  } catch (error) {
322
331
  logger.error(`Error running shutdown script: ${serializeError(error)}`);
package/dist/index.js CHANGED
@@ -11,6 +11,7 @@ import "./chunk-I62ZQYTP.js";
11
11
  // src/integration.ts
12
12
  import fs2 from "fs";
13
13
  import MagicString from "magic-string";
14
+ import { perEnvironmentState } from "vite";
14
15
 
15
16
  // src/hmr.ts
16
17
  import path from "path";
@@ -66,6 +67,16 @@ function serializeError(error) {
66
67
  return JSON.stringify(error);
67
68
  }
68
69
 
70
+ // src/vite-env.ts
71
+ import { isRunnableDevEnvironment } from "vite";
72
+ async function ssrImport(server, moduleId) {
73
+ const ssr = server.environments["ssr"];
74
+ if (!isRunnableDevEnvironment(ssr)) {
75
+ throw new Error("SSR environment is not runnable");
76
+ }
77
+ return ssr.runner.import(moduleId);
78
+ }
79
+
69
80
  // src/hmr.ts
70
81
  function setupBootHmr(server, entry, logger, getBootContext2) {
71
82
  const bootModuleId = `/${entry}`;
@@ -97,14 +108,14 @@ function setupBootHmr(server, entry, logger, getBootContext2) {
97
108
  logger.info(`boot dependency changed: ${changedPath}, rerunning hooks...`);
98
109
  const bootContext = getBootContext2();
99
110
  try {
100
- const oldModule = await server.ssrLoadModule(bootModuleId);
111
+ const oldModule = await ssrImport(server, bootModuleId);
101
112
  await runShutdown(oldModule, bootContext);
102
113
  } catch (error) {
103
114
  logger.error(`Error during boot HMR shutdown: ${serializeError(error)}`);
104
115
  }
105
116
  server.moduleGraph.invalidateAll();
106
117
  try {
107
- const newModule = await server.ssrLoadModule(bootModuleId);
118
+ const newModule = await ssrImport(server, bootModuleId);
108
119
  await runStartup(newModule, bootContext);
109
120
  } catch (error) {
110
121
  logger.error(`Error during boot HMR startup: ${serializeError(error)}`);
@@ -165,13 +176,11 @@ function getBootContext(server, config) {
165
176
  const { host, port } = getServerDefaults(config);
166
177
  return { dev: true, host, port };
167
178
  }
179
+ var getState = perEnvironmentState(() => ({ bootChunkRef: null, warmupModules: null }));
168
180
  function boot(options = {}) {
169
181
  const entry = resolveEntry(options.entry);
170
182
  const hmr = options.hmr ?? false;
171
- let isSSR = false;
172
- let bootChunkRef = null;
173
183
  let astroConfig = null;
174
- let warmupModules = null;
175
184
  return {
176
185
  name: "@astroscope/boot",
177
186
  hooks: {
@@ -183,19 +192,18 @@ function boot(options = {}) {
183
192
  // build plugin: handles entry.mjs injection, warmup manifest
184
193
  {
185
194
  name: "@astroscope/boot",
186
- configResolved(config2) {
187
- isSSR = !!config2.build?.ssr;
188
- },
189
195
  buildStart() {
190
- if (!isSSR) return;
196
+ if (this.environment.name !== "ssr") return;
197
+ const state = getState(this);
191
198
  try {
192
- bootChunkRef = this.emitFile({ type: "chunk", id: entry, name: "boot" });
199
+ state.bootChunkRef = this.emitFile({ type: "chunk", id: entry, name: "boot" });
193
200
  } catch {
194
201
  }
195
202
  },
196
203
  generateBundle(_, bundle) {
197
- if (!isSSR || !bootChunkRef) return;
198
- const bootChunkName = this.getFileName(bootChunkRef);
204
+ const state = getState(this);
205
+ if (!state.bootChunkRef) return;
206
+ const bootChunkName = this.getFileName(state.bootChunkRef);
199
207
  if (!bootChunkName) {
200
208
  logger.warn("boot chunk not found");
201
209
  return;
@@ -205,7 +213,7 @@ function boot(options = {}) {
205
213
  logger.warn("entry.mjs not found - boot injection skipped");
206
214
  return;
207
215
  }
208
- warmupModules = collectWarmupModules(bundle);
216
+ state.warmupModules = collectWarmupModules(bundle);
209
217
  const { host, port } = getServerDefaults(astroConfig);
210
218
  const prependCode = getPrependCode();
211
219
  const prefix = prependCode.length ? `${prependCode.join("\n")}
@@ -224,10 +232,11 @@ await __astroscope_bootSetup(__astroscope_boot, ${JSON.stringify({ host, port })
224
232
  logger.info(`injected ${bootChunkName} into entry.mjs`);
225
233
  },
226
234
  writeBundle(outputOptions) {
227
- if (!isSSR || !warmupModules) return;
235
+ const state = getState(this);
236
+ if (!state.warmupModules) return;
228
237
  const outDir = outputOptions.dir;
229
238
  if (!outDir) return;
230
- writeWarmupManifest(outDir, warmupModules, logger);
239
+ writeWarmupManifest(outDir, state.warmupModules, logger);
231
240
  }
232
241
  },
233
242
  // startup plugin: runs after all other configureServer hooks
@@ -238,7 +247,7 @@ await __astroscope_bootSetup(__astroscope_boot, ${JSON.stringify({ host, port })
238
247
  if (command !== "dev") return;
239
248
  try {
240
249
  const bootContext = getBootContext(server, astroConfig);
241
- const module = await server.ssrLoadModule(`/${entry}`);
250
+ const module = await ssrImport(server, `/${entry}`);
242
251
  await runStartup(module, bootContext);
243
252
  } catch (error) {
244
253
  logger.error(`Error running startup script: ${serializeError(error)}`);
@@ -246,7 +255,7 @@ await __astroscope_bootSetup(__astroscope_boot, ${JSON.stringify({ host, port })
246
255
  server.httpServer?.once("close", async () => {
247
256
  try {
248
257
  const bootContext = getBootContext(server, astroConfig);
249
- const module = await server.ssrLoadModule(`/${entry}`);
258
+ const module = await ssrImport(server, `/${entry}`);
250
259
  await runShutdown(module, bootContext);
251
260
  } catch (error) {
252
261
  logger.error(`Error running shutdown script: ${serializeError(error)}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astroscope/boot",
3
- "version": "0.3.4",
3
+ "version": "0.4.0",
4
4
  "description": "Startup and graceful shutdown hooks for Astro SSR",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -59,13 +59,13 @@
59
59
  "lint:fix": "eslint src --fix"
60
60
  },
61
61
  "devDependencies": {
62
- "astro": "^5.17.1",
62
+ "astro": "^6.0.2",
63
63
  "tsup": "^8.5.1",
64
- "typescript": "^5.9.3",
65
- "vite": "^6.4.1"
64
+ "typescript": "^5.9.3"
66
65
  },
67
66
  "peerDependencies": {
68
- "astro": "^5.0.0"
67
+ "astro": "^6.0.0",
68
+ "vite": "^7.0.0"
69
69
  },
70
70
  "dependencies": {
71
71
  "magic-string": "^0.30.21"