0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-23 21:53:55 -05:00

Adds a new "astro:build:generated" hook for SSG builds (#4772)

* adds a new "astro:build:generated" hook for SSG builds

* chore: add changeset
This commit is contained in:
Tony Sullivan 2022-09-15 21:02:07 +00:00 committed by GitHub
parent b2da8a124a
commit 03b18e8d1b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 38 additions and 0 deletions

View file

@ -0,0 +1,5 @@
---
'astro': minor
---
Adds a new "astro:build:generated" hook that runs after SSG builds finish but **before** build artifacts are cleaned up. This is a very specific use case, "astro:build:done" is probably what you're looking for.

View file

@ -1159,6 +1159,7 @@ export interface AstroIntegration {
target: 'client' | 'server'; target: 'client' | 'server';
updateConfig: (newConfig: ViteConfigWithSSR) => void; updateConfig: (newConfig: ViteConfigWithSSR) => void;
}) => void | Promise<void>; }) => void | Promise<void>;
'astro:build:generated'?: (options: { dir: URL }) => void | Promise<void>;
'astro:build:done'?: (options: { 'astro:build:done'?: (options: {
pages: { pathname: string }[]; pages: { pathname: string }[];
dir: URL; dir: URL;

View file

@ -19,6 +19,7 @@ import {
removeTrailingForwardSlash, removeTrailingForwardSlash,
} from '../../core/path.js'; } from '../../core/path.js';
import type { RenderOptions } from '../../core/render/core'; import type { RenderOptions } from '../../core/render/core';
import { runHookBuildGenerated } from '../../integrations/index.js';
import { BEFORE_HYDRATION_SCRIPT_ID, PAGE_SCRIPT_ID } from '../../vite-plugin-scripts/index.js'; import { BEFORE_HYDRATION_SCRIPT_ID, PAGE_SCRIPT_ID } from '../../vite-plugin-scripts/index.js';
import { call as callEndpoint } from '../endpoint/index.js'; import { call as callEndpoint } from '../endpoint/index.js';
import { debug, info } from '../logger/core.js'; import { debug, info } from '../logger/core.js';
@ -111,6 +112,9 @@ export async function generatePages(opts: StaticBuildOptions, internals: BuildIn
for (const pageData of eachPageData(internals)) { for (const pageData of eachPageData(internals)) {
await generatePage(opts, internals, pageData, ssrEntry, builtPaths); await generatePage(opts, internals, pageData, ssrEntry, builtPaths);
} }
await runHookBuildGenerated({ config: opts.astroConfig, buildConfig: opts.buildConfig, logging: opts.logging });
info(opts.logging, null, dim(`Completed in ${getTimeStat(timer, performance.now())}.\n`)); info(opts.logging, null, dim(`Completed in ${getTimeStat(timer, performance.now())}.\n`));
} }

View file

@ -263,6 +263,28 @@ export async function runHookBuildSsr({
} }
} }
export async function runHookBuildGenerated({
config,
buildConfig,
logging,
}: {
config: AstroConfig;
buildConfig: BuildConfig;
logging: LogOptions;
}) {
const dir = config.output === 'server' ? buildConfig.client : config.outDir;
for (const integration of config.integrations) {
if (integration?.hooks?.['astro:build:generated']) {
await withTakingALongTimeMsg({
name: integration.name,
hookResult: integration.hooks['astro:build:generated']({ dir }),
logging,
});
}
}
}
export async function runHookBuildDone({ export async function runHookBuildDone({
config, config,
buildConfig, buildConfig,

View file

@ -4,6 +4,8 @@ import { loadFixture } from './test-utils.js';
describe('Static build: dir takes the URL path to the output directory', () => { describe('Static build: dir takes the URL path to the output directory', () => {
/** @type {URL} */ /** @type {URL} */
let checkDir; let checkDir;
/** @type {URL} */
let checkGeneratedDir;
before(async () => { before(async () => {
const fixture = await loadFixture({ const fixture = await loadFixture({
root: './fixtures/static-build-dir/', root: './fixtures/static-build-dir/',
@ -11,6 +13,9 @@ describe('Static build: dir takes the URL path to the output directory', () => {
{ {
name: '@astrojs/dir', name: '@astrojs/dir',
hooks: { hooks: {
'astro:build:generated': ({ dir }) => {
checkGeneratedDir = dir;
},
'astro:build:done': ({ dir }) => { 'astro:build:done': ({ dir }) => {
checkDir = dir; checkDir = dir;
}, },
@ -25,5 +30,6 @@ describe('Static build: dir takes the URL path to the output directory', () => {
expect(removeTrailingSlash(checkDir.toString())).to.be.equal( expect(removeTrailingSlash(checkDir.toString())).to.be.equal(
removeTrailingSlash(new URL('./fixtures/static-build-dir/dist', import.meta.url).toString()) removeTrailingSlash(new URL('./fixtures/static-build-dir/dist', import.meta.url).toString())
); );
expect(checkDir.toString()).to.be.equal(checkGeneratedDir.toString());
}); });
}); });