Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logs for long-running integration hooks #4445

Merged
merged 4 commits into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions packages/astro/src/core/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class AstroBuilder {
debug('build', 'Initial setup...');
const { logging } = this;
this.timer.init = performance.now();
this.config = await runHookConfigSetup({ config: this.config, command: 'build' });
this.config = await runHookConfigSetup({ config: this.config, command: 'build', logging });
this.manifest = createRouteManifest({ config: this.config }, this.logging);

const viteConfig = await createVite(
Expand All @@ -76,7 +76,7 @@ class AstroBuilder {
},
{ astroConfig: this.config, logging, mode: 'build' }
);
await runHookConfigDone({ config: this.config });
await runHookConfigDone({ config: this.config, logging });
return { viteConfig };
}

Expand All @@ -87,7 +87,7 @@ class AstroBuilder {
server: new URL('./server/', this.config.outDir),
serverEntry: 'entry.mjs',
};
await runHookBuildStart({ config: this.config, buildConfig });
await runHookBuildStart({ config: this.config, buildConfig, logging: this.logging });

info(this.logging, 'build', `output target: ${colors.green(this.config.output)}`);
if (this.config._ctx.adapter) {
Expand Down Expand Up @@ -145,6 +145,7 @@ class AstroBuilder {
buildConfig,
pages: pageNames,
routes: Object.values(allPages).map((pd) => pd.route),
logging: this.logging,
});

if (this.logging.level && levels[this.logging.level] <= levels['info']) {
Expand Down
2 changes: 2 additions & 0 deletions packages/astro/src/core/build/static-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ async function ssrBuild(opts: StaticBuildOptions, internals: BuildInternals, inp
pages: internals.pagesByComponent,
vite: viteBuildConfig,
target: 'server',
logging: opts.logging,
});

return await vite.build(viteBuildConfig);
Expand Down Expand Up @@ -236,6 +237,7 @@ async function clientBuild(
pages: internals.pagesByComponent,
vite: viteBuildConfig,
target: 'client',
logging: opts.logging,
});

const buildResult = await vite.build(viteBuildConfig);
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/build/vite-plugin-ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export async function injectManifest(buildOpts: StaticBuildOptions, internals: B

const staticFiles = internals.staticFiles;
const manifest = buildManifest(buildOpts, internals, Array.from(staticFiles));
await runHookBuildSsr({ config: buildOpts.astroConfig, manifest });
await runHookBuildSsr({ config: buildOpts.astroConfig, manifest, logging: buildOpts.logging });

const chunk = internals.ssrEntryChunk;
const code = chunk.code;
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/create-vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export async function createVite(
htmlVitePlugin(),
jsxVitePlugin({ config: astroConfig, logging }),
astroPostprocessVitePlugin({ config: astroConfig }),
astroIntegrationsContainerPlugin({ config: astroConfig }),
astroIntegrationsContainerPlugin({ config: astroConfig, logging }),
astroScriptsPageSSRPlugin({ config: astroConfig }),
],
publicDir: fileURLToPath(astroConfig.publicDir),
Expand Down
10 changes: 5 additions & 5 deletions packages/astro/src/core/dev/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default async function dev(config: AstroConfig, options: DevOptions): Pro
const devStart = performance.now();
applyPolyfill();
await options.telemetry.record([]);
config = await runHookConfigSetup({ config, command: 'dev' });
config = await runHookConfigSetup({ config, command: 'dev', logging: options.logging });
const { host, port } = config.server;

// The client entrypoint for renderers. Since these are imported dynamically
Expand All @@ -50,9 +50,9 @@ export default async function dev(config: AstroConfig, options: DevOptions): Pro
},
{ astroConfig: config, logging: options.logging, mode: 'dev' }
);
await runHookConfigDone({ config });
await runHookConfigDone({ config, logging: options.logging });
const viteServer = await vite.createServer(viteConfig);
runHookServerSetup({ config, server: viteServer });
runHookServerSetup({ config, server: viteServer, logging: options.logging });
await viteServer.listen(port);

