address feedback

This commit is contained in:
Emanuele Stoppa 2023-07-26 09:07:09 +01:00
parent 95110f4aec
commit a25d3497e1
3 changed files with 116 additions and 139 deletions

View file

@ -1857,8 +1857,7 @@ export interface AstroIntegration {
name: string;
/** The different hooks available to extend. */
hooks: {
'astro:config:setup'?: (
options: {
'astro:config:setup'?: (options: {
config: AstroConfig;
command: 'dev' | 'build' | 'preview';
isRestart: boolean;
@ -1868,31 +1867,27 @@ export interface AstroIntegration {
injectScript: (stage: InjectedScriptStage, content: string) => void;
injectRoute: (injectRoute: InjectedRoute) => void;
addClientDirective: (directive: ClientDirectiveConfig) => void;
logger: AstroIntegrationLogger;
// TODO: Add support for `injectElement()` for full HTML element injection, not just scripts.
// This may require some refactoring of `scripts`, `styles`, and `links` into something
// more generalized. Consider the SSR use-case as well.
// injectElement: (stage: vite.HtmlTagDescriptor, element: string) => void;
},
bag: AstroIntegrationBag
) => void | Promise<void>;
'astro:config:done'?: (
options: {
}) => void | Promise<void>;
'astro:config:done'?: (options: {
config: AstroConfig;
setAdapter: (adapter: AstroAdapter) => void;
},
bag: AstroIntegrationBag
) => void | Promise<void>;
'astro:server:setup'?: (
options: { server: vite.ViteDevServer },
bag: AstroIntegrationBag
) => void | Promise<void>;
'astro:server:start'?: (
options: { address: AddressInfo },
bag: AstroIntegrationBag
) => void | Promise<void>;
'astro:server:done'?: (bag: AstroIntegrationBag) => void | Promise<void>;
'astro:build:ssr'?: (
options: {
logger: AstroIntegrationLogger;
}) => void | Promise<void>;
'astro:server:setup'?: (options: {
server: vite.ViteDevServer;
logger: AstroIntegrationLogger;
}) => void | Promise<void>;
'astro:server:start'?: (options: {
address: AddressInfo;
logger: AstroIntegrationLogger;
}) => void | Promise<void>;
'astro:server:done'?: (options: { logger: AstroIntegrationLogger }) => void | Promise<void>;
'astro:build:ssr'?: (options: {
manifest: SerializedSSRManifest;
/**
* This maps a {@link RouteData} to an {@link URL}, this URL represents
@ -1903,41 +1898,29 @@ export interface AstroIntegration {
* File path of the emitted middleware
*/
middlewareEntryPoint: URL | undefined;
},
bag: AstroIntegrationBag
) => void | Promise<void>;
'astro:build:start'?: (bag: AstroIntegrationBag) => void | Promise<void>;
'astro:build:setup'?: (
options: {
logger: AstroIntegrationLogger;
}) => void | Promise<void>;
'astro:build:start'?: (options: { logger: AstroIntegrationLogger }) => void | Promise<void>;
'astro:build:setup'?: (options: {
vite: vite.InlineConfig;
pages: Map<string, PageBuildData>;
target: 'client' | 'server';
updateConfig: (newConfig: vite.InlineConfig) => void;
},
bag: AstroIntegrationBag
) => void | Promise<void>;
'astro:build:generated'?: (
options: { dir: URL },
bag: AstroIntegrationBag
) => void | Promise<void>;
'astro:build:done'?: (
options: {
logger: AstroIntegrationLogger;
}) => void | Promise<void>;
'astro:build:generated'?: (options: {
dir: URL;
logger: AstroIntegrationLogger;
}) => void | Promise<void>;
'astro:build:done'?: (options: {
pages: { pathname: string }[];
dir: URL;
routes: RouteData[];
},
bag: AstroIntegrationBag
) => void | Promise<void>;
logger: AstroIntegrationLogger;
}) => void | Promise<void>;
};
}
/**
* A set of utilities that are passed at each hook
*/
export type AstroIntegrationBag = {
logger: AstroIntegrationLogger;
};
export type MiddlewareNext<R> = () => Promise<R>;
export type MiddlewareHandler<R> = (
context: APIContext,

View file

@ -157,7 +157,7 @@ export class AstroIntegrationLogger {
}
/**
* Creates a new logger instances with a new label, but the same log options.
* Creates a new logger instance with a new label, but the same log options.
*/
fork(label: string): AstroIntegrationLogger {
return new AstroIntegrationLogger(this.options, label);

View file

@ -5,6 +5,7 @@ import { fileURLToPath } from 'node:url';
import type { InlineConfig, ViteDevServer } from 'vite';
import type {
AstroConfig,
AstroIntegration,
AstroRenderer,
AstroSettings,
ContentEntryType,
@ -38,16 +39,16 @@ async function withTakingALongTimeMsg<T>({
return result;
}
// Internally used to store instances of loggers.
const Loggers = new Map<string, AstroIntegrationLogger>();
// Used internally to store instances of loggers.
const Loggers = new WeakMap<AstroIntegration, AstroIntegrationLogger>();
function getLogger(adapterName: string, logging: LogOptions) {
if (Loggers.has(adapterName)) {
function getLogger(integration: AstroIntegration, logging: LogOptions) {
if (Loggers.has(integration)) {
// SAFETY: we check the existence in the if block
return Loggers.get(adapterName)!;
return Loggers.get(integration)!;
}
const logger = new AstroIntegrationLogger(logging, adapterName);
Loggers.set(adapterName, logger);
const logger = new AstroIntegrationLogger(logging, integration.name);
Loggers.set(integration, logger);
return logger;
}
@ -85,7 +86,7 @@ export async function runHookConfigSetup({
* ```
*/
if (integration.hooks?.['astro:config:setup']) {
const logger = getLogger(integration.name, logging);
const logger = getLogger(integration, logging);
const hooks: HookParameters<'astro:config:setup'> = {
config: updatedConfig,
@ -122,6 +123,7 @@ export async function runHookConfigSetup({
}
addedClientDirectives.set(name, buildClientDirectiveEntrypoint(name, entrypoint));
},
logger,
};
// ---
@ -159,7 +161,7 @@ export async function runHookConfigSetup({
await withTakingALongTimeMsg({
name: integration.name,
hookResult: integration.hooks['astro:config:setup'](hooks, { logger }),
hookResult: integration.hooks['astro:config:setup'](hooks),
logging,
});
@ -182,12 +184,11 @@ export async function runHookConfigDone({
logging: LogOptions;
}) {
for (const integration of settings.config.integrations) {
const logger = getLogger(integration.name, logging);
const logger = getLogger(integration, logging);
if (integration?.hooks?.['astro:config:done']) {
await withTakingALongTimeMsg({
name: integration.name,
hookResult: integration.hooks['astro:config:done'](
{
hookResult: integration.hooks['astro:config:done']({
config: settings.config,
setAdapter(adapter) {
if (settings.adapter && settings.adapter.name !== adapter.name) {
@ -197,9 +198,8 @@ export async function runHookConfigDone({
}
settings.adapter = adapter;
},
},
{ logger }
),
logger,
}),
logging,
});
}
@ -217,10 +217,10 @@ export async function runHookServerSetup({
}) {
for (const integration of config.integrations) {
if (integration?.hooks?.['astro:server:setup']) {
const logger = getLogger(integration.name, logging);
const logger = getLogger(integration, logging);
await withTakingALongTimeMsg({
name: integration.name,
hookResult: integration.hooks['astro:server:setup']({ server }, { logger }),
hookResult: integration.hooks['astro:server:setup']({ server, logger }),
logging,
});
}
@ -237,12 +237,12 @@ export async function runHookServerStart({
logging: LogOptions;
}) {
for (const integration of config.integrations) {
const logger = getLogger(integration.name, logging);
const logger = getLogger(integration, logging);
if (integration?.hooks?.['astro:server:start']) {
await withTakingALongTimeMsg({
name: integration.name,
hookResult: integration.hooks['astro:server:start']({ address }, { logger }),
hookResult: integration.hooks['astro:server:start']({ address, logger }),
logging,
});
}
@ -257,7 +257,7 @@ export async function runHookServerDone({
logging: LogOptions;
}) {
for (const integration of config.integrations) {
const logger = getLogger(integration.name, logging);
const logger = getLogger(integration, logging);
if (integration?.hooks?.['astro:server:done']) {
await withTakingALongTimeMsg({
@ -278,7 +278,7 @@ export async function runHookBuildStart({
}) {
for (const integration of config.integrations) {
if (integration?.hooks?.['astro:build:start']) {
const logger = getLogger(integration.name, logging);
const logger = getLogger(integration, logging);
await withTakingALongTimeMsg({
name: integration.name,
@ -306,21 +306,19 @@ export async function runHookBuildSetup({
for (const integration of config.integrations) {
if (integration?.hooks?.['astro:build:setup']) {
const logger = getLogger(integration.name, logging);
const logger = getLogger(integration, logging);
await withTakingALongTimeMsg({
name: integration.name,
hookResult: integration.hooks['astro:build:setup'](
{
hookResult: integration.hooks['astro:build:setup']({
vite,
pages,
target,
updateConfig: (newConfig) => {
updatedConfig = mergeConfig(updatedConfig, newConfig);
},
},
{ logger }
),
logger,
}),
logging,
});
}
@ -346,18 +344,16 @@ export async function runHookBuildSsr({
}: RunHookBuildSsr) {
for (const integration of config.integrations) {
if (integration?.hooks?.['astro:build:ssr']) {
const logger = getLogger(integration.name, logging);
const logger = getLogger(integration, logging);
await withTakingALongTimeMsg({
name: integration.name,
hookResult: integration.hooks['astro:build:ssr'](
{
hookResult: integration.hooks['astro:build:ssr']({
manifest,
entryPoints,
middlewareEntryPoint,
},
{ logger }
),
logger,
}),
logging,
});
}
@ -374,12 +370,12 @@ export async function runHookBuildGenerated({
const dir = isServerLikeOutput(config) ? config.build.client : config.outDir;
for (const integration of config.integrations) {
const logger = getLogger(integration.name, logging);
const logger = getLogger(integration, logging);
if (integration?.hooks?.['astro:build:generated']) {
await withTakingALongTimeMsg({
name: integration.name,
hookResult: integration.hooks['astro:build:generated']({ dir }, { logger }),
hookResult: integration.hooks['astro:build:generated']({ dir, logger }),
logging,
});
}
@ -399,18 +395,16 @@ export async function runHookBuildDone({ config, pages, routes, logging }: RunHo
for (const integration of config.integrations) {
if (integration?.hooks?.['astro:build:done']) {
const logger = getLogger(integration.name, logging);
const logger = getLogger(integration, logging);
await withTakingALongTimeMsg({
name: integration.name,
hookResult: integration.hooks['astro:build:done'](
{
hookResult: integration.hooks['astro:build:done']({
pages: pages.map((p) => ({ pathname: p })),
dir,
routes,
},
{ logger }
),
logger,
}),
logging,
});
}