@astroscope/boot 0.3.2 → 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.
- package/dist/chunk-4PQTRKEQ.js +13 -0
- package/dist/index.cjs +57 -38
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +50 -37
- package/dist/prepend.cjs +38 -0
- package/dist/prepend.d.cts +14 -0
- package/dist/prepend.d.ts +14 -0
- package/dist/prepend.js +8 -0
- package/package.json +6 -2
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,
|
|
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 =
|
|
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,6 +226,15 @@ 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;
|
|
@@ -233,40 +252,9 @@ function boot(options = {}) {
|
|
|
233
252
|
updateConfig({
|
|
234
253
|
vite: {
|
|
235
254
|
plugins: [
|
|
255
|
+
// build plugin: handles entry.mjs injection, warmup manifest
|
|
236
256
|
{
|
|
237
257
|
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
258
|
configResolved(config2) {
|
|
271
259
|
isSSR = !!config2.build?.ssr;
|
|
272
260
|
},
|
|
@@ -291,13 +279,16 @@ function boot(options = {}) {
|
|
|
291
279
|
}
|
|
292
280
|
warmupModules = collectWarmupModules(bundle);
|
|
293
281
|
const { host, port } = getServerDefaults(astroConfig);
|
|
294
|
-
const
|
|
282
|
+
const prependCode = getPrependCode();
|
|
283
|
+
const prefix = prependCode.length ? `${prependCode.join("\n")}
|
|
284
|
+
` : "";
|
|
285
|
+
const injection = `${prefix}globalThis.__astroscope_server_url = import.meta.url;
|
|
295
286
|
import * as __astroscope_boot from './${bootChunkName}';
|
|
296
287
|
import { setup as __astroscope_bootSetup } from '@astroscope/boot/setup';
|
|
297
288
|
await __astroscope_bootSetup(__astroscope_boot, ${JSON.stringify({ host, port })});
|
|
298
289
|
`;
|
|
299
290
|
const s = new import_magic_string.default(entryChunk.code);
|
|
300
|
-
s.prepend(
|
|
291
|
+
s.prepend(injection);
|
|
301
292
|
entryChunk.code = s.toString();
|
|
302
293
|
if (entryChunk.map) {
|
|
303
294
|
entryChunk.map = s.generateMap({ hires: true });
|
|
@@ -310,6 +301,33 @@ await __astroscope_bootSetup(__astroscope_boot, ${JSON.stringify({ host, port })
|
|
|
310
301
|
if (!outDir) return;
|
|
311
302
|
writeWarmupManifest(outDir, warmupModules, logger);
|
|
312
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
|
+
}
|
|
313
331
|
}
|
|
314
332
|
]
|
|
315
333
|
}
|
|
@@ -320,5 +338,6 @@ await __astroscope_bootSetup(__astroscope_boot, ${JSON.stringify({ host, port })
|
|
|
320
338
|
}
|
|
321
339
|
// Annotate the CommonJS export names for ESM import in node:
|
|
322
340
|
0 && (module.exports = {
|
|
323
|
-
boot
|
|
341
|
+
boot,
|
|
342
|
+
prepend
|
|
324
343
|
});
|
package/dist/index.d.cts
CHANGED
package/dist/index.d.ts
CHANGED
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,
|
|
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 =
|
|
98
|
+
const bootContext = getBootContext2();
|
|
95
99
|
try {
|
|
96
100
|
const oldModule = await server.ssrLoadModule(bootModuleId);
|
|
97
101
|
await runShutdown(oldModule, bootContext);
|
|
@@ -152,6 +156,15 @@ 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;
|
|
@@ -169,40 +182,9 @@ function boot(options = {}) {
|
|
|
169
182
|
updateConfig({
|
|
170
183
|
vite: {
|
|
171
184
|
plugins: [
|
|
185
|
+
// build plugin: handles entry.mjs injection, warmup manifest
|
|
172
186
|
{
|
|
173
187
|
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
188
|
configResolved(config2) {
|
|
207
189
|
isSSR = !!config2.build?.ssr;
|
|
208
190
|
},
|
|
@@ -227,13 +209,16 @@ function boot(options = {}) {
|
|
|
227
209
|
}
|
|
228
210
|
warmupModules = collectWarmupModules(bundle);
|
|
229
211
|
const { host, port } = getServerDefaults(astroConfig);
|
|
230
|
-
const
|
|
212
|
+
const prependCode = getPrependCode();
|
|
213
|
+
const prefix = prependCode.length ? `${prependCode.join("\n")}
|
|
214
|
+
` : "";
|
|
215
|
+
const injection = `${prefix}globalThis.__astroscope_server_url = import.meta.url;
|
|
231
216
|
import * as __astroscope_boot from './${bootChunkName}';
|
|
232
217
|
import { setup as __astroscope_bootSetup } from '@astroscope/boot/setup';
|
|
233
218
|
await __astroscope_bootSetup(__astroscope_boot, ${JSON.stringify({ host, port })});
|
|
234
219
|
`;
|
|
235
220
|
const s = new MagicString(entryChunk.code);
|
|
236
|
-
s.prepend(
|
|
221
|
+
s.prepend(injection);
|
|
237
222
|
entryChunk.code = s.toString();
|
|
238
223
|
if (entryChunk.map) {
|
|
239
224
|
entryChunk.map = s.generateMap({ hires: true });
|
|
@@ -246,6 +231,33 @@ await __astroscope_bootSetup(__astroscope_boot, ${JSON.stringify({ host, port })
|
|
|
246
231
|
if (!outDir) return;
|
|
247
232
|
writeWarmupManifest(outDir, warmupModules, logger);
|
|
248
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
|
+
}
|
|
249
261
|
}
|
|
250
262
|
]
|
|
251
263
|
}
|
|
@@ -256,5 +268,6 @@ await __astroscope_bootSetup(__astroscope_boot, ${JSON.stringify({ host, port })
|
|
|
256
268
|
}
|
|
257
269
|
export {
|
|
258
270
|
boot,
|
|
259
|
-
boot as default
|
|
271
|
+
boot as default,
|
|
272
|
+
prepend
|
|
260
273
|
};
|
package/dist/prepend.cjs
ADDED
|
@@ -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 };
|
package/dist/prepend.js
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@astroscope/boot",
|
|
3
|
-
"version": "0.3.
|
|
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"
|