const devServerAddressInfo = viteServer.httpServer!.address() as AddressInfo;
Expand All @@ -77,7 +77,7 @@ export default async function dev(config: AstroConfig, options: DevOptions): Pro
warn(options.logging, null, msg.fsStrictWarning());
}

await runHookServerStart({ config, address: devServerAddressInfo });
await runHookServerStart({ config, address: devServerAddressInfo, logging: options.logging });

return {
address: devServerAddressInfo,
Expand All @@ -86,7 +86,7 @@ export default async function dev(config: AstroConfig, options: DevOptions): Pro
},
stop: async () => {
await viteServer.close();
await runHookServerDone({ config });
await runHookServerDone({ config, logging: options.logging });
},
};
}
141 changes: 112 additions & 29 deletions packages/astro/src/integrations/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { AddressInfo } from 'net';
import type { ViteDevServer } from 'vite';
import { bold } from 'kleur/colors';
import {
AstroConfig,
AstroRenderer,
Expand All @@ -11,13 +12,35 @@ import type { SerializedSSRManifest } from '../core/app/types';
import type { PageBuildData } from '../core/build/types';
import { mergeConfig } from '../core/config.js';
import type { ViteConfigWithSSR } from '../core/create-vite.js';
import { info, LogOptions } from '../core/logger/core.js';

async function withTakingALongTimeMsg<T>({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great function name :)

Copy link
Contributor Author

@bholmesdev bholmesdev Aug 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😆 Played with some short names and thought... why not name it what it is?

name,
hookResult,
timeoutMs = 3000,
logging,
}: {
name: string;
hookResult: T | Promise<T>;
timeoutMs?: number;
logging: LogOptions;
}): Promise<T> {
const timeout = setTimeout(() => {
info(logging, 'build', `Waiting for the ${bold(name)} integration...`);
}, timeoutMs);
const result = await hookResult;
clearTimeout(timeout);
return result;
}

export async function runHookConfigSetup({
config: _config,
command,
logging,
}: {
config: AstroConfig;
command: 'dev' | 'build';
logging: LogOptions;
}): Promise<AstroConfig> {
// An adapter is an integration, so if one is provided push it.
if (_config.adapter) {
Expand Down Expand Up @@ -65,25 +88,39 @@ export async function runHookConfigSetup({
writable: false,
enumerable: false,
});
await integration.hooks['astro:config:setup'](hooks);
await withTakingALongTimeMsg({
name: integration.name,
hookResult: integration.hooks['astro:config:setup'](hooks),
logging,
});
}
}
return updatedConfig;
}

export async function runHookConfigDone({ config }: { config: AstroConfig }) {
export async function runHookConfigDone({
config,
logging,
}: {
config: AstroConfig;
logging: LogOptions;
}) {
for (const integration of config.integrations) {
if (integration?.hooks?.['astro:config:done']) {
await integration.hooks['astro:config:done']({
config,
setAdapter(adapter) {
if (config._ctx.adapter && config._ctx.adapter.name !== adapter.name) {
throw new Error(
`Integration "${integration.name}" conflicts with "${config._ctx.adapter.name}". You can only configure one deployment integration.`
);
}
config._ctx.adapter = adapter;
},
await withTakingALongTimeMsg({
name: integration.name,
hookResult: integration.hooks['astro:config:done']({
config,
setAdapter(adapter) {
if (config._ctx.adapter && config._ctx.adapter.name !== adapter.name) {
throw new Error(
`Integration "${integration.name}" conflicts with "${config._ctx.adapter.name}". You can only configure one deployment integration.`
);
}
config._ctx.adapter = adapter;
},
}),
logging,
});
}
}
Expand All @@ -92,49 +129,77 @@ export async function runHookConfigDone({ config }: { config: AstroConfig }) {
export async function runHookServerSetup({
config,
server,
logging,
}: {
config: AstroConfig;
server: ViteDevServer;
logging: LogOptions;
}) {
for (const integration of config.integrations) {
if (integration?.hooks?.['astro:server:setup']) {
await integration.hooks['astro:server:setup']({ server });
await withTakingALongTimeMsg({
name: integration.name,
hookResult: integration.hooks['astro:server:setup']({ server }),
logging,
});
}
}
}

export async function runHookServerStart({
config,
address,
logging,
}: {
config: AstroConfig;
address: AddressInfo;
logging: LogOptions;
}) {
for (const integration of config.integrations) {
if (integration?.hooks?.['astro:server:start']) {
await integration.hooks['astro:server:start']({ address });
await withTakingALongTimeMsg({
name: integration.name,
hookResult: integration.hooks['astro:server:start']({ address }),
logging,
});
}
}
}

export async function runHookServerDone({ config }: { config: AstroConfig }) {
export async function runHookServerDone({
config,
logging,
}: {
config: AstroConfig;
logging: LogOptions;
}) {
for (const integration of config.integrations) {
if (integration?.hooks?.['astro:server:done']) {
await integration.hooks['astro:server:done']();
await withTakingALongTimeMsg({
name: integration.name,
hookResult: integration.hooks['astro:server:done'](),
logging,
});
}
}
}

export async function runHookBuildStart({
config,
buildConfig,
logging,
}: {
config: AstroConfig;
buildConfig: BuildConfig;
logging: LogOptions;
}) {
for (const integration of config.integrations) {
if (integration?.hooks?.['astro:build:start']) {
await integration.hooks['astro:build:start']({ buildConfig });
await withTakingALongTimeMsg({
name: integration.name,
hookResult: integration.hooks['astro:build:start']({ buildConfig }),
logging,
});
}
}
}
Expand All @@ -144,21 +209,27 @@ export async function runHookBuildSetup({
vite,
pages,
target,
logging,
}: {
config: AstroConfig;
vite: ViteConfigWithSSR;
pages: Map<string, PageBuildData>;
target: 'server' | 'client';
logging: LogOptions;
}) {
for (const integration of config.integrations) {
if (integration?.hooks?.['astro:build:setup']) {
await integration.hooks['astro:build:setup']({
vite,
pages,
target,
updateConfig: (newConfig) => {
mergeConfig(vite, newConfig);
},
await withTakingALongTimeMsg({
name: integration.name,
hookResult: integration.hooks['astro:build:setup']({
vite,
pages,
target,
updateConfig: (newConfig) => {
mergeConfig(vite, newConfig);
},
}),
logging,
});
}
}
Expand All @@ -167,13 +238,19 @@ export async function runHookBuildSetup({
export async function runHookBuildSsr({
config,
manifest,
logging,
}: {
config: AstroConfig;
manifest: SerializedSSRManifest;
logging: LogOptions;
}) {
for (const integration of config.integrations) {
if (integration?.hooks?.['astro:build:ssr']) {
await integration.hooks['astro:build:ssr']({ manifest });
await withTakingALongTimeMsg({
name: integration.name,
hookResult: integration.hooks['astro:build:ssr']({ manifest }),
logging,
});
}
}
}
Expand All @@ -183,20 +260,26 @@ export async function runHookBuildDone({
buildConfig,
pages,
routes,
logging,
}: {
config: AstroConfig;
buildConfig: BuildConfig;
pages: string[];
routes: RouteData[];
logging: LogOptions;
}) {
const dir = config.output === 'server' ? buildConfig.client : config.outDir;

for (const integration of config.integrations) {
if (integration?.hooks?.['astro:build:done']) {
await integration.hooks['astro:build:done']({
pages: pages.map((p) => ({ pathname: p })),
dir,
routes,
await withTakingALongTimeMsg({
name: integration.name,
hookResult: integration.hooks['astro:build:done']({
pages: pages.map((p) => ({ pathname: p })),
dir,
routes,
}),
logging,
});
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import { Plugin as VitePlugin } from 'vite';
import { AstroConfig } from '../@types/astro.js';
import { LogOptions } from '../core/logger/core.js';
import { runHookServerSetup } from '../integrations/index.js';

/** Connect Astro integrations into Vite, as needed. */
export default function astroIntegrationsContainerPlugin({
config,
logging,
}: {
config: AstroConfig;
logging: LogOptions;
}): VitePlugin {
return {
name: 'astro:integration-container',
configureServer(server) {
runHookServerSetup({ config, server });
runHookServerSetup({ config, server, logging });
},
};
}